Commit inicial
This commit is contained in:
50
main.ts
Normal file
50
main.ts
Normal file
@@ -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);
|
Reference in New Issue
Block a user