Give MCP tools provider-safe public names (#12875)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -186,10 +186,17 @@ impl McpFixtureServer {
|
||||
}
|
||||
|
||||
#[tool(name = "db.query", description = "A tool whose name contains a dot")]
|
||||
fn db_query(&self) -> Result<CallToolResult, McpError> {
|
||||
fn dotted_db_query(&self) -> Result<CallToolResult, McpError> {
|
||||
Ok(CallToolResult::success(vec![ContentBlock::text("rows")]))
|
||||
}
|
||||
|
||||
#[tool(description = "Spelled like db.query once the dot is replaced")]
|
||||
fn db_query(&self) -> Result<CallToolResult, McpError> {
|
||||
Ok(CallToolResult::success(vec![ContentBlock::text(
|
||||
"underscore rows",
|
||||
)]))
|
||||
}
|
||||
|
||||
#[tool(description = "Send a progress notification, then finish")]
|
||||
async fn notify(&self, peer: Peer<RoleServer>) -> Result<CallToolResult, McpError> {
|
||||
peer.notify_progress(ProgressNotificationParam::new(
|
||||
|
||||
@@ -132,7 +132,7 @@ impl ToolCatalog {
|
||||
let mut by_name = HashMap::new();
|
||||
for (extension, tools) in extensions.iter().zip(lists) {
|
||||
for tool in tools.iter() {
|
||||
let name = tool.name.to_string();
|
||||
let name = provider_safe_tool_name(&tool.name);
|
||||
if by_name.contains_key(&name) {
|
||||
warn!(
|
||||
tool = %name,
|
||||
@@ -155,9 +155,11 @@ impl ToolCatalog {
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let mut tool = tool.clone();
|
||||
tool.name = name.clone().into();
|
||||
by_name.insert(name, entries.len());
|
||||
entries.push(CatalogEntry {
|
||||
tool: tool.clone(),
|
||||
tool,
|
||||
extension: Arc::clone(extension),
|
||||
server_name,
|
||||
});
|
||||
@@ -171,6 +173,24 @@ impl ToolCatalog {
|
||||
}
|
||||
}
|
||||
|
||||
/// MCP allows tool names that providers reject (OpenAI requires
|
||||
/// `^[a-zA-Z0-9_-]{1,64}$`). A rewritten name carries a hash of the original
|
||||
/// so that `db.query` and `db_query`, or two names sharing their first 64
|
||||
/// characters, stay distinct tools.
|
||||
fn provider_safe_tool_name(name: &str) -> String {
|
||||
let is_safe = |c: char| c.is_ascii_alphanumeric() || c == '_' || c == '-';
|
||||
if name.len() <= 64 && name.chars().all(is_safe) {
|
||||
return name.to_string();
|
||||
}
|
||||
let sanitized: String = name
|
||||
.chars()
|
||||
.map(|c| if is_safe(c) { c } else { '_' })
|
||||
.take(55)
|
||||
.collect();
|
||||
let hash = blake3::hash(name.as_bytes()).to_hex();
|
||||
format!("{sanitized}_{:.8}", hash.as_str())
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ExtensionLease {
|
||||
id: LeaseId,
|
||||
|
||||
@@ -264,6 +264,8 @@ async fn inspect_context(lease: &ExtensionLease, name: &str) -> ContextReport {
|
||||
serde_json::from_str(&call_text(lease, name).await).unwrap()
|
||||
}
|
||||
|
||||
const DOTTED_DB_QUERY: &str = "fixture_http__db_query_9cffee1f";
|
||||
|
||||
/// One manager, three transports, one lease: the catalog, mangled-name
|
||||
/// recovery, filtering, app scoping, the restart-on-config-change rule, and
|
||||
/// what a held lease does and does not see when the registry changes.
|
||||
@@ -296,11 +298,22 @@ async fn extension_lifecycle_across_real_transports(stdio_version: ProtocolVersi
|
||||
let configs = vec![http.clone(), stdio.clone(), todo.clone(), skills.clone()];
|
||||
let lease = fx.resolve(&session, &configs).await;
|
||||
|
||||
// The catalog: prefixed per extension, filtered by available_tools.
|
||||
// The catalog: prefixed per extension, filtered by available_tools, and
|
||||
// spelled the way providers accept.
|
||||
let names = tool_names(&lease.tools().await);
|
||||
for name in &names {
|
||||
assert!(
|
||||
name.len() <= 64
|
||||
&& name
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'),
|
||||
"{name}"
|
||||
);
|
||||
}
|
||||
for expected in [
|
||||
"fixture_http__get_code",
|
||||
"fixture_http__db.query",
|
||||
"fixture_http__db_query",
|
||||
DOTTED_DB_QUERY,
|
||||
"fixture_stdio__get_code",
|
||||
"fixture_stdio__inspect_context",
|
||||
"todo__todo_write",
|
||||
@@ -382,15 +395,22 @@ async fn extension_lifecycle_across_real_transports(stdio_version: ProtocolVersi
|
||||
)
|
||||
.starts_with("Updated ("));
|
||||
|
||||
// Spellings a model mangles resolve to the same tools; an exact dotted
|
||||
// name is never rewritten.
|
||||
// Spellings a model mangles resolve to the same tools, and a sanitized
|
||||
// name reaches the server under its original name.
|
||||
assert_eq!(call_text(&lease, "fixture_stdio.get_code").await, FAKE_CODE);
|
||||
assert_eq!(
|
||||
call_text(&lease, "functions.fixture_stdio__get_code").await,
|
||||
FAKE_CODE
|
||||
);
|
||||
assert_eq!(call_text(&lease, "fixture_http__db.query").await, "rows");
|
||||
assert_eq!(call_text(&lease, "fixture_http.db.query").await, "rows");
|
||||
assert_eq!(call_text(&lease, DOTTED_DB_QUERY).await, "rows");
|
||||
assert_eq!(
|
||||
call_text(&lease, "fixture_http__db_query").await,
|
||||
"underscore rows"
|
||||
);
|
||||
assert_eq!(
|
||||
call_text(&lease, "fixture_http.db_query").await,
|
||||
"underscore rows"
|
||||
);
|
||||
let unknown = call(&lease, "no_such_tool", None).await.unwrap_err();
|
||||
assert_eq!(unknown.code, ErrorCode::RESOURCE_NOT_FOUND);
|
||||
assert!(
|
||||
|
||||
Reference in New Issue
Block a user