Share MCP tool specs until search results are selected (#45509)

## Why

Building MCP search entries eagerly cloned tool specs and normalized schemas even for tools that were never selected.

## What changed

Store search specs in `Arc<ToolSpec>` and let MCP search entries share the handler's spec. Materialize and normalize loadable specs only for selected results, preserving existing result formatting and dynamic-tool cache equality behavior.

## Testing

Add coverage for function, freeform, and namespace specs that verifies shared specs produce equivalent results, retain the source while needed, and release it when the search entry is dropped.

GitOrigin-RevId: 4260f2e2527834d8a856b1528654c6951aab8a7d
This commit is contained in:
Owen Lin authored and copyberry committed 2026-09-14 20:20:29 +00:00
1 parent b9bfc0aff8
commit ea3c4848d8
5 files changed
+145 -52

No files matched your search

+2 -2
View File
@@ -157,9 +157,9 @@ impl ToolExecutor<ToolInvocation> for McpHandler {
.map(str::to_string),
});
ToolSearchInfo::from_spec(
ToolSearchInfo::from_shared_spec(
build_mcp_search_text(&self.tool_info),
self.spec(),
Arc::clone(&self.spec),
source_info,
)
}
@@ -29,7 +29,7 @@ fn search_info_uses_connector_name_for_output_namespace_description() {
let handler = McpHandler::new(tool_info).expect("MCP tool spec should build");
let search_info = handler.search_info().expect("MCP search info");
let LoadableToolSpec::Namespace(namespace) = search_info.entry.output else {
let LoadableToolSpec::Namespace(namespace) = search_info.entry.to_loadable_spec() else {
panic!("expected namespace search output");
};
assert_eq!(namespace.description, "Tools for working with Calendar.");
@@ -57,7 +57,7 @@ fn mcp_namespace_descriptions_preserve_complete_metadata() {
description: Some(full_description.clone()),
})
);
let LoadableToolSpec::Namespace(namespace) = search_info.entry.output else {
let LoadableToolSpec::Namespace(namespace) = search_info.entry.to_loadable_spec() else {
panic!("expected namespace search output");
};
assert_eq!(namespace.description, full_description);
@@ -83,7 +83,7 @@ fn mcp_namespace_descriptions_are_bounded_at_512_kib() {
description: Some(full_description),
})
);
let LoadableToolSpec::Namespace(namespace) = search_info.entry.output else {
let LoadableToolSpec::Namespace(namespace) = search_info.entry.to_loadable_spec() else {
panic!("expected namespace search output");
};
assert_eq!(namespace.description, expected_description);
@@ -104,7 +104,7 @@ fn agent_plugin_namespace_descriptions_use_the_stricter_bound() {
description: Some(expected_description.clone()),
})
);
let LoadableToolSpec::Namespace(namespace) = search_info.entry.output else {
let LoadableToolSpec::Namespace(namespace) = search_info.entry.to_loadable_spec() else {
panic!("expected namespace search output");
};
assert_eq!(namespace.description, expected_description);
@@ -250,7 +250,7 @@ impl ToolSearchHandler {
results: impl IntoIterator<Item = &'a ToolSearchEntry>,
) -> Result<Vec<LoadableToolSpec>, FunctionCallError> {
Ok(coalesce_loadable_tool_specs(
results.into_iter().map(|entry| entry.output.clone()),
results.into_iter().map(ToolSearchEntry::to_loadable_spec),
))
}
}