fix(mcp-tools): Enhance object nested properties filtering (#3485)

- Improve filterPropertieAttributes to handle nested object and array types, preserving their structure while filtering attributes.

- Make parameters optional when no properties exist. (Fix #3270)
This commit is contained in:
yuna0x0 2025-03-19 17:16:37 +08:00 committed by GitHub
parent ed1f80da00
commit 82eb22d978
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,10 +17,33 @@ const supportedAttributes = [
'anyOf'
]
function filterPropertieAttributes(tool: MCPTool) {
function filterPropertieAttributes(tool: MCPTool, filterNestedObj = false) {
const roperties = tool.inputSchema.properties
const getSubMap = (obj: Record<string, any>, keys: string[]) => {
return Object.fromEntries(Object.entries(obj).filter(([key]) => keys.includes(key)))
const filtered = Object.fromEntries(Object.entries(obj).filter(([key]) => keys.includes(key)))
if (filterNestedObj) {
return {
...filtered,
...(obj.type === 'object' && obj.properties
? {
properties: Object.fromEntries(
Object.entries(obj.properties).map(([k, v]) => [
k,
(v as any).type === 'object' ? getSubMap(v as Record<string, any>, keys) : v
])
)
}
: {}),
...(obj.type === 'array' && obj.items?.type === 'object'
? {
items: getSubMap(obj.items, keys)
}
: {})
}
}
return filtered
}
for (const [key, val] of Object.entries(roperties)) {
@ -130,13 +153,18 @@ export function mcpToolsToGeminiTools(mcpTools: MCPTool[] | undefined): geminiTo
const functions: FunctionDeclaration[] = []
for (const tool of mcpTools) {
const properties = filterPropertieAttributes(tool, true)
const functionDeclaration: FunctionDeclaration = {
name: tool.id,
description: tool.description,
parameters: {
type: SchemaType.OBJECT,
properties: filterPropertieAttributes(tool)
}
...(Object.keys(properties).length > 0
? {
parameters: {
type: SchemaType.OBJECT,
properties
}
}
: {})
}
functions.push(functionDeclaration)
}