|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 4 | +import { PassThrough } from 'node:stream'; |
| 5 | +import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; |
| 6 | +import { WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js'; |
| 7 | +import { GetPromptRequestSchema } from '@modelcontextprotocol/sdk/types.js'; |
| 8 | +import { z } from 'zod'; |
| 9 | + |
| 10 | +import { MCPServerRuntime } from './mcp-server-runtime.js'; |
| 11 | +import { createPlatformMcpServer } from './request-params.js'; |
| 12 | +import type { McpDataBridge } from './mcp-http-tools.js'; |
| 13 | +import type { McpSkillBridge } from './skill-prompts.js'; |
| 14 | + |
| 15 | +/** |
| 16 | + * A request whose params fail the method's request schema is the CALLER's |
| 17 | + * mistake: JSON-RPC -32602 Invalid params with a sentence naming the param, |
| 18 | + * never -32603 Internal error carrying the serialised Zod issue list. Driven |
| 19 | + * over both real transports, because both build their server through the one |
| 20 | + * seam in `request-params.ts`. |
| 21 | + */ |
| 22 | + |
| 23 | +const INVALID_PARAMS = -32602; |
| 24 | +const INTERNAL_ERROR = -32603; |
| 25 | + |
| 26 | +function makeBridge(): McpDataBridge & McpSkillBridge { |
| 27 | + return { |
| 28 | + async listObjects() { |
| 29 | + return [{ name: 'crm_opportunity', label: 'Opportunity', fieldCount: 1 }]; |
| 30 | + }, |
| 31 | + async describeObject(name: string) { |
| 32 | + return name === 'crm_opportunity' |
| 33 | + ? { name, label: 'Opportunity', fields: [{ name: 'amount', type: 'currency', label: 'Amount', required: false }] } |
| 34 | + : null; |
| 35 | + }, |
| 36 | + async query() { return { records: [] }; }, |
| 37 | + async get() { return null; }, |
| 38 | + async create() { return {}; }, |
| 39 | + async update() { return {}; }, |
| 40 | + async remove() { return {}; }, |
| 41 | + async listSkills() { return []; }, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +async function overHttp(method: string, params: unknown): Promise<any> { |
| 46 | + const runtime = new MCPServerRuntime({ name: 'request-params', version: '0.0.0' }); |
| 47 | + const body = { jsonrpc: '2.0', id: 1, method, params }; |
| 48 | + const res = await runtime.handleHttpRequest( |
| 49 | + new Request('http://localhost/api/v1/mcp', { |
| 50 | + method: 'POST', |
| 51 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 52 | + body: JSON.stringify(body), |
| 53 | + }), |
| 54 | + { bridge: makeBridge(), parsedBody: body } as never, |
| 55 | + ); |
| 56 | + return res.json(); |
| 57 | +} |
| 58 | + |
| 59 | +/** The refusal shape every malformed request must get: the code, the named param, no JSON dump. */ |
| 60 | +function expectInvalidParams(frame: any, sentence: string): void { |
| 61 | + expect(frame.error, JSON.stringify(frame)).toBeDefined(); |
| 62 | + expect(frame.error.code).toBe(INVALID_PARAMS); |
| 63 | + expect(frame.error.message).toBe(`MCP error -32602: ${sentence}`); |
| 64 | +} |
| 65 | + |
| 66 | +describe('malformed request params answer -32602 Invalid params (HTTP transport)', () => { |
| 67 | + it('prompts/get with no name', async () => { |
| 68 | + expectInvalidParams(await overHttp('prompts/get', {}), 'params.name is required'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('tools/call with {}', async () => { |
| 72 | + expectInvalidParams(await overHttp('tools/call', {}), 'params.name is required'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('prompts/get with a non-string name', async () => { |
| 76 | + expectInvalidParams(await overHttp('prompts/get', { name: 42 }), 'params.name must be a string'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('initialize, a handler the SDK installs in its own constructor, takes the same answer', async () => { |
| 80 | + const frame = await overHttp('initialize', { capabilities: {}, clientInfo: { name: 't', version: '1' } }); |
| 81 | + expectInvalidParams(frame, 'params.protocolVersion is required'); |
| 82 | + }); |
| 83 | + |
| 84 | + it("control: a tool's own argument check keeps its answer", async () => { |
| 85 | + const frame = await overHttp('tools/call', { |
| 86 | + name: 'validate_expression', |
| 87 | + arguments: { objectName: 'crm_opportunity', expression: 'record.amount > 1', site: 'nope' }, |
| 88 | + }); |
| 89 | + expect(frame.error).toBeUndefined(); |
| 90 | + expect(frame.result.isError).toBe(true); |
| 91 | + expect(frame.result.content[0].text).toMatch( |
| 92 | + /^MCP error -32602: Input validation error: Invalid arguments for tool validate_expression: /, |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('malformed request params answer -32602 Invalid params (stdio transport)', () => { |
| 98 | + const closers: Array<() => Promise<void>> = []; |
| 99 | + afterEach(async () => { |
| 100 | + for (const close of closers.splice(0)) await close(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('tools/call with {} on the long-lived server', async () => { |
| 104 | + const runtime = new MCPServerRuntime({ name: 'request-params', version: '0.0.0' }); |
| 105 | + runtime.bridgeDataTools(makeBridge()); |
| 106 | + const stdin = new PassThrough(); |
| 107 | + const stdout = new PassThrough(); |
| 108 | + const transport = new StdioServerTransport(stdin, stdout); |
| 109 | + await runtime.server.connect(transport); |
| 110 | + closers.push(() => transport.close().catch(() => {})); |
| 111 | + |
| 112 | + const frame = await new Promise<any>((resolve) => { |
| 113 | + stdout.on('data', (chunk) => { |
| 114 | + for (const line of String(chunk).split('\n')) { |
| 115 | + if (line.trim()) resolve(JSON.parse(line)); |
| 116 | + } |
| 117 | + }); |
| 118 | + stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id: 7, method: 'tools/call', params: {} })}\n`); |
| 119 | + }); |
| 120 | + expect(frame.id).toBe(7); |
| 121 | + expectInvalidParams(frame, 'params.name is required'); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('only the request-schema parse is mapped', () => { |
| 126 | + it('a ZodError thrown inside a handler keeps the -32603 answer', async () => { |
| 127 | + const server = createPlatformMcpServer({ name: 'request-params', version: '0.0.0' }, { capabilities: { prompts: {} } }); |
| 128 | + server.server.setRequestHandler(GetPromptRequestSchema, async () => { |
| 129 | + z.object({ inner: z.string() }).parse({}); |
| 130 | + return { messages: [] }; |
| 131 | + }); |
| 132 | + const transport = new WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: undefined, enableJsonResponse: true }); |
| 133 | + await server.connect(transport); |
| 134 | + const body = { jsonrpc: '2.0', id: 1, method: 'prompts/get', params: { name: 'any' } }; |
| 135 | + try { |
| 136 | + const res = await transport.handleRequest( |
| 137 | + new Request('http://localhost/mcp', { |
| 138 | + method: 'POST', |
| 139 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 140 | + body: JSON.stringify(body), |
| 141 | + }), |
| 142 | + { parsedBody: body }, |
| 143 | + ); |
| 144 | + const frame = (await res.json()) as { error: { code: number } }; |
| 145 | + expect(frame.error.code).toBe(INTERNAL_ERROR); |
| 146 | + } finally { |
| 147 | + await server.close(); |
| 148 | + } |
| 149 | + }); |
| 150 | +}); |
0 commit comments