fix(schema): strip contentEncoding from MCP tool schemas for Gemini compatibility
The existing normalizeToolArgSchemas only applies to omo plugin tools (via tool-registry.ts), but MCP server tool schemas bypass this sanitization entirely. MCP schemas with contentEncoding/contentMediaType cause Gemini 400 errors. Add sanitizeJsonSchema() to strip unsupported keywords from MCP tool inputSchema before serialization in formatMcpCapabilities. Fixes #2200 Supersedes #2666
This commit is contained in:
@@ -40,3 +40,37 @@ export function normalizeToolArgSchemas<TDefinition extends Pick<ToolDefinition,
|
||||
|
||||
return toolDefinition
|
||||
}
|
||||
|
||||
// Schema keywords unsupported by Gemini — strip them from MCP tool schemas
|
||||
const UNSUPPORTED_SCHEMA_KEYWORDS = new Set(["contentEncoding", "contentMediaType"])
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
export function sanitizeJsonSchema(value: unknown, depth = 0, isPropertyName = false): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => sanitizeJsonSchema(item, depth + 1, false))
|
||||
}
|
||||
|
||||
if (!isRecord(value)) {
|
||||
return value
|
||||
}
|
||||
|
||||
const sanitized: Record<string, unknown> = {}
|
||||
|
||||
for (const [key, nestedValue] of Object.entries(value)) {
|
||||
if (!isPropertyName && UNSUPPORTED_SCHEMA_KEYWORDS.has(key)) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (depth === 0 && key === "$schema") {
|
||||
continue
|
||||
}
|
||||
|
||||
const childIsPropertyName = key === "properties" && !isPropertyName
|
||||
sanitized[key] = sanitizeJsonSchema(nestedValue, depth + 1, childIsPropertyName)
|
||||
}
|
||||
|
||||
return sanitized
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user