Skip to content

Commit 2586801

Browse files
committed
fix(server): handle undefined arguments for tools with all optional params
Fixes modelcontextprotocol#400
1 parent 8bf13e4 commit 2586801

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎packages/server/src/server/mcp.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export class McpServer {
273273
// If that fails, use the schema directly (for union/intersection/etc)
274274
const inputObj = normalizeObjectSchema(tool.inputSchema);
275275
const schemaToParse = inputObj ?? (tool.inputSchema as AnySchema);
276-
const parseResult = await safeParseAsync(schemaToParse, args);
276+
const parseResult = await safeParseAsync(schemaToParse, args ?? {});
277277
if (!parseResult.success) {
278278
const error = 'error' in parseResult ? parseResult.error : 'Unknown error';
279279
const errorMessage = getParseErrorMessage(error);

‎test/integration/test/server/mcp.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,61 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
10921092
);
10931093
});
10941094

1095+
/***
1096+
* Test: Tool with all optional parameters called without arguments (Issue #400)
1097+
* @see https://github.com/modelcontextprotocol/typescript-sdk/issues/400
1098+
*/
1099+
test('should accept undefined arguments when all tool params are optional', async () => {
1100+
const mcpServer = new McpServer({
1101+
name: 'test server',
1102+
version: '1.0'
1103+
});
1104+
const client = new Client({
1105+
name: 'test client',
1106+
version: '1.0'
1107+
});
1108+
1109+
mcpServer.tool(
1110+
'optional-params-tool',
1111+
{
1112+
limit: z.number().optional(),
1113+
offset: z.number().optional()
1114+
},
1115+
async ({ limit, offset }) => ({
1116+
content: [
1117+
{
1118+
type: 'text',
1119+
text: `limit: ${limit ?? 'default'}, offset: ${offset ?? 'default'}`
1120+
}
1121+
]
1122+
})
1123+
);
1124+
1125+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
1126+
1127+
await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]);
1128+
1129+
// Call tool without arguments (arguments is undefined)
1130+
const result = await client.request(
1131+
{
1132+
method: 'tools/call',
1133+
params: {
1134+
name: 'optional-params-tool'
1135+
// arguments is intentionally omitted (undefined)
1136+
}
1137+
},
1138+
CallToolResultSchema
1139+
);
1140+
1141+
expect(result.isError).toBeUndefined();
1142+
expect(result.content).toEqual([
1143+
{
1144+
type: 'text',
1145+
text: 'limit: default, offset: default'
1146+
}
1147+
]);
1148+
});
1149+
10951150
/***
10961151
* Test: Preventing Duplicate Tool Registration
10971152
*/

0 commit comments

Comments
 (0)