Skip to content
Open
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
4 changes: 3 additions & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"scripts": {
"clean": "rm -rf dist",
"clean:all": "npm run clean && rm -rf node_modules",
"rebuild": "npm run clean:all && npm install && npm run build",
"build": "tsc --emitDeclarationOnly && node ./build.js",
"build:bundle": "tsc --emitDeclarationOnly && node ./build.js --bundle",
"build:watch": "concurrently \"tsc --noEmit --watch --preserveWatchOutput\" \"node ./build.js --watch\"",
Expand All @@ -43,7 +44,8 @@
"@modelcontextprotocol/sdk": "^1.24.0",
"google-auth-library": "^10.3.0",
"lodash-es": "^4.17.23",
"zod": "3.25.76"
"zod": "^4.2.1",
"zod-to-json-schema": "^3.25.1"
},
"devDependencies": {
"@types/lodash-es": "^4.17.12",
Expand Down
23 changes: 15 additions & 8 deletions core/src/tools/function_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

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

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

Expand All @@ -15,17 +16,23 @@ import {ToolContext} from './tool_context.js';
/**
* Input parameters of the function tool.
*/
export type ToolInputParameters = undefined | ZodObject<ZodRawShape> | Schema;
export type ToolInputParameters =
| z3.ZodObject<z3.ZodRawShape>
| z4.ZodObject<z4.ZodRawShape>
| Schema
| undefined;

/*
* 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 @@ -133,7 +140,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 @@ -10,7 +10,7 @@ import {z} from 'zod';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const MCPToolSchemaObject = 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 MCPToolSchemaObject>;
Expand Down
Loading
Loading