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
This commit is contained in:
1 parent
90e5e5eae9
commit
a2a30e85a2
6 files changed
+38
-7
No files matched your search
@@ -90,17 +90,18 @@ const SDK_ERROR_CODES: Partial<Record<SdkErrorCode, McpToolErrorCode>> = {
|
||||
/**
|
||||
* 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. */
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in new issue
Block a user