You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

47 lines
1.2 KiB

import express from "express";
import {
ResourceTemplate,
McpServer,
} from "@modelcontextprotocol/sdk/server/mcp.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import { z } from "zod";
const server = new McpServer(
{
name: "天气工具包",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
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());
if (!data) {
return "无法获取天气";
}
return data;
});
const app = express();
let transport;
app.get("/sse", async (req, res) => {
transport = new SSEServerTransport("/messages", res);
await server.connect(transport);
});
app.post("/messages", async (req, res) => {
// Note: to support multiple simultaneous connections, these messages will
// need to be routed to a specific matching transport. (This logic isn't
// implemented here, for simplicity.)
await transport.handlePostMessage(req, res);
});
app.listen(3001);
console.log("Server running on http://localhost:3001");