-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix: parse MCP server names with underscores #2746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2245,6 +2245,21 @@ impl McpPool { | |||||||||||||||||||||||||||||||||||||||||||||||
| anyhow::bail!("Invalid MCP tool name: {prefixed_name}"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| let rest = &prefixed_name[4..]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| let mut resolved = None; | ||||||||||||||||||||||||||||||||||||||||||||||||
| for (idx, _) in rest.match_indices('_') { | ||||||||||||||||||||||||||||||||||||||||||||||||
| let server = &rest[..idx]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| let tool = &rest[idx + 1..]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| if server.is_empty() || tool.is_empty() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| if self.connections.contains_key(server) || self.config.servers.contains_key(server) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| resolved = Some((server, tool)); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| if let Some((server, tool)) = resolved { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return Ok((server, tool)); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2248
to
+2261
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Adding a new server whose name is a prefix-extension of an existing one (e.g. registering |
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let Some((server, tool)) = rest.split_once('_') else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| anyhow::bail!("Invalid MCP tool name format: {prefixed_name}"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -3638,6 +3653,49 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| #[tokio::test] | ||||||||||||||||||||||||||||||||||||||||||||||||
| async fn mcp_pool_call_tool_preserves_server_names_with_underscores() { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3656
to
+3657
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||||||||||||||||||||||||||
| let sent = Arc::new(Mutex::new(Vec::new())); | ||||||||||||||||||||||||||||||||||||||||||||||||
| let transport = ScriptedValueTransport { | ||||||||||||||||||||||||||||||||||||||||||||||||
| sent: Arc::clone(&sent), | ||||||||||||||||||||||||||||||||||||||||||||||||
| responses: VecDeque::from([json_frame(serde_json::json!({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| "jsonrpc": "2.0", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "id": 1, | ||||||||||||||||||||||||||||||||||||||||||||||||
| "result": {"ok": true} | ||||||||||||||||||||||||||||||||||||||||||||||||
| }))]), | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| let mut conn = test_connection(Box::new(transport)); | ||||||||||||||||||||||||||||||||||||||||||||||||
| conn.name = "my_db".to_string(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| conn.tools = vec![McpTool { | ||||||||||||||||||||||||||||||||||||||||||||||||
| name: "execute_sql".to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| description: None, | ||||||||||||||||||||||||||||||||||||||||||||||||
| input_schema: serde_json::json!({}), | ||||||||||||||||||||||||||||||||||||||||||||||||
| }]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let mut pool = McpPool::new(McpConfig { | ||||||||||||||||||||||||||||||||||||||||||||||||
| timeouts: McpTimeouts::default(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| servers: HashMap::new(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| pool.connections.insert("my_db".to_string(), conn); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| let result = pool | ||||||||||||||||||||||||||||||||||||||||||||||||
| .call_tool( | ||||||||||||||||||||||||||||||||||||||||||||||||
| "mcp_my_db_execute_sql", | ||||||||||||||||||||||||||||||||||||||||||||||||
| serde_json::json!({"query": "select 1"}), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(result, serde_json::json!({"ok": true})); | ||||||||||||||||||||||||||||||||||||||||||||||||
| let sent = sent.lock().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(sent[0]["method"], "tools/call"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(sent[0]["params"]["name"], "execute_sql"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!( | ||||||||||||||||||||||||||||||||||||||||||||||||
| sent[0]["params"]["arguments"], | ||||||||||||||||||||||||||||||||||||||||||||||||
| serde_json::json!({"query": "select 1"}) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| #[tokio::test] | ||||||||||||||||||||||||||||||||||||||||||||||||
| async fn json_rpc_session_error_is_marked_stale() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| let sent = Arc::new(Mutex::new(Vec::new())); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we want to resolve the tool name against the longest known server-name prefix, we can optimize this lookup by iterating backwards using
rmatch_indicesinstead ofmatch_indices.By searching from right to left, the first match we find that corresponds to a known server is guaranteed to be the longest prefix. This allows us to return early immediately, avoiding redundant map lookups and eliminating the need for the temporary
resolvedvariable.