Add ServerSessionT type var to Context (#271)
* Add ServerSessionT type var to Context * Passing locally * Try now
This commit is contained in:
parent
78fc5c12c0
commit
e756315dea
2 changed files with 44 additions and 38 deletions
|
|
@ -1,3 +1,5 @@
|
|||
from __future__ import annotations as _annotations
|
||||
|
||||
import inspect
|
||||
from typing import TYPE_CHECKING, Any, Callable
|
||||
|
||||
|
|
@ -9,6 +11,8 @@ from mcp.server.fastmcp.utilities.func_metadata import FuncMetadata, func_metada
|
|||
|
||||
if TYPE_CHECKING:
|
||||
from mcp.server.fastmcp.server import Context
|
||||
from mcp.server.session import ServerSessionT
|
||||
from mcp.shared.context import LifespanContextT
|
||||
|
||||
|
||||
class Tool(BaseModel):
|
||||
|
|
@ -68,7 +72,11 @@ class Tool(BaseModel):
|
|||
context_kwarg=context_kwarg,
|
||||
)
|
||||
|
||||
async def run(self, arguments: dict, context: "Context | None" = None) -> Any:
|
||||
async def run(
|
||||
self,
|
||||
arguments: dict[str, Any],
|
||||
context: Context[ServerSessionT, LifespanContextT] | None = None,
|
||||
) -> Any:
|
||||
"""Run the tool with arguments."""
|
||||
try:
|
||||
return await self.fn_metadata.call_fn_with_arg_validation(
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
from __future__ import annotations as _annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from mcp.server.fastmcp.exceptions import ToolError
|
||||
from mcp.server.fastmcp.tools.base import Tool
|
||||
from mcp.server.fastmcp.utilities.logging import get_logger
|
||||
from mcp.shared.context import LifespanContextT
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from mcp.server.fastmcp.server import Context
|
||||
from mcp.server.session import ServerSessionT
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
|
@ -43,7 +47,10 @@ class ToolManager:
|
|||
return tool
|
||||
|
||||
async def call_tool(
|
||||
self, name: str, arguments: dict, context: "Context | None" = None
|
||||
self,
|
||||
name: str,
|
||||
arguments: dict[str, Any],
|
||||
context: Context[ServerSessionT, LifespanContextT] | None = None,
|
||||
) -> Any:
|
||||
"""Call a tool by name with arguments."""
|
||||
tool = self.get_tool(name)
|
||||
|
|
|
|||
Loading…
Reference in a new issue