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
78 changes: 78 additions & 0 deletions common/src/mcp/__tests__/client-headers.test.ts
Original file line number Diff line number Diff line change
@@ -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<void>((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<void>((resolve) => http.close(() => resolve()))
}
}, 10_000)
8 changes: 8 additions & 0 deletions common/src/mcp/client.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -66,13 +68,19 @@ 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') {
return JSON.stringify({
type: 'sse',
url: config.url,
params: config.params,
headers: createHash('sha256')
.update(JSON.stringify(substituteEnvInRecord(config.headers)))
.digest('hex'),
})
}
config.type satisfies never
Expand Down
Loading