fix(core): Sanitize tool parameters to fix 400 API errors (#3300)

This commit is contained in:
BigUncle
2025-07-06 05:58:51 +08:00
committed by GitHub
parent 5c9372372c
commit b564d4a088
8 changed files with 438 additions and 176 deletions

View File

@@ -14,13 +14,8 @@ import {
import { parse } from 'shell-quote';
import { MCPServerConfig } from '../config/config.js';
import { DiscoveredMCPTool } from './mcp-tool.js';
import {
CallableTool,
FunctionDeclaration,
mcpToTool,
Schema,
} from '@google/genai';
import { ToolRegistry } from './tool-registry.js';
import { CallableTool, FunctionDeclaration, mcpToTool } from '@google/genai';
import { sanitizeParameters, ToolRegistry } from './tool-registry.js';
export const MCP_DEFAULT_TIMEOUT_MSEC = 10 * 60 * 1000; // default to 10 minutes
@@ -384,31 +379,3 @@ async function connectAndDiscover(
}
}
}
/**
* Sanitizes a JSON schema object to ensure compatibility with Vertex AI.
* This function recursively processes the schema to remove problematic properties
* that can cause issues with the Gemini API.
*
* @param schema The JSON schema object to sanitize (modified in-place)
*/
export function sanitizeParameters(schema?: Schema) {
if (!schema) {
return;
}
if (schema.anyOf) {
// Vertex AI gets confused if both anyOf and default are set.
schema.default = undefined;
for (const item of schema.anyOf) {
sanitizeParameters(item);
}
}
if (schema.items) {
sanitizeParameters(schema.items);
}
if (schema.properties) {
for (const item of Object.values(schema.properties)) {
sanitizeParameters(item);
}
}
}