|
1 | 1 | import type { JSONRPCMessage } from '@modelcontextprotocol/core'; |
2 | | -import { InMemoryTransport, isStandardSchema, LATEST_PROTOCOL_VERSION } from '@modelcontextprotocol/core'; |
| 2 | +import { InMemoryTransport, isStandardSchema, LATEST_PROTOCOL_VERSION, ProtocolErrorCode } from '@modelcontextprotocol/core'; |
3 | 3 | import { describe, expect, expectTypeOf, it, vi } from 'vitest'; |
4 | 4 | import * as z from 'zod/v4'; |
5 | 5 | import { McpServer } from '../../src/index.js'; |
@@ -119,6 +119,48 @@ describe('registerTool/registerPrompt accept raw Zod shape (auto-wrapped)', () = |
119 | 119 |
|
120 | 120 | await server.close(); |
121 | 121 | }); |
| 122 | + |
| 123 | + it('returns a protocol error for invalid tool arguments', async () => { |
| 124 | + const server = new McpServer({ name: 't', version: '1.0.0' }); |
| 125 | + |
| 126 | + server.registerTool('echo', { inputSchema: { x: z.number() } }, async ({ x }) => ({ |
| 127 | + content: [{ type: 'text' as const, text: String(x) }] |
| 128 | + })); |
| 129 | + |
| 130 | + const [client, srv] = InMemoryTransport.createLinkedPair(); |
| 131 | + await server.connect(srv); |
| 132 | + await client.start(); |
| 133 | + |
| 134 | + const responses: JSONRPCMessage[] = []; |
| 135 | + client.onmessage = m => responses.push(m); |
| 136 | + |
| 137 | + await client.send({ |
| 138 | + jsonrpc: '2.0', |
| 139 | + id: 1, |
| 140 | + method: 'initialize', |
| 141 | + params: { |
| 142 | + protocolVersion: LATEST_PROTOCOL_VERSION, |
| 143 | + capabilities: {}, |
| 144 | + clientInfo: { name: 'c', version: '1.0.0' } |
| 145 | + } |
| 146 | + } as JSONRPCMessage); |
| 147 | + await client.send({ jsonrpc: '2.0', method: 'notifications/initialized' } as JSONRPCMessage); |
| 148 | + await client.send({ |
| 149 | + jsonrpc: '2.0', |
| 150 | + id: 2, |
| 151 | + method: 'tools/call', |
| 152 | + params: { name: 'echo', arguments: { x: 'nope' } } |
| 153 | + } as JSONRPCMessage); |
| 154 | + |
| 155 | + await vi.waitFor(() => expect(responses.some(r => 'id' in r && r.id === 2)).toBe(true)); |
| 156 | + |
| 157 | + const response = responses.find(r => 'id' in r && r.id === 2) as { error?: { code: number; message: string }; result?: unknown }; |
| 158 | + expect(response.result).toBeUndefined(); |
| 159 | + expect(response.error?.code).toBe(ProtocolErrorCode.InvalidParams); |
| 160 | + expect(response.error?.message).toContain('Invalid arguments for tool echo'); |
| 161 | + |
| 162 | + await server.close(); |
| 163 | + }); |
122 | 164 | }); |
123 | 165 |
|
124 | 166 | describe('InferRawShape', () => { |
|
0 commit comments