From 9488899a49763c1d132646f0d4def79eb419d3dd Mon Sep 17 00:00:00 2001 From: Ahmad Faraj Date: Mon, 16 Nov 2026 09:22:45 +0200 Subject: [PATCH 1/2] feat(server): add tools-call-input-validation scenario (SEP-1303) Servers MUST validate all tool inputs (server/tools, security considerations), and since 2025-11-25 input validation errors are tool execution errors reported with isError: true. No scenario sent arguments that violate a tool's inputSchema. The scenario calls a new everything-server fixture, test_input_validation (required integer 1-10), once with valid arguments as a control and then with a wrong type, a missing required property and an out-of-range value: - tools-call-input-validation-valid: the valid call returns a text result - sep-1303-input-validation-rejected (FAILURE, MUST): no invalid call executes; an isError result or a JSON-RPC error both pass - sep-1303-input-validation-tool-error (WARNING, keyword-less): each rejection is an isError result with text content Adds a low-level-Server negative example with a vitest case, and src/seps/sep-1303.yaml. Closes #588 Signed-off-by: Ahmad Faraj --- .../servers/typescript/everything-server.ts | 22 ++ .../servers/typescript/no-input-validation.ts | 83 ++++++ src/scenarios/index.ts | 2 + src/scenarios/server/negative.test.ts | 42 +++ .../server/tools-input-validation.ts | 254 ++++++++++++++++++ src/seps/sep-1303.yaml | 10 + 6 files changed, 413 insertions(+) create mode 100644 examples/servers/typescript/no-input-validation.ts create mode 100644 src/scenarios/server/tools-input-validation.ts create mode 100644 src/seps/sep-1303.yaml diff --git a/examples/servers/typescript/everything-server.ts b/examples/servers/typescript/everything-server.ts index c76c1f56..cf2900d6 100644 --- a/examples/servers/typescript/everything-server.ts +++ b/examples/servers/typescript/everything-server.ts @@ -313,6 +313,28 @@ function createMcpServer() { } ); + // Input validation tool (SEP-1303): the SDK validates arguments against + // this schema before the handler runs. + mcpServer.registerTool( + 'test_input_validation', + { + description: 'Tests that schema-invalid arguments are rejected', + inputSchema: { + value: z + .number() + .int() + .min(1) + .max(10) + .describe('An integer from 1 to 10') + } + }, + async (args: { value: number }) => { + return { + content: [{ type: 'text', text: `executed: ${args.value}` }] + }; + } + ); + // Image content tool mcpServer.registerTool( 'test_image_content', diff --git a/examples/servers/typescript/no-input-validation.ts b/examples/servers/typescript/no-input-validation.ts new file mode 100644 index 00000000..177a0ea2 --- /dev/null +++ b/examples/servers/typescript/no-input-validation.ts @@ -0,0 +1,83 @@ +#!/usr/bin/env node + +/** + * Negative fixture for tools-call-input-validation (SEP-1303): publishes an + * inputSchema for test_input_validation but, like any handler on the + * low-level Server API, never checks tools/call arguments against it, so the + * tool runs on schema-invalid input and the check can be shown to emit + * FAILURE. + */ + +import { Server } from '@modelcontextprotocol/sdk/server/index.js'; +import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js'; +import { + CallToolRequestSchema, + ListToolsRequestSchema +} from '@modelcontextprotocol/sdk/types.js'; +import express from 'express'; + +function createServer() { + const server = new Server( + { name: 'no-input-validation', version: '1.0.0' }, + { capabilities: { tools: {} } } + ); + + server.setRequestHandler(ListToolsRequestSchema, async () => ({ + tools: [ + { + name: 'test_input_validation', + description: 'Publishes a schema but does not enforce it', + inputSchema: { + type: 'object', + properties: { + value: { type: 'integer', minimum: 1, maximum: 10 } + }, + required: ['value'] + } + } + ] + })); + + server.setRequestHandler(CallToolRequestSchema, async (request) => ({ + content: [ + { + type: 'text', + text: `executed: ${JSON.stringify(request.params.arguments?.value)}` + } + ] + })); + + return server; +} + +const app = express(); +app.use(express.json()); + +app.post('/mcp', async (req, res) => { + try { + const server = createServer(); + const transport = new StreamableHTTPServerTransport({ + sessionIdGenerator: undefined + }); + await server.connect(transport); + await transport.handleRequest(req, res, req.body); + } catch (error) { + if (!res.headersSent) { + res.status(500).json({ + jsonrpc: '2.0', + error: { + code: -32603, + message: `Internal error: ${error instanceof Error ? error.message : String(error)}` + }, + id: null + }); + } + } +}); + +const PORT = parseInt(process.env.PORT || '3010', 10); +app.listen(PORT, '127.0.0.1', () => { + console.log( + `No input validation negative test server running on http://localhost:${PORT}/mcp` + ); +}); diff --git a/src/scenarios/index.ts b/src/scenarios/index.ts index ff377f54..4f835d54 100644 --- a/src/scenarios/index.ts +++ b/src/scenarios/index.ts @@ -1,3 +1,4 @@ +import { ToolsCallInputValidationScenario } from './server/tools-input-validation'; import { Scenario, ClientScenario, @@ -190,6 +191,7 @@ const allClientScenariosList: ClientScenario[] = [ new ToolsCallMultipleContentTypesScenario(), new ToolsCallWithLoggingScenario(), new ToolsCallErrorScenario(), + new ToolsCallInputValidationScenario(), new ToolsCallWithProgressScenario(), new ToolsCallSamplingScenario(), new ToolsCallElicitationScenario(), diff --git a/src/scenarios/server/negative.test.ts b/src/scenarios/server/negative.test.ts index 746cf9ce..718bfc73 100644 --- a/src/scenarios/server/negative.test.ts +++ b/src/scenarios/server/negative.test.ts @@ -7,6 +7,7 @@ import { DNSRebindingProtectionScenario } from './dns-rebinding'; import { ResourcesNotFoundErrorScenario } from './resources'; import { CachingScenario } from './caching'; import { ToolsListScenario } from './tools'; +import { ToolsCallInputValidationScenario } from './tools-input-validation'; import { JsonSchema2020_12Scenario, sep2106KeywordCheckStatus @@ -465,6 +466,47 @@ describe('Server scenario negative tests', () => { }, 10000); }); + describe('tools-call-input-validation (SEP-1303)', () => { + let serverProcess: ChildProcess | null = null; + const PORT = 3010; + + beforeAll(async () => { + serverProcess = await startServer( + path.join( + process.cwd(), + 'examples/servers/typescript/no-input-validation.ts' + ), + PORT + ); + }, 35000); + + afterAll(async () => { + await stopServer(serverProcess); + }); + + it('emits FAILURE for input-validation-rejected against a server that executes tools on schema-invalid arguments', async () => { + const scenario = new ToolsCallInputValidationScenario(); + const checks = await scenario.run( + testContext(`http://localhost:${PORT}/mcp`, '2025-11-25') + ); + + expect( + checks.find((c) => c.id === 'tools-call-input-validation-valid')?.status + ).toBe('SUCCESS'); + const rejected = checks.find( + (c) => c.id === 'sep-1303-input-validation-rejected' + ); + expect(rejected?.status).toBe('FAILURE'); + expect(rejected?.errorMessage).toContain('wrong type'); + expect(rejected?.errorMessage).toContain('missing required property'); + expect(rejected?.errorMessage).toContain('out of range'); + expect( + checks.find((c) => c.id === 'sep-1303-input-validation-tool-error') + ?.status + ).toBe('WARNING'); + }, 15000); + }); + describe('tools-name-format', () => { let serverProcess: ChildProcess | null = null; const PORT = 3009; diff --git a/src/scenarios/server/tools-input-validation.ts b/src/scenarios/server/tools-input-validation.ts new file mode 100644 index 00000000..193e43f8 --- /dev/null +++ b/src/scenarios/server/tools-input-validation.ts @@ -0,0 +1,254 @@ +/** + * tools/call input validation (SEP-1303; "Servers MUST validate all tool + * inputs", server/tools#security-considerations). + */ + +import type { + ClientScenario, + ConformanceCheck, + SpecReference +} from '../../types'; +import { + JsonRpcError, + type Connection, + type RunContext +} from '../../connection'; +import { untestableCheck } from '../untestable'; + +const TOOL = 'test_input_validation'; + +const SECURITY_REF: SpecReference = { + id: 'MCP-Tools-Security', + url: 'https://modelcontextprotocol.io/specification/2025-11-25/server/tools#security-considerations' +}; +const ERROR_HANDLING_REF: SpecReference = { + id: 'SEP-1303', + url: 'https://modelcontextprotocol.io/specification/2025-11-25/server/tools#error-handling' +}; + +type Outcome = + | { kind: 'executed'; result: unknown } + | { kind: 'tool-error'; result: unknown } + | { kind: 'protocol-error'; code: number; message: string } + | { kind: 'transport-error'; message: string }; + +const INVALID_CASES: ReadonlyArray<{ + label: string; + args: Record; +}> = [ + { label: 'wrong type (string for integer)', args: { value: 'five' } }, + { label: 'missing required property', args: {} }, + { label: 'out of range (maximum 10)', args: { value: 11 } } +]; + +async function callTool( + conn: Connection, + args: Record +): Promise { + try { + const result = await conn.request>('tools/call', { + name: TOOL, + arguments: args + }); + return result?.isError === true + ? { kind: 'tool-error', result } + : { kind: 'executed', result }; + } catch (e) { + if (e instanceof JsonRpcError) { + return { kind: 'protocol-error', code: e.code, message: e.message }; + } + return { + kind: 'transport-error', + message: e instanceof Error ? e.message : String(e) + }; + } +} + +function hasTextContent(result: unknown): boolean { + const content = (result as { content?: unknown })?.content; + return ( + Array.isArray(content) && + content.some( + (c: any) => c?.type === 'text' && typeof c.text === 'string' && c.text + ) + ); +} + +export class ToolsCallInputValidationScenario implements ClientScenario { + name = 'tools-call-input-validation'; + readonly source = { introducedIn: '2025-11-25' } as const; + description = `Test that a server validates tool arguments against the tool's inputSchema. + +**Server Implementation Requirements:** + +Implement tool \`${TOOL}\` with \`inputSchema\`: + +\`\`\`json +{ + "type": "object", + "properties": { "value": { "type": "integer", "minimum": 1, "maximum": 10 } }, + "required": ["value"] +} +\`\`\` + +A valid call returns a text result. The harness then sends arguments that +violate the schema (wrong type, missing required property, out of range). +Servers MUST validate all tool inputs, so none of these may execute the tool. +Input validation errors are Tool Execution Errors: they are reported in the +tool result with \`isError: true\`, not as a JSON-RPC error.`; + + async run(ctx: RunContext): Promise { + let conn: Connection; + try { + conn = await ctx.connect(); + } catch (e) { + const reason = `could not connect: ${e instanceof Error ? e.message : String(e)}`; + return [ + untestableCheck( + 'tools-call-input-validation-valid', + 'ToolsCallInputValidationValid', + 'A call with schema-valid arguments returns a result', + reason, + [ERROR_HANDLING_REF] + ), + untestableCheck( + 'sep-1303-input-validation-rejected', + 'ToolsCallInputValidationRejected', + 'Schema-invalid arguments are not executed', + reason, + [SECURITY_REF] + ), + untestableCheck( + 'sep-1303-input-validation-tool-error', + 'ToolsCallInputValidationToolError', + 'Input validation errors are reported as tool execution errors', + reason, + [ERROR_HANDLING_REF], + 'WARNING' + ) + ]; + } + try { + return await this.runChecks(conn); + } finally { + await conn.close().catch(() => {}); + } + } + + private async runChecks(conn: Connection): Promise { + const checks: ConformanceCheck[] = []; + const ts = () => new Date().toISOString(); + + // Control: a valid call must succeed, or the invalid probes prove nothing. + const valid = await callTool(conn, { value: 5 }); + const validOk = valid.kind === 'executed' && hasTextContent(valid.result); + if (!validOk) { + const reason = `valid call to ${TOOL} with {"value": 5} did not return a text result (${valid.kind}${ + valid.kind === 'protocol-error' + ? ` ${valid.code}: ${valid.message}` + : '' + }${valid.kind === 'transport-error' ? `: ${valid.message}` : ''})`; + checks.push({ + id: 'tools-call-input-validation-valid', + name: 'ToolsCallInputValidationValid', + description: 'A call with schema-valid arguments returns a result', + status: 'FAILURE', + timestamp: ts(), + errorMessage: reason, + specReferences: [ERROR_HANDLING_REF], + details: { outcome: valid } + }); + checks.push( + untestableCheck( + 'sep-1303-input-validation-rejected', + 'ToolsCallInputValidationRejected', + 'Schema-invalid arguments are not executed', + reason, + [SECURITY_REF] + ), + untestableCheck( + 'sep-1303-input-validation-tool-error', + 'ToolsCallInputValidationToolError', + 'Input validation errors are reported as tool execution errors', + reason, + [ERROR_HANDLING_REF], + 'WARNING' + ) + ); + return checks; + } + checks.push({ + id: 'tools-call-input-validation-valid', + name: 'ToolsCallInputValidationValid', + description: 'A call with schema-valid arguments returns a result', + status: 'SUCCESS', + timestamp: ts(), + specReferences: [ERROR_HANDLING_REF], + details: { outcome: valid } + }); + + const cases: Array<{ label: string; args: unknown; outcome: Outcome }> = []; + for (const c of INVALID_CASES) { + cases.push({ ...c, outcome: await callTool(conn, c.args) }); + } + + // MUST: Validate all tool inputs. Any rejection (tool error or JSON-RPC + // error) satisfies it; an ordinary result means the tool ran. + const executed = cases.filter((c) => c.outcome.kind === 'executed'); + const transport = cases.filter((c) => c.outcome.kind === 'transport-error'); + const rejectedFail = executed.length > 0 || transport.length > 0; + checks.push({ + id: 'sep-1303-input-validation-rejected', + name: 'ToolsCallInputValidationRejected', + description: 'Schema-invalid arguments are not executed', + status: rejectedFail ? 'FAILURE' : 'SUCCESS', + timestamp: ts(), + errorMessage: rejectedFail + ? [ + ...executed.map( + (c) => + `${c.label}: tool executed and returned a result without isError` + ), + ...transport.map( + (c) => + `${c.label}: no JSON-RPC response (${(c.outcome as { message: string }).message})` + ) + ].join('; ') + : undefined, + specReferences: [SECURITY_REF], + details: { cases } + }); + + // Error taxonomy: input validation errors are tool execution errors. + const notToolError = cases.filter( + (c) => + c.outcome.kind !== 'tool-error' || + !hasTextContent((c.outcome as { result: unknown }).result) + ); + checks.push({ + id: 'sep-1303-input-validation-tool-error', + name: 'ToolsCallInputValidationToolError', + description: + 'Input validation errors are reported as tool execution errors', + status: notToolError.length > 0 ? 'WARNING' : 'SUCCESS', + timestamp: ts(), + errorMessage: + notToolError.length > 0 + ? notToolError + .map((c) => { + const o = c.outcome; + if (o.kind === 'protocol-error') + return `${c.label}: JSON-RPC error ${o.code} instead of an isError result`; + if (o.kind === 'tool-error') + return `${c.label}: isError result has no text content`; + return `${c.label}: ${o.kind}`; + }) + .join('; ') + : undefined, + specReferences: [ERROR_HANDLING_REF], + details: { cases } + }); + + return checks; + } +} diff --git a/src/seps/sep-1303.yaml b/src/seps/sep-1303.yaml new file mode 100644 index 00000000..74fb5ca8 --- /dev/null +++ b/src/seps/sep-1303.yaml @@ -0,0 +1,10 @@ +sep: 1303 +spec_url: https://modelcontextprotocol.io/specification/2025-11-25/server/tools#error-handling +requirements: + - check: sep-1303-input-validation-rejected + text: 'Servers MUST: Validate all tool inputs' + - check: sep-1303-input-validation-tool-error + text: 'Tool Execution Errors: Reported in tool results with `isError: true`: Input validation errors (e.g., date in wrong format, value out of range)' + + - text: 'Clients SHOULD provide tool execution errors to language models to enable self-correction.' + excluded: 'What a client passes to its language model is host-internal; not observable at the protocol level' From dfb4374879104f7160850b2693997ab266f43bad Mon Sep 17 00:00:00 2001 From: Ahmad Faraj Date: Mon, 16 Nov 2026 13:32:20 +0200 Subject: [PATCH 2/2] fix(server): accept any non-empty content in input-validation tool errors An isError result may carry image, audio or resource content; requiring a text block made sep-1303-input-validation-tool-error warn on valid tool execution errors. Signed-off-by: Ahmad Faraj --- src/scenarios/server/tools-input-validation.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/scenarios/server/tools-input-validation.ts b/src/scenarios/server/tools-input-validation.ts index 193e43f8..6ba14481 100644 --- a/src/scenarios/server/tools-input-validation.ts +++ b/src/scenarios/server/tools-input-validation.ts @@ -64,6 +64,11 @@ async function callTool( } } +function hasContent(result: unknown): boolean { + const content = (result as { content?: unknown })?.content; + return Array.isArray(content) && content.length > 0; +} + function hasTextContent(result: unknown): boolean { const content = (result as { content?: unknown })?.content; return ( @@ -223,7 +228,7 @@ tool result with \`isError: true\`, not as a JSON-RPC error.`; const notToolError = cases.filter( (c) => c.outcome.kind !== 'tool-error' || - !hasTextContent((c.outcome as { result: unknown }).result) + !hasContent((c.outcome as { result: unknown }).result) ); checks.push({ id: 'sep-1303-input-validation-tool-error', @@ -240,7 +245,7 @@ tool result with \`isError: true\`, not as a JSON-RPC error.`; if (o.kind === 'protocol-error') return `${c.label}: JSON-RPC error ${o.code} instead of an isError result`; if (o.kind === 'tool-error') - return `${c.label}: isError result has no text content`; + return `${c.label}: isError result has no content`; return `${c.label}: ${o.kind}`; }) .join('; ')