|
1 | 1 | #!/usr/bin/env node |
2 | 2 | /** |
3 | 3 | * Mock MCP Server for testing clients |
4 | | - * Verifies that clients call the tools/list method and returns an empty tool list |
| 4 | + * |
| 5 | + * Verifies that clients call `tools/list`, and advertises tools whose output |
| 6 | + * schemas cover both shapes a structured result can take: an object root, and |
| 7 | + * an array root (allowed as of protocol revision 2026-07-28). |
| 8 | + * |
| 9 | + * The array-rooted tool is deliberate. Clients that compile every declared |
| 10 | + * `outputSchema` up front — as the Go and Rust quickstart clients do — will |
| 11 | + * fail here if they assume an output schema is always `{"type": "object"}`. |
| 12 | + * |
| 13 | + * This uses the low-level `Server` rather than `McpServer` so the schemas are |
| 14 | + * written out literally, which is the point of a mock: what goes on the wire is |
| 15 | + * exactly what is in this file. |
5 | 16 | */ |
6 | 17 |
|
7 | | -import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
8 | | -import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
9 | | -import { ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; |
| 18 | +import type { Tool } from "@modelcontextprotocol/server"; |
| 19 | +import { Server } from "@modelcontextprotocol/server"; |
| 20 | +import { serveStdio } from "@modelcontextprotocol/server/stdio"; |
10 | 21 |
|
11 | | -const server = new McpServer( |
| 22 | +// Track whether tools/list was called |
| 23 | +let toolsListCalled = false; |
| 24 | + |
| 25 | +const TOOLS: Tool[] = [ |
12 | 26 | { |
13 | | - name: "mock-test-server", |
14 | | - version: "1.0.0", |
| 27 | + name: "get_alerts", |
| 28 | + description: "Mock alerts, returning a top-level array", |
| 29 | + inputSchema: { |
| 30 | + type: "object", |
| 31 | + properties: { state: { type: "string" } }, |
| 32 | + required: ["state"], |
| 33 | + }, |
| 34 | + // Array root: legal as of 2026-07-28, rejected by older revisions. |
| 35 | + outputSchema: { |
| 36 | + type: "array", |
| 37 | + items: { |
| 38 | + type: "object", |
| 39 | + properties: { event: { type: "string" }, area: { type: "string" } }, |
| 40 | + required: ["event", "area"], |
| 41 | + }, |
| 42 | + }, |
15 | 43 | }, |
16 | 44 | { |
17 | | - capabilities: { |
18 | | - tools: {}, |
| 45 | + name: "get_forecast", |
| 46 | + description: "Mock forecast, returning an object", |
| 47 | + inputSchema: { |
| 48 | + type: "object", |
| 49 | + properties: { |
| 50 | + latitude: { type: "number" }, |
| 51 | + longitude: { type: "number" }, |
| 52 | + }, |
| 53 | + required: ["latitude", "longitude"], |
19 | 54 | }, |
20 | | - } |
21 | | -); |
| 55 | + outputSchema: { |
| 56 | + type: "object", |
| 57 | + properties: { |
| 58 | + latitude: { type: "number" }, |
| 59 | + longitude: { type: "number" }, |
| 60 | + summary: { type: "string" }, |
| 61 | + }, |
| 62 | + required: ["latitude", "longitude", "summary"], |
| 63 | + }, |
| 64 | + }, |
| 65 | +]; |
22 | 66 |
|
23 | | -// Track whether tools/list was called |
24 | | -let toolsListCalled = false; |
| 67 | +function buildServer(): Server { |
| 68 | + const server = new Server( |
| 69 | + { name: "mock-test-server", version: "1.0.0" }, |
| 70 | + { capabilities: { tools: {} } }, |
| 71 | + ); |
25 | 72 |
|
26 | | -// Override the default tools/list handler to track calls |
27 | | -server.server.setRequestHandler(ListToolsRequestSchema, async () => { |
28 | | - toolsListCalled = true; |
29 | | - return { tools: [] }; |
30 | | -}); |
| 73 | + server.setRequestHandler("tools/list", async () => { |
| 74 | + toolsListCalled = true; |
| 75 | + return { tools: TOOLS }; |
| 76 | + }); |
| 77 | + |
| 78 | + server.setRequestHandler("tools/call", async (request) => { |
| 79 | + const { name, arguments: args = {} } = request.params; |
| 80 | + |
| 81 | + if (name === "get_alerts") { |
| 82 | + const state = String((args as { state?: unknown }).state ?? "??"); |
| 83 | + const alerts = [{ event: "Mock Warning", area: state }]; |
| 84 | + return { |
| 85 | + content: [{ type: "text", text: `1 alert for ${state}` }], |
| 86 | + structuredContent: alerts, |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + if (name === "get_forecast") { |
| 91 | + const { latitude = 0, longitude = 0 } = args as { |
| 92 | + latitude?: number; |
| 93 | + longitude?: number; |
| 94 | + }; |
| 95 | + const forecast = { latitude, longitude, summary: "Mock conditions" }; |
| 96 | + return { |
| 97 | + content: [{ type: "text", text: forecast.summary }], |
| 98 | + structuredContent: forecast, |
| 99 | + }; |
| 100 | + } |
31 | 101 |
|
32 | | -async function main() { |
33 | | - const transport = new StdioServerTransport(); |
34 | | - await server.connect(transport); |
35 | | - console.error("Mock MCP Server running on stdio"); |
| 102 | + return { |
| 103 | + content: [{ type: "text", text: `Unknown tool: ${name}` }], |
| 104 | + isError: true, |
| 105 | + }; |
| 106 | + }); |
| 107 | + |
| 108 | + return server; |
36 | 109 | } |
37 | 110 |
|
| 111 | +serveStdio(buildServer); |
| 112 | +console.error("Mock MCP Server running on stdio"); |
| 113 | + |
38 | 114 | // Verify that tools/list was called when the connection closes |
39 | 115 | process.stdin.on("end", () => { |
40 | 116 | if (!toolsListCalled) { |
41 | 117 | console.error("Error: Client did not call tools/list"); |
42 | 118 | process.exit(1); |
43 | 119 | } |
44 | 120 | }); |
45 | | - |
46 | | -main().catch((error) => { |
47 | | - console.error("Server error:", error); |
48 | | - process.exit(1); |
49 | | -}); |
|
0 commit comments