Fix server name normalization for tool routing compatibility

## Problem
MCP server names with invalid characters (spaces, special chars, etc.)
caused tool routing failures when prefixed to tool names, as OpenRouter
API requires tool names to match pattern ^[a-zA-Z0-9_-]{1,64}$

## Solution
- **Added normalizeServerName utility**: Converts invalid characters to underscores
- **Centralized normalization logic**: Shared utility prevents code duplication
- **Fixed tool routing**: Agent loop now matches normalized server names correctly
- **Updated tool discovery**: MCP tools use normalized prefixes consistently

## Examples
- "My Weather Service\!" → "My_Weather_Service"
- "Test@Server#123" → "Test_Server_123"
- "Server...with...dots" → "Server_with_dots"
- "  spaces_and__double__underscores  " → "spaces_and_double_underscores"

## Technical Details
- Created /src/utils/mcpUtils.ts for shared normalization function
- Updated MCPConnectionManager to use normalized prefixes in tool names
- Fixed agent loop to match connections using normalized names
- Maintains original server names in UI while using normalized names for API

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jerome 2025-06-12 14:23:51 +01:00
commit 0266b4871e
No known key found for this signature in database

View file

@ -17,6 +17,7 @@ import type {
MCPError,
} from '@/types/mcp';
import type { Tool } from '@/types/inference';
import { normalizeServerName } from '@/utils/mcpUtils';
interface MCPOAuthState {
codeVerifier: string;
@ -587,10 +588,11 @@ export class MCPConnectionManager {
// Transform MCP tools to our Tool interface with name prefixing
// Use double underscore instead of dot to comply with OpenRouter API requirements
const normalizedServerName = normalizeServerName(this.connection.name);
return result.tools.map(tool => ({
type: 'function' as const,
function: {
name: `${this.connection.name}__${tool.name}`,
name: `${normalizedServerName}__${tool.name}`,
description: `[${this.connection.name}] ${tool.description || ''}`,
parameters: tool.inputSchema || {},
},
@ -643,8 +645,9 @@ export class MCPConnectionManager {
}
// 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)
const normalizedServerName = normalizeServerName(this.connection.name);
const unprefixedName = toolName.startsWith(`${normalizedServerName}__`)
? toolName.slice(normalizedServerName.length + 2)
: toolName;
try {