{"record":{"id":"a4d11eb186716109","repo":"datawhalechina/hello-agents","slug":"mcp-fastmcp-pip-install-fastmcp-a4d11e","errorCode":null,"errorMessage":"创建内置 MCP 服务器需要 fastmcp 库。请安装: pip install fastmcp","messagePattern":"创建内置 MCP 服务器需要 fastmcp 库。请安装: pip install fastmcp","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/protocol_tools.py","lineNumber":240,"sourceCode":"                \"\"\"友好问候\"\"\"\n                return f\"Hello, {name}! 欢迎使用 HelloAgents MCP 工具！\"\n\n            @server.tool()\n            def get_system_info() -> dict:\n                \"\"\"获取系统信息\"\"\"\n                import platform\n                import sys\n                return {\n                    \"platform\": platform.system(),\n                    \"python_version\": sys.version,\n                    \"server_name\": \"HelloAgents-BuiltinServer\",\n                    \"tools_count\": 6\n                }\n\n            return server\n\n        except ImportError:\n            raise ImportError(\n                \"创建内置 MCP 服务器需要 fastmcp 库。请安装: pip install fastmcp\"\n            )\n\n    def _discover_tools(self):\n        \"\"\"发现MCP服务器提供的所有工具\"\"\"\n        try:\n            from hello_agents.protocols.mcp.client import MCPClient\n            import asyncio\n\n            async def discover():\n                client_source = self.server if self.server else self.server_command\n                async with MCPClient(client_source, self.server_args, env=self.env) as client:\n                    tools = await client.list_tools()\n                    return tools\n\n            # 运行异步发现\n            try:\n                loop = asyncio.get_running_loop()","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/protocol_tools.py#L222-L258","documentation":"ImportError raised when ProtocolTools tries to build its built-in MCP server but 'from fastmcp import FastMCP' fails — the optional dependency is not installed in the current environment. The message tells the user to pip install fastmcp; without it, the builtin server (calculator, greet, system-info tools) cannot be constructed.","triggerScenarios":"Instantiating the builtin MCP tool class in an environment where fastmcp is absent (not in requirements, different venv, slim Docker image); a transitive dependency downgrade removing it; Python version incompatible with the installed fastmcp wheel.","commonSituations":"Cloning the repo and running without 'pip install -e .[mcp]' or the extras; CI images trimmed of optional deps; mixing system Python and project venv so the install lands in the wrong interpreter.","solutions":["Install it into the active interpreter: pip install fastmcp (or add it to requirements/extras).","Verify with 'python -c \"import fastmcp\"' using the same interpreter that runs the app.","If optional, gate the feature: check importability before constructing the builtin server and disable those tools."],"exampleFix":"# before\nfrom tools.builtin.protocol_tools import BuiltinMCPServer\nserver = BuiltinMCPServer()  # ImportError\n\n# after (shell)\npip install fastmcp\npython -c \"import fastmcp; print(fastmcp.__version__)\"  # sanity check","handlingStrategy":"fallback","validationCode":"try:\n    import fastmcp  # noqa\n    FASTMCP_OK = True\nexcept ImportError:\n    FASTMCP_OK = False\n\nif not FASTMCP_OK:\n    print('fastmcp missing; builtin MCP tools disabled. pip install fastmcp')","typeGuard":"def fastmcp_available() -> bool:\n    try:\n        import fastmcp  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    tools = BuiltinMCPServer()\nexcept ImportError as e:\n    if 'fastmcp' in str(e):\n        tools = None  # run without builtin MCP tools; warn the user\n    else:\n        raise","preventionTips":["Pin fastmcp in requirements/extras for any MCP feature.","Probe optional imports at startup and disable features cleanly.","Install with the same interpreter/venv that runs the app."],"tags":["mcp","dependency","import-error","environment"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}