From a2a30e85a24d1b9978ebc0a9bb38899fee9213d5 Mon Sep 17 00:00:00 2001 From: sjw1231 Date: Mon, 9 Nov 2026 16:30:34 +0800 Subject: [PATCH] fix(mcp): report calls to a closed generation as MCP_SERVER_UNAVAILABLE A tool of a closed connection generation stays registered until the next re-sync, and the SDK refuses its new calls unsent; they now fail as McpToolError MCP_SERVER_UNAVAILABLE instead of MCP_CONNECTION_CLOSED, matching resource requests. A redacted request failure attaches no cause, so unredacted upstream text does not survive in the cause chain. The client entry exports McpToolError, McpLocalError, and their code types. Refs #7869 --- packages/mcp/mcp-client/src/diagnostic.ts | 14 +++++++++----- packages/mcp/mcp-client/src/index.ts | 2 ++ packages/mcp/mcp-client/src/tools.ts | 4 ++++ packages/mcp/mcp-client/tests/diagnostic.spec.ts | 10 ++++++++++ packages/mcp/mcp-client/tests/reconnect.spec.ts | 9 +++++++++ packages/mcp/mcp-resources/tests/resources.spec.ts | 6 ++++-- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/packages/mcp/mcp-client/src/diagnostic.ts b/packages/mcp/mcp-client/src/diagnostic.ts index 632d40d569..a8821645ab 100644 --- a/packages/mcp/mcp-client/src/diagnostic.ts +++ b/packages/mcp/mcp-client/src/diagnostic.ts @@ -90,17 +90,18 @@ const SDK_ERROR_CODES: Partial> = { /** * Classify one failed MCP request. An {@link McpLocalError}, such as the * generation's closure reason, keeps its identity; any other failure becomes - * an {@link McpToolError} whose `message` carries the JSON-RPC or SDK code - * and whose `cause` is the original failure. + * an {@link McpToolError} whose `message` carries the JSON-RPC or SDK code. + * Without `redact` the original failure is the `cause`; with `redact` no + * `cause` is attached, because the original text may contain secret values. * @param error - the request's rejection. * @param operation - names the request, such as `tool "echo"` or `resources/read`. - * @param redact - removes secret values from the message; identity when templates are disabled. + * @param redact - removes secret values from the message when templates are enabled. * @returns the classified failure to throw. */ export function mcpRequestError( error: unknown, operation: string, - redact: (text: string) => string = text => text, + redact?: (text: string) => string, ): HarnessError { if (error instanceof McpLocalError) return error let code: McpToolErrorCode = 'MCP_REQUEST_FAILED' @@ -110,7 +111,10 @@ export function mcpRequestError( code = SDK_ERROR_CODES[error.code] ?? code detail = `${error.code}: ${error.message}` } - return new McpToolError(redact(`MCP ${operation} failed: ${detail}`), code, { cause: error }) + const message = `MCP ${operation} failed: ${detail}` + return redact === undefined + ? new McpToolError(message, code, { cause: error }) + : new McpToolError(redact(message), code) } /** Placeholder written in place of a known secret value. */ diff --git a/packages/mcp/mcp-client/src/index.ts b/packages/mcp/mcp-client/src/index.ts index d13119def6..a742954e05 100644 --- a/packages/mcp/mcp-client/src/index.ts +++ b/packages/mcp/mcp-client/src/index.ts @@ -28,6 +28,8 @@ import type { McpConnectionStatusHandle } from './status.ts' import type {} from '@deepseek-ai/dsh-tools' export { createMcpToolDefinition } from './tools.ts' +export { McpLocalError, McpToolError } from './diagnostic.ts' +export type { McpLocalErrorCode, McpToolErrorCode } from './diagnostic.ts' export type { McpResult, McpToolDefinitionOptions } from './tools.ts' export type { ReconnectConfig, ResolvedReconnectPolicy } from './connection.ts' export type { McpConnectionStatusHandle } from './status.ts' diff --git a/packages/mcp/mcp-client/src/tools.ts b/packages/mcp/mcp-client/src/tools.ts index 8e8d529c10..29cc18d2ab 100644 --- a/packages/mcp/mcp-client/src/tools.ts +++ b/packages/mcp/mcp-client/src/tools.ts @@ -158,6 +158,10 @@ export async function syncTools( outputSchema: tool.outputSchema, taskRequired: tool.execution?.taskSupport === 'required', call: async (args, execution) => { + // The generation closed and its tools await re-sync; the SDK would refuse the request unsent. + if (opts.signal.aborted) { + throw new McpToolError(`mcp-client(${opts.serverName}): server is disconnected`, 'MCP_SERVER_UNAVAILABLE') + } try { const result = await client.callTool( { name: tool.name, arguments: args }, diff --git a/packages/mcp/mcp-client/tests/diagnostic.spec.ts b/packages/mcp/mcp-client/tests/diagnostic.spec.ts index 9470f73c93..b300d887b7 100644 --- a/packages/mcp/mcp-client/tests/diagnostic.spec.ts +++ b/packages/mcp/mcp-client/tests/diagnostic.spec.ts @@ -74,6 +74,16 @@ describe('local failure codes', () => { const closed = new McpLocalError('MCP connection closed', 'MCP_CONNECTION_CLOSED') expect(mcpRequestError(closed, 'resources/read', () => 'replaced')).toBe(closed) }) + + it('keeps the original failure as cause only when no redaction applies', () => { + const failure = new Error('rejected secret-value') + expect(mcpRequestError(failure, 'resources/read')).toMatchObject({ + message: 'MCP resources/read failed: Error: rejected secret-value', cause: failure, + }) + const redacted = mcpRequestError(failure, 'resources/read', text => text.replace('secret-value', '[redacted]')) + expect(redacted.message).toBe('MCP resources/read failed: Error: rejected [redacted]') + expect(redacted.cause).toBeUndefined() + }) }) describe('redactSecrets', () => { diff --git a/packages/mcp/mcp-client/tests/reconnect.spec.ts b/packages/mcp/mcp-client/tests/reconnect.spec.ts index 671892ce40..b2bc2d3520 100644 --- a/packages/mcp/mcp-client/tests/reconnect.spec.ts +++ b/packages/mcp/mcp-client/tests/reconnect.spec.ts @@ -310,6 +310,15 @@ describe('reconnect supervisor', () => { message: 'MCP connection closed', info: { name: 'McpLocalError', code: 'MCP_CONNECTION_CLOSED' }, }) + + // The closed generation's tools stay registered until re-sync; a new call is refused unsent. + const calls = mockCallTool.mock.calls.length + const unsent = await executeToolFixture(ctx, { name: 'mcp__srv__remote', arguments: {}, callId: nextCallId(), signal: testToolSignal }) + expect(unsent.error).toEqual({ + message: 'mcp-client(srv): server is disconnected', + info: { name: 'McpToolError', code: 'MCP_SERVER_UNAVAILABLE' }, + }) + expect(mockCallTool).toHaveBeenCalledTimes(calls) } finally { await handle.dispose() } diff --git a/packages/mcp/mcp-resources/tests/resources.spec.ts b/packages/mcp/mcp-resources/tests/resources.spec.ts index 992d591e14..0158761ec9 100644 --- a/packages/mcp/mcp-resources/tests/resources.spec.ts +++ b/packages/mcp/mcp-resources/tests/resources.spec.ts @@ -342,8 +342,10 @@ describe('MCP resource tools', () => { expect((await call(ctx, 'read_mcp_resource', { uri: 'docs://text' })).isError).toBe(true) const result = await call(ctx, 'read_mcp_resource', { server: 'missing', uri: 'docs://text' }) expect(result.isError).toBe(true) - expect(JSON.stringify(result.content)).toContain('unavailable in this agent') - expect(result.error?.info).toEqual({ name: 'ToolArgsError', code: 'INVALID_ARGS' }) + expect(result.error).toEqual({ + message: 'invalid arguments: server: MCP resource server "missing" is unavailable in this agent\'s scope', + info: { name: 'ToolArgsError', code: 'INVALID_ARGS' }, + }) }) it('resolves scoped providers and removes only the disposed registration', async () => {