Skip to content

Commit 8b4e11b

Browse files
author
Developer
committed
fix(opencode): Add MCP schema sanitization for Kimi K2.5 compatibility
- Add sanitizeSchema() function to filter invalid JSON schemas - Update convertMcpTool() to sanitize before spreading to Kimi K2.5 - Protects against empty object {} and invalid logical operators - Ensures all MCP schemas are valid Draft 7 before validation Resolves: JSON Schema not supported: could not understand the instance '{}' Ref: OpenCode issue anomalyco#11357 - Kimi K2.5 strict schema validation This provides complete protection against both: - Invalid bare list annotations (list → List[str]) - Future MCP servers with invalid schemas
1 parent 0f9cd78 commit 8b4e11b

1 file changed

Lines changed: 44 additions & 3 deletions

File tree

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,56 @@ export namespace MCP {
108108
})
109109
}
110110

111+
/**
112+
* Sanitize invalid JSON schemas from MCP servers
113+
* Ensures schemas are valid Draft 7 before sending to strict validators like Kimi K2.5
114+
*/
115+
function sanitizeSchema(schema: unknown): JSONSchema7 {
116+
const input = typeof schema === 'object' && schema !== null ? schema : {}
117+
118+
// Build valid base schema
119+
const result: JSONSchema7 = {
120+
type: "object",
121+
properties: {},
122+
additionalProperties: false,
123+
}
124+
125+
// Only add properties if they're valid objects
126+
if (input.properties && typeof input.properties === "object") {
127+
result.properties = input.properties as JSONSchema7["properties"]
128+
}
129+
130+
// Only add required if it's an array
131+
if (Array.isArray(input.required)) {
132+
const validProperties = result.properties || {}
133+
// Only include fields that exist in properties
134+
result.required = input.required.filter(field => field in validProperties)
135+
}
136+
137+
// Handle recursive schemas in properties
138+
if (result.properties) {
139+
for (const [key, value] of Object.entries(result.properties)) {
140+
if (typeof value === 'object' && value !== null) {
141+
result.properties[key] = sanitizeSchema(value) as JSONSchema7["properties"][string]
142+
}
143+
}
144+
}
145+
146+
return result
147+
}
148+
111149
// Convert MCP tool definition to AI SDK Tool type
112150
async function convertMcpTool(mcpTool: MCPToolDef, client: MCPClient): Promise<Tool> {
113151
const inputSchema = mcpTool.inputSchema
114152

115-
// Spread first, then override type to ensure it's always "object"
153+
// Sanitize invalid schemas before spreading
154+
const sanitized = sanitizeSchema(inputSchema)
155+
156+
// Build valid schema from sanitized input
116157
const schema: JSONSchema7 = {
117-
...(inputSchema as JSONSchema7),
158+
...sanitized,
118159
type: "object",
119-
properties: (inputSchema.properties ?? {}) as JSONSchema7["properties"],
160+
properties: sanitized.properties ?? {},
120161
additionalProperties: false,
121162
}
122163
const config = await Config.get()

0 commit comments

Comments
 (0)