Skip to content

Commit 5ed26c1

Browse files
committed
fix(mcp): read event streams with mixed-case content types
1 parent f5b5f9d commit 5ed26c1

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎apps/server/src/mcp/AcpMcpStdioBridge.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ describe("AcpMcpStdioBridge", () => {
9292
}),
9393
);
9494

95+
it.effect("forwards SSE responses with a mixed-case media type", () =>
96+
Effect.gen(function* () {
97+
const { input, written, bridge } = makeHarness(
98+
() =>
99+
new Response('data: {"jsonrpc":"2.0","id":7,"result":{"ok":true}}\n\n', {
100+
headers: { "content-type": "Text/Event-Stream; charset=UTF-8" },
101+
}),
102+
);
103+
const fiber = yield* Effect.forkChild(bridge);
104+
105+
input.end('{"jsonrpc":"2.0","id":7,"method":"tools/list"}\n');
106+
yield* Fiber.join(fiber);
107+
108+
expect(written.map((line) => JSON.parse(line))).toEqual([
109+
{ jsonrpc: "2.0", id: 7, result: { ok: true } },
110+
]);
111+
}),
112+
);
113+
95114
it.effect("forwards requests and replays the session id and protocol version", () =>
96115
Effect.gen(function* () {
97116
const { input, written, requests, bridge } = makeHarness((request) => {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
import * as Data from "effect/Data";
3+
import * as Effect from "effect/Effect";
4+
import * as Stream from "effect/Stream";
5+
6+
import { responsePayloads } from "./mcpResponsePayloads.ts";
7+
8+
class McpResponseTestError extends Data.TaggedError("McpResponseTestError")<{ cause: unknown }> {}
9+
10+
describe("responsePayloads", () => {
11+
it.effect.each([
12+
"text/event-stream",
13+
"text/event-stream; charset=utf-8",
14+
"Text/Event-Stream; Charset=UTF-8",
15+
"TEXT/EVENT-STREAM",
16+
])("reads SSE payloads with Content-Type %s", (contentType) =>
17+
Effect.gen(function* () {
18+
const response = new Response(
19+
'data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progress":1}}\n\n' +
20+
'data: {"jsonrpc":"2.0","id":7,"result":{"text":"Keep This Case"}}\n\n',
21+
{ headers: { "content-type": contentType } },
22+
);
23+
const payloads = yield* Stream.runCollect(
24+
responsePayloads(response, (cause) => new McpResponseTestError({ cause })),
25+
);
26+
27+
expect(payloads).toEqual([
28+
{ jsonrpc: "2.0", method: "notifications/progress", params: { progress: 1 } },
29+
{ jsonrpc: "2.0", id: 7, result: { text: "Keep This Case" } },
30+
]);
31+
}),
32+
);
33+
34+
it.effect.each(["application/json", 'application/json; profile="TEXT/EVENT-STREAM"'])(
35+
"reads a JSON response with Content-Type %s",
36+
(contentType) =>
37+
Effect.gen(function* () {
38+
const response = new Response('{"jsonrpc":"2.0","id":7,"result":{}}', {
39+
headers: { "content-type": contentType },
40+
});
41+
const payloads = yield* Stream.runCollect(
42+
responsePayloads(response, (cause) => new McpResponseTestError({ cause })),
43+
);
44+
45+
expect(payloads).toEqual([{ jsonrpc: "2.0", id: 7, result: {} }]);
46+
}),
47+
);
48+
});

‎packages/shared/src/mcpResponsePayloads.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function responsePayloads<E>(
4646
return Stream.unwrap(discardResponseBody(response).pipe(Effect.as(Stream.empty)));
4747
}
4848
const contentType = response.headers.get("content-type") ?? "";
49-
if (contentType.includes("text/event-stream")) {
49+
if (contentType.split(";", 1)[0]?.trim().toLowerCase() === "text/event-stream") {
5050
return Stream.fromAsyncIterable(sseDataLines(response), onError).pipe(
5151
Stream.mapEffect((data) => Effect.try({ try: () => JSON.parse(data), catch: onError })),
5252
);

0 commit comments

Comments
 (0)