diff --git a/CHANGELOG.md b/CHANGELOG.md index 7861d820..1a221751 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Repeated `connect -H` headers are all sent again. ### Fixed - `connect` with several `-H`/`--header` options now stores and sends every header, not just the last one. (#490) +- After a crashed bridge resumes its MCP session, `tools-list`, `prompts-list`, `resources-list` and `resources-templates-list` list the server's items again instead of returning an empty list, `tools-get` finds the tool, and tool results are checked against their output schema again. The SDK client learns no server capabilities when it skips the handshake on resumption, so mcpc now gives it the ones saved at connect. (#484) ## [0.8.0] - 2026-11-04 diff --git a/src/bridge/index.ts b/src/bridge/index.ts index 8199fdc6..39e802a6 100644 --- a/src/bridge/index.ts +++ b/src/bridge/index.ts @@ -845,6 +845,9 @@ class BridgeProcess { }), ...(sessionData._meta && { _meta: sessionData._meta }), }; + // The SDK client gates list requests (and more, see restoreServerCapabilities) on + // these capabilities; without them every list comes back empty (#484). + this.client.restoreServerCapabilities(sessionData.capabilities); } // Update session with protocol version, MCP session ID, and lastSeenAt diff --git a/src/core/mcp-client.ts b/src/core/mcp-client.ts index a8ed66f0..b43ccc25 100644 --- a/src/core/mcp-client.ts +++ b/src/core/mcp-client.ts @@ -29,6 +29,7 @@ import type { CompleteRequestParams, CompleteResult, LoggingLevel, + ServerCapabilities, Tool, } from '@modelcontextprotocol/client'; import { @@ -310,6 +311,16 @@ class TasksAwareClient extends SDKClient { } } +/** + * The SDK client state its handshake fills in, which a resumed connection never gets + * (see McpClient.restoreServerCapabilities). + */ +interface SdkClientHandshakeState { + _serverCapabilities: ServerCapabilities | undefined; + _listChangedConfig: ClientOptions['listChanged']; + _setupListChangedHandlers(config: NonNullable): void; +} + /** * MCP Client wrapper class * Provides a convenient interface to the MCP SDK Client with error handling and logging @@ -573,6 +584,33 @@ export class McpClient implements IMcpClient { return Promise.resolve(details); } + /** + * Give the SDK client the server capabilities of a resumed server-side session. + * + * Resuming by MCP session ID skips the handshake, so the SDK client never learns the + * server's capabilities. Its list methods then return an empty list without sending a + * request, it registers no list-changed handlers, and tool output schemas go + * unchecked because no `tools/list` result is ever cached (#484). The bridge passes + * the capabilities persisted by the connect that ran the handshake. Over Streamable + * HTTP the list-changed handlers still receive nothing, because resumption also skips + * the `notifications/initialized` that makes the SDK transport open its standalone + * SSE stream. + * + * The SDK has no public way to resume with known capabilities, so this sets the field + * the handshake would have set and registers the handlers it would have registered. + * Both are internals of the pinned SDK version; `test/unit/core/resume-capabilities.test.ts` + * runs against the real SDK client and fails if they change. + */ + restoreServerCapabilities(capabilities: ServerCapabilities | undefined): void { + if (!capabilities || this.client.getServerCapabilities() !== undefined) return; + const sdkClient = this.client as unknown as SdkClientHandshakeState; + sdkClient._serverCapabilities = capabilities; + if (sdkClient._listChangedConfig) { + sdkClient._setupListChangedHandlers(sdkClient._listChangedConfig); + } + this.logger.debug('Restored server capabilities of the resumed session:', capabilities); + } + /** * Protocol era of the connection: 'modern' for 2026-07-28+ (negotiated via * server/discover), 'legacy' for the 2025-era initialize handshake. diff --git a/test/e2e/suites/sessions/resume.test.sh b/test/e2e/suites/sessions/resume.test.sh index 03243ab9..fb89f65c 100755 --- a/test/e2e/suites/sessions/resume.test.sh +++ b/test/e2e/suites/sessions/resume.test.sh @@ -114,6 +114,28 @@ assert_json "$STDOUT" '.sessions[0].instructions | type == "string"' \ "grep should still match the server instructions after resume" test_pass +# Test: list commands still reach the server after resume. The SDK client gates +# them on capabilities it only learns from the handshake, so without the persisted +# ones they all return an empty list without sending a request (#484). +test_case "list commands return the server's items after resume" +run_mcpc "$SESSION" tools-list --json +assert_success +assert_json "$STDOUT" 'map(.name) | index("echo") != null' "tools-list should include echo" +run_mcpc "$SESSION" prompts-list --json +assert_success +assert_json "$STDOUT" 'map(.name) | index("greeting") != null' "prompts-list should include greeting" +run_mcpc "$SESSION" resources-list --json +assert_success +assert_json "$STDOUT" 'map(.uri) | index("test://dynamic/counter") != null' \ + "resources-list should include the counter resource" +run_mcpc "$SESSION" resources-templates-list --json +assert_success +assert_json "$STDOUT" 'map(.uriTemplate) | index("test://file/{path}") != null' \ + "resources-templates-list should include the file template" +run_mcpc "$SESSION" tools-get echo +assert_success +test_pass + # Test: the resources.subscribe capability check accepts a resumed session test_case "resources-subscribe works after resume" subscribe_uri="test://dynamic/counter" diff --git a/test/unit/core/resume-capabilities.test.ts b/test/unit/core/resume-capabilities.test.ts new file mode 100644 index 00000000..1485fe85 --- /dev/null +++ b/test/unit/core/resume-capabilities.test.ts @@ -0,0 +1,109 @@ +/** + * McpClient.restoreServerCapabilities against the real SDK client (#484). + * + * A connection whose transport already carries an MCP session ID skips the handshake, + * so the SDK client learns no server capabilities. restoreServerCapabilities fills in + * the SDK client state that the handshake would have set. Those are SDK internals, so + * this suite drives the real SDK client (unlike mcp-client.test.ts, which stubs it) and + * fails if a later SDK version renames or drops them. + */ + +import { vi } from 'vitest'; +import { InMemoryTransport } from '@modelcontextprotocol/client'; +import { Server } from '@modelcontextprotocol/sdk/server/index.js'; +import { + CallToolRequestSchema, + ListPromptsRequestSchema, + ListToolsRequestSchema, +} from '@modelcontextprotocol/sdk/types.js'; +import { McpClient } from '../../../src/core/mcp-client.js'; + +const CAPABILITIES = { tools: { listChanged: true }, prompts: {} }; + +const ECHO_TOOL = { + name: 'echo', + inputSchema: { type: 'object' as const, properties: {} }, + outputSchema: { + type: 'object' as const, + properties: { message: { type: 'string' } }, + required: ['message'], + }, +}; + +/** A legacy-era server, and an McpClient connected to it as if resuming a session. */ +async function resumedConnection(onToolsChanged = vi.fn()): Promise<{ + server: Server; + client: McpClient; +}> { + const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: CAPABILITIES }); + server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: [ECHO_TOOL] })); + server.setRequestHandler(ListPromptsRequestSchema, () => ({ prompts: [{ name: 'greeting' }] })); + // Breaks the tool's own output schema, which a validating client must reject. + server.setRequestHandler(CallToolRequestSchema, () => ({ + content: [], + structuredContent: { message: 42 }, + })); + + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); + await server.connect(serverTransport); + // A transport that carries a session ID makes the SDK skip the handshake. + (clientTransport as { sessionId?: string }).sessionId = 'resumed-session'; + + const client = new McpClient( + { name: 'test', version: '0.0.0' }, + { + protocolVersion: '2025-11-25', + listChanged: { tools: { autoRefresh: false, onChanged: onToolsChanged } }, + } + ); + await client.connect(clientTransport); + return { server, client }; +} + +describe('resumed session capabilities (real SDK client)', () => { + it('lists nothing before the capabilities are restored (the bug)', async () => { + const { client } = await resumedConnection(); + await expect(client.listTools()).resolves.toEqual({ tools: [] }); + await client.close(); + }); + + it('lists the server items once the capabilities are restored', async () => { + const { client } = await resumedConnection(); + client.restoreServerCapabilities(CAPABILITIES); + + const { tools } = await client.listTools(); + expect(tools.map((tool) => tool.name)).toEqual(['echo']); + const { prompts } = await client.listPrompts(); + expect(prompts.map((prompt) => prompt.name)).toEqual(['greeting']); + await expect(client.getServerDetails()).resolves.toMatchObject({ capabilities: CAPABILITIES }); + await client.close(); + }); + + it('handles list-changed notifications once the capabilities are restored', async () => { + const onToolsChanged = vi.fn(); + const { server, client } = await resumedConnection(onToolsChanged); + client.restoreServerCapabilities(CAPABILITIES); + + await server.sendToolListChanged(); + await vi.waitFor(() => expect(onToolsChanged).toHaveBeenCalled()); + await client.close(); + }); + + it('checks tool results against the output schema again', async () => { + const { client } = await resumedConnection(); + client.restoreServerCapabilities(CAPABILITIES); + + await client.listTools(); + await expect(client.callTool('echo', {})).rejects.toThrow(/output schema/i); + await client.close(); + }); + + it('does not replace capabilities the SDK client already has', async () => { + const { client } = await resumedConnection(); + client.restoreServerCapabilities(CAPABILITIES); + client.restoreServerCapabilities({ resources: {} }); + + await expect(client.getServerDetails()).resolves.toMatchObject({ capabilities: CAPABILITIES }); + await client.close(); + }); +});