Skip to content

Commit 1177a11

Browse files
committed
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]`
1 parent 99c402d commit 1177a11

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/mcp/client/sse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def sse_reader(
8484

8585
case "message":
8686
try:
87-
message = types.JSONRPCMessage.model_validate_json( # noqa: E501
87+
message = types.JSONRPCMessage.model_validate_json( # noqa: E501
8888
sse.data
8989
)
9090
logger.debug(

src/mcp/server/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
logger = logging.getLogger(__name__)
1818

19-
request_ctx: contextvars.ContextVar[RequestContext] = contextvars.ContextVar(
20-
"request_ctx"
19+
request_ctx: contextvars.ContextVar[RequestContext[ServerSession]] = (
20+
contextvars.ContextVar("request_ctx")
2121
)
2222

2323

@@ -115,7 +115,7 @@ def get_capabilities(
115115
)
116116

117117
@property
118-
def request_context(self) -> RequestContext:
118+
def request_context(self) -> RequestContext[ServerSession]:
119119
"""If called outside of a request context, this will raise a LookupError."""
120120
return request_ctx.get()
121121

0 commit comments

Comments
 (0)