Skip to content

Commit 3f533f9

Browse files
committed
fix: json schema silent drop
1 parent a71e6fb commit 3f533f9

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/server/mcp.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,9 +1024,10 @@ export class McpServer {
10241024
annotations = rest.shift() as ToolAnnotations;
10251025
}
10261026
} else if (typeof firstArg === 'object' && firstArg !== null) {
1027-
// Not a ZodRawShapeCompat, so must be annotations in this position
1028-
// Case: tool(name, annotations, cb)
1029-
// Or: tool(name, description, annotations, cb)
1027+
// ToolAnnotations values are primitives. Nested objects indicate a misplaced schema
1028+
if (Object.values(firstArg).some(v => typeof v === 'object' && v !== null)) {
1029+
throw new Error(`Tool ${name} expected a Zod schema or ToolAnnotations, but received an unrecognized object`);
1030+
}
10301031
annotations = rest.shift() as ToolAnnotations;
10311032
}
10321033
}
@@ -1386,7 +1387,7 @@ function isZodRawShapeCompat(obj: unknown): obj is ZodRawShapeCompat {
13861387

13871388
/**
13881389
* Converts a provided Zod schema to a Zod object if it is a ZodRawShapeCompat,
1389-
* otherwise returns the schema as is.
1390+
* otherwise returns the schema as is. Throws if the value is not a valid Zod schema.
13901391
*/
13911392
function getZodSchemaObject(schema: ZodRawShapeCompat | AnySchema | undefined): AnySchema | undefined {
13921393
if (!schema) {
@@ -1397,6 +1398,10 @@ function getZodSchemaObject(schema: ZodRawShapeCompat | AnySchema | undefined):
13971398
return objectFromShape(schema);
13981399
}
13991400

1401+
if (!isZodSchemaInstance(schema as object)) {
1402+
throw new Error('inputSchema must be a Zod schema or raw shape, received an unrecognized object');
1403+
}
1404+
14001405
return schema;
14011406
}
14021407

test/server/mcp.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,15 +2075,15 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
20752075

20762076
expect(() => {
20772077
mcpServer.tool('test', 'A tool', jsonSchema, cb);
2078-
}).toThrow(/Zod schema/);
2078+
}).toThrow(/unrecognized object/);
20792079

20802080
expect(() => {
20812081
mcpServer.registerTool(
20822082
'test',
20832083
{ description: 'A tool', inputSchema: jsonSchema },
20842084
cb
20852085
);
2086-
}).toThrow(/Zod schema/);
2086+
}).toThrow(/unrecognized object/);
20872087
});
20882088
});
20892089

0 commit comments

Comments
 (0)