-
Notifications
You must be signed in to change notification settings - Fork 355
fix(connectors): auto-prepend Bearer prefix for header-auth MCP connectors #492
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
Open
kristopolous
wants to merge
1
commit into
truefoundry:main
Choose a base branch
from
kristopolous:fix/mcp-header-auth-bearer-prefix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−27
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@truefoundry/trueforge-ui": patch | ||
| --- | ||
|
|
||
| Fix header-auth MCP connectors (e.g. Bright Data) rejecting pasted API keys with 401 because the required `Bearer ` scheme prefix wasn't applied. The Connect / Replace Key dialog now tests the pasted key live against the upstream server — as typed, then with a `Bearer ` prefix, then with a `Basic ` prefix — reporting each attempt in the dialog, and stores whichever one actually connects. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
packages/trueforge-ui/test/containers/SettingsBuilder/ConnectorSettings.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // @vitest-environment jsdom | ||
| import { fireEvent, render, screen, waitFor, within } from '@testing-library/react'; | ||
| import { beforeAll, describe, expect, it } from 'vitest'; | ||
|
|
||
| import ConnectorSettings from '@/containers/SettingsBuilder/ConnectorSettings.js'; | ||
| import { ServerProvider } from '@/server/ServerContext.js'; | ||
| import type { ConnectorBase, ConnectorCatalogEntry } from '@/server/types.js'; | ||
| import { createMockAgentUIServer, createMockCatalog } from '../../server/mockServer.js'; | ||
|
|
||
| beforeAll(() => { | ||
| HTMLDialogElement.prototype.showModal = function showModal() { | ||
| this.setAttribute('open', ''); | ||
| }; | ||
| HTMLDialogElement.prototype.close = function close() { | ||
| this.removeAttribute('open'); | ||
| this.dispatchEvent(new Event('close')); | ||
| }; | ||
| }); | ||
|
|
||
| const brightData: ConnectorCatalogEntry = { | ||
| id: 'bright-data', | ||
| name: 'bright-data', | ||
| description: 'Search the web and scrape pages, including sites behind bot protection.', | ||
| url: 'https://mcp.brightdata.com/mcp', | ||
| auth: { type: 'header', headerName: 'Authorization' }, | ||
| }; | ||
|
|
||
| const connectedBrightData: ConnectorBase = { | ||
| id: 'bright-data', | ||
| name: 'bright-data', | ||
| description: brightData.description ?? '', | ||
| url: brightData.url, | ||
| auth: { type: 'header', headerName: 'Authorization' }, | ||
| requiresAuth: false, | ||
| authenticated: true, | ||
| }; | ||
|
|
||
| /** `succeedsWhen` decides, per attempt, whether the just-saved header value should test as reachable. */ | ||
| function renderConnectorSettings(succeedsWhen: (headerValue: string) => boolean) { | ||
| let lastAuthValue: string | undefined; | ||
| const attempts: string[] = []; | ||
| const server = createMockAgentUIServer({ | ||
| catalog: createMockCatalog({ | ||
| connectorCatalog: { | ||
| getConnectorCatalog: async () => [brightData], | ||
| listConnectors: async () => [], | ||
| getConnector: async () => connectedBrightData, | ||
| getToolsByConnectorId: async () => { | ||
| if (lastAuthValue === undefined || !succeedsWhen(lastAuthValue)) { | ||
| throw new Error(`upstream returned 401 Unauthorized for "${lastAuthValue}"`); | ||
| } | ||
| return []; | ||
| }, | ||
| createConnector: async req => { | ||
| const value = req.auth.type === 'header' ? (req.auth.apiKey ?? '') : ''; | ||
| lastAuthValue = value; | ||
| attempts.push(value); | ||
| return connectedBrightData; | ||
| }, | ||
| updateConnector: async req => { | ||
| const value = req.auth.type === 'header' ? (req.auth.apiKey ?? '') : ''; | ||
| lastAuthValue = value; | ||
| attempts.push(value); | ||
| return connectedBrightData; | ||
| }, | ||
| authenticateConnector: async () => ({ authorization_endpoint: '' }), | ||
| disconnectConnector: async () => connectedBrightData, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| render( | ||
| <ServerProvider server={server}> | ||
| <ConnectorSettings /> | ||
| </ServerProvider>, | ||
| ); | ||
|
|
||
| return { attempts }; | ||
| } | ||
|
|
||
| async function openConnectModal() { | ||
| const row = await screen.findByText('bright-data'); | ||
| const connectButton = within(row.closest('article') as HTMLElement).getByRole('button', { name: 'Connect' }); | ||
| fireEvent.click(connectButton); | ||
| return within(await screen.findByRole('dialog')); | ||
| } | ||
|
|
||
| function submit(dialog: ReturnType<typeof within>, apiKey: string) { | ||
| fireEvent.change(dialog.getByLabelText('API key / token'), { target: { value: apiKey } }); | ||
| fireEvent.click(dialog.getByRole('button', { name: /Connect|Testing/ })); | ||
| } | ||
|
|
||
| describe('ConnectorSettings auto-probing header auth (fixes #490)', () => { | ||
| it('stores the raw key when it works on the first try', async () => { | ||
| const { attempts } = renderConnectorSettings(value => value === 'abc123'); | ||
| const dialog = await openConnectModal(); | ||
| submit(dialog, 'abc123'); | ||
|
|
||
| await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); | ||
| expect(attempts).toEqual(['abc123']); | ||
| }); | ||
|
|
||
| it('falls back to a Bearer prefix when the raw key is rejected, logging each attempt', async () => { | ||
| const { attempts } = renderConnectorSettings(value => value === 'Bearer abc123'); | ||
| const dialog = await openConnectModal(); | ||
| submit(dialog, 'abc123'); | ||
|
|
||
| await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); | ||
| expect(attempts).toEqual(['abc123', 'Bearer abc123']); | ||
| }); | ||
|
|
||
| it('falls back to Basic when both the raw key and Bearer are rejected', async () => { | ||
| const { attempts } = renderConnectorSettings(value => value === 'Basic abc123'); | ||
| const dialog = await openConnectModal(); | ||
| submit(dialog, 'abc123'); | ||
|
|
||
| await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); | ||
| expect(attempts).toEqual(['abc123', 'Bearer abc123', 'Basic abc123']); | ||
| }); | ||
|
|
||
| it('shows a final error, keeps the dialog open, and logs every failed attempt when all candidates fail', async () => { | ||
| const { attempts } = renderConnectorSettings(() => false); | ||
| const dialog = await openConnectModal(); | ||
| submit(dialog, 'abc123'); | ||
|
|
||
| expect(await dialog.findByText(/Could not connect with the provided key/)).toBeInTheDocument(); | ||
| expect(attempts).toEqual(['abc123', 'Bearer abc123', 'Basic abc123']); | ||
| expect(screen.getByRole('dialog')).toBeInTheDocument(); | ||
| expect(dialog.getByText(/Testing key… failed/)).toBeInTheDocument(); | ||
| expect(dialog.getByText(/Trying with prefix "Bearer"… failed/)).toBeInTheDocument(); | ||
| expect(dialog.getByText(/Trying with prefix "Basic"… failed/)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('does not double-prefix a key already typed with "Bearer "', async () => { | ||
| const { attempts } = renderConnectorSettings(value => value === 'Basic Bearer abc123'); | ||
| const dialog = await openConnectModal(); | ||
| submit(dialog, 'Bearer abc123'); | ||
|
|
||
| await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); | ||
| // No separate "Bearer Bearer abc123" attempt — the raw try already carried the prefix. | ||
| expect(attempts).toEqual(['Bearer abc123', 'Basic Bearer abc123']); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Probe retries cannot update new connectors
High Severity
existingConnectoris captured once before the probe loop, so first-time Connect always callscreateFromCatalogfor every candidate. The raw key is persisted on the first attempt; laterBearer/Basictries then hit a name conflict instead of updating. The working prefix is never stored, the dialog reports failure, and a connector remains saved with the rejected key.Reviewed by Cursor Bugbot for commit 4e5d13f. Configure here.