From a38e9b40d035683b6e707bd542d933bb0a2d907a Mon Sep 17 00:00:00 2001 From: trustfundkid Date: Mon, 30 Nov 2026 22:26:14 +0600 Subject: [PATCH] fix(tools-call-error): fail as untestable when test_error_handling is not advertised Previously, tools-call-error called tools/call directly and accepted any CallToolResult with isError: true and text content. Against a server that does not implement test_error_handling, SDKs returning a "tool not found" CallToolResult with isError: true caused a false positive. Check paginated tools/list first and report untestableCheck (FAILURE) per issue #248 when test_error_handling is missing, while preserving compatibility with servers whose test_error_handling fixture returns any non-empty error text. Fixes #604 --- src/scenarios/server/tools.test.ts | 159 ++++++++++++++++++++++++++++- src/scenarios/server/tools.ts | 95 +++++++++++++---- 2 files changed, 233 insertions(+), 21 deletions(-) diff --git a/src/scenarios/server/tools.test.ts b/src/scenarios/server/tools.test.ts index c8f72ad4..7cb89f8e 100644 --- a/src/scenarios/server/tools.test.ts +++ b/src/scenarios/server/tools.test.ts @@ -1,8 +1,9 @@ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, vi } from 'vitest'; import { buildToolsListDeterministicOrderCheck, buildToolsNameFormatCheck, toolNameFormatCheckApplies, + ToolsCallErrorScenario, ToolsListScenario, validateToolNameFormat } from './tools.js'; @@ -334,3 +335,159 @@ describe('buildToolsListDeterministicOrderCheck', () => { expect(check.source).toEqual({ introducedIn: '2026-07-28' }); }); }); + +describe('ToolsCallErrorScenario', () => { + function mockContext( + requestImpl: (method: string, params?: unknown) => Promise + ): { + ctx: RunContext; + close: ReturnType; + request: ReturnType; + } { + const close = vi.fn().mockResolvedValue(undefined); + const request = vi.fn(requestImpl); + const connection: Connection = { + notifications: [], + request: request as unknown as Connection['request'], + discover: async () => ({}), + close + }; + + return { + ctx: { + serverUrl: 'http://example.test/mcp', + specVersion: '2025-11-25', + connect: async () => connection + }, + close, + request + }; + } + + it('fails as untestable when the server does not advertise test_error_handling (#604)', async () => { + const { ctx, close, request } = mockContext(async (method) => { + if (method === 'tools/list') { + return { tools: [{ name: 'get_forecast' }] }; + } + // Even if a server would answer tools/call with an SDK "tool not found" + // CallToolResult (isError: true + text), the scenario must not accept it. + return { + isError: true, + content: [ + { + type: 'text', + text: 'MCP error -32602: Tool test_error_handling not found' + } + ] + }; + }); + + const checks = await new ToolsCallErrorScenario().run(ctx); + + expect(checks).toHaveLength(1); + expect(checks[0]).toMatchObject({ + id: 'tools-call-error', + status: 'FAILURE', + details: { untestable: true } + }); + expect(checks[0]?.errorMessage).toMatch(/^Not testable: /); + expect(checks[0]?.errorMessage).toContain('test_error_handling'); + expect(request).toHaveBeenCalledTimes(1); + expect(request).toHaveBeenCalledWith('tools/list', undefined); + expect(close).toHaveBeenCalledTimes(1); + }); + + it('finds test_error_handling across paginated tools/list and succeeds on isError: true with text', async () => { + const { ctx, close, request } = mockContext(async (method, params) => { + if (method === 'tools/list') { + const cursor = (params as { cursor?: string } | undefined)?.cursor; + if (!cursor) { + return { tools: [{ name: 'other_tool' }], nextCursor: 'page-2' }; + } + return { tools: [{ name: 'test_error_handling' }] }; + } + return { + isError: true, + content: [ + { + type: 'text', + text: 'Intentional fixture failure' + } + ] + }; + }); + + const checks = await new ToolsCallErrorScenario().run(ctx); + + expect(checks).toHaveLength(1); + expect(checks[0]?.id).toBe('tools-call-error'); + expect(checks[0]?.status).toBe('SUCCESS'); + expect(request).toHaveBeenCalledWith('tools/list', undefined); + expect(request).toHaveBeenCalledWith('tools/list', { cursor: 'page-2' }); + expect(request).toHaveBeenCalledWith('tools/call', { + name: 'test_error_handling', + arguments: {} + }); + expect(close).toHaveBeenCalledTimes(1); + }); + + it('fails when test_error_handling is advertised but returns isError: false', async () => { + const { ctx, close } = mockContext(async (method) => { + if (method === 'tools/list') { + return { tools: [{ name: 'test_error_handling' }] }; + } + return { + isError: false, + content: [{ type: 'text', text: 'not an error' }] + }; + }); + + const checks = await new ToolsCallErrorScenario().run(ctx); + + expect(checks[0]).toMatchObject({ + id: 'tools-call-error', + status: 'FAILURE', + errorMessage: 'Tool did not return isError: true' + }); + expect(close).toHaveBeenCalledTimes(1); + }); + + it('fails when test_error_handling returns isError: true with empty or whitespace text', async () => { + const { ctx, close } = mockContext(async (method) => { + if (method === 'tools/list') { + return { tools: [{ name: 'test_error_handling' }] }; + } + return { + isError: true, + content: [{ type: 'text', text: ' ' }] + }; + }); + + const checks = await new ToolsCallErrorScenario().run(ctx); + + expect(checks[0]).toMatchObject({ + id: 'tools-call-error', + status: 'FAILURE', + errorMessage: 'Error result missing error message' + }); + expect(close).toHaveBeenCalledTimes(1); + }); + + it('closes the connection when tools/call throws', async () => { + const { ctx, close } = mockContext(async (method) => { + if (method === 'tools/list') { + return { tools: [{ name: 'test_error_handling' }] }; + } + throw new Error('transport disconnected'); + }); + + const checks = await new ToolsCallErrorScenario().run(ctx); + + expect(checks[0]).toMatchObject({ + id: 'tools-call-error', + status: 'FAILURE', + errorMessage: 'Failed: transport disconnected' + }); + expect(close).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/scenarios/server/tools.ts b/src/scenarios/server/tools.ts index fd423f1d..03d77322 100644 --- a/src/scenarios/server/tools.ts +++ b/src/scenarios/server/tools.ts @@ -735,6 +735,44 @@ Implement tool \`test_tool_with_logging\` with no arguments. } } +const TOOLS_CALL_ERROR_TOOL_NAME = 'test_error_handling'; +const TOOLS_CALL_ERROR_MAX_LIST_PAGES = 50; +const TOOLS_CALL_ERROR_SPEC_REFS = [ + { + id: 'MCP-Error-Handling', + url: 'https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle' + } +]; + +async function serverAdvertisesTool( + conn: Awaited>, + toolName: string +): Promise { + let cursor: string | undefined; + const seenCursors = new Set(); + + for (let page = 0; page < TOOLS_CALL_ERROR_MAX_LIST_PAGES; page++) { + const listResult = await conn.request( + 'tools/list', + cursor !== undefined ? { cursor } : undefined + ); + if ( + Array.isArray(listResult?.tools) && + listResult.tools.some((tool) => tool?.name === toolName) + ) { + return true; + } + const nextCursor = listResult?.nextCursor; + if (!nextCursor || seenCursors.has(nextCursor)) { + return false; + } + seenCursors.add(nextCursor); + cursor = nextCursor; + } + + return false; +} + export class ToolsCallErrorScenario implements ClientScenario { name = 'tools-call-error'; readonly source = { introducedIn: '2025-06-18' } as const; @@ -762,19 +800,46 @@ Implement tool \`test_error_handling\` with no arguments. async run(ctx: RunContext): Promise { const checks: ConformanceCheck[] = []; + let conn: Awaited> | undefined; try { - const conn = await ctx.connect(); + conn = await ctx.connect(); + + // Verify the server actually advertises the fixture tool before calling it; + // otherwise an SDK's "tool not found" CallToolResult (which also sets + // isError: true with text content) produces a false positive (#604). + const hasFixtureTool = await serverAdvertisesTool( + conn, + TOOLS_CALL_ERROR_TOOL_NAME + ); + if (!hasFixtureTool) { + checks.push( + untestableCheck( + 'tools-call-error', + 'ToolsCallError', + 'Tool returns error correctly', + `server does not advertise the required '${TOOLS_CALL_ERROR_TOOL_NAME}' fixture tool`, + TOOLS_CALL_ERROR_SPEC_REFS, + 'FAILURE' + ) + ); + return checks; + } const result: any = await conn.request('tools/call', { - name: 'test_error_handling', + name: TOOLS_CALL_ERROR_TOOL_NAME, arguments: {} }); - // Check if result has isError flag - const hasIsError = result.isError === true; - const hasContent = result.content && result.content.length > 0; - const hasErrorMessage = hasContent && result.content[0].text; + // Check if result has isError flag and non-empty text error message + const hasIsError = result?.isError === true; + const content = Array.isArray(result?.content) ? result.content : []; + const hasErrorMessage = content.some( + (item: any) => + item?.type === 'text' && + typeof item.text === 'string' && + item.text.trim().length > 0 + ); checks.push({ id: 'tools-call-error', @@ -787,18 +852,11 @@ Implement tool \`test_error_handling\` with no arguments. : !hasErrorMessage ? 'Error result missing error message' : undefined, - specReferences: [ - { - id: 'MCP-Error-Handling', - url: 'https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle' - } - ], + specReferences: TOOLS_CALL_ERROR_SPEC_REFS, details: { result } }); - - await conn.close(); } catch (error) { checks.push({ id: 'tools-call-error', @@ -807,13 +865,10 @@ Implement tool \`test_error_handling\` with no arguments. status: 'FAILURE', timestamp: new Date().toISOString(), errorMessage: `Failed: ${error instanceof Error ? error.message : String(error)}`, - specReferences: [ - { - id: 'MCP-Error-Handling', - url: 'https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle' - } - ] + specReferences: TOOLS_CALL_ERROR_SPEC_REFS }); + } finally { + await conn?.close(); } return checks;