Skip to content

feat: Add duck-typed isZodType guard for Zod schemas across bundles #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,14 +660,30 @@ 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;

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;
Expand Down