mcp-server.ts•1.17 kB
import {
McpServer,
ResourceTemplate,
} from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
const mcpServer = new McpServer(
{
name: "ExampleMCPServer",
version: "1.0.0",
},
{
capabilities: {},
}
);
mcpServer.tool("add", { a: z.number(), b: z.number() }, async ({ a, b }) => {
console.log("add:>>", { a, b });
return {
content: [{ type: "text", text: String(a + b) }],
};
});
mcpServer.tool("weather", { city: z.string() }, async ({ city }) => {
const alerts = city === "上海" ? ["高温预警"] : [];
return {
content: [{ type: "text", text: JSON.stringify({ alerts }) }],
};
});
mcpServer.resource(
"document",
new ResourceTemplate("document://{name}", {
list: async () => {
return {
resources: [
{
name: "document-getting-started",
uri: "document://getting-started",
},
],
};
},
}),
async (uri, variables) => {
return {
contents: [
{
uri: uri.href,
text: "Getting Started",
mimeType: "text/plain",
},
],
};
}
);
export { mcpServer };