Skip to content

Commit 6a36cac

Browse files
cliffhallclaude
andcommitted
fix(everything): event store rejects an unknown id; kebab-case module name
Copilot round 1 on #5006: - replayEventsAfter throws for an unknown event id instead of returning undefined through a cast: getStreamIdForEventId is the SDK's path for an unknown id, so reaching replay with one is a caller error. - The event store module and its test become in-memory-event-store.ts, per the everything package's kebab-case file names, and both open with a purpose header. - The unknown-session POST table covers a batch body (null id). Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 6619ca8 commit 6a36cac

5 files changed

Lines changed: 31 additions & 18 deletions

File tree

src/everything/__tests__/inMemoryEventStore.test.ts renamed to src/everything/__tests__/in-memory-event-store.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
// Unit tests for the Streamable HTTP event store (#4087), called directly
2+
// rather than over HTTP: which events a replay sends, and what an unknown event
3+
// id yields, are exact here, where streamable-http.test.ts can only observe
4+
// them through the SDK (which asks getStreamIdForEventId first and refuses an
5+
// unknown id before replaying).
6+
17
import { describe, it, expect } from "vitest";
28
import type { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
3-
import { InMemoryEventStore } from "../transports/inMemoryEventStore.js";
9+
import { InMemoryEventStore } from "../transports/in-memory-event-store.js";
410

511
const message = (method: string): JSONRPCMessage => ({
612
jsonrpc: "2.0",
@@ -50,22 +56,19 @@ describe("InMemoryEventStore", () => {
5056
);
5157
});
5258

53-
it("returns undefined without replaying events for unknown event ids", async () => {
59+
it("rejects an unknown event id without replaying anything", async () => {
5460
const eventStore = new InMemoryEventStore();
5561
await eventStore.storeEvent("stream-a", message("notifications/stream-a"));
5662

5763
const replayedEvents: Array<{ eventId: string; message: JSONRPCMessage }> =
5864
[];
59-
const replayedStreamId = await eventStore.replayEventsAfter(
60-
"unknown-event-id",
61-
{
65+
await expect(
66+
eventStore.replayEventsAfter("unknown-event-id", {
6267
send: async (eventId, replayedMessage) => {
6368
replayedEvents.push({ eventId, message: replayedMessage });
6469
},
65-
},
66-
);
67-
68-
expect(replayedStreamId).toBeUndefined();
70+
}),
71+
).rejects.toThrow("Unknown event ID: unknown-event-id");
6972
expect(replayedEvents).toEqual([]);
7073
});
7174

‎src/everything/__tests__/streamable-http.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ describe("Streamable HTTP transport", () => {
276276

277277
it.each([
278278
["a notification", JSON.stringify({ jsonrpc: "2.0", method: "ping" })],
279+
["a batch", JSON.stringify([{ jsonrpc: "2.0", id: 8, method: "ping" }])],
279280
["malformed JSON", "{not json"],
280281
[
281282
"a body too large to read for its id",

‎src/everything/docs/structure.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ src/everything
5858
│ ├── trigger-sampling-request-async.ts
5959
│ └── trigger-url-elicitation.ts
6060
└── transports
61-
├── inMemoryEventStore.ts
61+
├── in-memory-event-store.ts
6262
├── listen.ts
6363
├── sse.ts
6464
├── stdio.ts
@@ -190,7 +190,7 @@ src/everything
190190

191191
### `transports/`
192192

193-
- `inMemoryEventStore.ts`
193+
- `in-memory-event-store.ts`
194194
- `InMemoryEventStore`: the Streamable HTTP transport's event store for resumability. Replays only the events of the stream a `Last-Event-ID` belongs to, and reports an unknown event ID as having no stream, so the SDK refuses the resume.
195195
- `listen.ts`
196196
- `listenOrExit(app, port, listeningMessage)`: binds an Express app to its port for both HTTP transports.

src/everything/transports/inMemoryEventStore.ts renamed to src/everything/transports/in-memory-event-store.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
// The Streamable HTTP transport's event store, which makes its SSE streams
2+
// resumable: a client that reconnects with a `Last-Event-ID` is sent the
3+
// events it missed (#4087).
4+
//
5+
// Events are kept per stream, and a replay sends only the later events of the
6+
// stream the given event belongs to, never another stream's. An unknown event
7+
// id has no stream: `getStreamIdForEventId` reports that as `undefined`, which
8+
// is how the SDK learns to refuse the resume before it calls
9+
// `replayEventsAfter`. In memory and unbounded, so for examples and testing,
10+
// not production. Split out of `streamableHttp.ts` (from #4099) so it can be
11+
// tested directly.
12+
113
import { randomUUID } from "node:crypto";
214
import type { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
315
import type {
@@ -6,8 +18,6 @@ import type {
618
StreamId,
719
} from "@modelcontextprotocol/sdk/server/streamableHttp.js";
820

9-
// Simple in-memory event store for SSE resumability.
10-
// Primarily intended for examples and testing, not production use.
1121
export class InMemoryEventStore implements EventStore {
1222
private events: Map<
1323
EventId,
@@ -35,10 +45,9 @@ export class InMemoryEventStore implements EventStore {
3545
): Promise<StreamId> {
3646
const lastEvent = this.events.get(lastEventId);
3747
if (!lastEvent) {
38-
// The SDK type currently requires a StreamId, but unknown event IDs have
39-
// no stream to resume. Return undefined at runtime to match the intended
40-
// EventStore semantics and Python API behavior.
41-
return undefined as unknown as StreamId;
48+
// The SDK asks getStreamIdForEventId first and refuses an unknown id, so
49+
// reaching here is a caller error: there is no stream to return.
50+
throw new Error(`Unknown event ID: ${lastEventId}`);
4251
}
4352

4453
const { streamId } = lastEvent;

‎src/everything/transports/streamableHttp.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { createServer } from "../server/index.js";
1414
import { randomUUID } from "node:crypto";
1515
import cors from "cors";
1616
import { listenOrExit } from "./listen.js";
17-
import { InMemoryEventStore } from "./inMemoryEventStore.js";
17+
import { InMemoryEventStore } from "./in-memory-event-store.js";
1818

1919
/** The most of a refused POST's body read to find its request id. */
2020
const MAX_ID_BODY_BYTES = 64 * 1024;

0 commit comments

Comments
 (0)