|
| 1 | +""" |
| 2 | +In-memory event store for demonstrating resumability functionality. |
| 3 | +
|
| 4 | +This is a simple implementation intended for examples and testing, |
| 5 | +not for production use where a persistent storage solution would be more appropriate. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +import time |
| 10 | +from collections.abc import Awaitable, Callable |
| 11 | +from uuid import uuid4 |
| 12 | + |
| 13 | +from mcp.server.streamableHttp import EventId, EventStore, StreamId |
| 14 | +from mcp.types import JSONRPCMessage |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class InMemoryEventStore(EventStore): |
| 20 | + """ |
| 21 | + Simple in-memory implementation of the EventStore interface for resumability. |
| 22 | + This is primarily intended for examples and testing, not for production use |
| 23 | + where a persistent storage solution would be more appropriate. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + self.events: dict[ |
| 28 | + str, tuple[str, JSONRPCMessage, float] |
| 29 | + ] = {} # event_id -> (stream_id, message, timestamp) |
| 30 | + |
| 31 | + async def store_event( |
| 32 | + self, stream_id: StreamId, message: JSONRPCMessage |
| 33 | + ) -> EventId: |
| 34 | + """Stores an event with a generated event ID.""" |
| 35 | + event_id = str(uuid4()) |
| 36 | + self.events[event_id] = (stream_id, message, time.time()) |
| 37 | + return event_id |
| 38 | + |
| 39 | + async def replay_events_after( |
| 40 | + self, |
| 41 | + last_event_id: EventId, |
| 42 | + send_callback: Callable[[EventId, JSONRPCMessage], Awaitable[None]], |
| 43 | + ) -> StreamId: |
| 44 | + """Replays events that occurred after the specified event ID.""" |
| 45 | + logger.debug(f"Attempting to replay events after {last_event_id}") |
| 46 | + logger.debug(f"Total events in store: {len(self.events)}") |
| 47 | + logger.debug(f"Event IDs in store: {list(self.events.keys())}") |
| 48 | + |
| 49 | + if not last_event_id or last_event_id not in self.events: |
| 50 | + logger.warning(f"Event ID {last_event_id} not found in store") |
| 51 | + return "" |
| 52 | + |
| 53 | + # Get the stream ID and timestamp from the last event |
| 54 | + stream_id, _, last_timestamp = self.events[last_event_id] |
| 55 | + |
| 56 | + # Find all events for this stream after the last event |
| 57 | + events_to_replay = [ |
| 58 | + (event_id, message) |
| 59 | + for event_id, (sid, message, timestamp) in self.events.items() |
| 60 | + if sid == stream_id and timestamp > last_timestamp |
| 61 | + ] |
| 62 | + |
| 63 | + # Sort by timestamp to ensure chronological order |
| 64 | + events_to_replay.sort(key=lambda x: self.events[x[0]][2]) |
| 65 | + |
| 66 | + logger.debug(f"Found {len(events_to_replay)} events to replay") |
| 67 | + logger.debug( |
| 68 | + f"Events to replay: {[event_id for event_id, _ in events_to_replay]}" |
| 69 | + ) |
| 70 | + |
| 71 | + # Send all events in order |
| 72 | + for event_id, message in events_to_replay: |
| 73 | + await send_callback(event_id, message) |
| 74 | + |
| 75 | + return stream_id |
0 commit comments