From 0a06544bda99bf2874089548af6778bec06f3de7 Mon Sep 17 00:00:00 2001 From: Jerome Date: Thu, 12 Jun 2025 12:06:06 +0100 Subject: [PATCH] Fix MCP tool naming for OpenRouter API compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Change tool name separator from dot to double underscore (server__tool) - Update tool discovery to use server__tool_name format - Update tool execution routing to parse double underscore separator - Fix OpenRouter API error: tool names must match pattern '^[a-zA-Z0-9_-]{1,64}' This resolves the silent failure when MCP tools were present in conversations. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/hooks/useAgentLoop.ts | 8 ++++---- src/mcp/connection.ts | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/hooks/useAgentLoop.ts b/src/hooks/useAgentLoop.ts index 78e9ab6..7c2e6bc 100644 --- a/src/hooks/useAgentLoop.ts +++ b/src/hooks/useAgentLoop.ts @@ -225,10 +225,10 @@ export function useAgentLoop(config: Partial = {}): UseAgentLoo return { result }; } - // Check if it's an MCP tool (prefixed with server name) - if (toolCall.function.name.includes('.')) { - // Extract server name from tool name (format: "server.tool_name") - const [serverName] = toolCall.function.name.split('.'); + // Check if it's an MCP tool (prefixed with server name using double underscore) + if (toolCall.function.name.includes('__')) { + // Extract server name from tool name (format: "server__tool_name") + const [serverName] = toolCall.function.name.split('__'); // Find the connection ID for this server name const connection = connections.find(conn => conn.name === serverName); diff --git a/src/mcp/connection.ts b/src/mcp/connection.ts index 93b0f8a..fde9646 100644 --- a/src/mcp/connection.ts +++ b/src/mcp/connection.ts @@ -581,10 +581,11 @@ export class MCPConnectionManager implements MCPConnectionManager { const result = await this.client.listTools(); // Transform MCP tools to our Tool interface with name prefixing + // Use double underscore instead of dot to comply with OpenRouter API requirements return result.tools.map(tool => ({ type: 'function' as const, function: { - name: `${this.connection.name}.${tool.name}`, + name: `${this.connection.name}__${tool.name}`, description: `[${this.connection.name}] ${tool.description || ''}`, parameters: tool.inputSchema || {}, }, @@ -636,9 +637,9 @@ export class MCPConnectionManager implements MCPConnectionManager { throw new Error('Client not connected'); } - // Remove the server prefix from the tool name - const unprefixedName = toolName.startsWith(`${this.connection.name}.`) - ? toolName.slice(this.connection.name.length + 1) + // Remove the server prefix from the tool name (using double underscore separator) + const unprefixedName = toolName.startsWith(`${this.connection.name}__`) + ? toolName.slice(this.connection.name.length + 2) : toolName; try {