Skip to content
Closed
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
20 changes: 20 additions & 0 deletions packages/core/src/tools/mcp-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,26 @@ describe('mcp-client', () => {
vi.unstubAllGlobals();
}
});

it('encodes non-ByteString header values for httpUrl transports', async () => {
const transport = await createTransport(
'test-server',
{
httpUrl: 'http://test-server',
headers: { 'X-Custom': 'mąka' },
},
false,
MOCK_CONTEXT,
);

const headers = (unwrap(transport) as TestableTransport)._requestInit
?.headers;

expect(() => new Headers(headers)).not.toThrow();
expect(headers?.['X-Custom']).toBe(
Buffer.from('mąka', 'utf8').toString('latin1'),
);
});
});

describe('should connect via url', () => {
Expand Down
30 changes: 26 additions & 4 deletions packages/core/src/tools/mcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,13 +1002,35 @@ function createTransportRequestInit(
}

return {
headers: {
headers: encodeHeaderValuesForFetch({
...expandedHeaders,
...headers,
},
}),
};
}

function encodeHeaderValuesForFetch(
headers: Record<string, string>,
): Record<string, string> {
return Object.fromEntries(
Object.entries(headers).map(([key, value]) => [
key,
isByteString(value)
? value
: Buffer.from(value, 'utf8').toString('latin1'),
]),
);
}

function isByteString(value: string): boolean {
for (const char of value) {
if (char.codePointAt(0)! > 255) {
return false;
}
}
return true;
}

/**
* Create an AuthProvider for the MCP Transport.
*
Expand Down Expand Up @@ -1673,10 +1695,10 @@ function createSSETransportWithAuth(
config: MCPServerConfig,
accessToken?: string | null,
): SSEClientTransport {
const headers = {
const headers = encodeHeaderValuesForFetch({
...config.headers,
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
};
});

const options: SSEClientTransportOptions = {};
if (Object.keys(headers).length > 0) {
Expand Down
Loading