Skip to content

Commit fb110a3

Browse files
committed
MCP: dispatch tools by sanitized wire name — servers named "127.0.0.1" / "my_server" were unreachable (LLM sees mcp_127001_*, dispatcher split on "_")
1 parent ac24351 commit fb110a3

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎Agent/AgentViewModel/NativeToolHandlers/NTH-Misc.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension AgentViewModel {
3232
let mcpTools = mcp.discoveredTools
3333
.filter { mcp.isToolEnabled(serverName: $0.serverName, toolName: $0.name) }
3434
.sorted { $0.name < $1.name }
35-
.map { "mcp_\($0.serverName)_\($0.name)" }
35+
.map { MCPService.wireName(serverName: $0.serverName, toolName: $0.name) }
3636
let all = builtIn + (mcpTools.isEmpty ? [] : ["--- MCP Tools ---"] + mcpTools)
3737
return all.joined(separator: "\n")
3838
// Memory tool — Claude-compatible (memory_20250818) filesystem-shaped

‎Agent/AgentViewModel/TaskExecution/MCPTools.swift‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ extension AgentViewModel {
1818
) async -> Bool {
1919
guard name.hasPrefix("mcp_") else { return false }
2020

21+
// Match on the sanitized wire name — server names may contain "." or "_" (e.g. "127.0.0.1", "my_server").
22+
let wireMatch = MCPService.shared.discoveredTools.first {
23+
MCPService.wireName(serverName: $0.serverName, toolName: $0.name) == name
24+
}
2125
let parts = name.dropFirst(4).split(separator: "_", maxSplits: 1)
22-
let serverName = String(parts.first ?? "")
23-
let toolName = String(parts.last ?? "")
26+
let serverName = wireMatch?.serverName ?? String(parts.first ?? "")
27+
let toolName = wireMatch?.name ?? String(parts.last ?? "")
2428

2529
// Snapshot disabled state once to avoid TOCTOU races
2630
let disabledSnapshot = MCPService.shared.disabledTools

‎Agent/MCP/MCPInstaller.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ enum MCPInstaller {
206206
}
207207

208208
private static func toolSummary(_ config: MCPServerConfig) -> String {
209-
let tools = MCPService.shared.discoveredTools.filter { $0.serverId == config.id }.map { "mcp_\(config.name)_\($0.name)" }
209+
let tools = MCPService.shared.discoveredTools.filter { $0.serverId == config.id }.map { MCPService.wireName(serverName: config.name, toolName: $0.name) }
210210
guard !tools.isEmpty else { return "No tools discovered yet." }
211211
let shown = tools.prefix(25).joined(separator: ", ")
212212
return "\(tools.count) tools available now: \(shown)\(tools.count > 25 ? ", …" : "")."

‎Agent/MCP/MCPService.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ final class MCPService: @unchecked Sendable {
3333
"\(serverName).\(toolName)"
3434
}
3535

36+
/// Tool name exactly as the LLM sees it — mirrors AgentTools.sanitizeToolName, which strips everything but
37+
/// [A-Za-z0-9_-] (server "127.0.0.1" → "mcp_127001_…"). Dispatch must match on this, not split on "_".
38+
nonisolated static func wireName(serverName: String, toolName: String) -> String {
39+
String("mcp_\(serverName)_\(toolName)".unicodeScalars.filter {
40+
CharacterSet.alphanumerics.contains($0) || $0 == "_" || $0 == "-"
41+
}.prefix(128))
42+
}
43+
3644
/// Check if a tool is enabled
3745
func isToolEnabled(serverName: String, toolName: String) -> Bool {
3846
!disabledTools.contains(Self.toolKey(serverName: serverName, toolName: toolName))

0 commit comments

Comments
 (0)