π The fast, Pythonic way to build MCP servers and clients.
fix: reject self-mount to prevent infinite recursion (#3925)
Bill Easton757678bc7e4d
diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.pyindex 967ef1ceb..1950d7a3b 100644--- a/src/fastmcp/server/server.py+++ b/src/fastmcp/server/server.py@@ -2047,6 +2047,9 @@ class FastMCP(from fastmcp.server.providers.fastmcp_provider import FastMCPProvider+ if server is self:+ raise ValueError("Cannot mount a server onto itself")+# Handle deprecated prefix parameterif prefix is not None:warnings.warn(diff --git a/tests/server/test_server_safety.py b/tests/server/test_server_safety.pynew file mode 100644index 000000000..2d85b098a--- /dev/null+++ b/tests/server/test_server_safety.py@@ -0,0 +1,10 @@+import pytest++from fastmcp import FastMCP+++class TestMountSafety:+ def test_self_mount_raises(self):+ mcp = FastMCP("test")+ with pytest.raises(ValueError, match="Cannot mount a server onto itself"):+ mcp.mount(mcp)