|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25759] The stateless Streamable HTTP surface serves POST alone: GET and |
| 5 | + * DELETE (and every other verb) answer `405` with `Allow: POST`. |
| 6 | + * |
| 7 | + * ## The defect |
| 8 | + * |
| 9 | + * `handleHttpRequest` builds a stateless transport (`sessionIdGenerator: |
| 10 | + * undefined`, `enableJsonResponse: true`) and closes the per-request server in |
| 11 | + * `finally`. Before this change a GET went on to the SDK's `handleGetRequest`, |
| 12 | + * which answered `200 text/event-stream`, and the stream closed with the |
| 13 | + * server. Measured on `5874d25302`: `200`, empty body, ~13 ms. The SDK client |
| 14 | + * reads a GET stream that ends as a dropped connection and opens another one; |
| 15 | + * production saw ~183 `GET /api/v1/mcp` a minute from one machine with Claude |
| 16 | + * Code sessions open (cloud#3103). A DELETE answered `200` and ended nothing, |
| 17 | + * because no session exists. |
| 18 | + * |
| 19 | + * ## What is pinned |
| 20 | + * |
| 21 | + * 1. GET, DELETE and the other verbs answer `405`, `Allow: POST`, with the |
| 22 | + * SDK transport's own JSON-RPC 405 body, and no bridge member is read. |
| 23 | + * 2. POST is unchanged: `initialize` and `tools/list` round trips answer |
| 24 | + * `200` with no session id. |
| 25 | + * 3. The SDK's own client, connected over this surface, opens the GET stream |
| 26 | + * once and never again, and its tool calls still work. This is the |
| 27 | + * reconnect loop itself: with the 200 stream, the same client re-opens the |
| 28 | + * GET every time it closes. |
| 29 | + */ |
| 30 | + |
| 31 | +import { describe, it, expect } from 'vitest'; |
| 32 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 33 | +import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; |
| 34 | + |
| 35 | +import { MCPServerRuntime } from './mcp-server-runtime.js'; |
| 36 | +import type { McpDataBridge } from './mcp-http-tools.js'; |
| 37 | + |
| 38 | +const URL_ = 'http://localhost/api/v1/mcp'; |
| 39 | + |
| 40 | +/** A bridge that records every member read, so a refused verb can be shown to read none. */ |
| 41 | +function makeBridge(): McpDataBridge & { calls: string[] } { |
| 42 | + const calls: string[] = []; |
| 43 | + return { |
| 44 | + calls, |
| 45 | + async listObjects() { |
| 46 | + calls.push('listObjects'); |
| 47 | + return [{ name: 'task', label: 'Task', fieldCount: 1 }]; |
| 48 | + }, |
| 49 | + async describeObject() { |
| 50 | + calls.push('describeObject'); |
| 51 | + return { name: 'task', fields: [{ name: 'title', type: 'text' }] }; |
| 52 | + }, |
| 53 | + async query(object: string) { |
| 54 | + calls.push('query'); |
| 55 | + return { object, records: [], total: 0 }; |
| 56 | + }, |
| 57 | + async get(_object: string, id: string) { |
| 58 | + calls.push('get'); |
| 59 | + return { id }; |
| 60 | + }, |
| 61 | + async create(object: string, data: any) { |
| 62 | + calls.push('create'); |
| 63 | + return { object, id: 'n1', record: data }; |
| 64 | + }, |
| 65 | + async update(object: string, id: string, data: any) { |
| 66 | + calls.push('update'); |
| 67 | + return { object, id, record: data }; |
| 68 | + }, |
| 69 | + async remove(object: string, id: string) { |
| 70 | + calls.push('remove'); |
| 71 | + return { object, id, success: true }; |
| 72 | + }, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +/** The headers a claude-code client sends on its GET (cloud#3103's reading). */ |
| 77 | +const CLIENT_GET_HEADERS = { |
| 78 | + accept: 'text/event-stream', |
| 79 | + 'mcp-protocol-version': '2025-06-18', |
| 80 | + 'x-api-key': 'osk_test', |
| 81 | +}; |
| 82 | + |
| 83 | +const INIT = { |
| 84 | + jsonrpc: '2.0', |
| 85 | + id: 0, |
| 86 | + method: 'initialize', |
| 87 | + params: { protocolVersion: '2025-06-18', capabilities: {}, clientInfo: { name: 't', version: '1' } }, |
| 88 | +}; |
| 89 | + |
| 90 | +async function post(runtime: MCPServerRuntime, body: unknown, bridge?: McpDataBridge): Promise<Response> { |
| 91 | + return runtime.handleHttpRequest( |
| 92 | + new Request(URL_, { |
| 93 | + method: 'POST', |
| 94 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 95 | + body: JSON.stringify(body), |
| 96 | + }), |
| 97 | + { bridge, parsedBody: body }, |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +async function expectMethodNotAllowed(res: Response): Promise<void> { |
| 102 | + expect(res.status).toBe(405); |
| 103 | + expect(res.headers.get('allow')).toBe('POST'); |
| 104 | + expect(res.headers.get('content-type')).toContain('application/json'); |
| 105 | + expect(res.headers.get('mcp-session-id')).toBeNull(); |
| 106 | + expect(await res.json()).toEqual({ |
| 107 | + jsonrpc: '2.0', |
| 108 | + error: { code: -32000, message: 'Method not allowed.' }, |
| 109 | + id: null, |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +describe('[#25759] the stateless /api/v1/mcp surface answers every verb but POST with 405', () => { |
| 114 | + it('GET answers 405 with Allow: POST, not an SSE stream that ends at once', async () => { |
| 115 | + const runtime = new MCPServerRuntime({ name: 'objectstack-test', version: '9.9.9' }); |
| 116 | + const bridge = makeBridge(); |
| 117 | + const res = await runtime.handleHttpRequest(new Request(URL_, { method: 'GET', headers: CLIENT_GET_HEADERS }), { |
| 118 | + bridge, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(res.headers.get('content-type')).not.toContain('text/event-stream'); |
| 122 | + await expectMethodNotAllowed(res); |
| 123 | + expect(bridge.calls, 'a refused verb reads no bridge member').toEqual([]); |
| 124 | + }); |
| 125 | + |
| 126 | + it('DELETE answers 405 with Allow: POST: there is no session to end', async () => { |
| 127 | + const runtime = new MCPServerRuntime({ name: 'objectstack-test', version: '9.9.9' }); |
| 128 | + for (const headers of [{}, { 'mcp-session-id': 'never-issued', 'mcp-protocol-version': '2025-06-18' }]) { |
| 129 | + const res = await runtime.handleHttpRequest(new Request(URL_, { method: 'DELETE', headers }), { |
| 130 | + bridge: makeBridge(), |
| 131 | + }); |
| 132 | + await expectMethodNotAllowed(res); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + it('every other verb answers 405 with Allow: POST (the SDK used to name GET and DELETE in Allow)', async () => { |
| 137 | + const runtime = new MCPServerRuntime({ name: 'objectstack-test', version: '9.9.9' }); |
| 138 | + for (const method of ['PUT', 'PATCH', 'OPTIONS']) { |
| 139 | + const res = await runtime.handleHttpRequest(new Request(URL_, { method }), { bridge: makeBridge() }); |
| 140 | + await expectMethodNotAllowed(res); |
| 141 | + } |
| 142 | + const head = await runtime.handleHttpRequest(new Request(URL_, { method: 'HEAD' }), { bridge: makeBridge() }); |
| 143 | + expect(head.status).toBe(405); |
| 144 | + expect(head.headers.get('allow')).toBe('POST'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('POST is unchanged: initialize and tools/list round trips answer 200 with no session id', async () => { |
| 148 | + const runtime = new MCPServerRuntime({ name: 'objectstack-test', version: '9.9.9' }); |
| 149 | + const bridge = makeBridge(); |
| 150 | + |
| 151 | + const init = await post(runtime, INIT, bridge); |
| 152 | + expect(init.status).toBe(200); |
| 153 | + expect(init.headers.get('mcp-session-id')).toBeNull(); |
| 154 | + const initJson: any = await init.json(); |
| 155 | + expect(initJson.result.serverInfo).toEqual({ name: 'objectstack-test', version: '9.9.9' }); |
| 156 | + expect(initJson.result.capabilities.tools).toBeDefined(); |
| 157 | + |
| 158 | + const initialized = await post(runtime, { jsonrpc: '2.0', method: 'notifications/initialized' }, bridge); |
| 159 | + expect(initialized.status).toBe(202); |
| 160 | + |
| 161 | + const list = await post(runtime, { jsonrpc: '2.0', id: 1, method: 'tools/list' }, bridge); |
| 162 | + expect(list.status).toBe(200); |
| 163 | + const names = ((await list.json()) as any).result.tools.map((t: any) => t.name); |
| 164 | + expect(names).toContain('query_records'); |
| 165 | + expect(names).toContain('list_objects'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('the SDK client opens the GET stream once and never again, and its calls still work', async () => { |
| 169 | + const runtime = new MCPServerRuntime({ name: 'objectstack-test', version: '9.9.9' }); |
| 170 | + const bridge = makeBridge(); |
| 171 | + const seen: string[] = []; |
| 172 | + |
| 173 | + // Route the client's fetch straight into the surface, as a host's dispatcher does. |
| 174 | + const fetchIntoSurface = async (input: string | URL | Request, init?: RequestInit): Promise<Response> => { |
| 175 | + const request = new Request(input, init); |
| 176 | + seen.push(request.method); |
| 177 | + const text = request.method === 'POST' ? await request.text() : ''; |
| 178 | + return runtime.handleHttpRequest(request, { bridge, parsedBody: text ? JSON.parse(text) : undefined }); |
| 179 | + }; |
| 180 | + |
| 181 | + const transport = new StreamableHTTPClientTransport(new URL(URL_), { |
| 182 | + fetch: fetchIntoSurface as typeof fetch, |
| 183 | + // A reconnect as fast as the loop can go, so a dropped GET stream shows |
| 184 | + // up as many GETs inside the window below rather than one per second. |
| 185 | + reconnectionOptions: { |
| 186 | + initialReconnectionDelay: 5, |
| 187 | + maxReconnectionDelay: 5, |
| 188 | + reconnectionDelayGrowFactor: 1, |
| 189 | + maxRetries: 1000, |
| 190 | + }, |
| 191 | + }); |
| 192 | + const client = new Client({ name: 'verb-test', version: '1.0.0' }); |
| 193 | + const errors: unknown[] = []; |
| 194 | + client.onerror = (err) => errors.push(err); |
| 195 | + |
| 196 | + try { |
| 197 | + await client.connect(transport); |
| 198 | + const tools = await client.listTools(); |
| 199 | + expect(tools.tools.map((t) => t.name)).toContain('query_records'); |
| 200 | + |
| 201 | + await new Promise((resolve) => setTimeout(resolve, 250)); |
| 202 | + |
| 203 | + const gets = seen.filter((m) => m === 'GET').length; |
| 204 | + expect(gets, `the client opened the GET stream ${gets} times: ${seen.join(' ')}`).toBe(1); |
| 205 | + expect(errors, 'a 405 on GET is the "no stream offered" answer, not a client error').toEqual([]); |
| 206 | + } finally { |
| 207 | + await client.close(); |
| 208 | + } |
| 209 | + }); |
| 210 | +}); |
0 commit comments