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/fresh-headers-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/client": patch
---

Send MCP standard headers on Streamable HTTP POST requests.
20 changes: 20 additions & 0 deletions packages/client/src/client/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,25 @@
maxStepUpRetries?: number;
};

function setMcpStandardPostHeaders(headers: Headers, message: JSONRPCMessage | JSONRPCMessage[]): void {
if (Array.isArray(message) || !('method' in message) || typeof message.method !== 'string') {
return;
}

headers.set('Mcp-Method', message.method);

const params = 'params' in message ? message.params : undefined;
if (!params || typeof params !== 'object' || Array.isArray(params)) {
return;
}

const requestParams = params as { name?: unknown; uri?: unknown };
const mcpName = typeof requestParams.name === 'string' ? requestParams.name : requestParams.uri;
if (typeof mcpName === 'string') {
headers.set('Mcp-Name', mcpName);
}
}

/**
* Standard/auth header names the transport owns. The per-request
* `TransportSendOptions.headers` carrier MUST NOT be able to override these —
Expand Down Expand Up @@ -372,7 +391,7 @@
stepUpRetries: number
): Promise<'AUTHORIZED' | 'REDIRECT'> {
if (this._onInsufficientScope === 'throw') {
throw new InsufficientScopeError({

Check failure on line 394 in packages/client/src/client/streamableHttp.ts

View workflow job for this annotation

GitHub Actions / test-e2e (22)

scenarios/hosting-entry-auth.test.ts > typescript:hosting:entry:auth:insufficient-scope-403 [entryStateless 2025-11-25] > verifies

InsufficientScopeError: Insufficient scope: required "files:write" ❯ StreamableHTTPClientTransport._stepUpAuthorize ../../packages/client/src/client/streamableHttp.ts:394:19 ❯ StreamableHTTPClientTransport._send ../../packages/client/src/client/streamableHttp.ts:1045:51 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { requiredScope: 'files:write', resourceMetadataUrl: undefined, errorDescription: 'files:write required for this operation' }

Check failure on line 394 in packages/client/src/client/streamableHttp.ts

View workflow job for this annotation

GitHub Actions / test-e2e (20)

scenarios/hosting-entry-auth.test.ts > typescript:hosting:entry:auth:insufficient-scope-403 [entryStateless 2025-11-25] > verifies

InsufficientScopeError: Insufficient scope: required "files:write" ❯ StreamableHTTPClientTransport._stepUpAuthorize ../../packages/client/src/client/streamableHttp.ts:394:19 ❯ StreamableHTTPClientTransport._send ../../packages/client/src/client/streamableHttp.ts:1045:51 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { requiredScope: 'files:write', resourceMetadataUrl: undefined, errorDescription: 'files:write required for this operation' }

Check failure on line 394 in packages/client/src/client/streamableHttp.ts

View workflow job for this annotation

GitHub Actions / test-e2e (24)

scenarios/hosting-entry-auth.test.ts > typescript:hosting:entry:auth:insufficient-scope-403 [entryStateless 2025-11-25] > verifies

InsufficientScopeError: Insufficient scope: required "files:write" ❯ StreamableHTTPClientTransport._stepUpAuthorize ../../packages/client/src/client/streamableHttp.ts:394:19 ❯ StreamableHTTPClientTransport._send ../../packages/client/src/client/streamableHttp.ts:1045:51 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { requiredScope: 'files:write', resourceMetadataUrl: undefined, errorDescription: 'files:write required for this operation' }
requiredScope: challenge.scope,
resourceMetadataUrl: challenge.resourceMetadataUrl,
errorDescription: challenge.errorDescription
Expand Down Expand Up @@ -959,6 +978,7 @@
const userAccept = headers.get('accept');
const types = [...(userAccept?.split(',').map(s => s.trim().toLowerCase()) ?? []), 'application/json', 'text/event-stream'];
headers.set('accept', [...new Set(types)].join(', '));
setMcpStandardPostHeaders(headers, message);

// Per-request abort: when the caller supplies a request-scoped
// signal (the `subscriptions/listen` driver), aborting it cancels
Expand Down
110 changes: 110 additions & 0 deletions packages/client/test/client/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,112 @@ describe('StreamableHTTPClientTransport', () => {
);
});

it('should include MCP standard headers on POST requests', async () => {
const message: JSONRPCMessage = {
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: 'read_file',
arguments: { path: 'README.md' }
},
id: 'test-id'
};

(globalThis.fetch as Mock).mockResolvedValueOnce({
ok: true,
status: 202,
headers: new Headers()
});

await transport.send(message);

const headers = (globalThis.fetch as Mock).mock.calls.at(-1)![1].headers as Headers;
expect(headers.get('mcp-method')).toBe('tools/call');
expect(headers.get('mcp-name')).toBe('read_file');
});

it('should use params.uri for the MCP name header', async () => {
const message: JSONRPCMessage = {
jsonrpc: '2.0',
method: 'resources/read',
params: {
uri: 'file:///README.md'
},
id: 'test-id'
};

(globalThis.fetch as Mock).mockResolvedValueOnce({
ok: true,
status: 202,
headers: new Headers()
});

await transport.send(message);

const headers = (globalThis.fetch as Mock).mock.calls.at(-1)![1].headers as Headers;
expect(headers.get('mcp-method')).toBe('resources/read');
expect(headers.get('mcp-name')).toBe('file:///README.md');
});

it('should include the MCP method header for notifications', async () => {
const message: JSONRPCMessage = {
jsonrpc: '2.0',
method: 'notifications/initialized'
};

(globalThis.fetch as Mock).mockResolvedValueOnce({
ok: true,
status: 202,
headers: new Headers()
});

await transport.send(message);

const headers = (globalThis.fetch as Mock).mock.calls.at(-1)![1].headers as Headers;
expect(headers.get('mcp-method')).toBe('notifications/initialized');
expect(headers.get('mcp-name')).toBeNull();
});

it('should not include MCP method headers for JSON-RPC responses', async () => {
const message: JSONRPCMessage = {
jsonrpc: '2.0',
id: 'test-id',
result: {}
};

(globalThis.fetch as Mock).mockResolvedValueOnce({
ok: true,
status: 202,
headers: new Headers()
});

await transport.send(message);

const headers = (globalThis.fetch as Mock).mock.calls.at(-1)![1].headers as Headers;
expect(headers.get('mcp-method')).toBeNull();
expect(headers.get('mcp-name')).toBeNull();
});

it('should omit the MCP name header when params are absent', async () => {
const message: JSONRPCMessage = {
jsonrpc: '2.0',
method: 'initialize',
id: 'test-id'
};

(globalThis.fetch as Mock).mockResolvedValueOnce({
ok: true,
status: 202,
headers: new Headers()
});

await transport.send(message);

const headers = (globalThis.fetch as Mock).mock.calls.at(-1)![1].headers as Headers;
expect(headers.get('mcp-method')).toBe('initialize');
expect(headers.get('mcp-name')).toBeNull();
});

it('should send batch messages', async () => {
const messages: JSONRPCMessage[] = [
{ jsonrpc: '2.0', method: 'test1', params: {}, id: 'id1' },
Expand All @@ -85,6 +191,10 @@ describe('StreamableHTTPClientTransport', () => {
body: JSON.stringify(messages)
})
);

const headers = (globalThis.fetch as Mock).mock.calls.at(-1)![1].headers as Headers;
expect(headers.get('mcp-method')).toBeNull();
expect(headers.get('mcp-name')).toBeNull();
});

it('should store session ID received during initialization', async () => {
Expand Down
Loading