π The fast, Pythonic way to build MCP servers and clients.
Keep generated OpenAPI component names unique when a suffixed name is taken (#5337)
Asem ahmed59d158a1ebe9
diff --git a/fastmcp_slim/fastmcp/server/providers/openapi/provider.py b/fastmcp_slim/fastmcp/server/providers/openapi/provider.pyindex c16f14034..93ac67e24 100644--- a/fastmcp_slim/fastmcp/server/providers/openapi/provider.py+++ b/fastmcp_slim/fastmcp/server/providers/openapi/provider.py@@ -236,11 +236,18 @@ class OpenAPIProvider(Provider):component_type: Literal["tool", "resource", "resource_template", "prompt"],) -> str:"""Ensure the name is unique by appending numbers if needed."""- self._used_names[component_type][name] += 1- if self._used_names[component_type][name] == 1:+ used_names = self._used_names[component_type]+ used_names[name] += 1+ if used_names[name] == 1:return name- new_name = f"{name}_{self._used_names[component_type][name]}"+ # Skip suffixed names that are already taken, such as an operation+ # whose own name is `name_2`, and reserve the one we hand out.+ new_name = f"{name}_{used_names[name]}"+ while new_name in used_names:+ used_names[name] += 1+ new_name = f"{name}_{used_names[name]}"+ used_names[new_name] += 1logger.debug(f"Name collision: '{name}' exists as {component_type}. Using '{new_name}'.")diff --git a/tests/server/providers/openapi/test_server.py b/tests/server/providers/openapi/test_server.pyindex 501bb2ec2..9da641fc6 100644--- a/tests/server/providers/openapi/test_server.py+++ b/tests/server/providers/openapi/test_server.py@@ -7,6 +7,7 @@ from fastmcp import FastMCPfrom fastmcp.client import Clientfrom fastmcp.server.providers.openapi import OpenAPIProviderfrom fastmcp.server.providers.openapi.provider import DEFAULT_TIMEOUT+from fastmcp.server.providers.openapi.routing import MCPType, RouteMapclass TestOpenAPIProviderServerVariables:@@ -361,3 +362,85 @@ class TestOpenAPIProviderBasicFunctionality:"title": "User",}assert tool.output_schema == expected_output_schema+++def names_spec(*routes: tuple[str, str]) -> dict:+ return {+ "openapi": "3.1.0",+ "info": {"title": "Names", "version": "1.0.0"},+ "paths": {+ path: {+ "get": {+ "operationId": operation_id,+ "responses": {"200": {"description": "OK"}},+ }+ }+ for path, operation_id in routes+ },+ }+++class TestOpenAPIProviderComponentNames:+ """Generated component names stay unique so no route is silently dropped."""++ async def test_duplicate_names_get_numbered_suffixes(self):+ spec = names_spec(+ ("/a", "list_items__a"), ("/b", "list_items__b"), ("/c", "list_items__c")+ )+ server = FastMCP.from_openapi(+ spec, client=httpx2.AsyncClient(base_url="http://test")+ )++ async with Client(server) as client:+ tools = await client.list_tools()++ assert sorted(tool.name for tool in tools) == [+ "list_items",+ "list_items_2",+ "list_items_3",+ ]++ @pytest.mark.parametrize(+ "routes",+ [+ [("/a", "list_items__a"), ("/b", "list_items__b"), ("/c", "list_items_2")],+ [("/c", "list_items_2"), ("/a", "list_items__a"), ("/b", "list_items__b")],+ ],+ )+ async def test_suffix_does_not_reuse_an_existing_name(+ self, routes: list[tuple[str, str]]+ ):+ requested_paths: list[str] = []++ def handler(request: httpx2.Request) -> httpx2.Response:+ requested_paths.append(request.url.path)+ return httpx2.Response(200, json={})++ api = httpx2.AsyncClient(+ base_url="http://test", transport=httpx2.MockTransport(handler)+ )+ server = FastMCP.from_openapi(names_spec(*routes), client=api)++ async with Client(server) as client:+ tools = await client.list_tools()+ for tool in tools:+ await client.call_tool(tool.name, {})++ assert len(tools) == 3+ assert len({tool.name for tool in tools}) == 3+ assert sorted(requested_paths) == ["/a", "/b", "/c"]++ async def test_resource_suffix_does_not_reuse_an_existing_name(self):+ spec = names_spec(+ ("/a", "list_items__a"), ("/b", "list_items__b"), ("/c", "list_items_2")+ )+ server = FastMCP.from_openapi(+ spec,+ client=httpx2.AsyncClient(base_url="http://test"),+ route_maps=[RouteMap(mcp_type=MCPType.RESOURCE)],+ )++ async with Client(server) as client:+ resources = await client.list_resources()++ assert len({str(resource.uri) for resource in resources}) == 3