Skip to content
Open
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/tangy-laws-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server": patch
---

fix(server): validate exact Accept media types
27 changes: 23 additions & 4 deletions packages/server/src/server/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isJSONRPCRequest,
isJSONRPCResultResponse,
JSONRPCMessageSchema,
mediaTypeEssence,
SUPPORTED_PROTOCOL_VERSIONS
} from '@modelcontextprotocol/core-internal';

Expand Down Expand Up @@ -256,6 +257,23 @@ export interface HandleRequestOptions {
* };
* ```
*/

/**
* Checks whether an Accept header lists an exact media type.
*
* Commas inside quoted parameter values are not list separators. Parse each
* media range with the shared media-type parser so parameters and casing are
* handled consistently with Content-Type validation.
*/
function acceptsMediaType(acceptHeader: string | null, expected: string): boolean {
if (!acceptHeader) {
return false;
}

const mediaRanges = acceptHeader.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/);
return mediaRanges.some(range => mediaTypeEssence(range) === expected);
}

export class WebStandardStreamableHTTPServerTransport implements Transport {
// when sessionId is not set (undefined), it means the transport is in stateless mode
private sessionIdGenerator: (() => string) | undefined;
Expand Down Expand Up @@ -506,7 +524,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
private async handleGetRequest(req: Request): Promise<Response> {
// The client MUST include an Accept header, listing text/event-stream as a supported content type.
const acceptHeader = req.headers.get('accept');
if (!acceptHeader?.includes('text/event-stream')) {
if (!acceptsMediaType(acceptHeader, 'text/event-stream')) {
this.onerror?.(new Error('Not Acceptable: Client must accept text/event-stream'));
return this.createJsonErrorResponse(406, -32_000, 'Not Acceptable: Client must accept text/event-stream');
}
Expand Down Expand Up @@ -785,9 +803,10 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
// Validate the Accept header
const acceptHeader = req.headers.get('accept');
// The client MUST include an Accept header, listing both application/json and text/event-stream as supported content types.
// Accept is a comma-separated list, so a substring check is the intended semantics here (unlike Content-Type below).
// eslint-disable-next-line no-restricted-syntax
if (!acceptHeader?.includes('application/json') || !acceptHeader.includes('text/event-stream')) {
if (
!acceptsMediaType(acceptHeader, 'application/json') ||
!acceptsMediaType(acceptHeader, 'text/event-stream')
) {
this.onerror?.(new Error('Not Acceptable: Client must accept both application/json and text/event-stream'));
return this.createJsonErrorResponse(
406,
Expand Down
47 changes: 47 additions & 0 deletions packages/server/test/server/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,26 @@ describe('Zod v4', () => {
expectErrorResponse(errorData, -32_000, /Not Acceptable/);
});

it('should reject Accept values that only contain required media types as substrings', async () => {
const request = createRequest('POST', TEST_MESSAGES.initialize, {
accept: 'application/jsonx, text/event-stream-bogus'
});
const response = await transport.handleRequest(request);

expect(response.status).toBe(406);
const errorData = await response.json();
expectErrorResponse(errorData, -32_000, /Not Acceptable/);
});

it('should accept case-insensitive media types with parameters', async () => {
const request = createRequest('POST', TEST_MESSAGES.initialize, {
accept: 'Application/JSON; q=0.9, Text/Event-Stream; charset=utf-8'
});
const response = await transport.handleRequest(request);

expect(response.status).toBe(200);
});

it('should reject request with wrong Content-Type header', async () => {
const request = new Request('http://localhost/mcp', {
method: 'POST',
Expand Down Expand Up @@ -371,6 +391,33 @@ describe('Zod v4', () => {
expectErrorResponse(errorData, -32_000, /Not Acceptable/);
});

it('should reject GET Accept values that only contain text/event-stream as a substring', async () => {
sessionId = await initializeServer();

const request = createRequest('GET', undefined, {
sessionId,
accept: 'text/event-stream-bogus'
});
const response = await transport.handleRequest(request);

expect(response.status).toBe(406);
const errorData = await response.json();
expectErrorResponse(errorData, -32_000, /Not Acceptable/);
});

it('should accept a case-insensitive parameterized SSE media type', async () => {
sessionId = await initializeServer();

const request = createRequest('GET', undefined, {
sessionId,
accept: 'Text/Event-Stream; charset=utf-8'
});
const response = await transport.handleRequest(request);

expect(response.status).toBe(200);
expect(response.headers.get('content-type')).toBe('text/event-stream');
});

it('should reject second standalone SSE stream', async () => {
sessionId = await initializeServer();

Expand Down
Loading