normalize string ID to int in server messages for compatibility (#851)

Co-authored-by: 晓明 王 <wxm@v1i.cc>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
This commit is contained in:
xiaoming wang
2025-06-08 15:15:21 +08:00
committed by GitHub
parent b0b44c2fe4
commit 2cbc435c6c
2 changed files with 18 additions and 2 deletions

View File

@@ -34,7 +34,7 @@ LATEST_PROTOCOL_VERSION = "2025-03-26"
ProgressToken = str | int ProgressToken = str | int
Cursor = str Cursor = str
Role = Literal["user", "assistant"] Role = Literal["user", "assistant"]
RequestId = str | int RequestId = Annotated[int | str, Field(union_mode="left_to_right")]
AnyFunction: TypeAlias = Callable[..., Any] AnyFunction: TypeAlias = Callable[..., Any]
@@ -353,7 +353,7 @@ class ProgressNotificationParams(NotificationParams):
"""Total number of items to process (or total progress required), if known.""" """Total number of items to process (or total progress required), if known."""
message: str | None = None message: str | None = None
""" """
Message related to progress. This should provide relevant human readable Message related to progress. This should provide relevant human readable
progress information. progress information.
""" """
model_config = ConfigDict(extra="allow") model_config = ConfigDict(extra="allow")

View File

@@ -8,12 +8,14 @@ import anyio
import httpx import httpx
import pytest import pytest
import uvicorn import uvicorn
from inline_snapshot import snapshot
from pydantic import AnyUrl from pydantic import AnyUrl
from starlette.applications import Starlette from starlette.applications import Starlette
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import Response from starlette.responses import Response
from starlette.routing import Mount, Route from starlette.routing import Mount, Route
import mcp.types as types
from mcp.client.session import ClientSession from mcp.client.session import ClientSession
from mcp.client.sse import sse_client from mcp.client.sse import sse_client
from mcp.server import Server from mcp.server import Server
@@ -503,3 +505,17 @@ async def test_request_context_isolation(context_server: None, server_url: str)
assert ctx["request_id"] == f"request-{i}" assert ctx["request_id"] == f"request-{i}"
assert ctx["headers"].get("x-request-id") == f"request-{i}" assert ctx["headers"].get("x-request-id") == f"request-{i}"
assert ctx["headers"].get("x-custom-value") == f"value-{i}" assert ctx["headers"].get("x-custom-value") == f"value-{i}"
def test_sse_message_id_coercion():
"""Test that string message IDs that look like integers are parsed as integers.
See <https://github.com/modelcontextprotocol/python-sdk/pull/851> for more details.
"""
json_message = '{"jsonrpc": "2.0", "id": "123", "method": "ping", "params": null}'
msg = types.JSONRPCMessage.model_validate_json(json_message)
assert msg == snapshot(
types.JSONRPCMessage(
root=types.JSONRPCRequest(method="ping", jsonrpc="2.0", id=123)
)
)