Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-node-set-cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/node": patch
---

Preserve every Set-Cookie field when adapting a web response to Node.
6 changes: 4 additions & 2 deletions packages/middleware/node/src/toNodeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface NodeIncomingMessageLike extends AsyncIterable<unknown> {

/** Minimal duck-typed shape of a Node.js `ServerResponse` accepted by {@linkcode toNodeHandler}. */
export interface NodeServerResponseLike {
writeHead(statusCode: number, headers?: Record<string, string>): unknown;
writeHead(statusCode: number, headers?: Record<string, string | string[]>): unknown;
write(chunk: string | Uint8Array): unknown;
end(chunk?: string | Uint8Array): unknown;
on(event: string, listener: (...args: unknown[]) => void): unknown;
Expand Down Expand Up @@ -172,10 +172,12 @@ export function toNodeHandler(handler: FetchLikeMcpHandler, opts?: ToNodeHandler
}
}

const headers: Record<string, string> = {};
const headers: Record<string, string | string[]> = {};
for (const [name, value] of response.headers) {
headers[name] = value;
}
const cookies = response.headers.getSetCookie();
if (cookies.length > 0) headers['set-cookie'] = cookies;
res.writeHead(response.status, headers);
if (response.body === null) {
finished = true;
Expand Down
16 changes: 14 additions & 2 deletions packages/middleware/node/test/toNodeHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ describe('toNodeHandler', () => {

interface FakeNodeResponse extends NodeServerResponseLike {
statusCode: number;
headers: Record<string, string> | undefined;
headers: Record<string, string | string[]> | undefined;
}

function nodeRequestResponse(body: unknown): {
Expand Down Expand Up @@ -454,7 +454,7 @@ function nodeRequestResponse(body: unknown): {
const res: FakeNodeResponse = {
statusCode: 0,
headers: undefined,
writeHead(statusCode: number, headers?: Record<string, string>) {
writeHead(statusCode: number, headers?: Record<string, string | string[]>) {
this.statusCode = statusCode;
this.headers = headers;
return this;
Expand Down Expand Up @@ -484,3 +484,15 @@ function nodeRequestResponse(body: unknown): {
}
};
}

describe('response cookies', () => {
it('passes each Set-Cookie field separately to Node', async () => {
const cookies = ['first=one; Path=/; HttpOnly', 'second=two; Path=/; HttpOnly'];
const response = new Response(null, { headers: cookies.map(cookie => ['set-cookie', cookie] as [string, string]) });
const writeHead = vi.fn();
const res: NodeServerResponseLike = { writeHead, on: vi.fn(), write: vi.fn(), end: vi.fn() };
const req = Object.assign(Readable.from([]), { method: 'GET', url: '/', headers: { host: 'localhost' } });
await toNodeHandler({ fetch: async () => response })(req, res);
expect(writeHead).toHaveBeenCalledWith(200, { 'set-cookie': cookies });
});
});
Loading