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:
parent
ed1f80da00
commit
82eb22d978
@ -17,10 +17,33 @@ const supportedAttributes = [
|
|||||||
'anyOf'
|
'anyOf'
|
||||||
]
|
]
|
||||||
|
|
||||||
function filterPropertieAttributes(tool: MCPTool) {
|
function filterPropertieAttributes(tool: MCPTool, filterNestedObj = false) {
|
||||||
const roperties = tool.inputSchema.properties
|
const roperties = tool.inputSchema.properties
|
||||||
const getSubMap = (obj: Record<string, any>, keys: string[]) => {
|
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)) {
|
for (const [key, val] of Object.entries(roperties)) {
|
||||||
@ -130,13 +153,18 @@ export function mcpToolsToGeminiTools(mcpTools: MCPTool[] | undefined): geminiTo
|
|||||||
const functions: FunctionDeclaration[] = []
|
const functions: FunctionDeclaration[] = []
|
||||||
|
|
||||||
for (const tool of mcpTools) {
|
for (const tool of mcpTools) {
|
||||||
|
const properties = filterPropertieAttributes(tool, true)
|
||||||
const functionDeclaration: FunctionDeclaration = {
|
const functionDeclaration: FunctionDeclaration = {
|
||||||
name: tool.id,
|
name: tool.id,
|
||||||
description: tool.description,
|
description: tool.description,
|
||||||
parameters: {
|
...(Object.keys(properties).length > 0
|
||||||
type: SchemaType.OBJECT,
|
? {
|
||||||
properties: filterPropertieAttributes(tool)
|
parameters: {
|
||||||
}
|
type: SchemaType.OBJECT,
|
||||||
|
properties
|
||||||
|
}
|
||||||
|
}
|
||||||
|
: {})
|
||||||
}
|
}
|
||||||
functions.push(functionDeclaration)
|
functions.push(functionDeclaration)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user