|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { Server } from "@modelcontextprotocol/sdk/server/index.js" |
| 3 | +import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js" |
| 4 | +import { GetPromptRequestSchema, ListPromptsRequestSchema } from "@modelcontextprotocol/sdk/types.js" |
| 5 | +import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
| 6 | +import { Effect, Logger } from "effect" |
| 7 | +import { McpCatalog } from "@/mcp/catalog" |
| 8 | +import { MCP } from "../../src/mcp/index" |
| 9 | +import { testEffect } from "../lib/effect" |
| 10 | + |
| 11 | +// Fork regression tests for task 2.4 (upstream anomalyco/opencode#53051): |
| 12 | +// an MCP server can echo configured secrets in error text. The engine log |
| 13 | +// must carry a fixed message plus safe metadata only — never the secret. |
| 14 | +// Capturing the structured log entries is stronger than reading the flushed |
| 15 | +// file: a value that never enters the logging pipeline can never reach the |
| 16 | +// flushed opencode.log. |
| 17 | + |
| 18 | +const SECRET = "FAKE_MCP_LOG_SECRET_ONLY" |
| 19 | + |
| 20 | +type Logged = Array<unknown> |
| 21 | + |
| 22 | +function collector(seen: Logged) { |
| 23 | + return Logger.make((opts) => { |
| 24 | + seen.push(opts.message) |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +function text(seen: Logged) { |
| 29 | + return JSON.stringify(seen) |
| 30 | +} |
| 31 | + |
| 32 | +function entriesWith(seen: Logged, message: string) { |
| 33 | + return seen.filter( |
| 34 | + (entry): entry is [unknown, ...Array<unknown>] => |
| 35 | + Array.isArray(entry) && entry.some((part) => part === message), |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +describe("mcp error metadata", () => { |
| 40 | + test("drops free-text messages but keeps error name and code", () => { |
| 41 | + const serverError = new Error(`list failed: ${SECRET}`) as Error & { code: number } |
| 42 | + serverError.name = "McpError" |
| 43 | + serverError.code = -32603 |
| 44 | + expect(McpCatalog.errorMeta(serverError)).toEqual({ errorName: "McpError", errorCode: -32603 }) |
| 45 | + }) |
| 46 | + |
| 47 | + test("never stringifies unknown failures", () => { |
| 48 | + expect(McpCatalog.errorMeta(`raw string ${SECRET}`)).toEqual({ errorName: "UnknownError" }) |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +test("failed list logging keeps the secret out of the engine log", async () => { |
| 53 | + const seen: Logged = [] |
| 54 | + await Effect.runPromise( |
| 55 | + McpCatalog.fetch("secret-server", {} as never, () => Promise.reject(new Error(`boom ${SECRET}`)), "prompts").pipe( |
| 56 | + Effect.provide(Logger.layer([collector(seen)])), |
| 57 | + ), |
| 58 | + ) |
| 59 | + expect(text(seen)).not.toContain(SECRET) |
| 60 | + const entries = entriesWith(seen, "failed to get prompts") |
| 61 | + expect(entries.length).toBeGreaterThan(0) |
| 62 | + expect(text(entries)).toContain("secret-server") |
| 63 | + expect(text(entries)).toContain("Error") |
| 64 | +}) |
| 65 | + |
| 66 | +const it = testEffect(LayerNode.compile(MCP.node)) |
| 67 | + |
| 68 | +// Echo-error server in the shape of the issue repro: every request handler |
| 69 | +// answers with an error carrying the configured secret value. |
| 70 | +function echoErrorServer() { |
| 71 | + return Effect.acquireRelease( |
| 72 | + Effect.promise(async () => { |
| 73 | + const protocol = new Server({ name: "echo-error", version: "1.0.0" }, { capabilities: { prompts: {} } }) |
| 74 | + protocol.setRequestHandler(ListPromptsRequestSchema, () => { |
| 75 | + throw new Error(`list failed: ${SECRET}`) |
| 76 | + }) |
| 77 | + protocol.setRequestHandler(GetPromptRequestSchema, () => { |
| 78 | + throw new Error(`get failed: ${SECRET}`) |
| 79 | + }) |
| 80 | + const transport = new WebStandardStreamableHTTPServerTransport({ |
| 81 | + sessionIdGenerator: () => crypto.randomUUID(), |
| 82 | + enableJsonResponse: true, |
| 83 | + }) |
| 84 | + await protocol.connect(transport) |
| 85 | + const http = Bun.serve({ |
| 86 | + port: 0, |
| 87 | + fetch: (request) => transport.handleRequest(request), |
| 88 | + }) |
| 89 | + return { |
| 90 | + url: http.url.toString(), |
| 91 | + close: async () => { |
| 92 | + await protocol.close().catch(() => {}) |
| 93 | + http.stop(true) |
| 94 | + }, |
| 95 | + } |
| 96 | + }), |
| 97 | + (server) => Effect.promise(server.close), |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +it.instance("echoing server errors never reach the engine log", () => |
| 102 | + Effect.gen(function* () { |
| 103 | + const seen: Logged = [] |
| 104 | + yield* Effect.provide( |
| 105 | + Effect.gen(function* () { |
| 106 | + const server = yield* echoErrorServer() |
| 107 | + const mcp = yield* MCP.Service |
| 108 | + yield* mcp.add("echo-error", { type: "remote", url: server.url, oauth: false }) |
| 109 | + |
| 110 | + // List path (McpCatalog.fetch sink) and single-request path |
| 111 | + // (withClient sink) both fail with the secret-bearing server error. |
| 112 | + yield* mcp.prompts() |
| 113 | + yield* mcp.getPrompt("echo-error", "anything") |
| 114 | + }), |
| 115 | + Logger.layer([collector(seen)]), |
| 116 | + ) |
| 117 | + |
| 118 | + expect(text(seen)).not.toContain(SECRET) |
| 119 | + const lists = entriesWith(seen, "failed to get prompts") |
| 120 | + const singles = entriesWith(seen, "failed to getPrompt") |
| 121 | + expect(lists.length).toBeGreaterThan(0) |
| 122 | + expect(singles.length).toBeGreaterThan(0) |
| 123 | + expect(text([...lists, ...singles])).toContain("echo-error") |
| 124 | + }), |
| 125 | +) |
0 commit comments