Skip to content

Extract BaseSession request ID increment logic #1102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/mcp/shared/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ async def send_request(
Do not use this method to emit notifications! Use send_notification()
instead.
"""
request_id = self._request_id
self._request_id = request_id + 1
request_id = self.next_request_id()

response_stream, response_stream_reader = anyio.create_memory_object_stream[JSONRPCResponse | JSONRPCError](1)
self._response_streams[request_id] = response_stream
Expand Down Expand Up @@ -468,3 +467,12 @@ async def _handle_incoming(
) -> None:
"""A generic handler for incoming messages. Overwritten by subclasses."""
pass

def next_request_id(self) -> int:
"""
Increment the request ID and return its current value. Can be
overwritten by subclasses.
"""
request_id = self._request_id
self._request_id = request_id + 1
return request_id
Loading