The bug in InMemoryEventStore break SSE stream resumability
Opened this issue · 2 comments
Describe the bug
The standalong sse stream Id is coded to _GET_stream and the current InMemoryEventStore generate event Ids in the format -
${streamId}-${Date.now()}_${Math.random().toString(36).substring(2, 10)}
The replayEventsAfter(...) is called when client try to re-connect and resume the stream which further calls getStreamIdFromEventId(...) -
// Extract the stream ID from the event ID
const streamId = this.getStreamIdFromEventId(lastEventId);
if (!streamId) {
return '';
}
The issue is that getStreamIdFromEventId(...) will always return an empty string as the streamId due to the current eventId format and the code will return from this if check.
To Reproduce
Steps to reproduce the behavior:
- Create a tool with at least 2 elicitation requests.
- Call the tool.
- Try to reconnect with SSE stream - nothing will be returned.
Expected behavior
MCP client should be able to re-connect to SSE stream.
Fix
The fix could be to use a different delimiter than underscore when combining streamId with rest of the eventId.
Used dash("-") in below sample fix -
/**
* Generates a unique event ID for a given stream ID
*/
private generateEventId(streamId: string): string {
---> return `${streamId}**-**${Date.now()}_${Math.random().toString(36).substring(2, 10)}`;
}
/**
* Extracts the stream ID from an event ID
*/
private getStreamIdFromEventId(eventId: string): string {
---> const parts = eventId.split('**-**');
return parts.length > 0 ? parts[0] : '';
}
Hi @y0geshdev , thanks for filing this!
Which version / revision of the SDK are you using? In the current codebase the format & split should be both using underscores, see https://github.com/modelcontextprotocol/typescript-sdk/blame/7146eede0b9cc6b87a94f8361e3bd884b59b6508/src/examples/shared/inMemoryEventStore.ts#L15C11-L15C26
Happy to reopen if i missed something :-)
@ochafik I checked out the latest version (1.20.0) and tried resuming stream using last-event-id and it doesn't work.
The issue is that streamId for resumable standalone SSE stream is _GET_stream. Due to this, InMemoryEventStore.generateEventId(...) will return an eventId of format _GET_stream_.... Now when I sent this format eventId as last-event-id in header to resume the stream, InMemoryEventStore.getStreamIdFromEventId(...) will try to extract the streamId from eventId and will always return '' (empty string) -
// InMemoryEventStore.ts
/**
* Extracts the stream ID from an event ID
*/
private getStreamIdFromEventId(eventId: string): string { // the eventId for resumable stream is like - _GET_stream_...
const parts = eventId.split('_'); // parts = ['', GET, stream, ...]
return parts.length > 0 ? parts[0] : ''; // this will always return '' (empty string)
}
The fix is to use a different delimited than _ (as this is being used in StreamableHTTPServerTransport_standaloneSseStreamId ) to append and extract streamId in InMemoryEventStore from eventId.
To repro the issue, try to resume the SSE event stream.
Can we re-open this issue and fix this please?