Skip to content

Commit d0d7e9e

Browse files
fix(shared): match the SSE media type alone, not its parameters
Lowercasing the whole Content-Type and using includes let a parameter value such as profile=".../Text/Event-Stream" on a JSON response select the SSE reader and drop the JSON envelope. Compare only the trimmed media type before the first semicolon, case-insensitively. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bc7ed3f commit d0d7e9e

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎packages/shared/src/mcpResponsePayloads.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ it.effect("reads SSE line endings across byte-chunk boundaries without splitting
7070
}),
7171
);
7272

73-
it.effect("reads SSE whatever the case of the media type", () =>
73+
it.effect("picks the SSE reader from the media type alone, whatever its case", () =>
7474
Effect.gen(function* () {
7575
const response = new Response('data: {"id":1}\n\n', {
7676
headers: { "content-type": "Text/Event-Stream; charset=utf-8" },
@@ -79,5 +79,15 @@ it.effect("reads SSE whatever the case of the media type", () =>
7979
responsePayloads(response, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })),
8080
);
8181
expect(Array.from(payloads)).toEqual([{ id: 1 }]);
82+
83+
const json = new Response('{"jsonrpc":"2.0","id":1,"result":{}}', {
84+
headers: {
85+
"content-type": 'application/json; profile="https://example.com/Text/Event-Stream"',
86+
},
87+
});
88+
const jsonPayloads = yield* Stream.runCollect(
89+
responsePayloads(json, (cause) => ({ _tag: "ResponsePayloadError" as const, cause })),
90+
);
91+
expect(Array.from(jsonPayloads)).toEqual([{ jsonrpc: "2.0", id: 1, result: {} }]);
8292
}),
8393
);

‎packages/shared/src/mcpResponsePayloads.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export function responsePayloads<E>(
3535
if (response.status === 202 || response.status === 204) {
3636
return Stream.unwrap(discardResponseBody(response).pipe(Effect.as(Stream.empty)));
3737
}
38-
const contentType = response.headers.get("content-type")?.toLowerCase() ?? "";
39-
if (contentType.includes("text/event-stream")) {
38+
const mediaType = (response.headers.get("content-type") ?? "").split(";", 1)[0]!.trim();
39+
if (mediaType.toLowerCase() === "text/event-stream") {
4040
return Stream.fromAsyncIterable(sseDataLines(response), onError).pipe(
4141
Stream.mapEffect((data) => Effect.try({ try: () => JSON.parse(data), catch: onError })),
4242
);

0 commit comments

Comments
 (0)