Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 158 additions & 1 deletion src/scenarios/server/tools.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -334,3 +335,159 @@ describe('buildToolsListDeterministicOrderCheck', () => {
expect(check.source).toEqual({ introducedIn: '2026-07-28' });
});
});

describe('ToolsCallErrorScenario', () => {
function mockContext(
requestImpl: (method: string, params?: unknown) => Promise<unknown>
): {
ctx: RunContext;
close: ReturnType<typeof vi.fn>;
request: ReturnType<typeof vi.fn>;
} {
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);
});
});
95 changes: 75 additions & 20 deletions src/scenarios/server/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<RunContext['connect']>>,
toolName: string
): Promise<boolean> {
let cursor: string | undefined;
const seenCursors = new Set<string>();

for (let page = 0; page < TOOLS_CALL_ERROR_MAX_LIST_PAGES; page++) {
const listResult = await conn.request<ListToolsResult>(
'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;
Expand Down Expand Up @@ -762,19 +800,46 @@ Implement tool \`test_error_handling\` with no arguments.

async run(ctx: RunContext): Promise<ConformanceCheck[]> {
const checks: ConformanceCheck[] = [];
let conn: Awaited<ReturnType<RunContext['connect']>> | 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<CallToolResult>('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',
Expand All @@ -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',
Expand All @@ -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;
Expand Down