Skip to content

Commit 8b191a4

Browse files
authored
[v2.0.x] Point imports of mcp.server.fastmcp at the migration guide (#3393)
1 parent 6f69a37 commit 8b191a4

5 files changed

Lines changed: 82 additions & 8 deletions

File tree

.github/workflows/publish-pypi.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # v8.2.0
2222
with:
2323
enable-cache: false
24-
version: 0.9.5
24+
version: 0.12.5
2525

2626
- name: Set up Python 3.12
2727
run: uv python install 3.12
@@ -57,7 +57,7 @@ jobs:
5757
path: dist/
5858

5959
- name: Publish package distributions to PyPI
60-
uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # release/v1
60+
uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2
6161
with:
6262
# Lets a re-run after a partially failed upload publish the remaining
6363
# files instead of erroring on the ones already on PyPI.

docs/migration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Every section heading below names the API it affects, so searching this page for
1212

1313
| Change | First symptom | Section |
1414
|---|---|---|
15-
| `FastMCP` renamed to `MCPServer` | `ModuleNotFoundError: No module named 'mcp.server.fastmcp'` | [`FastMCP` renamed](#fastmcp-renamed-to-mcpserver) |
15+
| `FastMCP` renamed to `MCPServer` | `ModuleNotFoundError: No module named 'mcp.server.fastmcp'` (newer 2.x releases follow it with a pointer to this guide) | [`FastMCP` renamed](#fastmcp-renamed-to-mcpserver) |
1616
| Fields renamed from camelCase to snake_case | `AttributeError: 'Tool' object has no attribute 'inputSchema'` | [snake_case fields](#field-names-changed-from-camelcase-to-snake_case) |
1717
| `mcp.types` names removed | `ImportError: cannot import name 'Content' from 'mcp.types'` | [Removed types](#removed-type-aliases-and-classes) |
1818
| `McpError` renamed to `MCPError` | `ImportError: cannot import name 'McpError' from 'mcp'` | [`McpError` renamed](#mcperror-renamed-to-mcperror) |
@@ -667,6 +667,8 @@ All submodules under `mcp.server.fastmcp.*` are now under `mcp.server.mcpserver.
667667
- `ToolError`, `ResourceError` — from `mcp.server.mcpserver.exceptions`
668668
- `MCPServerError` (renamed from `FastMCPError`) — from `mcp.server.mcpserver.exceptions`
669669

670+
Importing `mcp.server.fastmcp`, or anything below it, raises `ModuleNotFoundError` (newer 2.x releases include a link to this section in its message), so existing `except ImportError` or `except ModuleNotFoundError` fallbacks around the v1 import keep working.
671+
670672
### What is unchanged on `MCPServer`
671673

672674
Beyond the changes covered in this section, the everyday `FastMCP` surface carries over to `MCPServer` as-is:

scripts/docs/gen_ref_pages.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
# it from `src/` would emit the unimportable `mcp-types.mcp_types.*`.
3131
PACKAGES = (ROOT / "src" / "mcp", ROOT / "src" / "mcp-types" / "mcp_types")
3232

33-
# Alias packages that mirror another package's namespaces (`mcp.types` mirrors
34-
# `mcp_types`, `mcp.types.version` mirrors `mcp_types.version`): the mirrored
35-
# package's pages are the canonical rendering, so an alias, and every module
36-
# under it, earns no page of its own.
37-
EXCLUDED = frozenset({"mcp.types"})
33+
# Module paths that get no page, and neither does anything under them: alias
34+
# packages that mirror another package's namespaces (`mcp.types` mirrors
35+
# `mcp_types`), whose canonical rendering is the mirrored package's pages; and
36+
# removed v1 import paths (`mcp.server.fastmcp`) that only raise a pointer to
37+
# the migration guide and carry no API.
38+
EXCLUDED = frozenset({"mcp.types", "mcp.server.fastmcp"})
3839

3940
_KIND_SECTIONS = {
4041
griffe.Kind.MODULE: "Modules",

src/mcp/server/fastmcp.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Removed in mcp 2: `FastMCP` is now `mcp.server.mcpserver.MCPServer`.
2+
3+
This module has no API. Importing it, or anything below it, raises
4+
`ModuleNotFoundError` with a message that points at the migration guide. It
5+
exists only because the bare "No module named 'mcp.server.fastmcp'" gave v1
6+
code no hint that the installed SDK is a different major version.
7+
"""
8+
9+
_MESSAGE = (
10+
"No module named 'mcp.server.fastmcp'. This is mcp 2.x, where FastMCP was renamed to MCPServer "
11+
"(from mcp.server.mcpserver import MCPServer) and other APIs changed; see the migration guide at "
12+
"https://py.sdk.modelcontextprotocol.io/v2/migration/#fastmcp-renamed-to-mcpserver "
13+
"or pin 'mcp<2' to keep running v1 code."
14+
)
15+
16+
raise ModuleNotFoundError(_MESSAGE, name=__name__)

tests/server/test_fastmcp.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""The removed v1 import path `mcp.server.fastmcp` fails with a pointer to the migration guide."""
2+
3+
import importlib
4+
import sys
5+
6+
import pytest
7+
from inline_snapshot import snapshot
8+
9+
import mcp.server
10+
from mcp.server.mcpserver import MCPServer
11+
12+
13+
def test_importing_fastmcp_raises_module_not_found_that_points_at_the_migration_guide() -> None:
14+
"""SDK-defined: the v1 path fails with the same exception type and `.name` as a module
15+
that genuinely does not exist, but the message names the replacement and the guide."""
16+
with pytest.raises(ModuleNotFoundError) as exc_info:
17+
importlib.import_module("mcp.server.fastmcp")
18+
19+
assert exc_info.value.name == "mcp.server.fastmcp"
20+
assert str(exc_info.value) == snapshot(
21+
"No module named 'mcp.server.fastmcp'. This is mcp 2.x, where FastMCP was renamed to MCPServer "
22+
"(from mcp.server.mcpserver import MCPServer) and other APIs changed; see the migration guide at "
23+
"https://py.sdk.modelcontextprotocol.io/v2/migration/#fastmcp-renamed-to-mcpserver "
24+
"or pin 'mcp<2' to keep running v1 code."
25+
)
26+
# A module that raises while executing is never cached, so nothing is left behind.
27+
assert "mcp.server.fastmcp" not in sys.modules
28+
assert not hasattr(mcp.server, "fastmcp")
29+
30+
31+
def test_importing_a_fastmcp_submodule_raises_the_parent_pointer() -> None:
32+
"""SDK-defined: a deep v1 path executes `mcp.server.fastmcp` first, so it fails with that
33+
module's message and `.name` rather than a bare error for the leaf."""
34+
with pytest.raises(ModuleNotFoundError) as parent:
35+
importlib.import_module("mcp.server.fastmcp")
36+
with pytest.raises(ModuleNotFoundError) as exc_info:
37+
importlib.import_module("mcp.server.fastmcp.utilities.types")
38+
39+
assert exc_info.value.name == "mcp.server.fastmcp"
40+
assert str(exc_info.value) == str(parent.value)
41+
42+
43+
def test_v1_first_import_shim_falls_back_to_mcpserver() -> None:
44+
"""SDK-defined: projects that support both majors try the v1 import and fall back on
45+
`ModuleNotFoundError` (the narrowest guard seen in the wild), which is why the pointer is
46+
raised as exactly that type and not as a bare `ImportError` or after a warning."""
47+
fell_back = False
48+
try:
49+
server_class: type = importlib.import_module("mcp.server.fastmcp").FastMCP
50+
except ModuleNotFoundError:
51+
fell_back = True
52+
server_class = MCPServer
53+
54+
assert fell_back
55+
assert server_class is MCPServer

0 commit comments

Comments
 (0)