Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Fix #5559: Configuration helpers still discard explicit port=0 (#5562)

Nandana Dileep2c48fcbe14e4
diff --git a/fastmcp_slim/fastmcp/utilities/cli.py b/fastmcp_slim/fastmcp/utilities/cli.py
index 1f0a8d360..810cf4190 100644
--- a/fastmcp_slim/fastmcp/utilities/cli.py
+++ b/fastmcp_slim/fastmcp/utilities/cli.py
@@ -130,7 +130,7 @@ def load_and_merge_config(
config_dict["deployment"]["transport"] = transport_override
if host_override := cli_overrides.get("host"):
config_dict["deployment"]["host"] = host_override
- if port_override := cli_overrides.get("port"):
+ if (port_override := cli_overrides.get("port")) is not None:
config_dict["deployment"]["port"] = port_override
if path_override := cli_overrides.get("path"):
config_dict["deployment"]["path"] = path_override
diff --git a/fastmcp_slim/fastmcp/utilities/mcp_server_config/v1/mcp_server_config.py b/fastmcp_slim/fastmcp/utilities/mcp_server_config/v1/mcp_server_config.py
index c7009fa94..572472312 100644
--- a/fastmcp_slim/fastmcp/utilities/mcp_server_config/v1/mcp_server_config.py
+++ b/fastmcp_slim/fastmcp/utilities/mcp_server_config/v1/mcp_server_config.py
@@ -298,7 +298,7 @@ class MCPServerConfig(BaseModel):
# Build deployment config if any deployment args provided
deployment = None
- if any([transport, host, port, path, log_level, env, cwd, args]):
+ if any([transport, host, path, log_level, env, cwd, args]) or port is not None:
# Convert streamable-http to http for backward compatibility
if transport == "streamable-http":
transport = "http"
@@ -399,7 +399,7 @@ class MCPServerConfig(BaseModel):
run_args["transport"] = self.deployment.transport
if self.deployment.host:
run_args["host"] = self.deployment.host
- if self.deployment.port:
+ if self.deployment.port is not None:
run_args["port"] = self.deployment.port
if self.deployment.path:
run_args["path"] = self.deployment.path
diff --git a/tests/cli/test_mcp_server_config_integration.py b/tests/cli/test_mcp_server_config_integration.py
index 5b2cfecf6..9b6cff148 100644
--- a/tests/cli/test_mcp_server_config_integration.py
+++ b/tests/cli/test_mcp_server_config_integration.py
@@ -3,6 +3,7 @@
import json
import sys
from pathlib import Path
+from unittest.mock import AsyncMock, patch
import pytest
@@ -291,3 +292,20 @@ class TestConfigValidation:
)
assert config.deployment is not None
assert config.deployment.log_level == level
+
+
+class TestRunServer:
+ async def test_run_server_forwards_port_zero(self, tmp_path):
+ server_file = tmp_path / "server.py"
+ server_file.write_text("from fastmcp import FastMCP\nmcp = FastMCP('test')\n")
+ config = MCPServerConfig(
+ source={"path": str(server_file)},
+ deployment={"transport": "http", "port": 0},
+ )
+
+ with patch(
+ "fastmcp.server.server.FastMCP.run_async", new_callable=AsyncMock
+ ) as run_async:
+ await config.run_server()
+
+ run_async.assert_awaited_once_with(transport="http", port=0)
diff --git a/tests/utilities/test_cli.py b/tests/utilities/test_cli.py
index f463c2913..43a840323 100644
--- a/tests/utilities/test_cli.py
+++ b/tests/utilities/test_cli.py
@@ -2,6 +2,7 @@
from pathlib import Path
+from fastmcp.utilities.cli import load_and_merge_config
from fastmcp.utilities.mcp_server_config.v1.environments.uv import UVEnvironment
@@ -193,3 +194,13 @@ class TestEnvironmentNeedsUV:
"""Test that needs_uv returns False with empty lists."""
env = UVEnvironment(dependencies=None, editable=None)
assert env._must_run_with_uv() is False
+
+
+class TestLoadAndMergeConfig:
+ def test_port_zero_override_is_kept(self, tmp_path: Path):
+ server_file = tmp_path / "server.py"
+ server_file.write_text("from fastmcp import FastMCP\nmcp = FastMCP('test')\n")
+
+ config, _ = load_and_merge_config(str(server_file), port=0)
+
+ assert config.deployment.port == 0