Files
STCourses/packages/feature-ai/test/fixtures/fake-mcp.ts
T
Lucas WintherandClaude Opus 5.5 cfc41c6a63 fix(ai): stdio MCP servers get PATH, HOME, LANG and their configured env only
STC_SECRET, database URLs and API keys are no longer inherited by MCP child
processes. Test: a fake server reports its environment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 04:47:47 +01:00

34 lines
1.7 KiB
TypeScript

// A fake MCP server for tests. stdio: newline-delimited JSON-RPC on stdin/stdout.
// Exported `handle` is reused by the HTTP fake in mcp.test.ts.
export function handle(msg: any): any | null {
if (msg.id === undefined) return null; // notification
switch (msg.method) {
case "initialize":
return { jsonrpc: "2.0", id: msg.id, result: { protocolVersion: "2025-06-18", capabilities: { tools: {} }, serverInfo: { name: "fake", version: "1" } } };
case "tools/list":
return { jsonrpc: "2.0", id: msg.id, result: { tools: [{ name: "echo", description: "Echo text back", inputSchema: { type: "object", properties: { text: { type: "string" } }, required: ["text"] } }] } };
case "tools/call":
if (msg.params?.name === "env") return { jsonrpc: "2.0", id: msg.id, result: { content: [{ type: "text", text: JSON.stringify(process.env) }] } };
if (msg.params?.name !== "echo") return { jsonrpc: "2.0", id: msg.id, error: { code: -32602, message: `Unknown tool ${msg.params?.name}` } };
return { jsonrpc: "2.0", id: msg.id, result: { content: [{ type: "text", text: `echo: ${msg.params.arguments?.text}` }] } };
default:
return { jsonrpc: "2.0", id: msg.id, error: { code: -32601, message: "Method not found" } };
}
}
if (import.meta.main) {
if (process.argv.includes("--exit-immediately")) process.exit(3);
let buf = "";
for await (const chunk of Bun.stdin.stream()) {
buf += new TextDecoder().decode(chunk);
let nl: number;
while ((nl = buf.indexOf("\n")) !== -1) {
const line = buf.slice(0, nl).trim();
buf = buf.slice(nl + 1);
if (!line) continue;
const reply = handle(JSON.parse(line));
if (reply) process.stdout.write(JSON.stringify(reply) + "\n");
}
}
}