Skip to content

Commit 90f5e91

Browse files
author
晓明 王
committed
normalize string ID to int in server messages for compatibility
Some non-standard SSE servers may return numeric IDs as strings, even when the original request used an integer. This change ensures that message.root.id is always an integer to maintain consistency and avoid type issues in downstream processing.
1 parent 05b7156 commit 90f5e91

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/mcp/client/sse.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ async def sse_reader(
9898
message = types.JSONRPCMessage.model_validate_json( # noqa: E501
9999
sse.data
100100
)
101+
102+
# Normalize ID to int if it's a numeric string.
103+
# Some non-standard SSE servers return numeric IDs as strings,
104+
# even if the original request used an integer ID.
105+
if isinstance(message.root, types.JSONRPCResponse):
106+
msg_id = message.root.id
107+
if isinstance(msg_id, str) and msg_id.isdigit():
108+
message.root.id = int(msg_id)
109+
elif not isinstance(msg_id, int):
110+
logger.warning(
111+
"Ignored message with "
112+
f"invalid ID: {msg_id!r}"
113+
)
114+
continue
115+
101116
logger.debug(
102117
f"Received server message: {message}"
103118
)

0 commit comments

Comments
 (0)