GeezerVison is showing github.com — fix(opencode): recover expired MCP sessions (#32088) · moonug/opencode@c7dee9c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +1Lines changed: 1 addition & 0 deletions Collapse file Copy file name to clipboardExpand all lines: package.json +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 149 | 149 | | 150 | 150 | | 151 | 151 | | 152 | | - | | 152 | + | | 153 | + | 153 | 154 | } | 154 | 155 | } |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js" | | 2 | +import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js" | | 3 | +import { LATEST_PROTOCOL_VERSION } from "@modelcontextprotocol/sdk/types.js" | | 4 | + | | 5 | +const posts: Array<{ method: string; session: string | null }> = [] | | 6 | +let initializeCount = 0 | | 7 | +let pingCount = 0 | | 8 | +const server = Bun.serve({ | | 9 | + port: 0, | | 10 | + async fetch(request) { | | 11 | + if (request.method === "GET") return new Response(null, { status: 405 }) | | 12 | + if (request.method === "DELETE") return new Response(null, { status: 200 }) | | 13 | + | | 14 | + const message = (await request.json()) as { id?: number; method: string } | | 15 | + const session = request.headers.get("mcp-session-id") | | 16 | + posts.push({ method: message.method, session }) | | 17 | + | | 18 | + if (message.method === "initialize") { | | 19 | + initializeCount++ | | 20 | + return Response.json( | | 21 | + { | | 22 | + jsonrpc: "2.0", | | 23 | + id: message.id, | | 24 | + result: { | | 25 | + protocolVersion: LATEST_PROTOCOL_VERSION, | | 26 | + capabilities: {}, | | 27 | + serverInfo: { name: "test", version: "1" }, | | 28 | + }, | | 29 | + }, | | 30 | + { headers: { "mcp-session-id": initializeCount === 1 ? "expired" : "replacement" } }, | | 31 | + ) | | 32 | + } | | 33 | + | | 34 | + if (message.method === "notifications/initialized") return new Response(null, { status: 202 }) | | 35 | + | | 36 | + pingCount++ | | 37 | + if (pingCount === 1) return new Response("Session not found", { status: 404 }) | | 38 | + return Response.json({ jsonrpc: "2.0", id: message.id, result: {} }) | | 39 | + }, | | 40 | +}) | | 41 | +const client = new Client({ name: "test", version: "1" }) | | 42 | + | | 43 | +try { | | 44 | + await client.connect(new StreamableHTTPClientTransport(server.url)) | | 45 | + await client.ping() | | 46 | + process.stdout.write(JSON.stringify(posts)) | | 47 | +} finally { | | 48 | + await client.close() | | 49 | + server.stop(true) | | 50 | +} |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import path from "node:path" | | 2 | +import { describe, expect, test } from "bun:test" | | 3 | + | | 4 | +describe("mcp session recovery", () => { | | 5 | + test("reinitializes and retries once after a session-bound POST returns 404", async () => { | | 6 | + const child = Bun.spawn([process.execPath, path.join(import.meta.dir, "../fixture/mcp-session-recovery.ts")], { | | 7 | + cwd: path.join(import.meta.dir, "../.."), | | 8 | + stdout: "pipe", | | 9 | + stderr: "pipe", | | 10 | + }) | | 11 | + const [code, stdout, stderr] = await Promise.all([ | | 12 | + child.exited, | | 13 | + Bun.readableStreamToText(child.stdout), | | 14 | + Bun.readableStreamToText(child.stderr), | | 15 | + ]) | | 16 | + | | 17 | + expect(code, stderr).toBe(0) | | 18 | + expect(JSON.parse(stdout)).toEqual([ | | 19 | + { method: "initialize", session: null }, | | 20 | + { method: "notifications/initialized", session: "expired" }, | | 21 | + { method: "ping", session: "expired" }, | | 22 | + { method: "initialize", session: null }, | | 23 | + { method: "notifications/initialized", session: "replacement" }, | | 24 | + { method: "ping", session: "replacement" }, | | 25 | + ]) | | 26 | + }) | | 27 | +}) |
|