diff --git a/apps/docs-mcp/src/__tests__/mcp.test.ts b/apps/docs-mcp/src/__tests__/mcp.test.ts index c9fde62d2..abd98867a 100644 --- a/apps/docs-mcp/src/__tests__/mcp.test.ts +++ b/apps/docs-mcp/src/__tests__/mcp.test.ts @@ -1,6 +1,6 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; -import { describe, expect, test } from "vitest"; +import { describe, expect, test, vi } from "vitest"; import { app } from "../index"; describe("private MCP transport", () => { @@ -34,6 +34,61 @@ describe("private MCP transport", () => { } }); + test.each([ + { + args: { libs: ["nextjs"] }, + error: + "Failed to list features: Set OPENROUTER_API_KEY on the docs-mcp worker", + name: "get-available-features", + }, + { + args: { query: "tracing" }, + error: + "Failed to search docs: Failed to fetch https://docs.sentry.io/doctree.json: 503 Service Unavailable", + name: "search-docs", + }, + ])("marks $name failures as MCP errors", async ({ args, error, name }) => { + vi.stubGlobal( + "fetch", + vi.fn( + async () => + new Response("Unavailable", { + status: 503, + statusText: "Service Unavailable", + }), + ), + ); + + const client = new Client({ name: "docs-mcp-test", version: "1.0.0" }); + const transport = new StreamableHTTPClientTransport( + new URL("https://docs-mcp.internal/mcp"), + { + fetch: async (input, init) => + await app.request(new Request(input, init), undefined, { + CF_VERSION_METADATA: { id: "test", tag: "test", timestamp: "test" }, + }), + }, + ); + + try { + await client.connect(transport); + const result = await client.callTool({ arguments: args, name }); + + expect(result).toEqual({ + content: [ + { + text: JSON.stringify({ error }), + type: "text", + }, + ], + isError: true, + }); + } finally { + vi.unstubAllGlobals(); + await client.close(); + } + }); + test("rejects MCP requests outside the service binding", async () => { const response = await app.request( "https://docs-mcp.getsentry.workers.dev/mcp", diff --git a/apps/docs-mcp/src/mcp.ts b/apps/docs-mcp/src/mcp.ts index 39d4ad744..f3d46ee38 100644 --- a/apps/docs-mcp/src/mcp.ts +++ b/apps/docs-mcp/src/mcp.ts @@ -24,6 +24,7 @@ function errorEnvelope(message: string) { type: "text" as const, }, ], + isError: true, }; }