From 82eb22d978abdfc229a0e4451e15a2b0e27ac082 Mon Sep 17 00:00:00 2001 From: yuna0x0 Date: Wed, 19 Mar 2025 17:16:37 +0800 Subject: [PATCH] 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) --- src/renderer/src/utils/mcp-tools.ts | 40 ++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/renderer/src/utils/mcp-tools.ts b/src/renderer/src/utils/mcp-tools.ts index c2302df1..e2ea124f 100644 --- a/src/renderer/src/utils/mcp-tools.ts +++ b/src/renderer/src/utils/mcp-tools.ts @@ -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, 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, 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) }