Skip to content
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@modelcontextprotocol/sdk": "^1.24.0",
"google-auth-library": "^10.3.0",
"lodash-es": "^4.17.22",
"zod": "3.25.76"
"zod": "^4.2.1"
},
"devDependencies": {
"@types/lodash-es": "^4.17.12",
Expand Down
38 changes: 18 additions & 20 deletions core/src/tools/function_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,33 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {FunctionDeclaration, Schema, Type} from '@google/genai';
import {
type infer as zInfer,
ZodObject,
type ZodRawShape,
} from 'zod';
import { FunctionDeclaration, Schema, Type } from '@google/genai';
import { z as z3 } from 'zod/v3';
import { z as z4 } from 'zod/v4';

import {isZodObject, zodObjectToSchema} from '../utils/simple_zod_to_json.js';
import { isZodObject, zodObjectToSchema } from '../utils/simple_zod_to_json.js';

import {BaseTool, RunAsyncToolRequest} from './base_tool.js';
import {ToolContext} from './tool_context.js';
import { BaseTool, RunAsyncToolRequest } from './base_tool.js';
import { ToolContext } from './tool_context.js';

/**
* Input parameters of the function tool.
*/
export type ToolInputParameters =
| undefined
| ZodObject<ZodRawShape>
| Schema;
export type ToolInputParameters = | undefined | z3.ZodObject<z3.ZodRawShape> | z4.ZodObject | Schema;

type ZodObject<T extends Record<string, any>> = z3.ZodObject<z3.ZodRawShape> | z4.ZodObject<T>;

/*
* The arguments of the function tool.
*/
export type ToolExecuteArgument<TParameters extends ToolInputParameters> =
TParameters extends ZodObject<infer T, infer U, infer V>
? zInfer<ZodObject<T, U, V>>
: TParameters extends Schema
? unknown
: string;
TParameters extends z3.ZodObject<infer T, infer U, infer V>
? z3.infer<z3.ZodObject<T, U, V>>
: TParameters extends z4.ZodObject<infer T>
? z4.infer<z4.ZodObject<T>>
: TParameters extends Schema
? unknown
: string;

/*
* The function to execute by the tool.
Expand Down Expand Up @@ -65,7 +63,7 @@ export type ToolOptions<
function toSchema<TParameters extends ToolInputParameters>(
parameters: TParameters): Schema {
if (parameters === undefined) {
return {type: Type.OBJECT, properties: {}};
return { type: Type.OBJECT, properties: {} };
}

if (isZodObject(parameters)) {
Expand Down Expand Up @@ -120,7 +118,7 @@ export class FunctionTool<
override async runAsync(req: RunAsyncToolRequest): Promise<unknown> {
try {
let validatedArgs: unknown = req.args;
if (this.parameters instanceof ZodObject) {
if (isZodObject(this.parameters)) {
validatedArgs = this.parameters.parse(req.args);
}
return await this.execute(
Expand Down
2 changes: 1 addition & 1 deletion core/src/utils/gemini_schema_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {z} from 'zod';

const MCPToolSchema = z.object({
type: z.literal('object'),
properties: z.record(z.unknown()).optional(),
properties: z.record(z.string(), z.unknown()).optional(),
required: z.string().array().optional(),
});
type MCPToolSchema = z.infer<typeof MCPToolSchema>;
Expand Down
Loading
Loading