|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | +/** |
| 3 | + * Minimal MCP Server for integration testing (stdio transport) |
| 4 | + * |
| 5 | + * This server exposes tools, prompts, and resources for testing the probe functionality. |
| 6 | + */ |
| 7 | + |
| 8 | +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; |
| 9 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 10 | +import { |
| 11 | + CallToolRequestSchema, |
| 12 | + GetPromptRequestSchema, |
| 13 | + ListPromptsRequestSchema, |
| 14 | + ListResourcesRequestSchema, |
| 15 | + ListToolsRequestSchema, |
| 16 | + ReadResourceRequestSchema, |
| 17 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 18 | + |
| 19 | +const server = new Server( |
| 20 | + { |
| 21 | + name: "test-stdio-server", |
| 22 | + version: "1.0.0", |
| 23 | + }, |
| 24 | + { |
| 25 | + capabilities: { |
| 26 | + tools: {}, |
| 27 | + prompts: {}, |
| 28 | + resources: {}, |
| 29 | + }, |
| 30 | + } |
| 31 | +); |
| 32 | + |
| 33 | +// Define tools |
| 34 | +server.setRequestHandler(ListToolsRequestSchema, async () => { |
| 35 | + return { |
| 36 | + tools: [ |
| 37 | + { |
| 38 | + name: "greet", |
| 39 | + description: "Greets a person by name", |
| 40 | + inputSchema: { |
| 41 | + type: "object" as const, |
| 42 | + properties: { |
| 43 | + name: { type: "string", description: "Name to greet" }, |
| 44 | + }, |
| 45 | + required: ["name"], |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "add", |
| 50 | + description: "Adds two numbers", |
| 51 | + inputSchema: { |
| 52 | + type: "object" as const, |
| 53 | + properties: { |
| 54 | + a: { type: "number", description: "First number" }, |
| 55 | + b: { type: "number", description: "Second number" }, |
| 56 | + }, |
| 57 | + required: ["a", "b"], |
| 58 | + }, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }; |
| 62 | +}); |
| 63 | + |
| 64 | +server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 65 | + const { name, arguments: args } = request.params; |
| 66 | + |
| 67 | + if (name === "greet") { |
| 68 | + return { |
| 69 | + content: [{ type: "text", text: `Hello, ${(args as { name: string }).name}!` }], |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + if (name === "add") { |
| 74 | + const { a, b } = args as { a: number; b: number }; |
| 75 | + // Return as embedded JSON to test normalization |
| 76 | + return { |
| 77 | + content: [ |
| 78 | + { |
| 79 | + type: "text", |
| 80 | + text: JSON.stringify({ result: a + b, operation: "add", inputs: { b, a } }), |
| 81 | + }, |
| 82 | + ], |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + throw new Error(`Unknown tool: ${name}`); |
| 87 | +}); |
| 88 | + |
| 89 | +// Define prompts |
| 90 | +server.setRequestHandler(ListPromptsRequestSchema, async () => { |
| 91 | + return { |
| 92 | + prompts: [ |
| 93 | + { |
| 94 | + name: "code-review", |
| 95 | + description: "Review code for issues", |
| 96 | + arguments: [ |
| 97 | + { name: "code", description: "The code to review", required: true }, |
| 98 | + { name: "language", description: "Programming language", required: false }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + ], |
| 102 | + }; |
| 103 | +}); |
| 104 | + |
| 105 | +server.setRequestHandler(GetPromptRequestSchema, async (request) => { |
| 106 | + if (request.params.name === "code-review") { |
| 107 | + const args = request.params.arguments || {}; |
| 108 | + return { |
| 109 | + messages: [ |
| 110 | + { |
| 111 | + role: "user" as const, |
| 112 | + content: { |
| 113 | + type: "text" as const, |
| 114 | + text: `Please review this ${args.language || "code"}:\n\n${args.code}`, |
| 115 | + }, |
| 116 | + }, |
| 117 | + ], |
| 118 | + }; |
| 119 | + } |
| 120 | + throw new Error(`Unknown prompt: ${request.params.name}`); |
| 121 | +}); |
| 122 | + |
| 123 | +// Define resources |
| 124 | +server.setRequestHandler(ListResourcesRequestSchema, async () => { |
| 125 | + return { |
| 126 | + resources: [ |
| 127 | + { |
| 128 | + uri: "test://readme", |
| 129 | + name: "README", |
| 130 | + description: "Project readme file", |
| 131 | + mimeType: "text/plain", |
| 132 | + }, |
| 133 | + ], |
| 134 | + }; |
| 135 | +}); |
| 136 | + |
| 137 | +server.setRequestHandler(ReadResourceRequestSchema, async (request) => { |
| 138 | + if (request.params.uri === "test://readme") { |
| 139 | + return { |
| 140 | + contents: [ |
| 141 | + { |
| 142 | + uri: "test://readme", |
| 143 | + mimeType: "text/plain", |
| 144 | + text: "# Test Server\n\nThis is a test MCP server.", |
| 145 | + }, |
| 146 | + ], |
| 147 | + }; |
| 148 | + } |
| 149 | + throw new Error(`Unknown resource: ${request.params.uri}`); |
| 150 | +}); |
| 151 | + |
| 152 | +// Start the server |
| 153 | +async function main() { |
| 154 | + const transport = new StdioServerTransport(); |
| 155 | + await server.connect(transport); |
| 156 | +} |
| 157 | + |
| 158 | +main().catch(console.error); |
0 commit comments