mcp-server-clima
Version:
MCP server para información del clima
98 lines (97 loc) • 2.9 kB
JavaScript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { CallToolRequestSchema, ListToolsRequestSchema, McpError, ErrorCode, } from "@modelcontextprotocol/sdk/types.js";
// Definición de herramienta
const GET_CURRENT_TIME_TOOL = {
name: "get_current_time",
description: "Obtiene la fecha y hora actuales",
inputSchema: {
type: "object",
properties: {
format: {
type: "string",
description: "Formato de fecha (opcional)",
enum: ["short", "full"]
}
}
}
};
const TOOLS = [GET_CURRENT_TIME_TOOL];
// Manejador de herramienta
async function handleGetCurrentTime(params) {
const { format = "full" } = params;
try {
const now = new Date();
let timeString;
if (format === "short") {
timeString = now.toLocaleTimeString();
}
else {
timeString = now.toString();
}
return {
content: [{
type: "text",
text: JSON.stringify({
time: timeString
}, null, 2)
}],
isError: false
};
}
catch (error) {
return {
content: [{
type: "text",
text: JSON.stringify({
error: error instanceof Error ? error.message : String(error)
}, null, 2)
}],
isError: true
};
}
}
// Configuración del servidor
const server = new Server({
name: "time-server",
version: "0.1.0",
}, {
capabilities: {
tools: {},
},
});
// Manejadores de solicitudes
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: TOOLS,
}));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
switch (request.params.name) {
case "get_current_time": {
return await handleGetCurrentTime(request.params.arguments);
}
default:
throw new McpError(ErrorCode.MethodNotFound, `Herramienta desconocida: ${request.params.name}`);
}
}
catch (error) {
return {
content: [{
type: "text",
text: `Error: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true
};
}
});
// Iniciar servidor
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Servidor MCP Mínimo ejecutándose en stdio");
}
runServer().catch((error) => {
console.error("Error fatal al ejecutar el servidor:", error);
process.exit(1);
});