Skip to content

Commit 40f6ba7

Browse files
[v2] move to Headers object (modelcontextprotocol#1327)
Co-authored-by: Matt <77928207+mattzcarey@users.noreply.github.com>
1 parent 2edbe8c commit 40f6ba7

4 files changed

Lines changed: 12 additions & 12 deletions

File tree

packages/core/src/types/types.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,19 +2384,14 @@ type Flatten<T> = T extends Primitive
23842384

23852385
type Infer<Schema extends z.ZodTypeAny> = Flatten<z.infer<Schema>>;
23862386

2387-
/**
2388-
* Headers that are compatible with both Node.js and the browser.
2389-
*/
2390-
export type IsomorphicHeaders = Record<string, string | string[] | undefined>;
2391-
23922387
/**
23932388
* Information about the incoming request.
23942389
*/
23952390
export interface RequestInfo {
23962391
/**
23972392
* The headers of the request.
23982393
*/
2399-
headers: IsomorphicHeaders;
2394+
headers: Headers;
24002395
}
24012396

24022397
/**

packages/middleware/node/test/streamableHttp.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import type {
1212
JSONRPCResultResponse,
1313
RequestId
1414
} from '@modelcontextprotocol/core';
15+
import type { EventId, EventStore, StreamId } from '@modelcontextprotocol/server';
16+
import { McpServer } from '@modelcontextprotocol/server';
1517
import type { ZodMatrixEntry } from '@modelcontextprotocol/test-helpers';
1618
import { listenOnRandomPort, zodTestMatrix } from '@modelcontextprotocol/test-helpers';
19+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
1720

1821
import { NodeStreamableHTTPServerTransport } from '../src/streamableHttp.js';
19-
import { McpServer } from '@modelcontextprotocol/server';
20-
import type { EventId, EventStore, StreamId } from '@modelcontextprotocol/server';
21-
import { describe, expect, beforeEach, afterEach, it } from 'vitest';
2222

2323
async function getFreePort() {
2424
return new Promise(res => {
@@ -402,10 +402,15 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
402402
'A simple test tool with request info',
403403
{ name: z.string().describe('Name to greet') },
404404
async ({ name }, { requestInfo }): Promise<CallToolResult> => {
405+
// Convert Headers object to plain object for JSON serialization
406+
// Headers is a Web API class that doesn't serialize with JSON.stringify
407+
const serializedRequestInfo = {
408+
headers: Object.fromEntries(requestInfo?.headers ?? new Headers())
409+
};
405410
return {
406411
content: [
407412
{ type: 'text', text: `Hello, ${name}!` },
408-
{ type: 'text', text: `${JSON.stringify(requestInfo)}` }
413+
{ type: 'text', text: `${JSON.stringify(serializedRequestInfo)}` }
409414
]
410415
};
411416
}

packages/server/src/server/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class Server<
168168
if (this._capabilities.logging) {
169169
this.setRequestHandler(SetLevelRequestSchema, async (request, extra) => {
170170
const transportSessionId: string | undefined =
171-
extra.sessionId || (extra.requestInfo?.headers['mcp-session-id'] as string) || undefined;
171+
extra.sessionId || (extra.requestInfo?.headers.get('mcp-session-id') as string) || undefined;
172172
const { level } = request.params;
173173
const parseResult = LoggingLevelSchema.safeParse(level);
174174
if (parseResult.success) {

packages/server/src/server/streamableHttp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
596596

597597
// Build request info from headers
598598
const requestInfo: RequestInfo = {
599-
headers: Object.fromEntries(req.headers.entries())
599+
headers: req.headers
600600
};
601601

602602
let rawMessage;

0 commit comments

Comments
 (0)