Skip to content

Commit b1db976

Browse files
fix: prompts/get rejects omitted arguments when all argsSchema fields are optional
Mirror the tools fix from #1404 to the prompts handler: default undefined arguments to {} before schema validation so that a prompt with all-optional argsSchema fields accepts a request that omits arguments entirely. Closes #1869
1 parent 7ba58da commit b1db976

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server": patch
3+
---
4+
5+
Fix prompts/get rejecting omitted arguments when all argsSchema fields are optional

packages/server/src/server/mcp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ function createPromptHandler(
12711271
const typedCallback = callback as (args: unknown, ctx: ServerContext) => GetPromptResult | Promise<GetPromptResult>;
12721272

12731273
return async (args, ctx) => {
1274-
const parseResult = await validateStandardSchema(argsSchema, args);
1274+
const parseResult = await validateStandardSchema(argsSchema, args ?? {});
12751275
if (!parseResult.success) {
12761276
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid arguments for prompt ${name}: ${parseResult.error}`);
12771277
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4254,6 +4254,54 @@ describe('Zod v4', () => {
42544254
]);
42554255
});
42564256

4257+
/***
4258+
* Test: prompts/get with omitted arguments when all argsSchema fields are optional (#1869)
4259+
*/
4260+
test('should accept omitted arguments in prompts/get when all argsSchema fields are optional', async () => {
4261+
const mcpServer = new McpServer({
4262+
name: 'test server',
4263+
version: '1.0'
4264+
});
4265+
4266+
const client = new Client({
4267+
name: 'test client',
4268+
version: '1.0'
4269+
});
4270+
4271+
mcpServer.registerPrompt(
4272+
'echo',
4273+
{
4274+
argsSchema: z.object({
4275+
context: z.string().optional()
4276+
})
4277+
},
4278+
({ context }) => ({
4279+
messages: [
4280+
{
4281+
role: 'user' as const,
4282+
content: { type: 'text' as const, text: `context: ${context ?? 'none'}` }
4283+
}
4284+
]
4285+
})
4286+
);
4287+
4288+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
4289+
4290+
await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]);
4291+
4292+
// Call prompts/get without arguments -- should not throw
4293+
const result = await client.request({
4294+
method: 'prompts/get',
4295+
params: { name: 'echo' }
4296+
});
4297+
4298+
expect(result.messages).toHaveLength(1);
4299+
expect(result.messages[0]!.content).toEqual({
4300+
type: 'text',
4301+
text: 'context: none'
4302+
});
4303+
});
4304+
42574305
/***
42584306
* Test: Prompt Registration with _meta field
42594307
*/

0 commit comments

Comments
 (0)