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/stateless-405-allow-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/server': patch
---

fix(server): set `Allow: POST` on legacy stateless 405 responses for non-POST requests (RFC 9110).
18 changes: 17 additions & 1 deletion packages/server/src/server/createMcpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,23 @@ function createLegacyStatelessFallback(
): LegacyHttpHandler {
return async (request, options) => {
if (request.method.toUpperCase() !== 'POST') {
return jsonRpcErrorResponse(405, -32_000, 'Method not allowed.');
// RFC 9110 §15.5.6 requires Allow on 405. Stateless serving only accepts POST.
return Response.json(
{
jsonrpc: '2.0',
error: {
code: -32_000,
message: 'Method not allowed.'
},
id: null
},
{
status: 405,
headers: {
Allow: 'POST'
}
}
);
}
try {
const product = await factory({
Expand Down
1 change: 1 addition & 0 deletions packages/server/test/server/createMcpHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ describe('createMcpHandler — stateless legacy fallback (the default)', () => {
for (const method of ['GET', 'DELETE']) {
const response = await handler.fetch(new Request('http://localhost/mcp', { method }));
expect(response.status).toBe(405);
expect(response.headers.get('Allow')).toBe('POST');
const body = (await response.json()) as JSONRPCErrorBody;
expect(body.error.code).toBe(-32_000);
expect(body.error.message).toBe('Method not allowed.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('legacyStatelessFallback', () => {
for (const method of ['GET', 'DELETE']) {
const response = await handler(new Request('http://localhost/mcp', { method }));
expect(response.status).toBe(405);
expect(response.headers.get('Allow')).toBe('POST');
const body = (await response.json()) as JSONRPCErrorBody;
expect(body.error.code).toBe(-32_000);
expect(body.error.message).toBe('Method not allowed.');
Expand Down
Loading