datawhalechina/hello-agents · error · ImportError

创建内置 MCP 服务器需要 fastmcp 库。请安装: pip install fastmcp

Error message

创建内置 MCP 服务器需要 fastmcp 库。请安装: pip install fastmcp

What it means

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.

Source

Thrown at Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/protocol_tools.py:240

                """友好问候"""
                return f"Hello, {name}! 欢迎使用 HelloAgents MCP 工具!"

            @server.tool()
            def get_system_info() -> dict:
                """获取系统信息"""
                import platform
                import sys
                return {
                    "platform": platform.system(),
                    "python_version": sys.version,
                    "server_name": "HelloAgents-BuiltinServer",
                    "tools_count": 6
                }

            return server

        except ImportError:
            raise ImportError(
                "创建内置 MCP 服务器需要 fastmcp 库。请安装: pip install fastmcp"
            )

    def _discover_tools(self):
        """发现MCP服务器提供的所有工具"""
        try:
            from hello_agents.protocols.mcp.client import MCPClient
            import asyncio

            async def discover():
                client_source = self.server if self.server else self.server_command
                async with MCPClient(client_source, self.server_args, env=self.env) as client:
                    tools = await client.list_tools()
                    return tools

            # 运行异步发现
            try:
                loop = asyncio.get_running_loop()

View on GitHub (pinned to 606a07d341)

Solutions

  1. Install it into the active interpreter: pip install fastmcp (or add it to requirements/extras).
  2. Verify with 'python -c "import fastmcp"' using the same interpreter that runs the app.
  3. If optional, gate the feature: check importability before constructing the builtin server and disable those tools.

Example fix

# before
from tools.builtin.protocol_tools import BuiltinMCPServer
server = BuiltinMCPServer()  # ImportError

# after (shell)
pip install fastmcp
python -c "import fastmcp; print(fastmcp.__version__)"  # sanity check
Defensive patterns

Strategy: fallback

Validate before calling

try:
    import fastmcp  # noqa
    FASTMCP_OK = True
except ImportError:
    FASTMCP_OK = False

if not FASTMCP_OK:
    print('fastmcp missing; builtin MCP tools disabled. pip install fastmcp')

Type guard

def fastmcp_available() -> bool:
    try:
        import fastmcp  # noqa: F401
        return True
    except ImportError:
        return False

Try / catch

try:
    tools = BuiltinMCPServer()
except ImportError as e:
    if 'fastmcp' in str(e):
        tools = None  # run without builtin MCP tools; warn the user
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/a4d11eb186716109. Report an issue: GitHub.