feat: add lifespan support to low-level MCP server

Adds a context manager based lifespan API in mcp.server.lowlevel.server to manage server lifecycles in a
type-safe way. This enables servers to:
- Initialize resources on startup and clean them up on shutdown
- Pass context data from startup to request handlers
- Support async startup/shutdown operations
This commit is contained in:
David Soria Parra
2025-02-11 12:14:58 +00:00
parent f10665db4c
commit 2c7bd8343e
2 changed files with 59 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Generic, TypeVar
from typing import Any, Generic, TypeVar
from mcp.shared.session import BaseSession
from mcp.types import RequestId, RequestParams
@@ -12,3 +12,4 @@ class RequestContext(Generic[SessionT]):
request_id: RequestId
meta: RequestParams.Meta | None
session: SessionT
lifespan_context: Any