Fixes #12841: Plumb headers through google_credentials transport crea… (#12849)

Co-authored-by: David McWherter <davidmcw@gmail.com>
This commit is contained in:
davidmcwherter
2025-11-10 17:06:31 -05:00
committed by GitHub
parent 37ca643a64
commit 4af4f8644d
2 changed files with 34 additions and 12 deletions

View File

@@ -501,6 +501,9 @@ describe('mcp-client', () => {
oauth: {
scopes: ['scope1'],
},
headers: {
'X-Goog-User-Project': 'myproject',
},
},
false,
);
@@ -509,6 +512,11 @@ describe('mcp-client', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const authProvider = (transport as any)._authProvider;
expect(authProvider).toBeInstanceOf(GoogleCredentialProvider);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const googUserProject = (transport as any)._requestInit?.headers?.[
'X-Goog-User-Project'
];
expect(googUserProject).toBe('myproject');
});
it('should use GoogleCredentialProvider with SSE transport', async () => {

View File

@@ -394,6 +394,24 @@ async function handleAutomaticOAuth(
}
}
/**
* Create RequestInit for TransportOptions.
*
* @param mcpServerConfig The MCP server configuration
* @param headers Additional headers
*/
function createTransportRequestInit(
mcpServerConfig: MCPServerConfig,
headers: Record<string, string>,
): RequestInit {
return {
headers: {
...mcpServerConfig.headers,
...headers,
},
};
}
/**
* Create a transport with OAuth token for the given server configuration.
*
@@ -411,12 +429,9 @@ async function createTransportWithOAuth(
if (mcpServerConfig.httpUrl) {
// Create HTTP transport with OAuth token
const oauthTransportOptions: StreamableHTTPClientTransportOptions = {
requestInit: {
headers: {
...mcpServerConfig.headers,
Authorization: `Bearer ${accessToken}`,
},
},
requestInit: createTransportRequestInit(mcpServerConfig, {
Authorization: `Bearer ${accessToken}`,
}),
};
return new StreamableHTTPClientTransport(
@@ -426,12 +441,9 @@ async function createTransportWithOAuth(
} else if (mcpServerConfig.url) {
// Create SSE transport with OAuth token in Authorization header
return new SSEClientTransport(new URL(mcpServerConfig.url), {
requestInit: {
headers: {
...mcpServerConfig.headers,
Authorization: `Bearer ${accessToken}`,
},
},
requestInit: createTransportRequestInit(mcpServerConfig, {
Authorization: `Bearer ${accessToken}`,
}),
});
}
@@ -1170,6 +1182,7 @@ export async function createTransport(
const transportOptions:
| StreamableHTTPClientTransportOptions
| SSEClientTransportOptions = {
requestInit: createTransportRequestInit(mcpServerConfig, {}),
authProvider: provider,
};
@@ -1197,6 +1210,7 @@ export async function createTransport(
const transportOptions:
| StreamableHTTPClientTransportOptions
| SSEClientTransportOptions = {
requestInit: createTransportRequestInit(mcpServerConfig, {}),
authProvider: provider,
};
if (mcpServerConfig.httpUrl) {