{"record":{"id":"b9a9f98d52940650","repo":"PrefectHQ/fastmcp","slug":"cannot-mount-a-server-onto-itself","errorCode":null,"errorMessage":"Cannot mount a server onto itself","messagePattern":"Cannot mount a server onto itself","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/server.py","lineNumber":2352,"sourceCode":"        When a server is mounted without a namespace (namespace=None), its tools, resources, templates,\n        and prompts are accessible with their original names. Multiple servers can be mounted\n        without namespaces, and they will be tried in order until a match is found.\n\n        The mounted server's lifespan is executed when the parent server starts, and its\n        middleware chain is invoked for all operations (tool calls, resource reads, prompts).\n\n        Args:\n            server: The FastMCP server to mount.\n            namespace: Optional namespace to use for the mounted server's objects. If None,\n                the server's objects are accessible with their original names.\n            tool_names: Optional mapping of original tool names to custom names. Use this\n                to override namespaced names. Keys are the original tool names from the\n                mounted server.\n        \"\"\"\n        from fastmcp.server.providers.fastmcp_provider import FastMCPProvider\n\n        if server is self:\n            raise ValueError(\"Cannot mount a server onto itself\")\n\n        # Warn if parent masks errors but child doesn't (or vice versa)\n        if self._mask_error_details and not server._mask_error_details:\n            logger.warning(\n                f\"Parent server {self.name!r} has mask_error_details=True but \"\n                f\"mounted server {server.name!r} does not. Error details from \"\n                f\"{server.name!r} may leak through to clients. Set \"\n                f\"mask_error_details=True on the child server to prevent this.\"\n            )\n\n        # Create provider and add it with namespace\n        provider: Provider = FastMCPProvider(server)\n\n        # Apply tool renames first (scoped to this provider), then namespace\n        # So foo → bar with namespace=\"baz\" becomes baz_bar\n        if tool_names:\n            transforms = {\n                old_name: ToolTransformConfig(name=new_name)","sourceCodeStart":2334,"sourceCodeEnd":2370,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/server.py#L2334-L2370","documentation":"FastMCP.mount() rejects mounting a server onto itself (server is self), which would create infinite recursion when resolving tools/resources/prompts. Guard clause raising ValueError.","triggerScenarios":"Calling mcp.mount(mcp) — passing the same FastMCP instance as both parent and child, often via a variable that accidentally aliases self.","commonSituations":"Factory functions returning the parent by mistake; refactoring where a variable meant to hold a subserver still references the parent.","solutions":["Pass a distinct FastMCP instance to mount()","Check that the variable passed to mount is actually the subserver, not the parent","Build the child server in its own factory/constructor"],"exampleFix":"// before\nmcp.mount(mcp)  # self-mount\n// after\nsub = FastMCP(\"subserver\")\nmcp.mount(sub)","handlingStrategy":"validation","validationCode":"if sub_server is mcp:\n    raise ValueError(\"refusing to mount server onto itself\")","typeGuard":null,"tryCatchPattern":"try:\n    mcp.mount(child)\nexcept ValueError as e:\n    logger.error(\"mount failed: %s\", e)","preventionTips":["Construct child servers in dedicated factories","Assert child is not parent before mount","Avoid reusing variable names that may alias the parent"],"tags":["python","server","mount","recursion"],"backgroundTag":"self-referential-mount","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}