π The fast, Pythonic way to build MCP servers and clients.
Keep OpenAPI path segments that contain a colon (#5613)
Asem ahmed7d44072040bf
diff --git a/fastmcp_slim/fastmcp/utilities/openapi/director.py b/fastmcp_slim/fastmcp/utilities/openapi/director.pyindex 7067586a0..68568734e 100644--- a/fastmcp_slim/fastmcp/utilities/openapi/director.py+++ b/fastmcp_slim/fastmcp/utilities/openapi/director.py@@ -4,7 +4,7 @@ import ioimport json as _jsonfrom email.message import Messagefrom typing import Any, ClassVar-from urllib.parse import quote, unquote, urljoin+from urllib.parse import quote, unquoteimport httpx2from jsonschema_path import SchemaPath@@ -428,8 +428,9 @@ class RequestDirector:safe_value = quote(str(param_value), safe="").replace(".", "%2E")url_path = url_path.replace(placeholder, safe_value)- # Combine with base URL- return urljoin(base_url.rstrip("/") + "/", url_path.lstrip("/"))+ # Combine with base URL. Plain concatenation: urljoin would read a+ # leading "name:" segment (e.g. "/job-1:cancel") as a URL scheme.+ return f"{base_url.rstrip('/')}/{url_path.lstrip('/')}"# Export public symbolsdiff --git a/tests/utilities/openapi/test_path_values.py b/tests/utilities/openapi/test_path_values.pyindex 00acc60d3..7b6dff1e8 100644--- a/tests/utilities/openapi/test_path_values.py+++ b/tests/utilities/openapi/test_path_values.py@@ -93,3 +93,71 @@ def test_path_value_rejects_excessive_encoding_layers():RequestDirector(SchemaPath.from_dict({}))._build_url("/items/{id}", {"id": value}, "https://api.example.com")+++ "base_url",+ [+ "https://api.example.com",+ "https://api.example.com/",+ "https://api.example.com/v1",+ "https://api.example.com/v1/",+ ],+)+ "template,params,path",+ [+ ("/things:batchGet", {}, "/things:batchGet"),+ ("/{job}:cancel", {"job": "abc123"}, "/abc123:cancel"),+ ("/jobs/{job}:cancel", {"job": "abc123"}, "/jobs/abc123:cancel"),+ ("/v1/things:search", {}, "/v1/things:search"),+ ("things", {}, "/things"),+ ],+)+def test_path_keeps_leading_segment_with_colon(+ base_url: str, template: str, params: dict[str, str], path: str+):+ url = RequestDirector(SchemaPath.from_dict({}))._build_url(+ template, params, base_url+ )+ assert url == base_url.rstrip("/") + path+++async def test_colon_path_segment_reaches_http_endpoint():+ spec = {+ "openapi": "3.1.0",+ "info": {"title": "API", "version": "1"},+ "paths": {+ "/{job}:cancel": {+ "post": {+ "operationId": "cancel_job",+ "parameters": [+ {+ "name": "job",+ "in": "path",+ "required": True,+ "schema": {"type": "string"},+ }+ ],+ "responses": {"200": {"description": "OK"}},+ }+ }+ },+ }+ requests: list[httpx2.Request] = []++ def capture(request: httpx2.Request) -> httpx2.Response:+ requests.append(request)+ return httpx2.Response(200, json={"ok": True})++ async with httpx2.AsyncClient(+ base_url="https://api.example.com/v1",+ transport=httpx2.MockTransport(capture),+ ) as http:+ server = FastMCP.from_openapi(openapi_spec=spec, client=http)+ async with Client(server) as client:+ await client.call_tool("cancel_job", {"job": "abc123"})++ assert [str(request.url) for request in requests] == [+ "https://api.example.com/v1/abc123:cancel"+ ]