99 asynccontextmanager ,
1010)
1111from itertools import chain
12- from typing import Any , Callable , Generic , Literal , Sequence
12+ from typing import Any , Callable , Generic , Literal , Sequence , TypeVar
1313
1414import anyio
1515import pydantic_core
@@ -564,7 +564,10 @@ def _convert_to_content(
564564 return [TextContent (type = "text" , text = result )]
565565
566566
567- class Context (BaseModel , Generic [LifespanContextT ]):
567+ ServerSessionT = TypeVar ("ServerSessionT" , bound = ServerSession )
568+
569+
570+ class Context (BaseModel , Generic [ServerSessionT , LifespanContextT ]):
568571 """Context object providing access to MCP capabilities.
569572
570573 This provides a cleaner interface to MCP's RequestContext functionality.
@@ -598,13 +601,13 @@ def my_tool(x: int, ctx: Context) -> str:
598601 The context is optional - tools that don't need it can omit the parameter.
599602 """
600603
601- _request_context : RequestContext [ServerSession , LifespanContextT ] | None
604+ _request_context : RequestContext [ServerSessionT , LifespanContextT ] | None
602605 _fastmcp : FastMCP | None
603606
604607 def __init__ (
605608 self ,
606609 * ,
607- request_context : RequestContext [ServerSession , LifespanContextT ] | None = None ,
610+ request_context : RequestContext [ServerSessionT , LifespanContextT ] | None = None ,
608611 fastmcp : FastMCP | None = None ,
609612 ** kwargs : Any ,
610613 ):
@@ -620,7 +623,7 @@ def fastmcp(self) -> FastMCP:
620623 return self ._fastmcp
621624
622625 @property
623- def request_context (self ) -> RequestContext [ServerSession , LifespanContextT ]:
626+ def request_context (self ) -> RequestContext [ServerSessionT , LifespanContextT ]:
624627 """Access to the underlying request context."""
625628 if self ._request_context is None :
626629 raise ValueError ("Context is not available outside of a request" )
0 commit comments