fix mcp tool names that are long or have invalid characters (based on 400 error) (#602)

This commit is contained in:
Olcan 2025-05-29 16:13:11 -07:00 committed by GitHub
parent 2db5d83023
commit fe049c286f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 3 deletions

View File

@ -116,12 +116,25 @@ async function connectAndDiscover(
}; };
removeSchemaProps(tool.inputSchema); removeSchemaProps(tool.inputSchema);
// if there are multiple MCP servers, prefix tool name with mcpServerName to avoid collisions
let toolNameForModel = tool.name;
if (Object.keys(mcpServers).length > 1) {
toolNameForModel = mcpServerName + '__' + toolNameForModel;
}
// replace invalid characters (based on 400 error message) with underscores
toolNameForModel = toolNameForModel.replace(/[^a-zA-Z0-9_.-]/g, '_');
// if longer than 63 characters, replace middle with '___'
// note 400 error message says max length is 64, but actual limit seems to be 63
if (toolNameForModel.length > 63) {
toolNameForModel =
toolNameForModel.slice(0, 28) + '___' + toolNameForModel.slice(-32);
}
toolRegistry.registerTool( toolRegistry.registerTool(
new DiscoveredMCPTool( new DiscoveredMCPTool(
mcpClient, mcpClient,
Object.keys(mcpServers).length > 1 toolNameForModel,
? mcpServerName + '__' + tool.name
: tool.name,
tool.description ?? '', tool.description ?? '',
tool.inputSchema, tool.inputSchema,
tool.name, tool.name,