From 693429492f78734b4b28fbcf7c3258b5ecc266d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Sat, 19 Apr 2025 22:18:59 +0200 Subject: [PATCH] Commit inicial --- .gitignore | 2 ++ main.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 19 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 main.ts create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5f19d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..8cbf469 --- /dev/null +++ b/main.ts @@ -0,0 +1,50 @@ +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; +import { z } from "zod"; + +// 1. Crear el servidor +// Es la interfaz principal con el protocolo MCP. Maneja la comunicación entre el cliente y el servidor. + +const server = new McpServer({ + name: 'Demo', + version: '1.0.0' +}); +// 2. Definir las herramientas +// Las herramientas le permiten al LLM realizar acciones a través de tu servidor. + +server.tool( + 'fetch_weather', + 'Tool to fetch the weather of a city', // Descripción de la herramienta + { + city: z.string().describe('City name') + }, + async ({ city }) => { + const response = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}&count=10&language=en&format=json`) + const data = await response.json() + if (! data.hasOwnProperty("results")) { + return { + content: [ + { + type: 'text', + text: `No se encontró información del tiempo para ${city}` + } + ] + } + } + const { latitude, longitude } = data.results[0] + const weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m¤t=temperature_2m,precipitation,is_day,rain,wind_speed_10m&forecast_days=1`) + const weatherData = await weatherResponse.json() + return { + content: [ + { + type: 'text', + text: JSON.stringify(weatherData, null, 2) + } + ] + } + } +) + +// 3. Escuchar las conexiones del cliente +const transport = new StdioServerTransport(); // Para ejecutarlo en local para probar. +await server.connect(transport); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..b5278aa --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "mcp-01", + "version": "1.0.0", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Ricardo Montañana", + "license": "MIT", + "description": "", + "dependencies": { + "@modelcontextprotocol/sdk": "^1.10.1", + "zod": "^3.24.3" + }, + "devDependencies": { + "typescript": "^5.8.3" + } +}