From 861d5e7b36ec6a3778716a63e7acc3d8309efdde Mon Sep 17 00:00:00 2001 From: Huang Jia Date: Thu, 26 Jun 2025 11:26:09 +0800 Subject: [PATCH 1/5] fix: add ReceiveResultT into Base Session --- src/mcp/shared/session.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mcp/shared/session.py b/src/mcp/shared/session.py index 6536272d9..93cbbbde8 100644 --- a/src/mcp/shared/session.py +++ b/src/mcp/shared/session.py @@ -75,6 +75,7 @@ def __init__( SendNotificationT, SendResultT, ReceiveRequestT, + ReceiveResultT, ReceiveNotificationT ]""", on_complete: Callable[["RequestResponder[ReceiveRequestT, SendResultT]"], Any], @@ -162,6 +163,7 @@ class BaseSession( SendNotificationT, SendResultT, ReceiveRequestT, + ReceiveResultT, ReceiveNotificationT, ], ): From c3b9a0bd77af690766253c4ecd13d1d4eca06052 Mon Sep 17 00:00:00 2001 From: Huang Jia Date: Thu, 26 Jun 2025 12:02:12 +0800 Subject: [PATCH 2/5] fix: add ReceiveResultT into Base Session --- src/mcp/shared/context.py | 2 +- src/mcp/shared/progress.py | 9 +++++---- tests/shared/test_progress_notifications.py | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/mcp/shared/context.py b/src/mcp/shared/context.py index f3006e7d5..4050d2b40 100644 --- a/src/mcp/shared/context.py +++ b/src/mcp/shared/context.py @@ -6,7 +6,7 @@ from mcp.shared.session import BaseSession from mcp.types import RequestId, RequestParams -SessionT = TypeVar("SessionT", bound=BaseSession[Any, Any, Any, Any, Any]) +SessionT = TypeVar("SessionT", bound=BaseSession[Any, Any, Any, Any, Any, Any]) LifespanContextT = TypeVar("LifespanContextT") RequestT = TypeVar("RequestT", default=Any) diff --git a/src/mcp/shared/progress.py b/src/mcp/shared/progress.py index 1ad81a779..d784f85b4 100644 --- a/src/mcp/shared/progress.py +++ b/src/mcp/shared/progress.py @@ -13,6 +13,7 @@ SendNotificationT, SendRequestT, SendResultT, + ReceiveResultT, ) from mcp.types import ProgressToken @@ -23,8 +24,8 @@ class Progress(BaseModel): @dataclass -class ProgressContext(Generic[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT]): - session: BaseSession[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT] +class ProgressContext(Generic[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT, ReceiveResultT]): + session: BaseSession[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveResultT, ReceiveNotificationT] progress_token: ProgressToken total: float | None current: float = field(default=0.0, init=False) @@ -40,12 +41,12 @@ async def progress(self, amount: float, message: str | None = None) -> None: @contextmanager def progress( ctx: RequestContext[ - BaseSession[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT], + BaseSession[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveResultT, ReceiveNotificationT], LifespanContextT, ], total: float | None = None, ) -> Generator[ - ProgressContext[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT], + ProgressContext[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT, ReceiveResultT], None, ]: if ctx.meta is None or ctx.meta.progressToken is None: diff --git a/tests/shared/test_progress_notifications.py b/tests/shared/test_progress_notifications.py index 08bcb2662..069738a9d 100644 --- a/tests/shared/test_progress_notifications.py +++ b/tests/shared/test_progress_notifications.py @@ -290,7 +290,7 @@ async def handle_client_message( # cast for type checker typed_context = cast( RequestContext[ - BaseSession[Any, Any, Any, Any, Any], + BaseSession[Any, Any, Any, Any, Any, Any], Any, ], request_context, From 39995aabd78e2891e8cbe7a6e8833996fde3b1ff Mon Sep 17 00:00:00 2001 From: Huang Jia Date: Thu, 26 Jun 2025 12:09:13 +0800 Subject: [PATCH 3/5] fix: add ReceiveResultT into Base Session --- src/mcp/client/session.py | 11 ++++++----- src/mcp/server/session.py | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/mcp/client/session.py b/src/mcp/client/session.py index 948817140..a967e5539 100644 --- a/src/mcp/client/session.py +++ b/src/mcp/client/session.py @@ -96,11 +96,12 @@ async def _default_logging_callback( class ClientSession( BaseSession[ - types.ClientRequest, - types.ClientNotification, - types.ClientResult, - types.ServerRequest, - types.ServerNotification, + types.ClientRequest, # SendRequestT + types.ClientNotification, # SendNotificationT + types.ClientResult, # SendResultT + types.ServerRequest, # ReceiveRequestT + Any, # ReceiveResultT,兼容所有服务端返回结果类型 + types.ServerNotification, # ReceiveNotificationT ] ): def __init__( diff --git a/src/mcp/server/session.py b/src/mcp/server/session.py index 5c696b136..50c29055f 100644 --- a/src/mcp/server/session.py +++ b/src/mcp/server/session.py @@ -74,6 +74,7 @@ class ServerSession( types.ServerNotification, types.ServerResult, types.ClientRequest, + Any, types.ClientNotification, ] ): From f0594a27bae862e8081c2586be06fbc11bcc808a Mon Sep 17 00:00:00 2001 From: Huang Jia Date: Thu, 26 Jun 2025 12:25:39 +0800 Subject: [PATCH 4/5] style: fix ruff issues --- src/mcp/cli/__init__.py | 1 + src/mcp/cli/cli.py | 4 ++++ src/mcp/client/session.py | 12 ++++++------ src/mcp/client/websocket.py | 5 +++++ src/mcp/shared/progress.py | 18 +++++++++++++----- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/mcp/cli/__init__.py b/src/mcp/cli/__init__.py index 3ef56d806..0552a6616 100644 --- a/src/mcp/cli/__init__.py +++ b/src/mcp/cli/__init__.py @@ -1,4 +1,5 @@ """FastMCP CLI package.""" +# pyright: reportUnknownVariableType=false from .cli import app diff --git a/src/mcp/cli/cli.py b/src/mcp/cli/cli.py index 69e2921f1..0a4aa4da8 100644 --- a/src/mcp/cli/cli.py +++ b/src/mcp/cli/cli.py @@ -1,4 +1,8 @@ """MCP CLI tools.""" +# pyright: reportMissingImports=false +# pyright: reportUnknownMemberType=false +# pyright: reportUnknownVariableType=false +# pyright: reportUntypedFunctionDecorator=false import importlib.metadata import importlib.util diff --git a/src/mcp/client/session.py b/src/mcp/client/session.py index a967e5539..400c78f68 100644 --- a/src/mcp/client/session.py +++ b/src/mcp/client/session.py @@ -96,12 +96,12 @@ async def _default_logging_callback( class ClientSession( BaseSession[ - types.ClientRequest, # SendRequestT - types.ClientNotification, # SendNotificationT - types.ClientResult, # SendResultT - types.ServerRequest, # ReceiveRequestT - Any, # ReceiveResultT,兼容所有服务端返回结果类型 - types.ServerNotification, # ReceiveNotificationT + types.ClientRequest, # SendRequestT + types.ClientNotification, # SendNotificationT + types.ClientResult, # SendResultT + types.ServerRequest, # ReceiveRequestT + Any, # ReceiveResultT,兼容所有服务端返回结果类型 + types.ServerNotification, # ReceiveNotificationT ] ): def __init__( diff --git a/src/mcp/client/websocket.py b/src/mcp/client/websocket.py index 0a371610b..85e0dbf91 100644 --- a/src/mcp/client/websocket.py +++ b/src/mcp/client/websocket.py @@ -1,3 +1,8 @@ +# pyright: reportMissingImports=false +# pyright: reportUnknownVariableType=false +# pyright: reportUnknownArgumentType=false +# pyright: reportUnknownMemberType=false + import json import logging from collections.abc import AsyncGenerator diff --git a/src/mcp/shared/progress.py b/src/mcp/shared/progress.py index d784f85b4..76d06b58e 100644 --- a/src/mcp/shared/progress.py +++ b/src/mcp/shared/progress.py @@ -10,10 +10,10 @@ BaseSession, ReceiveNotificationT, ReceiveRequestT, + ReceiveResultT, SendNotificationT, SendRequestT, SendResultT, - ReceiveResultT, ) from mcp.types import ProgressToken @@ -24,8 +24,12 @@ class Progress(BaseModel): @dataclass -class ProgressContext(Generic[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT, ReceiveResultT]): - session: BaseSession[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveResultT, ReceiveNotificationT] +class ProgressContext( + Generic[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT, ReceiveResultT] +): + session: BaseSession[ + SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveResultT, ReceiveNotificationT + ] progress_token: ProgressToken total: float | None current: float = field(default=0.0, init=False) @@ -41,12 +45,16 @@ async def progress(self, amount: float, message: str | None = None) -> None: @contextmanager def progress( ctx: RequestContext[ - BaseSession[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveResultT, ReceiveNotificationT], + BaseSession[ + SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveResultT, ReceiveNotificationT + ], LifespanContextT, ], total: float | None = None, ) -> Generator[ - ProgressContext[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT, ReceiveResultT], + ProgressContext[ + SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT, ReceiveResultT + ], None, ]: if ctx.meta is None or ctx.meta.progressToken is None: From 9479e948646960fbb9d61d0e3baf71a20dc23c4d Mon Sep 17 00:00:00 2001 From: Huang Jia Date: Thu, 26 Jun 2025 17:27:26 +0800 Subject: [PATCH 5/5] add ReceiveResultT --- src/mcp/client/session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mcp/client/session.py b/src/mcp/client/session.py index 400c78f68..3d6b9fb31 100644 --- a/src/mcp/client/session.py +++ b/src/mcp/client/session.py @@ -100,7 +100,7 @@ class ClientSession( types.ClientNotification, # SendNotificationT types.ClientResult, # SendResultT types.ServerRequest, # ReceiveRequestT - Any, # ReceiveResultT,兼容所有服务端返回结果类型 + Any, # ReceiveResultT,Any Type types.ServerNotification, # ReceiveNotificationT ] ):