Skip to content

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

Stop preloading the server before launching the Inspector (#6241)

nate nowack66bf8296d844
diff --git a/fastmcp_slim/fastmcp/cli/cli.py b/fastmcp_slim/fastmcp/cli/cli.py
index 7cd8e6633..600a6f366 100644
--- a/fastmcp_slim/fastmcp/cli/cli.py
+++ b/fastmcp_slim/fastmcp/cli/cli.py
@@ -260,10 +260,9 @@ async def inspector(
sys.exit(1)
assert config is not None # For type checker
- # Skip server-object validation in module mode β€” the module
- # manages its own startup and may not expose an importable server.
- if not module:
- await config.source.load_server()
+ # Let the child server load the source. Importing it here would happen
+ # outside the configured uv environment and would invoke factories a
+ # second time when `fastmcp run` starts.
env_vars = {}
if ui_port:
diff --git a/tests/cli/test_run.py b/tests/cli/test_run.py
index 487f2e750..342c7ecee 100644
--- a/tests/cli/test_run.py
+++ b/tests/cli/test_run.py
@@ -1073,6 +1073,40 @@ class TestInspectorModuleMode:
assert "--transport" not in cmd
+class TestInspectorServerLoading:
+ """The inspector leaves loading the server to the configured environment."""
+
+ async def test_server_is_not_imported_in_the_invoking_environment(
+ self, tmp_path: Path
+ ):
+ """A dependency supplied only via --with must not be imported up front."""
+ server_file = tmp_path / "server.py"
+ server_file.write_text(
+ "import fastmcp_missing_dependency_for_test\n"
+ "from fastmcp import FastMCP\n\n"
+ "mcp = FastMCP('test')\n"
+ )
+ mock_process = MagicMock()
+ mock_process.returncode = 0
+
+ with (
+ patch("fastmcp.cli.cli._get_npx_command", return_value="npx"),
+ patch(
+ "fastmcp.cli.cli.subprocess.run", return_value=mock_process
+ ) as mock_subprocess,
+ pytest.raises(SystemExit) as exc_info,
+ ):
+ await inspector(
+ str(server_file),
+ with_packages=["fastmcp_missing_dependency_for_test"],
+ )
+
+ assert exc_info.value.code == 0
+ cmd = mock_subprocess.call_args.args[0]
+ assert cmd[:2] == ["npx", "@modelcontextprotocol/inspector"]
+ assert "fastmcp_missing_dependency_for_test" in cmd
+
+
class TestInspectorHttpDeployment:
"""Test the inspector command with an HTTP deployment in fastmcp.json."""