diff --git a/.cursor/mcp.json b/.cursor/mcp.json new file mode 100644 index 0000000..ed893b1 --- /dev/null +++ b/.cursor/mcp.json @@ -0,0 +1,17 @@ +{ + "mcpServers": { + "天气": { + "url": "http://localhost:3001/sse", + "env": {} + }, + "filesystem": { + "command": "npx", + "args": [ + "-y", + "@modelcontextprotocol/server-filesystem", + "D:/@code/demo/x6-demo", + "D:/@code/demo/x6-demo" + ] + } + } +} \ No newline at end of file diff --git a/readme.md b/readme.md index f275c18..48e05e0 100644 --- a/readme.md +++ b/readme.md @@ -12,6 +12,8 @@ - https://mcpservers.org/ - https://www.npmjs.com/package/@modelcontextprotocol/sdk#server-capabilities +- https://geekdaxue.co/read/MCP-doc/resources + { "mcpServers": { diff --git a/weather/server-sse.js b/weather/server-sse.js index 1a7683f..a593638 100644 --- a/weather/server-sse.js +++ b/weather/server-sse.js @@ -18,16 +18,55 @@ const server = new McpServer( } ); -server.tool("get-weather", { state: z.string() }, async ({ state }) => { - const url = `https://cn.apihz.cn/api/tianqi/tqyb.php?id=88888888&key=88888888&sheng=四川&place=绵阳`; - const data = await fetch(url).then((res) => res.json()); +server.tool( + "get-weather", + { province: z.string(), city: z.string() }, + async ({ province, city }) => { + const url = `https://cn.apihz.cn/api/tianqi/tqyb.php?id=88888888&key=88888888&sheng=${province}&place=${city}`; + const data = await fetch(url).then((res) => res.json()); + // 如果返回的code不为200,说明获取天气失败,返回错误信息。 + if (data.code !== 200) { + return { + content: [ + { + type: "text", + text: "获取天气失败", + }, + ], + }; + } - if (!data) { - return "无法获取天气"; + return { + content: [ + { + type: "text", + text: JSON.stringify(data, null, 2), + }, + ], + }; } +); - return data; -}); +server.prompt( + "get-weather-prompt", + { + province: z.string(), + city: z.string(), + }, + async ({ province, city }) => { + return { + messages: [ + { + role: "user", + content: { + type: "text", + text: `请帮我查询${province}省${city}市的天气情况,包括温度、湿度、风向等信息。请用中文回答。`, + }, + }, + ], + }; + } +); const app = express(); let transport;