-
Notifications
You must be signed in to change notification settings - Fork 535
fix: surface MCP OAuth errors to browser instead of silently swallowing #850
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c8be07e
fix: surface MCP OAuth errors to browser instead of silently swallowi…
6838e0e
fix: surface MCP OAuth errors to browser instead of silently swallowi…
06da34f
Refactor and remove hardcoded html return page to prevent XSS
e15e34f
Swap out HTML for Response to avoid XSS
1ca14fa
Fix type error by using the correct type
3ba1f2a
Fix failing tests
27fa3f1
Fix changeset naming
1a41bee
Remove unneeded comment
cd88a86
fix: address review feedback on OAuth error handling
threepointone 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 @@ | ||
| --- | ||
| "agents": patch | ||
| --- | ||
|
|
||
| Fix: MCP OAuth callback errors are now returned as structured results instead of throwing unhandled exceptions. Errors with an active connection properly transition to "failed" state and are surfaced to clients via WebSocket broadcast. |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import type { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||
| import escapeHtml from "escape-html"; | ||
| import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js"; | ||
| import type { | ||
| CallToolRequest, | ||
|
|
@@ -53,7 +52,7 @@ export type MCPServerOptions = { | |
| */ | ||
| export type MCPOAuthCallbackResult = | ||
| | { serverId: string; authSuccess: true; authError?: undefined } | ||
| | { serverId: string; authSuccess: false; authError: string }; | ||
| | { serverId?: string; authSuccess: false; authError: string }; | ||
|
|
||
| /** | ||
| * Options for registering an MCP server | ||
|
|
@@ -106,11 +105,14 @@ export type MCPClientOAuthCallbackConfig = { | |
| customHandler?: (result: MCPClientOAuthResult) => Response; | ||
| }; | ||
|
|
||
| export type MCPClientOAuthResult = { | ||
| serverId: string; | ||
| authSuccess: boolean; | ||
| authError?: string; | ||
| }; | ||
| export type MCPClientOAuthResult = | ||
|
Contributor
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. love this |
||
| | { serverId: string; authSuccess: true; authError?: undefined } | ||
| | { | ||
| serverId?: string; | ||
| authSuccess: false; | ||
| /** May contain untrusted content from external OAuth providers. Escape appropriately for your output context. */ | ||
| authError: string; | ||
| }; | ||
|
|
||
| export type MCPClientManagerOptions = { | ||
| storage: DurableObjectStorage; | ||
|
|
@@ -723,39 +725,93 @@ export class MCPClientManager { | |
| }); | ||
| } | ||
|
|
||
| async handleCallbackRequest(req: Request): Promise<MCPOAuthCallbackResult> { | ||
| private validateCallbackRequest( | ||
| req: Request | ||
| ): | ||
| | { valid: true; serverId: string; code: string; state: string } | ||
| | { valid: false; serverId?: string; error: string } { | ||
| const url = new URL(req.url); | ||
| const code = url.searchParams.get("code"); | ||
| const state = url.searchParams.get("state"); | ||
| const error = url.searchParams.get("error"); | ||
| const errorDescription = url.searchParams.get("error_description"); | ||
|
|
||
| // Early validation - these throw because we can't identify the connection | ||
| // Early validation - return errors because we can't identify the connection | ||
| if (!state) { | ||
| throw new Error("Unauthorized: no state provided"); | ||
| return { | ||
| valid: false, | ||
| error: "Unauthorized: no state provided" | ||
| }; | ||
| } | ||
|
|
||
| const serverId = this.extractServerIdFromState(state); | ||
| if (!serverId) { | ||
| throw new Error( | ||
| "No serverId found in state parameter. Expected format: {nonce}.{serverId}" | ||
| ); | ||
| return { | ||
| valid: false, | ||
| error: | ||
| "No serverId found in state parameter. Expected format: {nonce}.{serverId}" | ||
| }; | ||
| } | ||
|
|
||
| if (error) { | ||
| return { | ||
| serverId: serverId, | ||
| valid: false, | ||
| error: errorDescription || error | ||
| }; | ||
| } | ||
|
|
||
| if (!code) { | ||
| return { | ||
| serverId: serverId, | ||
| valid: false, | ||
| error: "Unauthorized: no code provided" | ||
| }; | ||
| } | ||
|
|
||
| const servers = this.getServersFromStorage(); | ||
| const serverExists = servers.some((server) => server.id === serverId); | ||
| if (!serverExists) { | ||
| throw new Error( | ||
| `No server found with id "${serverId}". Was the request matched with \`isCallbackRequest()\`?` | ||
| ); | ||
| return { | ||
| serverId: serverId, | ||
| valid: false, | ||
| error: `No server found with id "${serverId}". Was the request matched with \`isCallbackRequest()\`?` | ||
| }; | ||
| } | ||
|
|
||
| if (this.mcpConnections[serverId] === undefined) { | ||
| throw new Error(`Could not find serverId: ${serverId}`); | ||
| return { | ||
| serverId: serverId, | ||
| valid: false, | ||
| error: `No connection found for serverId "${serverId}".` | ||
| }; | ||
| } | ||
|
|
||
| // We have a valid connection - all errors from here should fail the connection | ||
| const conn = this.mcpConnections[serverId]; | ||
| return { | ||
| valid: true, | ||
| serverId, | ||
| code: code, | ||
| state: state | ||
| }; | ||
| } | ||
|
|
||
| async handleCallbackRequest(req: Request): Promise<MCPOAuthCallbackResult> { | ||
| const validation = this.validateCallbackRequest(req); | ||
|
|
||
| if (!validation.valid) { | ||
| if (validation.serverId && this.mcpConnections[validation.serverId]) { | ||
| return this.failConnection(validation.serverId, validation.error); | ||
| } | ||
|
|
||
| return { | ||
| serverId: validation.serverId, | ||
| authSuccess: false, | ||
| authError: validation.error | ||
| }; | ||
| } | ||
|
|
||
| const { serverId, code, state } = validation; | ||
| const conn = this.mcpConnections[serverId]; // We have a valid connection - all errors from here should fail the connection | ||
|
|
||
| try { | ||
| if (!conn.options.transport.authProvider) { | ||
|
|
@@ -774,15 +830,6 @@ export class MCPClientManager { | |
| throw new Error(stateValidation.error || "Invalid state"); | ||
| } | ||
|
|
||
| if (error) { | ||
| // Escape external OAuth error params to prevent XSS | ||
| throw new Error(escapeHtml(errorDescription || error)); | ||
| } | ||
|
|
||
| if (!code) { | ||
| throw new Error("Unauthorized: no code provided"); | ||
| } | ||
|
|
||
| // Already authenticated - just return success | ||
| if ( | ||
| conn.connectionState === MCPConnectionState.READY || | ||
|
|
||
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.