[v1.x] Complete the FastMCP Settings model at import time (#3352)

This commit is contained in:
Max 2026-08-21 12:02:46 +01:00 • committed by GitHub
commit ae3338f6b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View file

@ -1087,6 +1087,11 @@ class FastMCP(Generic[LifespanResultT]):
raise ValueError(str(e))
# `Settings.lifespan` refers to FastMCP, which is only defined above; complete the model now so
# settings sources never see an unresolved annotation when a FastMCP instance is created.
Settings.model_rebuild()
class StreamableHTTPASGIApp:
"""
ASGI application for Streamable HTTP server transport.

View file

@ -10,6 +10,7 @@ from starlette.routing import Mount, Route
from mcp.server.fastmcp import Context, FastMCP
from mcp.server.fastmcp.prompts.base import Message, UserMessage
from mcp.server.fastmcp.resources import FileResource, FunctionResource
from mcp.server.fastmcp.server import Settings
from mcp.server.fastmcp.utilities.types import Audio, Image
from mcp.server.session import ServerSession
from mcp.server.transport_security import TransportSecuritySettings
@ -31,6 +32,11 @@ if TYPE_CHECKING:
from mcp.server.fastmcp import Context
def test_settings_model_is_complete_at_import():
"""The Settings model resolves its FastMCP annotation at import, so building one needs no deferred rebuild."""
assert Settings.__pydantic_complete__
class TestServer:
@pytest.mark.anyio
async def test_create_server(self):