From 34937b72f2b5257a1833ed80af539551d705884e Mon Sep 17 00:00:00 2001 From: David Soria Parra Date: Fri, 11 Oct 2024 11:22:04 +0100 Subject: [PATCH 1/3] pyproject: Remove strict typing for now We currently have 21 non strict typing issues and 112 strict typing issues. Most of the strict ones come from generic handling. This is the initial step to tackle the non strict errors by ignoring strict errors and adding github actions to check for type errors. Once this is place we move back to using strict types. --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e0cb38b..4d2258b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,6 @@ packages = ["mcp_python"] [tool.pyright] include = ["mcp_python", "tests"] -typeCheckingMode = "strict" venvPath = "." venv = ".venv" From 04ad96e6cd549d8e10fc6a8b04df109e45a29b57 Mon Sep 17 00:00:00 2001 From: David Soria Parra Date: Fri, 11 Oct 2024 10:55:55 +0100 Subject: [PATCH 2/3] Typing fixes Strict pyright mode results in a lot of issues regarding non fully determined types, due to Generics. These are some issues I came across today. We are still far from being clean on pyright. --- mcp_python/server/__init__.py | 6 +++--- mcp_python/server/stdio.py | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/mcp_python/server/__init__.py b/mcp_python/server/__init__.py index 375cbfe..581f259 100644 --- a/mcp_python/server/__init__.py +++ b/mcp_python/server/__init__.py @@ -20,6 +20,8 @@ from mcp_python.types import ( CompleteRequest, ErrorData, JSONRPCMessage, + ListPromptsRequest, + ListPromptsResult, ListResourcesRequest, ListResourcesResult, LoggingLevel, @@ -57,8 +59,6 @@ class Server: return request_ctx.get() def list_prompts(self): - from mcp_python.types import ListPromptsRequest, ListPromptsResult - def decorator(func: Callable[[], Awaitable[list[Prompt]]]): logger.debug(f"Registering handler for PromptListRequest") @@ -90,7 +90,7 @@ class Server: async def handler(req: GetPromptRequest): prompt_get = await func(req.params.name, req.params.arguments) - messages = [] + messages: list[SamplingMessage] = [] for message in prompt_get.messages: match message.content: case str() as text_content: diff --git a/mcp_python/server/stdio.py b/mcp_python/server/stdio.py index bfcc76c..b55df0e 100644 --- a/mcp_python/server/stdio.py +++ b/mcp_python/server/stdio.py @@ -7,10 +7,9 @@ from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStre from mcp_python.types import JSONRPCMessage - @asynccontextmanager async def stdio_server( - stdin: anyio.AsyncFile | None = None, stdout: anyio.AsyncFile | None = None + stdin: anyio.AsyncFile[str] | None = None, stdout: anyio.AsyncFile[str] | None = None ): """ Server transport for stdio: this communicates with an MCP client by reading from the current process' stdin and writing to stdout. From ac6064b016157d3922c39fe0144441fc3ab975d0 Mon Sep 17 00:00:00 2001 From: David Soria Parra Date: Fri, 11 Oct 2024 11:48:52 +0100 Subject: [PATCH 3/3] Make request and notification method generic The request and notification method were defined as str but later overwritten in subclasses with literals. This causes a reportIncompatibleVariableOverride issue. We need to make method generic. --- mcp_python/types.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mcp_python/types.py b/mcp_python/types.py index becc2b1..1087526 100644 --- a/mcp_python/types.py +++ b/mcp_python/types.py @@ -41,23 +41,23 @@ class NotificationParams(BaseModel): This parameter name is reserved by MCP to allow clients and servers to attach additional metadata to their notifications. """ - RequestParamsT = TypeVar("RequestParamsT", bound=RequestParams) NotificationParamsT = TypeVar("NotificationParamsT", bound=NotificationParams) +MethodT = TypeVar("MethodT", bound=str) -class Request(BaseModel, Generic[RequestParamsT]): +class Request(BaseModel, Generic[RequestParamsT, MethodT]): """Base class for JSON-RPC requests.""" - method: str + method: MethodT params: RequestParamsT model_config = ConfigDict(extra="allow") -class Notification(BaseModel, Generic[NotificationParamsT]): +class Notification(BaseModel, Generic[NotificationParamsT, MethodT]): """Base class for JSON-RPC notifications.""" - method: str + method: MethodT model_config = ConfigDict(extra="allow")