diff --git a/src/server/mcp.ts b/src/server/mcp.ts index dccfa04e..a8e7e5f8 100644 --- a/src/server/mcp.ts +++ b/src/server/mcp.ts @@ -660,6 +660,20 @@ export class McpServer { throw new Error(`Tool ${name} is already registered`); } + // Runtime type guard for Zod types that works across bundles/realms + const isZodType = (value: unknown): value is ZodTypeAny => { + if (typeof value !== "object" || value === null) return false; + + if (!("_def" in value)) return false; + + const def = (value as { _def: unknown })._def; + if (typeof def !== "object" || def === null) return false; + + if (!("typeName" in def)) return false; + + return typeof (def as { typeName: unknown }).typeName === "string"; + }; + // Helper to check if an object is a Zod schema (ZodRawShape) const isZodRawShape = (obj: unknown): obj is ZodRawShape => { if (typeof obj !== "object" || obj === null) return false; @@ -667,7 +681,9 @@ export class McpServer { const isEmptyObject = z.object({}).strict().safeParse(obj).success; // Check if object is empty or at least one property is a ZodType instance - return isEmptyObject || Object.values(obj as object).some(v => v instanceof ZodType); + return ( + isEmptyObject || Object.values(obj as object).some((v) => isZodType(v)) + ); }; let description: string | undefined;