Return a specific server session instance of request context

We currently return a generic instance of RequestContext without
a specialization on the Session type. This makes it impossible
for servers to typesafe call `list_roots()` and other methods.

We now return a specific instance of `RequestContext[ServerSession]`
This commit is contained in:
David Soria Parra
2024-11-11 21:04:44 +00:00
parent 99c402d575
commit 1177a11910
2 changed files with 4 additions and 4 deletions

View File

@@ -16,8 +16,8 @@ from mcp.shared.session import RequestResponder
logger = logging.getLogger(__name__)
request_ctx: contextvars.ContextVar[RequestContext] = contextvars.ContextVar(
"request_ctx"
request_ctx: contextvars.ContextVar[RequestContext[ServerSession]] = (
contextvars.ContextVar("request_ctx")
)
@@ -115,7 +115,7 @@ class Server:
)
@property
def request_context(self) -> RequestContext:
def request_context(self) -> RequestContext[ServerSession]:
"""If called outside of a request context, this will raise a LookupError."""
return request_ctx.get()