-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(mcp): recover cleanly from OAuth failures #5595
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
j15z
wants to merge
5
commits into
staging
Choose a base branch
from
fix/mcp-page
base: staging
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.
+1,448
−195
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e72a777
fix(mcp): improve OAuth failure recovery
j15z 97b3e73
fix(mcp): address OAuth recovery review findings
j15z c0bd4e4
fix(mcp): prefer static bearer auth in connection tests
j15z 23320ee
fix(mcp): preserve discovery failure state
j15z 7b2378c
Merge remote-tracking branch 'origin/staging' into fix/mcp-page
j15z 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
167 changes: 167 additions & 0 deletions
167
apps/sim/app/api/mcp/servers/[id]/refresh/route.test.ts
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,167 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import type { NextRequest } from 'next/server' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockClearCache, mockDiscoverServerTools, mockSelect, mockUpdateSet } = vi.hoisted(() => ({ | ||
| mockClearCache: vi.fn(), | ||
| mockDiscoverServerTools: vi.fn(), | ||
| mockSelect: vi.fn(), | ||
| mockUpdateSet: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@sim/db', () => ({ | ||
| db: { | ||
| select: mockSelect, | ||
| update: vi.fn().mockReturnValue({ set: mockUpdateSet }), | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/utils/with-route-handler', () => ({ | ||
| withRouteHandler: (handler: unknown) => handler, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/mcp/middleware', () => ({ | ||
| withMcpAuth: | ||
| () => | ||
| ( | ||
| handler: ( | ||
| request: NextRequest, | ||
| context: { userId: string; workspaceId: string; requestId: string }, | ||
| routeContext: { params: Promise<{ id: string }> } | ||
| ) => Promise<Response> | ||
| ) => | ||
| (request: NextRequest, routeContext: { params: Promise<{ id: string }> }) => | ||
| handler( | ||
| request, | ||
| { userId: 'user-1', workspaceId: 'workspace-1', requestId: 'request-1' }, | ||
| routeContext | ||
| ), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/mcp/service', () => ({ | ||
| mcpService: { | ||
| clearCache: mockClearCache, | ||
| discoverServerTools: mockDiscoverServerTools, | ||
| }, | ||
| })) | ||
|
|
||
| import { POST } from '@/app/api/mcp/servers/[id]/refresh/route' | ||
|
|
||
| const initialServer = { | ||
| id: 'server-1', | ||
| workspaceId: 'workspace-1', | ||
| name: 'OAuth Server', | ||
| url: 'https://example.com/mcp', | ||
| connectionStatus: 'connected', | ||
| lastError: null, | ||
| lastConnected: new Date('2026-01-01T00:00:00.000Z'), | ||
| toolCount: 4, | ||
| statusConfig: { consecutiveFailures: 0, lastSuccessfulDiscovery: null }, | ||
| } | ||
|
|
||
| const persistedServer = { | ||
| ...initialServer, | ||
| connectionStatus: 'disconnected', | ||
| lastError: null, | ||
| toolCount: 0, | ||
| } | ||
|
|
||
| function selectRows(rows: unknown[]) { | ||
| return { | ||
| from: vi.fn().mockReturnValue({ | ||
| where: vi.fn().mockReturnValue({ | ||
| limit: vi.fn().mockResolvedValue(rows), | ||
| }), | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| describe('MCP server refresh route', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockSelect.mockReturnValueOnce(selectRows([initialServer])) | ||
| mockUpdateSet.mockReturnValue({ | ||
| where: vi.fn().mockReturnValue({ returning: vi.fn().mockResolvedValue([persistedServer]) }), | ||
| }) | ||
| }) | ||
|
|
||
| it('preserves the service-persisted OAuth pending status', async () => { | ||
| mockDiscoverServerTools.mockRejectedValueOnce(new Error('OAuth authorization required')) | ||
|
|
||
| const request = new Request('http://localhost/api/mcp/servers/server-1/refresh', { | ||
| method: 'POST', | ||
| }) as NextRequest | ||
| const response = await POST(request, { params: Promise.resolve({ id: 'server-1' }) }) | ||
| const body = await response.json() | ||
|
|
||
| expect(body.data).toEqual( | ||
| expect.objectContaining({ | ||
| status: 'disconnected', | ||
| error: null, | ||
| }) | ||
| ) | ||
| expect(mockUpdateSet).not.toHaveBeenCalledWith( | ||
| expect.objectContaining({ connectionStatus: expect.anything() }) | ||
| ) | ||
| }) | ||
|
|
||
| it('reports the discovery failure when status persistence leaves a stale connected row', async () => { | ||
| const reflectedSecret = 'Bearer reflected-static-token' | ||
| mockDiscoverServerTools.mockRejectedValueOnce( | ||
| new Error(`Upstream reflected ${reflectedSecret}`) | ||
| ) | ||
| mockUpdateSet.mockReturnValueOnce({ | ||
| where: vi.fn().mockReturnValue({ | ||
| returning: vi.fn().mockResolvedValue([initialServer]), | ||
| }), | ||
| }) | ||
|
|
||
| const request = new Request('http://localhost/api/mcp/servers/server-1/refresh', { | ||
| method: 'POST', | ||
| }) as NextRequest | ||
| const response = await POST(request, { params: Promise.resolve({ id: 'server-1' }) }) | ||
| const body = await response.json() | ||
|
|
||
| expect(body.data).toEqual( | ||
| expect.objectContaining({ | ||
| status: 'disconnected', | ||
| error: 'Internal server error', | ||
| workflowsUpdated: 0, | ||
| }) | ||
| ) | ||
| expect(JSON.stringify(body)).not.toContain(reflectedSecret) | ||
| expect(mockClearCache).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('preserves a connected status from a newer successful discovery', async () => { | ||
| mockDiscoverServerTools.mockRejectedValueOnce(new Error('Connection failed')) | ||
| const newerSuccessfulServer = { | ||
| ...initialServer, | ||
| lastConnected: new Date(Date.now() + 60_000), | ||
| toolCount: 7, | ||
| } | ||
| mockUpdateSet.mockReturnValueOnce({ | ||
| where: vi.fn().mockReturnValue({ | ||
| returning: vi.fn().mockResolvedValue([newerSuccessfulServer]), | ||
| }), | ||
| }) | ||
|
|
||
| const request = new Request('http://localhost/api/mcp/servers/server-1/refresh', { | ||
| method: 'POST', | ||
| }) as NextRequest | ||
| const response = await POST(request, { params: Promise.resolve({ id: 'server-1' }) }) | ||
| const body = await response.json() | ||
|
|
||
| expect(body.data).toEqual( | ||
| expect.objectContaining({ | ||
| status: 'connected', | ||
| error: null, | ||
| toolCount: 7, | ||
| workflowsUpdated: 0, | ||
| }) | ||
| ) | ||
| expect(mockClearCache).toHaveBeenCalledWith('workspace-1') | ||
| }) | ||
| }) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.