From 185045f8054b22e21fe5e361166ba49e95505a6e Mon Sep 17 00:00:00 2001 From: SuYao Date: Thu, 24 Jul 2025 10:17:45 +0800 Subject: [PATCH] fix(inputSchemas): convert input schemas to JSON schema format for consistency across DifyKnowledgeServer and FileSystemServer (#8444) * fix(inputSchemas): convert input schemas to JSON schema format for consistency across DifyKnowledgeServer and FileSystemServer * fix(AnthropicAPIClient): handle empty accumulated JSON input gracefully and improve auto-approval logic for built-in tools --- src/main/mcpServers/dify-knowledge.ts | 2 +- src/main/mcpServers/filesystem.ts | 20 +++++++++---------- .../clients/anthropic/AnthropicAPIClient.ts | 2 +- src/renderer/src/utils/mcp-tools.ts | 3 +++ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/mcpServers/dify-knowledge.ts b/src/main/mcpServers/dify-knowledge.ts index f9a990834f..2bd2c4adda 100644 --- a/src/main/mcpServers/dify-knowledge.ts +++ b/src/main/mcpServers/dify-knowledge.ts @@ -91,7 +91,7 @@ class DifyKnowledgeServer { { name: 'search_knowledge', description: 'Search knowledge by id and query', - inputSchema: SearchKnowledgeArgsSchema + inputSchema: z.toJSONSchema(SearchKnowledgeArgsSchema) } ] } diff --git a/src/main/mcpServers/filesystem.ts b/src/main/mcpServers/filesystem.ts index 6858b8d820..3b3c5ed799 100644 --- a/src/main/mcpServers/filesystem.ts +++ b/src/main/mcpServers/filesystem.ts @@ -340,7 +340,7 @@ class FileSystemServer { 'Handles various text encodings and provides detailed error messages ' + 'if the file cannot be read. Use this tool when you need to examine ' + 'the contents of a single file. Only works within allowed directories.', - inputSchema: ReadFileArgsSchema + inputSchema: z.toJSONSchema(ReadFileArgsSchema) }, { name: 'read_multiple_files', @@ -350,7 +350,7 @@ class FileSystemServer { "or compare multiple files. Each file's content is returned with its " + "path as a reference. Failed reads for individual files won't stop " + 'the entire operation. Only works within allowed directories.', - inputSchema: ReadMultipleFilesArgsSchema + inputSchema: z.toJSONSchema(ReadMultipleFilesArgsSchema) }, { name: 'write_file', @@ -358,7 +358,7 @@ class FileSystemServer { 'Create a new file or completely overwrite an existing file with new content. ' + 'Use with caution as it will overwrite existing files without warning. ' + 'Handles text content with proper encoding. Only works within allowed directories.', - inputSchema: WriteFileArgsSchema + inputSchema: z.toJSONSchema(WriteFileArgsSchema) }, { name: 'edit_file', @@ -366,7 +366,7 @@ class FileSystemServer { 'Make line-based edits to a text file. Each edit replaces exact line sequences ' + 'with new content. Returns a git-style diff showing the changes made. ' + 'Only works within allowed directories.', - inputSchema: EditFileArgsSchema + inputSchema: z.toJSONSchema(EditFileArgsSchema) }, { name: 'create_directory', @@ -375,7 +375,7 @@ class FileSystemServer { 'nested directories in one operation. If the directory already exists, ' + 'this operation will succeed silently. Perfect for setting up directory ' + 'structures for projects or ensuring required paths exist. Only works within allowed directories.', - inputSchema: CreateDirectoryArgsSchema + inputSchema: z.toJSONSchema(CreateDirectoryArgsSchema) }, { name: 'list_directory', @@ -384,7 +384,7 @@ class FileSystemServer { 'Results clearly distinguish between files and directories with [FILE] and [DIR] ' + 'prefixes. This tool is essential for understanding directory structure and ' + 'finding specific files within a directory. Only works within allowed directories.', - inputSchema: ListDirectoryArgsSchema + inputSchema: z.toJSONSchema(ListDirectoryArgsSchema) }, { name: 'directory_tree', @@ -393,7 +393,7 @@ class FileSystemServer { "Each entry includes 'name', 'type' (file/directory), and 'children' for directories. " + 'Files have no children array, while directories always have a children array (which may be empty). ' + 'The output is formatted with 2-space indentation for readability. Only works within allowed directories.', - inputSchema: DirectoryTreeArgsSchema + inputSchema: z.toJSONSchema(DirectoryTreeArgsSchema) }, { name: 'move_file', @@ -402,7 +402,7 @@ class FileSystemServer { 'and rename them in a single operation. If the destination exists, the ' + 'operation will fail. Works across different directories and can be used ' + 'for simple renaming within the same directory. Both source and destination must be within allowed directories.', - inputSchema: MoveFileArgsSchema + inputSchema: z.toJSONSchema(MoveFileArgsSchema) }, { name: 'search_files', @@ -412,7 +412,7 @@ class FileSystemServer { 'is case-insensitive and matches partial names. Returns full paths to all ' + "matching items. Great for finding files when you don't know their exact location. " + 'Only searches within allowed directories.', - inputSchema: SearchFilesArgsSchema + inputSchema: z.toJSONSchema(SearchFilesArgsSchema) }, { name: 'get_file_info', @@ -421,7 +421,7 @@ class FileSystemServer { 'information including size, creation time, last modified time, permissions, ' + 'and type. This tool is perfect for understanding file characteristics ' + 'without reading the actual content. Only works within allowed directories.', - inputSchema: GetFileInfoArgsSchema + inputSchema: z.toJSONSchema(GetFileInfoArgsSchema) }, { name: 'list_allowed_directories', diff --git a/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts b/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts index 8d35ec3888..9d2857815a 100644 --- a/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts +++ b/src/renderer/src/aiCore/clients/anthropic/AnthropicAPIClient.ts @@ -675,7 +675,7 @@ export class AnthropicAPIClient extends BaseApiClient< const toolCall = toolCalls[rawChunk.index] if (toolCall) { try { - toolCall.input = JSON.parse(accumulatedJson) + toolCall.input = accumulatedJson ? JSON.parse(accumulatedJson) : {} logger.debug(`Tool call id: ${toolCall.id}, accumulated json: ${accumulatedJson}`) controller.enqueue({ type: ChunkType.MCP_TOOL_CREATED, diff --git a/src/renderer/src/utils/mcp-tools.ts b/src/renderer/src/utils/mcp-tools.ts index fa0a12c5f3..ca3e355768 100644 --- a/src/renderer/src/utils/mcp-tools.ts +++ b/src/renderer/src/utils/mcp-tools.ts @@ -488,6 +488,9 @@ export function getMcpServerByTool(tool: MCPTool) { } export function isToolAutoApproved(tool: MCPTool, server?: MCPServer): boolean { + if (tool.isBuiltIn) { + return true + } const effectiveServer = server ?? getMcpServerByTool(tool) return effectiveServer ? !effectiveServer.disabledAutoApproveTools?.includes(tool.name) : false }