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) }