Skip to content

Commit 5a51448

Browse files
晓明 王wangxm345566462
晓明 王
authored andcommitted
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 095d152 commit 5a51448

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/mcp/client/sse.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,21 @@ async def sse_reader(
102102
# Normalize ID to int if it's a numeric string.
103103
# Some non-standard SSE servers return numeric IDs as strings,
104104
# even if the original request used an integer ID.
105-
if isinstance(message.root, types.JSONRPCResponse):
105+
if isinstance( # noqa: E501
106+
message.root,
107+
types.JSONRPCResponse
108+
):
106109
msg_id = message.root.id
107-
if isinstance(msg_id, str) and msg_id.isdigit():
110+
if (isinstance(msg_id, str) and
111+
msg_id.isdigit()):
108112
message.root.id = int(msg_id)
109113
elif not isinstance(msg_id, int):
110114
logger.warning(
111115
"Ignored message with "
112116
f"invalid ID: {msg_id!r}"
113117
)
114118
continue
115-
119+
116120
logger.debug(
117121
f"Received server message: {message}"
118122
)

0 commit comments

Comments
 (0)