Skip to content

Commit fccbd43

Browse files
jvdmrjlowinJoris Vandermeersch
authored
Add FASTMCP_TRANSPORT setting for default transport selection (#1796)
Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Co-authored-by: Joris Vandermeersch <joris.vandermeersch@vrt.be>
1 parent 404b820 commit fccbd43

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/fastmcp/server/mixins/transport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async def run_async(
5353
if show_banner is None:
5454
show_banner = fastmcp.settings.show_server_banner
5555
if transport is None:
56-
transport = "stdio"
56+
transport = fastmcp.settings.transport
5757
if transport not in {"stdio", "http", "sse", "streamable-http"}:
5858
raise ValueError(f"Unknown transport: {transport}")
5959

src/fastmcp/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ def normalize_log_level(cls, v):
227227
),
228228
] = None
229229

230+
# Transport settings
231+
transport: Literal["stdio", "http", "sse", "streamable-http"] = "stdio"
232+
230233
# HTTP settings
231234
host: str = "127.0.0.1"
232235
port: int = 8000

tests/utilities/test_tests.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from unittest.mock import AsyncMock, patch
2+
13
import fastmcp
4+
from fastmcp import FastMCP
25
from fastmcp.utilities.tests import temporary_settings
36

47

@@ -8,3 +11,31 @@ def test_temporary_settings(self):
811
with temporary_settings(log_level="ERROR"):
912
assert fastmcp.settings.log_level == "ERROR"
1013
assert fastmcp.settings.log_level == "DEBUG"
14+
15+
16+
class TestTransportSetting:
17+
def test_transport_default_is_stdio(self):
18+
assert fastmcp.settings.transport == "stdio"
19+
20+
def test_transport_setting_can_be_changed(self):
21+
with temporary_settings(transport="http"):
22+
assert fastmcp.settings.transport == "http"
23+
assert fastmcp.settings.transport == "stdio"
24+
25+
async def test_run_async_uses_transport_setting(self):
26+
mcp = FastMCP("test")
27+
with temporary_settings(transport="http"):
28+
with patch.object(
29+
mcp, "run_http_async", new_callable=AsyncMock
30+
) as mock_http:
31+
await mcp.run_async()
32+
mock_http.assert_called_once()
33+
34+
async def test_run_async_explicit_transport_overrides_setting(self):
35+
mcp = FastMCP("test")
36+
with temporary_settings(transport="http"):
37+
with patch.object(
38+
mcp, "run_stdio_async", new_callable=AsyncMock
39+
) as mock_stdio:
40+
await mcp.run_async(transport="stdio")
41+
mock_stdio.assert_called_once()

0 commit comments

Comments
 (0)