Skip to content

Commit bced33d

Browse files
authored
Merge pull request #468 from geelen/fix-tool-annotation-confusion
Fix Zod object detection logic
2 parents bf7fd44 + 4a295a4 commit bced33d

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/server/mcp.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -659,16 +659,6 @@ export class McpServer {
659659
if (this._registeredTools[name]) {
660660
throw new Error(`Tool ${name} is already registered`);
661661
}
662-
663-
// Helper to check if an object is a Zod schema (ZodRawShape)
664-
const isZodRawShape = (obj: unknown): obj is ZodRawShape => {
665-
if (typeof obj !== "object" || obj === null) return false;
666-
667-
const isEmptyObject = z.object({}).strict().safeParse(obj).success;
668-
669-
// Check if object is empty or at least one property is a ZodType instance
670-
return isEmptyObject || Object.values(obj as object).some(v => v instanceof ZodType);
671-
};
672662

673663
let description: string | undefined;
674664
if (typeof rest[0] === "string") {
@@ -931,6 +921,24 @@ const EMPTY_OBJECT_JSON_SCHEMA = {
931921
type: "object" as const,
932922
};
933923

924+
// Helper to check if an object is a Zod schema (ZodRawShape)
925+
function isZodRawShape(obj: unknown): obj is ZodRawShape {
926+
if (typeof obj !== "object" || obj === null) return false;
927+
928+
const isEmptyObject = Object.keys(obj).length === 0;
929+
930+
// Check if object is empty or at least one property is a ZodType instance
931+
// Note: use heuristic check to avoid instanceof failure across different Zod versions
932+
return isEmptyObject || Object.values(obj as object).some(isZodTypeLike);
933+
}
934+
935+
function isZodTypeLike(value: unknown): value is ZodType {
936+
return value !== null &&
937+
typeof value === 'object' &&
938+
'parse' in value && typeof value.parse === 'function' &&
939+
'safeParse' in value && typeof value.safeParse === 'function';
940+
}
941+
934942
/**
935943
* Additional, optional information for annotating a resource.
936944
*/

0 commit comments

Comments
 (0)