-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Make OAuth callback URIs configurable #585
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
mxcoppell
wants to merge
4
commits into
modelcontextprotocol:main
Choose a base branch
from
mxcoppell:feature/oauth-flow
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.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import http from "http"; | ||
| import { URL } from "url"; | ||
|
|
||
| interface OAuthCallbackServer { | ||
| server: http.Server; | ||
| port: number; | ||
| url: string; | ||
| } | ||
|
|
||
| export class OAuthCallbackManager { | ||
| private servers: OAuthCallbackServer[] = []; | ||
| private mcpInspectorUrl: string; | ||
|
|
||
| constructor(mcpInspectorUrl: string) { | ||
| this.mcpInspectorUrl = mcpInspectorUrl; | ||
| } | ||
|
|
||
| private createCallbackServer(callbackUrl: string, isDebug: boolean = false): OAuthCallbackServer | null { | ||
| try { | ||
| const parsedUrl = new URL(callbackUrl); | ||
| const port = parseInt(parsedUrl.port, 10); | ||
|
|
||
| if (!port || isNaN(port)) { | ||
| console.warn(`Invalid port in OAuth callback URL: ${callbackUrl}`); | ||
| return null; | ||
| } | ||
|
|
||
| const server = http.createServer((req, res) => { | ||
| const reqUrl = new URL(req.url || "", `http://${req.headers.host}`); | ||
|
|
||
| // Get OAuth parameters from the query string | ||
| const code = reqUrl.searchParams.get("code"); | ||
| const state = reqUrl.searchParams.get("state"); | ||
| const error = reqUrl.searchParams.get("error"); | ||
| const errorDescription = reqUrl.searchParams.get("error_description"); | ||
|
|
||
| // Build redirect URL to MCP Inspector | ||
| const inspectorPath = isDebug ? "/oauth/callback/debug" : "/oauth/callback"; | ||
| const redirectUrl = new URL(inspectorPath, this.mcpInspectorUrl); | ||
|
|
||
| // Forward all query parameters | ||
| reqUrl.searchParams.forEach((value, key) => { | ||
| redirectUrl.searchParams.set(key, value); | ||
| }); | ||
|
|
||
| // Send redirect response | ||
| res.writeHead(302, { | ||
| "Location": redirectUrl.toString(), | ||
| "Content-Type": "text/html", | ||
| }); | ||
|
|
||
| const redirectHtml = ` | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>OAuth Redirect</title> | ||
| <meta charset="utf-8"> | ||
| </head> | ||
| <body> | ||
| <h2>OAuth Authentication</h2> | ||
| <p>Redirecting to MCP Inspector...</p> | ||
| <p>If you are not redirected automatically, <a href="${redirectUrl.toString()}">click here</a>.</p> | ||
| <script> | ||
| // Automatic redirect | ||
| window.location.href = "${redirectUrl.toString()}"; | ||
| </script> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| res.end(redirectHtml); | ||
|
|
||
| console.log(`OAuth ${isDebug ? "debug " : ""}callback received on port ${port}`); | ||
| if (code) { | ||
| console.log(` Authorization code: ${code.substring(0, 10)}...`); | ||
| } | ||
| if (error) { | ||
| console.log(` Error: ${error} - ${errorDescription || "No description"}`); | ||
| } | ||
| console.log(` Redirecting to: ${redirectUrl.toString()}`); | ||
| }); | ||
|
|
||
| return { server, port, url: callbackUrl }; | ||
| } catch (error) { | ||
| console.error(`Failed to create OAuth callback server for ${callbackUrl}:`, error); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| start(): void { | ||
| const oauthCallback = process.env.OAUTH_MCP_INSPECTOR_CALLBACK; | ||
| const oauthDebugCallback = process.env.OAUTH_MCP_INSPECTOR_DEBUG_CALLBACK; | ||
|
|
||
| if (oauthCallback) { | ||
| const callbackServer = this.createCallbackServer(oauthCallback, false); | ||
| if (callbackServer) { | ||
| callbackServer.server.listen(callbackServer.port, () => { | ||
| console.log(`🔗 OAuth callback server listening on ${callbackServer.url}`); | ||
| }); | ||
|
|
||
| callbackServer.server.on("error", (err) => { | ||
| if ((err as any).code === "EADDRINUSE") { | ||
| console.warn(`⚠️ OAuth callback port ${callbackServer.port} is in use`); | ||
| } else { | ||
| console.error(`OAuth callback server error:`, err); | ||
| } | ||
| }); | ||
|
|
||
| this.servers.push(callbackServer); | ||
| } | ||
| } | ||
|
|
||
| if (oauthDebugCallback) { | ||
| const debugCallbackServer = this.createCallbackServer(oauthDebugCallback, true); | ||
| if (debugCallbackServer) { | ||
| debugCallbackServer.server.listen(debugCallbackServer.port, () => { | ||
| console.log(`🔗 OAuth debug callback server listening on ${debugCallbackServer.url}`); | ||
| }); | ||
|
|
||
| debugCallbackServer.server.on("error", (err) => { | ||
| if ((err as any).code === "EADDRINUSE") { | ||
| console.warn(`⚠️ OAuth debug callback port ${debugCallbackServer.port} is in use`); | ||
| } else { | ||
| console.error(`OAuth debug callback server error:`, err); | ||
| } | ||
| }); | ||
|
|
||
| this.servers.push(debugCallbackServer); | ||
| } | ||
| } | ||
|
|
||
| if (this.servers.length === 0) { | ||
| console.log("No OAuth callback URLs configured"); | ||
| } | ||
| } | ||
|
|
||
| stop(): void { | ||
| this.servers.forEach(({ server, port }) => { | ||
| server.close(() => { | ||
| console.log(`OAuth callback server on port ${port} stopped`); | ||
| }); | ||
| }); | ||
| this.servers = []; | ||
| } | ||
| } |
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.