From 0b899cf0869dcdb0234671ec385ac99fd6b20a95 Mon Sep 17 00:00:00 2001 From: VIOLETee0 <1842771550@qq.com> Date: Sat, 28 Nov 2026 02:31:01 +0800 Subject: [PATCH] Separate MCP clients by resolved request headers --- .../src/mcp/__tests__/client-headers.test.ts | 78 +++++++++++++++++++ common/src/mcp/client.ts | 8 ++ 2 files changed, 86 insertions(+) create mode 100644 common/src/mcp/__tests__/client-headers.test.ts diff --git a/common/src/mcp/__tests__/client-headers.test.ts b/common/src/mcp/__tests__/client-headers.test.ts new file mode 100644 index 0000000000..6b2d96b787 --- /dev/null +++ b/common/src/mcp/__tests__/client-headers.test.ts @@ -0,0 +1,78 @@ +import { createServer } from 'node:http' + +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' +import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js' +import { expect, test } from 'bun:test' + +import { getMCPClient, listMCPTools } from '../client' + +/** + * Uses a real loopback MCP transport to detect reuse of another identity's + * catalog. HTTP initialization and shutdown can take over a second. + */ +test('separates remote clients by their authorization headers', async () => { + const http = createServer(async (req, res) => { + if (req.method !== 'POST') { + res.writeHead(405).end() + return + } + const identity = + req.headers.authorization === 'Bearer fixture-alpha' + ? 'alpha' + : req.headers.authorization === 'Bearer fixture-beta' + ? 'beta' + : 'unknown' + const rpc = new McpServer({ name: 'header-identity', version: '1.0.0' }) + rpc.registerTool(`who_${identity}`, { inputSchema: {} }, async () => ({ + content: [{ type: 'text', text: identity }], + })) + const transport = new StreamableHTTPServerTransport({ + sessionIdGenerator: undefined, + enableJsonResponse: true, + }) + res.on('close', () => { + void rpc.close() + }) + try { + await rpc.connect(transport) + await transport.handleRequest(req, res) + } catch (error) { + if (!res.headersSent) res.writeHead(500).end(String(error)) + } + }) + + try { + await new Promise((resolve, reject) => { + http.once('error', reject) + http.listen(0, '127.0.0.1', resolve) + }) + const address = http.address() + if (!address || typeof address === 'string') throw new Error('Missing port') + const base = { + type: 'http' as const, + url: `http://127.0.0.1:${address.port}/mcp`, + params: {}, + } + const alphaConfig = { + ...base, + headers: { Authorization: 'Bearer fixture-alpha' }, + } + const alphaId = await getMCPClient(alphaConfig) + const alpha = await listMCPTools(alphaId) + const betaId = await getMCPClient({ + ...base, + headers: { Authorization: 'Bearer fixture-beta' }, + }) + const beta = await listMCPTools(betaId) + + expect(beta.tools.map((tool) => tool.name)).toEqual(['who_beta']) + expect(alpha.tools.map((tool) => tool.name)).toEqual(['who_alpha']) + expect(betaId).not.toBe(alphaId) + expect(await getMCPClient(alphaConfig)).toBe(alphaId) + expect(alphaId).not.toContain('fixture-alpha') + expect(betaId).not.toContain('fixture-beta') + } finally { + http.closeAllConnections() + await new Promise((resolve) => http.close(() => resolve())) + } +}, 10_000) diff --git a/common/src/mcp/client.ts b/common/src/mcp/client.ts index 1c2c986fd6..2ca705ebe1 100644 --- a/common/src/mcp/client.ts +++ b/common/src/mcp/client.ts @@ -1,3 +1,5 @@ +import { createHash } from 'node:crypto' + import { Client } from '@modelcontextprotocol/sdk/client/index.js' import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js' import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js' @@ -66,6 +68,9 @@ function hashConfig(config: MCPConfig): string { type: 'http', url: config.url, params: config.params, + headers: createHash('sha256') + .update(JSON.stringify(substituteEnvInRecord(config.headers))) + .digest('hex'), }) } if (config.type === 'sse') { @@ -73,6 +78,9 @@ function hashConfig(config: MCPConfig): string { type: 'sse', url: config.url, params: config.params, + headers: createHash('sha256') + .update(JSON.stringify(substituteEnvInRecord(config.headers))) + .digest('hex'), }) } config.type satisfies never