PrefectHQ/fastmcp · error · AttributeError

module {__name__!r} has no attribute {name!r}

Error message

module {__name__!r} has no attribute {name!r}

What it means

The module-level __getattr__ of fastmcp.server falls through to `raise AttributeError(f"module {__name__!r} has no attribute {name!r}")` for any attribute name not explicitly handled (context/dependencies/Context/FastMCP/create_proxy). This is standard PEP 562 lazy-module behavior: unknown attributes raise the usual AttributeError.

Source

Thrown at fastmcp_slim/fastmcp/server/__init__.py:29

def __getattr__(name: str) -> object:
    if name in {"context", "dependencies"}:
        return importlib.import_module(f"fastmcp.server.{name}")
    if name == "Context":
        try:
            from .context import Context
        except ImportError as exc:
            raise ImportError(_install_hints.SERVER_SUPPORT) from exc

        return Context
    if name in {"FastMCP", "create_proxy"}:
        try:
            from .server import FastMCP, create_proxy
        except ImportError as exc:
            raise ImportError(_install_hints.SERVER_SUPPORT) from exc

        return FastMCP if name == "FastMCP" else create_proxy
    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


__all__ = ["Context", "FastMCP", "create_proxy"]

View on GitHub (pinned to 1f02114297)

Solutions

  1. Fix the attribute name; check `fastmcp.server.__all__` (Context, FastMCP, create_proxy) or use dir(fastmcp.server).
  2. Import the symbol from its real submodule (e.g. fastmcp.server.middleware for middleware) instead of the package root.
  3. If the name should exist, confirm server support is installed — but note missing server deps raise ImportError (214/215), not this AttributeError.
  4. For dynamic lookups, validate the configured name against __all__ before getattr().

Example fix

// before
from fastmcp.server import Server  # wrong name

// after
from fastmcp import FastMCP  # or from fastmcp.server import FastMCP
Defensive patterns

Strategy: validation

Validate before calling

import fastmcp.server
name = 'FastMCP'
if name not in fastmcp.server.__all__:
    raise AttributeError(f'{name} not exported by fastmcp.server; available: {fastmcp.server.__all__}')

Try / catch

try:
    attr = getattr(fastmcp.server, name)
except AttributeError as e:
    logger.error('bad import name: %s', e)

Prevention

When it happens

Trigger: Access any non-existent attribute on fastmcp.server, e.g. `from fastmcp.server import Server`, `fastmcp.server.Someting`, or a typo like `fastmcp.server.FastMCP2` — the fallthrough at fastmcp_slim/fastmcp/server/__init__.py:29 raises AttributeError.

Common situations: Typos in imports; migrating code that used different symbol names across FastMCP versions; IDE auto-import picking the wrong module; dynamically getattr()-ing names from config strings that don't match __all__.

Related errors


AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29). Data as JSON: /api/errors/2099d9c035a7c493. Report an issue: GitHub.