Skip to content

Commit f7cdff5

Browse files
committed
Support Zod v3 and v4.
1 parent 96a6d75 commit f7cdff5

File tree

8 files changed

+2903
-2502
lines changed

8 files changed

+2903
-2502
lines changed

core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@modelcontextprotocol/sdk": "^1.24.0",
4444
"google-auth-library": "^10.3.0",
4545
"lodash-es": "^4.17.22",
46-
"zod": "3.25.76"
46+
"zod": "^4.2.1"
4747
},
4848
"devDependencies": {
4949
"@types/lodash-es": "^4.17.12",

core/src/tools/function_tool.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,33 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {FunctionDeclaration, Schema, Type} from '@google/genai';
8-
import {
9-
type infer as zInfer,
10-
ZodObject,
11-
type ZodRawShape,
12-
} from 'zod';
7+
import { FunctionDeclaration, Schema, Type } from '@google/genai';
8+
import { z as z3 } from 'zod/v3';
9+
import { z as z4 } from 'zod/v4';
1310

14-
import {isZodObject, zodObjectToSchema} from '../utils/simple_zod_to_json.js';
11+
import { isZodObject, zodObjectToSchema } from '../utils/simple_zod_to_json.js';
1512

16-
import {BaseTool, RunAsyncToolRequest} from './base_tool.js';
17-
import {ToolContext} from './tool_context.js';
13+
import { BaseTool, RunAsyncToolRequest } from './base_tool.js';
14+
import { ToolContext } from './tool_context.js';
1815

1916
/**
2017
* Input parameters of the function tool.
2118
*/
22-
export type ToolInputParameters =
23-
| undefined
24-
| ZodObject<ZodRawShape>
25-
| Schema;
19+
export type ToolInputParameters = | undefined | z3.ZodObject<z3.ZodRawShape> | z4.ZodObject | Schema;
20+
21+
type ZodObject<T extends Record<string, any>> = z3.ZodObject<z3.ZodRawShape> | z4.ZodObject<T>;
2622

2723
/*
2824
* The arguments of the function tool.
2925
*/
3026
export type ToolExecuteArgument<TParameters extends ToolInputParameters> =
31-
TParameters extends ZodObject<infer T, infer U, infer V>
32-
? zInfer<ZodObject<T, U, V>>
33-
: TParameters extends Schema
34-
? unknown
35-
: string;
27+
TParameters extends z3.ZodObject<infer T, infer U, infer V>
28+
? z3.infer<z3.ZodObject<T, U, V>>
29+
: TParameters extends z4.ZodObject<infer T>
30+
? z4.infer<z4.ZodObject<T>>
31+
: TParameters extends Schema
32+
? unknown
33+
: string;
3634

3735
/*
3836
* The function to execute by the tool.
@@ -65,7 +63,7 @@ export type ToolOptions<
6563
function toSchema<TParameters extends ToolInputParameters>(
6664
parameters: TParameters): Schema {
6765
if (parameters === undefined) {
68-
return {type: Type.OBJECT, properties: {}};
66+
return { type: Type.OBJECT, properties: {} };
6967
}
7068

7169
if (isZodObject(parameters)) {
@@ -120,7 +118,7 @@ export class FunctionTool<
120118
override async runAsync(req: RunAsyncToolRequest): Promise<unknown> {
121119
try {
122120
let validatedArgs: unknown = req.args;
123-
if (this.parameters instanceof ZodObject) {
121+
if (isZodObject(this.parameters)) {
124122
validatedArgs = this.parameters.parse(req.args);
125123
}
126124
return await this.execute(

core/src/utils/gemini_schema_util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {z} from 'zod';
99

1010
const MCPToolSchema = z.object({
1111
type: z.literal('object'),
12-
properties: z.record(z.unknown()).optional(),
12+
properties: z.record(z.string(), z.unknown()).optional(),
1313
required: z.string().array().optional(),
1414
});
1515
type MCPToolSchema = z.infer<typeof MCPToolSchema>;

0 commit comments

Comments
 (0)