Skip to content

Commit 4f2942e

Browse files
author
Jay Hemnani
committed
fix: add explicit type annotation for call_tool decorator
Replace `Callable[..., Awaitable[...]]` with a concrete `CallToolHandler` type alias that specifies the exact expected function signature `(str, dict[str, Any]) -> Awaitable[...]`. This enables static type checkers (mypy/pyright) to validate decorated functions without requiring `# type: ignore` comments, making the `py.typed` marker actually useful. Fixes #1822 Github-Issue: #1822
1 parent c7cbfbb commit 4f2942e

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ async def main():
105105
UnstructuredContent: TypeAlias = Iterable[types.ContentBlock]
106106
CombinationContent: TypeAlias = tuple[UnstructuredContent, StructuredContent]
107107

108+
# type alias for call_tool handler function signature
109+
CallToolHandler: TypeAlias = Callable[
110+
[str, dict[str, Any]],
111+
Awaitable[
112+
UnstructuredContent | StructuredContent | CombinationContent | types.CallToolResult | types.CreateTaskResult
113+
],
114+
]
115+
108116
# This will be properly typed in each Server instance's context
109117
request_ctx: contextvars.ContextVar[RequestContext[ServerSession, Any, Any]] = contextvars.ContextVar("request_ctx")
110118

@@ -497,18 +505,7 @@ def call_tool(self, *, validate_input: bool = True):
497505
If outputSchema is defined, validates structuredContent or errors if missing.
498506
"""
499507

500-
def decorator(
501-
func: Callable[
502-
...,
503-
Awaitable[
504-
UnstructuredContent
505-
| StructuredContent
506-
| CombinationContent
507-
| types.CallToolResult
508-
| types.CreateTaskResult
509-
],
510-
],
511-
):
508+
def decorator(func: CallToolHandler) -> CallToolHandler:
512509
logger.debug("Registering handler for CallToolRequest")
513510

514511
async def handler(req: types.CallToolRequest):

0 commit comments

Comments
 (0)