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:
YeonGyu-Kim
2026-03-24 16:24:44 +09:00
parent 03dc903e8e
commit 5e856b4fde
2 changed files with 36 additions and 1 deletions
+34
View File
@@ -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
}