mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-18 22:44:20 +01:00
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:
@@ -34,7 +34,7 @@ LATEST_PROTOCOL_VERSION = "2025-03-26"
|
||||
ProgressToken = str | int
|
||||
Cursor = str
|
||||
Role = Literal["user", "assistant"]
|
||||
RequestId = str | int
|
||||
RequestId = Annotated[int | str, Field(union_mode="left_to_right")]
|
||||
AnyFunction: TypeAlias = Callable[..., Any]
|
||||
|
||||
|
||||
@@ -353,7 +353,7 @@ class ProgressNotificationParams(NotificationParams):
|
||||
"""Total number of items to process (or total progress required), if known."""
|
||||
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.
|
||||
"""
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
@@ -8,12 +8,14 @@ import anyio
|
||||
import httpx
|
||||
import pytest
|
||||
import uvicorn
|
||||
from inline_snapshot import snapshot
|
||||
from pydantic import AnyUrl
|
||||
from starlette.applications import Starlette
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
from starlette.routing import Mount, Route
|
||||
|
||||
import mcp.types as types
|
||||
from mcp.client.session import ClientSession
|
||||
from mcp.client.sse import sse_client
|
||||
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["headers"].get("x-request-id") == f"request-{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)
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user