Repository navigation
fix(plugins): complete OAuth connections #162
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7c73204
fix(plugins): complete OAuth connections
leoisadev1 1877abf
fix(plugins): preserve OAuth success during recovery
leoisadev1 6a91db9
fix(plugins): keep OAuth health after session recovery
leoisadev1 346e7db
Merge remote-tracking branch 'origin/main' into t3code/fix-video-atta…
leoisadev1 4cac846
fix(plugins): report OAuth session recovery failures
leoisadev1 b1130e5
fix(plugins): surface OAuth recovery issues
leoisadev1 1f85e8b
fix(plugins): identify OAuth recovery sessions
leoisadev1 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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
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
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
169 changes: 169 additions & 0 deletions
169
apps/server/src/provider/McpServerAuthentication.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,169 @@ | ||
| import type { McpManager, McpServerStatus } from "@mastra/code-sdk/mcp/index"; | ||
| import { McpServerId, type McpServer } from "@t3tools/contracts"; | ||
| import { describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { authenticateMcpServer } from "./McpServerAuthentication.ts"; | ||
|
|
||
| const server: McpServer = { | ||
| id: McpServerId.make("builtin-hoplite"), | ||
| name: "Hoplite", | ||
| transport: "url", | ||
| url: "https://mcp.hoplite.ai", | ||
| enabled: true, | ||
| createdAt: "2026-09-03T00:00:00.000Z", | ||
| updatedAt: "2026-09-03T00:00:00.000Z", | ||
| }; | ||
|
|
||
| function status(overrides: Partial<McpServerStatus> = {}): McpServerStatus { | ||
| return { | ||
| name: "builtin-hoplite", | ||
| connected: false, | ||
| needsAuth: true, | ||
| toolCount: 0, | ||
| toolNames: [], | ||
| transport: "http", | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("MCP server authentication", () => { | ||
| it("opens OAuth, waits for connection, and reconnects other live managers", async () => { | ||
| const authorizationUrl = "https://hoplite.ai/oauth/authorize"; | ||
| const first = { | ||
| reconnectServer: vi.fn(async () => status()), | ||
| authenticateServer: vi.fn(async (_name, options) => { | ||
| options?.onAuthorizationUrl?.(authorizationUrl); | ||
| return status({ connected: true, needsAuth: false, toolCount: 4 }); | ||
| }), | ||
| } as unknown as McpManager; | ||
| const second = { | ||
| reconnectServer: vi.fn(async () => | ||
| status({ connected: true, needsAuth: false, toolCount: 4 }), | ||
| ), | ||
| } as unknown as McpManager; | ||
| const onAuthorizationUrl = vi.fn(); | ||
| const recordSuccess = vi.fn(); | ||
| const result = await authenticateMcpServer({ | ||
| server, | ||
| managers: [first, second], | ||
| createManager: vi.fn(), | ||
| onAuthorizationUrl, | ||
| recordSuccess, | ||
| recordFailure: vi.fn(), | ||
| recordReconnectFailure: vi.fn(), | ||
| }); | ||
|
|
||
| expect(result.connected).toBe(true); | ||
| expect(onAuthorizationUrl).toHaveBeenCalledWith(authorizationUrl); | ||
| expect(second.reconnectServer).toHaveBeenCalledWith("builtin-hoplite"); | ||
| expect(recordSuccess).toHaveBeenCalledWith(server.id); | ||
| }); | ||
|
|
||
| it("records the real authentication failure", async () => { | ||
| const manager = { | ||
| reconnectServer: vi.fn(async () => status()), | ||
| authenticateServer: vi.fn(async () => status({ error: "Authentication cancelled." })), | ||
| } as unknown as McpManager; | ||
| const recordFailure = vi.fn(); | ||
| await expect( | ||
| authenticateMcpServer({ | ||
| server, | ||
| managers: [manager], | ||
| createManager: vi.fn(), | ||
| onAuthorizationUrl: vi.fn(), | ||
| recordSuccess: vi.fn(), | ||
| recordFailure, | ||
| recordReconnectFailure: vi.fn(), | ||
| }), | ||
| ).rejects.toThrow("Authentication cancelled."); | ||
| expect(recordFailure).toHaveBeenCalledWith(server.id, "Authentication cancelled."); | ||
| }); | ||
|
|
||
| it("initializes and closes a temporary manager when no thread is active", async () => { | ||
| const manager = { | ||
| init: vi.fn(async () => undefined), | ||
| reconnectServer: vi.fn(async () => | ||
| status({ connected: true, needsAuth: false, toolCount: 2 }), | ||
| ), | ||
| disconnect: vi.fn(async () => undefined), | ||
| } as unknown as McpManager; | ||
| const createManager = vi.fn(() => manager); | ||
|
|
||
| await authenticateMcpServer({ | ||
| server, | ||
| managers: [], | ||
| createManager, | ||
| onAuthorizationUrl: vi.fn(), | ||
| recordSuccess: vi.fn(), | ||
| recordFailure: vi.fn(), | ||
| recordReconnectFailure: vi.fn(), | ||
| }); | ||
|
|
||
| expect(createManager).toHaveBeenCalledOnce(); | ||
| expect(manager.init).toHaveBeenCalledOnce(); | ||
| expect(manager.disconnect).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("cancels a pending manager authentication when the request stops", async () => { | ||
| const controller = new AbortController(); | ||
| let finishAuthentication!: (value: McpServerStatus) => void; | ||
| const manager = { | ||
| reconnectServer: vi.fn(async () => status()), | ||
| authenticateServer: vi.fn( | ||
| async () => | ||
| await new Promise<McpServerStatus>((resolve) => { | ||
| finishAuthentication = resolve; | ||
| }), | ||
| ), | ||
| cancelServerAuthentication: vi.fn(async () => { | ||
| finishAuthentication(status({ cancelled: true, error: "Authentication cancelled." })); | ||
| }), | ||
| } as unknown as McpManager; | ||
|
|
||
| const authentication = authenticateMcpServer({ | ||
| server, | ||
| managers: [manager], | ||
| createManager: vi.fn(), | ||
| onAuthorizationUrl: vi.fn(), | ||
| signal: controller.signal, | ||
| recordSuccess: vi.fn(), | ||
| recordFailure: vi.fn(), | ||
| recordReconnectFailure: vi.fn(), | ||
| }); | ||
| await vi.waitFor(() => expect(manager.authenticateServer).toHaveBeenCalledOnce()); | ||
| controller.abort(); | ||
|
|
||
| await expect(authentication).rejects.toThrow("Authentication cancelled."); | ||
| expect(manager.cancelServerAuthentication).toHaveBeenCalledWith("builtin-hoplite"); | ||
| }); | ||
|
|
||
| it("keeps OAuth successful when a secondary manager cannot reconnect", async () => { | ||
| const primary = { | ||
| reconnectServer: vi.fn(async () => status()), | ||
| authenticateServer: vi.fn(async () => | ||
| status({ connected: true, needsAuth: false, toolCount: 4 }), | ||
| ), | ||
| } as unknown as McpManager; | ||
| const secondary = { | ||
| reconnectServer: vi.fn(async () => status({ error: "Secondary session failed." })), | ||
| } as unknown as McpManager; | ||
| const recordSuccess = vi.fn(); | ||
| const recordFailure = vi.fn(); | ||
| const recordReconnectFailure = vi.fn(); | ||
|
|
||
| const result = await authenticateMcpServer({ | ||
| server, | ||
| managers: [primary, secondary], | ||
| createManager: vi.fn(), | ||
| onAuthorizationUrl: vi.fn(), | ||
| recordSuccess, | ||
| recordFailure, | ||
| recordReconnectFailure, | ||
| }); | ||
|
|
||
| expect(result.connected).toBe(true); | ||
| expect(recordSuccess).toHaveBeenCalledWith(server.id); | ||
| expect(recordFailure).not.toHaveBeenCalled(); | ||
| expect(recordReconnectFailure).toHaveBeenCalledWith(server.id, "Secondary session failed."); | ||
| }); | ||
| }); |
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,66 @@ | ||
| import type { McpManager, McpServerStatus } from "@mastra/code-sdk/mcp/index"; | ||
| import type { McpServer, McpServerId } from "@t3tools/contracts"; | ||
|
|
||
| interface AuthenticateMcpServerOptions { | ||
| readonly server: McpServer; | ||
| readonly managers: readonly McpManager[]; | ||
| readonly createManager: () => McpManager; | ||
| readonly onAuthorizationUrl: (url: string) => void; | ||
| readonly signal?: AbortSignal; | ||
| readonly recordSuccess: (serverId: McpServerId) => void; | ||
| readonly recordFailure: (serverId: McpServerId, message: string) => void; | ||
| readonly recordReconnectFailure: (serverId: McpServerId, message: string) => void; | ||
| } | ||
|
|
||
| function failureMessage(cause: unknown): string { | ||
| return cause instanceof Error ? cause.message : String(cause); | ||
| } | ||
|
|
||
| function requireConnected(status: McpServerStatus): McpServerStatus { | ||
| if (status.connected) return status; | ||
| throw new Error(status.error ?? `MCP server '${status.name}' did not connect.`); | ||
| } | ||
|
|
||
| export async function authenticateMcpServer( | ||
| options: AuthenticateMcpServerOptions, | ||
| ): Promise<McpServerStatus> { | ||
| const temporary = options.managers.length === 0; | ||
| const manager = options.managers[0] ?? options.createManager(); | ||
| const serverId = String(options.server.id); | ||
| const cancelAuthentication = () => { | ||
| void manager.cancelServerAuthentication(serverId).catch(() => undefined); | ||
| }; | ||
| options.signal?.addEventListener("abort", cancelAuthentication, { once: true }); | ||
|
|
||
| try { | ||
| let connected: McpServerStatus; | ||
| try { | ||
| if (temporary) await manager.init(); | ||
|
|
||
| const reconnectStatus = await manager.reconnectServer(serverId); | ||
| const authenticatedStatus = reconnectStatus.connected | ||
| ? reconnectStatus | ||
| : await manager.authenticateServer(serverId, { | ||
| onAuthorizationUrl: options.onAuthorizationUrl, | ||
| }); | ||
| connected = requireConnected(authenticatedStatus); | ||
| options.recordSuccess(options.server.id); | ||
| } catch (cause) { | ||
| options.recordFailure(options.server.id, failureMessage(cause)); | ||
| throw cause; | ||
| } | ||
|
|
||
| for (const other of options.managers.slice(1)) { | ||
| try { | ||
| requireConnected(await other.reconnectServer(serverId)); | ||
| } catch (cause) { | ||
| options.recordReconnectFailure(options.server.id, failureMessage(cause)); | ||
| } | ||
| } | ||
|
|
||
| return connected; | ||
| } finally { | ||
| options.signal?.removeEventListener("abort", cancelAuthentication); | ||
| if (temporary) await manager.disconnect().catch(() => undefined); | ||
| } | ||
| } | ||
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
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.