9
9
asynccontextmanager ,
10
10
)
11
11
from itertools import chain
12
- from typing import Any , Callable , Generic , Literal , Sequence
12
+ from typing import Any , Callable , Generic , Literal , Sequence , TypeVar
13
13
14
14
import anyio
15
15
import pydantic_core
@@ -564,7 +564,10 @@ def _convert_to_content(
564
564
return [TextContent (type = "text" , text = result )]
565
565
566
566
567
- class Context (BaseModel , Generic [LifespanContextT ]):
567
+ ServerSessionT = TypeVar ("ServerSessionT" , bound = ServerSession )
568
+
569
+
570
+ class Context (BaseModel , Generic [ServerSessionT , LifespanContextT ]):
568
571
"""Context object providing access to MCP capabilities.
569
572
570
573
This provides a cleaner interface to MCP's RequestContext functionality.
@@ -598,13 +601,13 @@ def my_tool(x: int, ctx: Context) -> str:
598
601
The context is optional - tools that don't need it can omit the parameter.
599
602
"""
600
603
601
- _request_context : RequestContext [ServerSession , LifespanContextT ] | None
604
+ _request_context : RequestContext [ServerSessionT , LifespanContextT ] | None
602
605
_fastmcp : FastMCP | None
603
606
604
607
def __init__ (
605
608
self ,
606
609
* ,
607
- request_context : RequestContext [ServerSession , LifespanContextT ] | None = None ,
610
+ request_context : RequestContext [ServerSessionT , LifespanContextT ] | None = None ,
608
611
fastmcp : FastMCP | None = None ,
609
612
** kwargs : Any ,
610
613
):
@@ -620,7 +623,7 @@ def fastmcp(self) -> FastMCP:
620
623
return self ._fastmcp
621
624
622
625
@property
623
- def request_context (self ) -> RequestContext [ServerSession , LifespanContextT ]:
626
+ def request_context (self ) -> RequestContext [ServerSessionT , LifespanContextT ]:
624
627
"""Access to the underlying request context."""
625
628
if self ._request_context is None :
626
629
raise ValueError ("Context is not available outside of a request" )
0 commit comments