-
Notifications
You must be signed in to change notification settings - Fork 401
feat: add --oauth login for the --http local entry #404
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
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae34534
feat: add --oauth login for the --http local entry
barryroodt 57249b1
fix: harden local OAuth client
barryroodt 1661479
refactor: use oauth4webapi for the OAuth protocol core
barryroodt 55e16f6
test: assert the OAuth token source is read on every request
barryroodt 36fa211
Merge branch 'feat/local-http-entry' into feat/local-http-oauth
mattrossman 6620bcb
refactor: drive --oauth with the MCP client SDK auth flow
mattrossman d6b3ee4
docs: note where --oauth stores its session
mattrossman 9f140f0
chore: trim comments in --oauth changes
mattrossman 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
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
114 changes: 114 additions & 0 deletions
114
packages/mcp-server-supabase/src/transports/oauth-client.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,114 @@ | ||
| import { spawn } from 'node:child_process'; | ||
| import { once } from 'node:events'; | ||
| import { mkdir, readFile, writeFile } from 'node:fs/promises'; | ||
| import { createServer } from 'node:http'; | ||
| import { homedir } from 'node:os'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { | ||
| auth, | ||
| type OAuthClientProvider, | ||
| type StoredOAuthClientInformation, | ||
| type StoredOAuthTokens, | ||
| } from '@modelcontextprotocol/client'; | ||
|
|
||
| // Same directory the Supabase CLI keeps its login in. | ||
| export const STORE_PATH = join( | ||
| process.env.SUPABASE_HOME ?? join(homedir(), '.supabase'), | ||
| 'mcp-oauth.json' | ||
| ); | ||
| const REDIRECT_URL = 'http://127.0.0.1:3112/callback'; | ||
|
|
||
| type Stored = { | ||
| client?: StoredOAuthClientInformation; | ||
| tokens?: StoredOAuthTokens; | ||
| codeVerifier?: string; | ||
| expiresAt?: number; | ||
| }; | ||
|
|
||
| async function read(): Promise<Stored> { | ||
| try { | ||
| return JSON.parse(await readFile(STORE_PATH, 'utf8')); | ||
| } catch { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| async function write(patch: Stored) { | ||
| await mkdir(dirname(STORE_PATH), { recursive: true }); | ||
| await writeFile(STORE_PATH, JSON.stringify({ ...(await read()), ...patch }), { | ||
| mode: 0o600, | ||
| }); | ||
| } | ||
|
|
||
| const provider: OAuthClientProvider = { | ||
| redirectUrl: REDIRECT_URL, | ||
| clientMetadata: { | ||
| client_name: 'Supabase MCP local dev server', | ||
| redirect_uris: [REDIRECT_URL], | ||
| grant_types: ['authorization_code', 'refresh_token'], | ||
| response_types: ['code'], | ||
| token_endpoint_auth_method: 'client_secret_post', | ||
| }, | ||
| clientInformation: async () => (await read()).client, | ||
| saveClientInformation: (client) => write({ client }), | ||
| tokens: async () => (await read()).tokens, | ||
| saveTokens: (tokens) => | ||
| write({ | ||
| tokens, | ||
| expiresAt: Date.now() + (tokens.expires_in ?? 3600) * 1000, | ||
| }), | ||
| saveCodeVerifier: (codeVerifier) => write({ codeVerifier }), | ||
| codeVerifier: async () => { | ||
| const { codeVerifier } = await read(); | ||
| if (!codeVerifier) throw new Error('No pending sign-in'); | ||
| return codeVerifier; | ||
| }, | ||
| invalidateCredentials: (scope) => | ||
| write({ | ||
| ...(scope === 'all' || scope === 'client' ? { client: undefined } : {}), | ||
| ...(scope === 'all' || scope === 'tokens' ? { tokens: undefined } : {}), | ||
| }), | ||
| redirectToAuthorization: (url) => { | ||
| console.error(`Sign in to Supabase in your browser:\n${url}`); | ||
| const openers: Partial<Record<NodeJS.Platform, string>> = { | ||
| darwin: 'open', | ||
| win32: 'start', | ||
| }; | ||
| const opener = openers[process.platform] ?? 'xdg-open'; | ||
| spawn(opener, [url.href], { stdio: 'ignore', detached: true }).unref(); | ||
| }, | ||
| }; | ||
|
|
||
| async function waitForCode() { | ||
| const server = createServer((req, res) => { | ||
| const code = new URL(req.url ?? '/', REDIRECT_URL).searchParams.get('code'); | ||
| res.end(code ? 'Signed in. You can close this tab.' : 'Missing code.'); | ||
| if (code) server.emit('code', code); | ||
| }); | ||
| server.listen(3112, '127.0.0.1'); | ||
| await once(server, 'listening'); | ||
| const [code] = await once(server, 'code'); | ||
| server.close(); | ||
| return String(code); | ||
| } | ||
|
|
||
| /** Signs in if needed and returns a token getter that refreshes on expiry. */ | ||
| export async function login(serverUrl: string) { | ||
| if ((await auth(provider, { serverUrl })) === 'REDIRECT') { | ||
| await auth(provider, { serverUrl, authorizationCode: await waitForCode() }); | ||
| } | ||
| // Refresh tokens are single use, so concurrent callers share one refresh. | ||
| let refreshing: Promise<unknown> | undefined; | ||
| return async () => { | ||
| let { tokens, expiresAt = 0 } = await read(); | ||
| if (!tokens || expiresAt < Date.now() + 60_000) { | ||
| refreshing ??= auth(provider, { serverUrl }).finally(() => { | ||
| refreshing = undefined; | ||
| }); | ||
| await refreshing; | ||
| ({ tokens } = await read()); | ||
| } | ||
| if (!tokens) throw new Error('Not signed in'); | ||
| return tokens.access_token; | ||
| }; | ||
| } |
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.
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.
Note
@modelcontextprotocol/clientbecomes a runtime dep with my changes so--oauthcan use the SDK'sauth()flow. Platform already has it at the same catalog version as a devDependency of mgmt-api, so the next bump will pull it (plus some small transitives) into the image as install weight. Platform doesn't reach the code that uses it though.