π The fast, Pythonic way to build MCP servers and clients.
fix(search): rebuild the BM25 index when the tool name set changes (#6282)
Felix Shi7039f79079cf
diff --git a/fastmcp_slim/fastmcp/server/transforms/search/bm25.py b/fastmcp_slim/fastmcp/server/transforms/search/bm25.pyindex c1f26d87f..bbb48fb27 100644--- a/fastmcp_slim/fastmcp/server/transforms/search/bm25.py+++ b/fastmcp_slim/fastmcp/server/transforms/search/bm25.py@@ -81,8 +81,8 @@ class _BM25Index:def _catalog_hash(tools: Sequence[Tool]) -> str:- """SHA256 hash of sorted tool searchable text for staleness detection."""- key = json.dumps(sorted(_extract_searchable_text(t) for t in tools))+ """SHA256 hash of sorted tool names and searchable text for staleness detection."""+ key = json.dumps(sorted((t.name, _extract_searchable_text(t)) for t in tools))return hashlib.sha256(key.encode()).hexdigest()diff --git a/tests/server/transforms/test_search.py b/tests/server/transforms/test_search.pyindex b82eb5cb7..72b6020f4 100644--- a/tests/server/transforms/test_search.py+++ b/tests/server/transforms/test_search.py@@ -369,6 +369,25 @@ class TestBM25Search:assert results == [updated]assert transform._index is index+ async def test_search_rebuilds_when_names_change_but_searchable_text_matches(+ self,+ ) -> None:+ def action() -> str:+ return "ok"++ first = Tool.from_function(action, name="foo", description="bar")+ other = Tool.from_function(action, name="baz")+ # "foo" with description "bar" and the bare name "foo bar" produce the+ # same searchable text, so a text-only staleness check would keep the+ # stale name list and fail to resolve the renamed tool.+ renamed = Tool.from_function(action, name="foo bar")++ transform = BM25SearchTransform()+ assert await transform._search([first, other], "bar") == [first]++ results = await transform._search([renamed, other], "bar")+ assert results == [renamed]+async def test_search_rebuilds_when_description_contains_catalog_delimiter(self,) -> None: