-
Notifications
You must be signed in to change notification settings - Fork 10
feat(mcp server): add support for oauth authentication #1439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,59 @@ import { MultiServerMCPClient } from '@langchain/mcp-adapters'; | |
| import { McpConnectionError } from './types/errors'; | ||
| import McpServerRemoteTool from './types/mcp-server-remote-tool'; | ||
|
|
||
| export type McpAuthenticationType = 'none' | 'bearer' | 'oauth2'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dead code |
||
|
|
||
| export type McpServerConfig = MultiServerMCPClient['config']['mcpServers'][string]; | ||
|
|
||
| export type McpConfiguration = { | ||
| configs: MultiServerMCPClient['config']['mcpServers']; | ||
| authenticationTypes?: Record<string, McpAuthenticationType>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to remove |
||
| } & Omit<MultiServerMCPClient['config'], 'mcpServers'>; | ||
|
|
||
| /** | ||
| * Injects the OAuth token as Authorization header into HTTP-based transport configurations. | ||
| * For stdio transports, returns the config unchanged. | ||
| */ | ||
| export function injectOauthToken(serverConfig: McpServerConfig, token?: string): McpServerConfig { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move both functions into an dedicated file:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't forget to export it |
||
| if (!token) return serverConfig; | ||
|
|
||
| // Only inject token for HTTP-based transports (sse, http) | ||
| if (serverConfig.type === 'http' || serverConfig.type === 'sse') { | ||
| const { oauthConfig, ...headers } = serverConfig.headers || {}; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is oauthConfig ? |
||
|
|
||
| return { | ||
| ...serverConfig, | ||
| headers: { | ||
| ...headers, | ||
| Authorization: token, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| return serverConfig; | ||
| } | ||
|
|
||
| /** | ||
| * Injects OAuth tokens into all server configurations. | ||
| * Returns a new McpConfiguration with tokens injected, or undefined if no configs provided. | ||
| */ | ||
| export function injectOauthTokens( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| mcpConfigs: McpConfiguration | undefined, | ||
| mcpOAuthTokens: Record<string, string> | undefined, | ||
| ): McpConfiguration | undefined { | ||
| if (!mcpConfigs) return undefined; | ||
| if (!mcpOAuthTokens) return mcpConfigs; | ||
|
|
||
| const configsWithTokens = Object.fromEntries( | ||
| Object.entries(mcpConfigs.configs).map(([name, serverConfig]) => [ | ||
| name, | ||
| injectOauthToken(serverConfig, mcpOAuthTokens[name]), | ||
| ]), | ||
| ); | ||
|
|
||
| return { ...mcpConfigs, configs: configsWithTokens }; | ||
| } | ||
|
|
||
| export default class McpClient { | ||
| private readonly mcpClients: Record<string, MultiServerMCPClient> = {}; | ||
| private readonly logger?: Logger; | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we do the same in the server side, we could also add this function in an util in the ai-proxy util