Skip to content

Commit fdae89e

Browse files
fix: use X-MCP-Proxy-Auth header for proxy authentication
Changed proxy authentication to use a custom header (X-MCP-Proxy-Auth) instead of the standard Authorization header. This prevents conflicts when the proxy needs to forward the client's Authorization header to upstream MCP servers. - Updated client to send proxy auth token in X-MCP-Proxy-Auth header - Updated server auth middleware to check X-MCP-Proxy-Auth header - Applies to all transport types (SSE, stdio, streamable-http) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 65910eb commit fdae89e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

client/src/lib/hooks/useConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export function useConnection({
246246
const proxyAuthToken = getMCPProxyAuthToken(config);
247247
const headers: HeadersInit = {};
248248
if (proxyAuthToken) {
249-
headers["Authorization"] = `Bearer ${proxyAuthToken}`;
249+
headers["X-MCP-Proxy-Auth"] = `Bearer ${proxyAuthToken}`;
250250
}
251251
const proxyHealthResponse = await fetch(proxyHealthUrl, { headers });
252252
const proxyHealth = await proxyHealthResponse.json();
@@ -335,7 +335,7 @@ export function useConnection({
335335
const proxyAuthToken = getMCPProxyAuthToken(config);
336336
const proxyHeaders: HeadersInit = {};
337337
if (proxyAuthToken) {
338-
proxyHeaders["Authorization"] = `Bearer ${proxyAuthToken}`;
338+
proxyHeaders["X-MCP-Proxy-Auth"] = `Bearer ${proxyAuthToken}`;
339339
}
340340

341341
// Create appropriate transport

server/src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,17 @@ const authMiddleware = (
138138
});
139139
};
140140

141-
const authHeader = req.headers.authorization;
142-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
141+
const authHeader = req.headers["x-mcp-proxy-auth"];
142+
const authHeaderValue = Array.isArray(authHeader)
143+
? authHeader[0]
144+
: authHeader;
145+
146+
if (!authHeaderValue || !authHeaderValue.startsWith("Bearer ")) {
143147
sendUnauthorized();
144148
return;
145149
}
146150

147-
const providedToken = authHeader.substring(7); // Remove 'Bearer ' prefix
151+
const providedToken = authHeaderValue.substring(7); // Remove 'Bearer ' prefix
148152
const expectedToken = sessionToken;
149153

150154
// Convert to buffers for timing-safe comparison
@@ -196,7 +200,9 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
196200

197201
const headers = getHttpHeaders(req, transportType);
198202

199-
console.log(`SSE transport: url=${url}, headers=${Object.keys(headers)}`);
203+
console.log(
204+
`SSE transport: url=${url}, headers=${JSON.stringify(headers)}`,
205+
);
200206

201207
const transport = new SSEClientTransport(new URL(url), {
202208
eventSourceInit: {

0 commit comments

Comments
 (0)