Skip to content

Commit af64a0e

Browse files
committed
remove custom headers
1 parent 251bb9d commit af64a0e

File tree

3 files changed

+11
-160
lines changed

3 files changed

+11
-160
lines changed

src/server/mcp.test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ describe("tool()", () => {
379379
expect(receivedSessionId).toBe("test-session-123");
380380
});
381381

382-
test("should provide sendNotification withing tool call", async () => {
382+
test("should provide sendNotification within tool call", async () => {
383383
const mcpServer = new McpServer(
384384
{
385385
name: "test server",
@@ -401,12 +401,10 @@ describe("tool()", () => {
401401
);
402402

403403
let receivedLogMessage: string | undefined;
404-
405-
const loggingMessage = "hello here is log message 1"
404+
const loggingMessage = "hello here is log message 1";
406405

407406
client.setNotificationHandler(LoggingMessageNotificationSchema, (notification) => {
408407
receivedLogMessage = notification.params.data as string;
409-
410408
});
411409

412410
mcpServer.tool("test-tool", async ({ sendNotification }) => {
@@ -422,15 +420,10 @@ describe("tool()", () => {
422420
});
423421

424422
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
425-
// Set a test sessionId on the server transport
426-
serverTransport.sessionId = "test-session-123";
427-
428-
429423
await Promise.all([
430424
client.connect(clientTransport),
431425
mcpServer.server.connect(serverTransport),
432426
]);
433-
434427
await client.request(
435428
{
436429
method: "tools/call",

src/server/streamableHttp.test.ts

Lines changed: 2 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe("StreamableHTTPServerTransport", () => {
9999

100100
await transport.handleRequest(req, mockResponse);
101101

102-
expect(mockResponse.writeHead).toHaveBeenCalledWith(404, {});
102+
expect(mockResponse.writeHead).toHaveBeenCalledWith(404);
103103
// check if the error response is a valid JSON-RPC error format
104104
expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('"jsonrpc":"2.0"'));
105105
expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('"error"'));
@@ -116,7 +116,7 @@ describe("StreamableHTTPServerTransport", () => {
116116

117117
await transport.handleRequest(req, mockResponse);
118118

119-
expect(mockResponse.writeHead).toHaveBeenCalledWith(400, {});
119+
expect(mockResponse.writeHead).toHaveBeenCalledWith(400);
120120
expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('"jsonrpc":"2.0"'));
121121
expect(mockResponse.end).toHaveBeenCalledWith(expect.stringContaining('"message":"Bad Request: Mcp-Session-Id header is required"'));
122122
});
@@ -842,135 +842,4 @@ describe("StreamableHTTPServerTransport", () => {
842842
expect(onMessageMock).not.toHaveBeenCalledWith(requestBodyMessage);
843843
});
844844
});
845-
846-
describe("Custom Headers", () => {
847-
const customHeaders = {
848-
"X-Custom-Header": "custom-value",
849-
"X-API-Version": "1.0",
850-
"Access-Control-Allow-Origin": "*"
851-
};
852-
853-
let transportWithHeaders: StreamableHTTPServerTransport;
854-
let mockResponse: jest.Mocked<ServerResponse>;
855-
856-
beforeEach(() => {
857-
transportWithHeaders = new StreamableHTTPServerTransport({ sessionId: randomUUID(), customHeaders });
858-
mockResponse = createMockResponse();
859-
});
860-
861-
it("should include custom headers in SSE response", async () => {
862-
const req = createMockRequest({
863-
method: "POST",
864-
headers: {
865-
"content-type": "application/json",
866-
accept: "application/json, text/event-stream",
867-
"mcp-session-id": transportWithHeaders.sessionId
868-
},
869-
body: JSON.stringify({
870-
jsonrpc: "2.0",
871-
method: "test",
872-
params: {},
873-
id: "test-headers-id"
874-
})
875-
});
876-
877-
await transportWithHeaders.handleRequest(req, mockResponse);
878-
879-
expect(mockResponse.writeHead).toHaveBeenCalledWith(
880-
200,
881-
expect.objectContaining({
882-
...customHeaders,
883-
"Content-Type": "text/event-stream",
884-
"Cache-Control": "no-cache",
885-
"Connection": "keep-alive",
886-
"mcp-session-id": transportWithHeaders.sessionId
887-
})
888-
);
889-
});
890-
891-
it("should include custom headers in error responses", async () => {
892-
const req = createMockRequest({
893-
method: "GET",
894-
headers: {
895-
accept: "application/json, text/event-stream",
896-
"mcp-session-id": "invalid-session-id"
897-
},
898-
});
899-
900-
await transportWithHeaders.handleRequest(req, mockResponse);
901-
902-
expect(mockResponse.writeHead).toHaveBeenCalledWith(
903-
404,
904-
expect.objectContaining(customHeaders)
905-
);
906-
});
907-
908-
it("should not override essential headers with custom headers", async () => {
909-
const transportWithConflictingHeaders = new StreamableHTTPServerTransport({
910-
sessionId: randomUUID(),
911-
customHeaders: {
912-
"Content-Type": "text/plain",
913-
"X-Custom-Header": "custom-value"
914-
}
915-
});
916-
917-
const req = createMockRequest({
918-
method: "POST",
919-
headers: {
920-
"content-type": "application/json",
921-
accept: "application/json, text/event-stream",
922-
"mcp-session-id": transportWithConflictingHeaders.sessionId
923-
},
924-
body: JSON.stringify({
925-
jsonrpc: "2.0",
926-
method: "test",
927-
params: {},
928-
id: "test-conflict-id"
929-
})
930-
});
931-
932-
await transportWithConflictingHeaders.handleRequest(req, mockResponse);
933-
934-
expect(mockResponse.writeHead).toHaveBeenCalledWith(
935-
200,
936-
expect.objectContaining({
937-
"Content-Type": "text/event-stream",
938-
"X-Custom-Header": "custom-value"
939-
})
940-
);
941-
});
942-
943-
it("should work with empty custom headers", async () => {
944-
const transportWithoutHeaders = new StreamableHTTPServerTransport({
945-
sessionId: randomUUID(),
946-
});
947-
948-
const req = createMockRequest({
949-
method: "POST",
950-
headers: {
951-
"content-type": "application/json",
952-
accept: "application/json, text/event-stream",
953-
"mcp-session-id": transportWithoutHeaders.sessionId
954-
},
955-
body: JSON.stringify({
956-
jsonrpc: "2.0",
957-
method: "test",
958-
params: {},
959-
id: "test-empty-headers-id"
960-
})
961-
});
962-
963-
await transportWithoutHeaders.handleRequest(req, mockResponse);
964-
965-
expect(mockResponse.writeHead).toHaveBeenCalledWith(
966-
200,
967-
expect.objectContaining({
968-
"Content-Type": "text/event-stream",
969-
"Cache-Control": "no-cache",
970-
"Connection": "keep-alive",
971-
"mcp-session-id": transportWithoutHeaders.sessionId
972-
})
973-
);
974-
});
975-
});
976845
});

src/server/streamableHttp.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ export interface StreamableHTTPServerTransportOptions {
1313
/**
1414
* The session ID SHOULD be globally unique and cryptographically secure (e.g., a securely generated UUID, a JWT, or a cryptographic hash)
1515
*
16-
* When sessionId is not set, the transport will be in stateless mode.
16+
* When there is no sessionId, the transport will not perform session management.
1717
*/
1818
sessionId: string | undefined;
1919

20-
/**
21-
* Custom headers to be included in all responses
22-
* These headers will be added to both SSE and regular HTTP responses
23-
*/
24-
customHeaders?: Record<string, string>;
20+
2521

2622
}
2723

@@ -63,7 +59,6 @@ export class StreamableHTTPServerTransport implements Transport {
6359
// when sessionId is not set (undefined), it means the transport is in stateless mode
6460
private _sessionId: string | undefined;
6561
private _started: boolean = false;
66-
private _customHeaders: Record<string, string>;
6762
private _sseResponseMapping: Map<RequestId, ServerResponse> = new Map();
6863

6964
onclose?: () => void;
@@ -72,7 +67,6 @@ export class StreamableHTTPServerTransport implements Transport {
7267

7368
constructor(options: StreamableHTTPServerTransportOptions) {
7469
this._sessionId = options.sessionId;
75-
this._customHeaders = options.customHeaders || {};
7670
}
7771

7872
/**
@@ -97,7 +91,7 @@ export class StreamableHTTPServerTransport implements Transport {
9791
} else if (req.method === "DELETE") {
9892
await this.handleDeleteRequest(req, res);
9993
} else {
100-
res.writeHead(405, this._customHeaders).end(JSON.stringify({
94+
res.writeHead(405).end(JSON.stringify({
10195
jsonrpc: "2.0",
10296
error: {
10397
code: -32000,
@@ -120,7 +114,6 @@ export class StreamableHTTPServerTransport implements Transport {
120114
}
121115

122116
res.writeHead(405, {
123-
...this._customHeaders,
124117
"Allow": "POST, DELETE"
125118
}).end(JSON.stringify({
126119
jsonrpc: "2.0",
@@ -193,9 +186,7 @@ export class StreamableHTTPServerTransport implements Transport {
193186
msg => 'method' in msg && msg.method === 'initialize'
194187
);
195188
if (isInitializationRequest) {
196-
const headers: Record<string, string> = {
197-
...this._customHeaders
198-
};
189+
const headers: Record<string, string> = {};
199190

200191
if (this._sessionId !== undefined) {
201192
headers["mcp-session-id"] = this._sessionId;
@@ -232,14 +223,12 @@ export class StreamableHTTPServerTransport implements Transport {
232223
}
233224
} else if (hasRequests) {
234225
const headers: Record<string, string> = {
235-
...this._customHeaders,
236226
"Content-Type": "text/event-stream",
237227
"Cache-Control": "no-cache",
238228
Connection: "keep-alive",
239229
};
240230

241-
// For initialization requests, always include the session ID if we have one
242-
// even if we're in stateless mode
231+
// After initialization, always include the session ID if we have one
243232
if (this._sessionId !== undefined) {
244233
headers["mcp-session-id"] = this._sessionId;
245234
}
@@ -296,7 +285,7 @@ export class StreamableHTTPServerTransport implements Transport {
296285

297286
if (!sessionId) {
298287
// Non-initialization requests without a session ID should return 400 Bad Request
299-
res.writeHead(400, this._customHeaders).end(JSON.stringify({
288+
res.writeHead(400).end(JSON.stringify({
300289
jsonrpc: "2.0",
301290
error: {
302291
code: -32000,
@@ -307,7 +296,7 @@ export class StreamableHTTPServerTransport implements Transport {
307296
return false;
308297
} else if ((Array.isArray(sessionId) ? sessionId[0] : sessionId) !== this._sessionId) {
309298
// Reject requests with invalid session ID with 404 Not Found
310-
res.writeHead(404, this._customHeaders).end(JSON.stringify({
299+
res.writeHead(404).end(JSON.stringify({
311300
jsonrpc: "2.0",
312301
error: {
313302
code: -32001,

0 commit comments

Comments
 (0)