Skip to content

Commit 20006b3

Browse files
author
Ron Mordechai
committed
Extract BaseSession request ID increment logic
This allows the request ID increment logic to be overwritten by subclasses. I have a unique need to share sessions' underlying input and output streams, but not the sessions themselves. This would require keeping track of request IDs globally to avoid overlap. Having this logic overridable lets me do that.
1 parent 5c9cf29 commit 20006b3

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/mcp/shared/session.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ async def send_request(
233233
Do not use this method to emit notifications! Use send_notification()
234234
instead.
235235
"""
236-
request_id = self._request_id
237-
self._request_id = request_id + 1
236+
request_id = self.next_request_id()
238237

239238
response_stream, response_stream_reader = anyio.create_memory_object_stream[JSONRPCResponse | JSONRPCError](1)
240239
self._response_streams[request_id] = response_stream
@@ -468,3 +467,12 @@ async def _handle_incoming(
468467
) -> None:
469468
"""A generic handler for incoming messages. Overwritten by subclasses."""
470469
pass
470+
471+
def next_request_id(self) -> int:
472+
"""
473+
Increment the request ID and return its current value. Can be
474+
overwritten by subclasses.
475+
"""
476+
request_id = self._request_id
477+
self._request_id = request_id + 1
478+
return request_id

0 commit comments

Comments
 (0)