π The fast, Pythonic way to build MCP servers and clients.
fix(cli): honor explicit URL transport (#5529)
mikada978232be41
diff --git a/fastmcp_slim/fastmcp/cli/client.py b/fastmcp_slim/fastmcp/cli/client.pyindex 090b282ab..2d1c3c929 100644--- a/fastmcp_slim/fastmcp/cli/client.py+++ b/fastmcp_slim/fastmcp/cli/client.py@@ -8,6 +8,7 @@ import sysimport warningsfrom pathlib import Pathfrom typing import Annotated, Any, Literal+from urllib.parse import urlsplit, urlunsplitimport cycloptsimport mcp_types@@ -64,9 +65,10 @@ def resolve_server_spec(Exactly one of ``server_spec`` or ``command`` should be provided.Resolution order for ``server_spec``:- 1. URLs (``http://``, ``https://``) β passed through as-is.- If ``--transport`` is ``sse``, the URL is rewritten to end with ``/sse``- so ``infer_transport`` picks the right transport.+ 1. URLs (``http://``, ``https://``) β passed through as-is unless an+ explicit transport is selected. In that case, the selected transport+ is constructed directly; SSE URLs are rewritten to end with ``/sse``+ while preserving their query and fragment.2. Existing file paths, or strings ending in ``.py``/``.js``/``.json``.3. Anything else β name-based resolution via ``resolve_name``.@@ -94,8 +96,15 @@ def resolve_server_spec(# 1. URLif spec.startswith(("http://", "https://")):- if transport == "sse" and not spec.rstrip("/").endswith("/sse"):- spec = spec.rstrip("/") + "/sse"+ if transport == "sse":+ parsed = urlsplit(spec)+ path = parsed.path+ if not path.rstrip("/").endswith("/sse"):+ path = f"{path.rstrip('/')}/sse"+ spec = urlunsplit(parsed._replace(path=path))+ return SSETransport(spec)+ if transport == "http":+ return StreamableHttpTransport(spec)return spec# 2. File path (must be a file, not a directory)diff --git a/tests/cli/test_client_commands.py b/tests/cli/test_client_commands.pyindex 5b04bb4d2..d76bc8296 100644--- a/tests/cli/test_client_commands.py+++ b/tests/cli/test_client_commands.py@@ -29,6 +29,7 @@ from fastmcp.cli.client import ()from fastmcp.client.auth.oauth import OAuthfrom fastmcp.client.client import CallToolResult+from fastmcp.client.transports.http import StreamableHttpTransportfrom fastmcp.client.transports.sse import SSETransportfrom fastmcp.client.transports.stdio import StdioTransportfrom fastmcp.mcp_config import RemoteMCPServer@@ -354,19 +355,45 @@ class TestResolveServerSpec:def test_transport_sse_rewrites_url(self):result = resolve_server_spec("http://localhost:8000/mcp", transport="sse")- assert result == "http://localhost:8000/mcp/sse"+ assert isinstance(result, SSETransport)+ assert result.url == "http://localhost:8000/mcp/sse"def test_transport_sse_no_duplicate_suffix(self):result = resolve_server_spec("http://localhost:8000/sse", transport="sse")- assert result == "http://localhost:8000/sse"+ assert isinstance(result, SSETransport)+ assert result.url == "http://localhost:8000/sse"def test_transport_sse_trailing_slash(self):result = resolve_server_spec("http://localhost:8000/mcp/", transport="sse")- assert result == "http://localhost:8000/mcp/sse"+ assert isinstance(result, SSETransport)+ assert result.url == "http://localhost:8000/mcp/sse"def test_transport_http_leaves_url_unchanged(self):result = resolve_server_spec("http://localhost:8000/mcp", transport="http")- assert result == "http://localhost:8000/mcp"+ assert isinstance(result, StreamableHttpTransport)+ assert result.url == "http://localhost:8000/mcp"++ @pytest.mark.parametrize("transport", ["http", "sse"])+ def test_explicit_transport_preserves_query_and_fragment(self, transport):+ result = resolve_server_spec(+ "https://example.test/mcp?token=abc#section", transport=transport+ )+ if transport == "sse":+ assert isinstance(result, SSETransport)+ assert result.url == "https://example.test/mcp/sse?token=abc#section"+ else:+ assert isinstance(result, StreamableHttpTransport)+ assert result.url == "https://example.test/mcp?token=abc#section"++ def test_explicit_http_overrides_sse_path(self):+ result = resolve_server_spec("https://example.test/sse", transport="http")+ assert isinstance(result, StreamableHttpTransport)+ assert result.url == "https://example.test/sse"++ def test_no_explicit_transport_preserves_inference(self):+ assert resolve_server_spec("https://example.test/sse?token=abc") == (+ "https://example.test/sse?token=abc"+ )# ---------------------------------------------------------------------------