Skip to content

Commit ae3338f

Browse files
authored
[v1.x] Complete the FastMCP Settings model at import time (#3352)
1 parent 98b7159 commit ae3338f

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

src/mcp/server/fastmcp/server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,11 @@ async def get_prompt(self, name: str, arguments: dict[str, Any] | None = None) -
10871087
raise ValueError(str(e))
10881088

10891089

1090+
# `Settings.lifespan` refers to FastMCP, which is only defined above; complete the model now so
1091+
# settings sources never see an unresolved annotation when a FastMCP instance is created.
1092+
Settings.model_rebuild()
1093+
1094+
10901095
class StreamableHTTPASGIApp:
10911096
"""
10921097
ASGI application for Streamable HTTP server transport.

tests/server/fastmcp/test_server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from mcp.server.fastmcp import Context, FastMCP
1111
from mcp.server.fastmcp.prompts.base import Message, UserMessage
1212
from mcp.server.fastmcp.resources import FileResource, FunctionResource
13+
from mcp.server.fastmcp.server import Settings
1314
from mcp.server.fastmcp.utilities.types import Audio, Image
1415
from mcp.server.session import ServerSession
1516
from mcp.server.transport_security import TransportSecuritySettings
@@ -31,6 +32,11 @@
3132
from mcp.server.fastmcp import Context
3233

3334

35+
def test_settings_model_is_complete_at_import():
36+
"""The Settings model resolves its FastMCP annotation at import, so building one needs no deferred rebuild."""
37+
assert Settings.__pydantic_complete__
38+
39+
3440
class TestServer:
3541
@pytest.mark.anyio
3642
async def test_create_server(self):

0 commit comments

Comments
 (0)