π The fast, Pythonic way to build MCP servers and clients.
Add FASTMCP_TRANSPORT setting for default transport selection (#1796)
Joris Vandermeerschfccbd43c1ea4
diff --git a/src/fastmcp/server/mixins/transport.py b/src/fastmcp/server/mixins/transport.pyindex 1f069a02d..833b5e385 100644--- a/src/fastmcp/server/mixins/transport.py+++ b/src/fastmcp/server/mixins/transport.py@@ -53,7 +53,7 @@ class TransportMixin:if show_banner is None:show_banner = fastmcp.settings.show_server_bannerif transport is None:- transport = "stdio"+ transport = fastmcp.settings.transportif transport not in {"stdio", "http", "sse", "streamable-http"}:raise ValueError(f"Unknown transport: {transport}")diff --git a/src/fastmcp/settings.py b/src/fastmcp/settings.pyindex 561a80437..bc6d22065 100644--- a/src/fastmcp/settings.py+++ b/src/fastmcp/settings.py@@ -227,6 +227,9 @@ class Settings(BaseSettings):),] = None+ # Transport settings+ transport: Literal["stdio", "http", "sse", "streamable-http"] = "stdio"+# HTTP settingshost: str = "127.0.0.1"port: int = 8000diff --git a/tests/utilities/test_tests.py b/tests/utilities/test_tests.pyindex 6cbcc0dce..107781294 100644--- a/tests/utilities/test_tests.py+++ b/tests/utilities/test_tests.py@@ -1,4 +1,7 @@+from unittest.mock import AsyncMock, patch+import fastmcp+from fastmcp import FastMCPfrom fastmcp.utilities.tests import temporary_settings@@ -8,3 +11,31 @@ class TestTemporarySettings:with temporary_settings(log_level="ERROR"):assert fastmcp.settings.log_level == "ERROR"assert fastmcp.settings.log_level == "DEBUG"+++class TestTransportSetting:+ def test_transport_default_is_stdio(self):+ assert fastmcp.settings.transport == "stdio"++ def test_transport_setting_can_be_changed(self):+ with temporary_settings(transport="http"):+ assert fastmcp.settings.transport == "http"+ assert fastmcp.settings.transport == "stdio"++ async def test_run_async_uses_transport_setting(self):+ mcp = FastMCP("test")+ with temporary_settings(transport="http"):+ with patch.object(+ mcp, "run_http_async", new_callable=AsyncMock+ ) as mock_http:+ await mcp.run_async()+ mock_http.assert_called_once()++ async def test_run_async_explicit_transport_overrides_setting(self):+ mcp = FastMCP("test")+ with temporary_settings(transport="http"):+ with patch.object(+ mcp, "run_stdio_async", new_callable=AsyncMock+ ) as mock_stdio:+ await mcp.run_async(transport="stdio")+ mock_stdio.assert_called_once()