mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-04-03 14:27:20 +02:00
🔑 fix: Robust MCP OAuth Detection in Tool-Call Flow (#12418)
* fix(api): add buildOAuthToolCallName utility for MCP OAuth flows
Extract a shared utility that builds the synthetic tool-call name
used during MCP OAuth flows (oauth_mcp_{normalizedServerName}).
Uses startsWith on the raw serverName (not the normalized form) to
guard against double-wrapping, so names that merely normalize to
start with oauth_mcp_ (e.g., oauth@mcp@server) are correctly
prefixed while genuinely pre-wrapped names are left as-is.
Add 8 unit tests covering normal names, pre-wrapped names, _mcp_
substrings, special characters, non-ASCII, and empty string inputs.
* fix(backend): use buildOAuthToolCallName in MCP OAuth flows
Replace inline tool-call name construction in both reconnectServer
(MCP.js) and createOAuthEmitter (ToolService.js) with the shared
buildOAuthToolCallName utility. Remove unused normalizeServerName
import from ToolService.js. Fix import ordering in both files.
This ensures the oauth_mcp_ prefix is consistently applied so the
client correctly identifies MCP OAuth flows and binds the CSRF
cookie to the right server.
* fix(client): robust MCP OAuth detection and split handling in ToolCall
- Fix split() destructuring to preserve tail segments for server names
containing _mcp_ (e.g., foo_mcp_bar no longer truncated to foo).
- Add auth URL redirect_uri fallback: when the tool-call name lacks
the _mcp_ delimiter, parse redirect_uri for the MCP callback path.
Set function_name to the extracted server name so progress text
shows the server, not the raw tool-call ID.
- Display server name instead of literal "oauth" as function_name,
gated on auth presence to avoid misidentifying real tools named
"oauth".
- Consolidate three independent new URL(auth) parses into a single
parsedAuthUrl useMemo shared across detection, actionId, and
authDomain hooks.
- Replace any type on ProgressText test mock with structural type.
- Add 8 tests covering delimiter detection, multi-segment names,
function_name display, redirect_uri fallback, normalized _mcp_
server names, and non-MCP action auth exclusion.
* chore: fix import order in utils.test.ts
* fix(client): drop auth gate on OAuth displayName so completed flows show server name
The createOAuthEnd handler re-emits the toolCall delta without auth,
so auth is cleared on the client after OAuth completes. Gating
displayName on `func === 'oauth' && auth` caused completed OAuth
steps to render "Completed oauth" instead of "Completed my-server".
Remove the `&& auth` gate — within the MCP delimiter branch the
func="oauth" check alone is sufficient. Also remove `auth` from the
useMemo dep array since only `parsedAuthUrl` is referenced. Update
the test to assert correct post-completion display.
This commit is contained in:
parent
359cc63b41
commit
8e2721011e
6 changed files with 255 additions and 33 deletions
|
|
@ -1,4 +1,9 @@
|
|||
import { normalizeServerName, redactServerSecrets, redactAllServerSecrets } from '~/mcp/utils';
|
||||
import {
|
||||
buildOAuthToolCallName,
|
||||
normalizeServerName,
|
||||
redactAllServerSecrets,
|
||||
redactServerSecrets,
|
||||
} from '~/mcp/utils';
|
||||
import type { ParsedServerConfig } from '~/mcp/types';
|
||||
|
||||
describe('normalizeServerName', () => {
|
||||
|
|
@ -28,6 +33,49 @@ describe('normalizeServerName', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('buildOAuthToolCallName', () => {
|
||||
it('should prefix a simple server name with oauth_mcp_', () => {
|
||||
expect(buildOAuthToolCallName('my-server')).toBe('oauth_mcp_my-server');
|
||||
});
|
||||
|
||||
it('should not double-wrap a name that already starts with oauth_mcp_', () => {
|
||||
expect(buildOAuthToolCallName('oauth_mcp_my-server')).toBe('oauth_mcp_my-server');
|
||||
});
|
||||
|
||||
it('should correctly handle server names containing _mcp_ substring', () => {
|
||||
const result = buildOAuthToolCallName('my_mcp_server');
|
||||
expect(result).toBe('oauth_mcp_my_mcp_server');
|
||||
});
|
||||
|
||||
it('should normalize non-ASCII server names before prefixing', () => {
|
||||
const result = buildOAuthToolCallName('我的服务');
|
||||
expect(result).toMatch(/^oauth_mcp_server_\d+$/);
|
||||
});
|
||||
|
||||
it('should normalize special characters before prefixing', () => {
|
||||
expect(buildOAuthToolCallName('server@name!')).toBe('oauth_mcp_server_name');
|
||||
});
|
||||
|
||||
it('should handle empty string server name gracefully', () => {
|
||||
const result = buildOAuthToolCallName('');
|
||||
expect(result).toMatch(/^oauth_mcp_server_\d+$/);
|
||||
});
|
||||
|
||||
it('should treat a name already starting with oauth_mcp_ as pre-wrapped', () => {
|
||||
// At the function level, a name starting with the oauth prefix is
|
||||
// indistinguishable from a pre-wrapped name — guard prevents double-wrapping.
|
||||
// Server names with this prefix should be blocked at registration time.
|
||||
expect(buildOAuthToolCallName('oauth_mcp_github')).toBe('oauth_mcp_github');
|
||||
});
|
||||
|
||||
it('should not treat special chars that normalize to oauth_mcp_* as pre-wrapped', () => {
|
||||
// oauth@mcp@server does NOT start with 'oauth_mcp_' before normalization,
|
||||
// so the guard correctly does not fire and the prefix is added.
|
||||
const result = buildOAuthToolCallName('oauth@mcp@server');
|
||||
expect(result).toBe('oauth_mcp_oauth_mcp_server');
|
||||
});
|
||||
});
|
||||
|
||||
describe('redactServerSecrets', () => {
|
||||
it('should strip apiKey.key from admin-sourced keys', () => {
|
||||
const config: ParsedServerConfig = {
|
||||
|
|
|
|||
|
|
@ -97,6 +97,22 @@ export function normalizeServerName(serverName: string): string {
|
|||
return normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the synthetic tool-call name used during MCP OAuth flows.
|
||||
* Format: `oauth<mcp_delimiter><normalizedServerName>`
|
||||
*
|
||||
* Guards against the caller passing a pre-wrapped name (one that already
|
||||
* starts with the oauth prefix in its original, un-normalized form) to
|
||||
* prevent double-wrapping.
|
||||
*/
|
||||
export function buildOAuthToolCallName(serverName: string): string {
|
||||
const oauthPrefix = `oauth${Constants.mcp_delimiter}`;
|
||||
if (serverName.startsWith(oauthPrefix)) {
|
||||
return normalizeServerName(serverName);
|
||||
}
|
||||
return `${oauthPrefix}${normalizeServerName(serverName)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes a URL by removing query parameters to prevent credential leakage in logs.
|
||||
* @param url - The URL to sanitize (string or URL object)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue