Repository navigation
feat: temporary read-only CLI tokens authorized through MCP - #29
Merged
Merged
Conversation
An agent that only has MCP configured can let a command line next to it
call the HTTP API as the same user, read-only, without the token passing
through the model.
- MCP tool authorize_cli returns a one-time code (10 minutes, stored as a
digest), host (the API base the MCP request used, SOURCEGRAPH_URL form)
and exchange_path; POST <host>/auth/cli/exchange {code} creates a
temporary read-only personal token with the caller token's scope.
- Temporary tokens: read-only (retrieval, MCP retrieval tools, contents
preparation; everything else 403 TOKEN_READ_ONLY), no site
administration, 3 hours after their last admitted use and at most 24
hours, fixed scope, 20 per user, deleted with their parent or when the
parent is rescoped or a tag in scope is renamed.
- Stores: SQL and MongoDB implement the temporary-key and code methods;
MongoDB removes temporary keys with a TTL index on max_expires_at, SQL
rows expire through the hourly prune.
- Website labels temporary tokens and hides their visibility button; CLI
tokens list shows a kind column; CLI 0.6.0; skill and docs updated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Background
Agents often have only MCP configured: the service address and a personal token live in the MCP client settings. A command line running next to the agent (for example a knowledge tool that verifies source citations through the raw-read HTTP routes) has neither the address nor a token, and the token must not pass through the model's context.
Changes
authorize_cli(optionalclient): returns a one-timecode(single use, 10 minutes, stored as a digest),host(the API base address this MCP request came in on, the same form asSOURCEGRAPH_URL) andexchange_path(/auth/cli/exchange). Listed on personal-token and anonymous connections; without a token it answers401 LOGIN_REQUIRED. 30 authorizations a minute per user.POST <host>/auth/cli/exchange{code}(no sign-in): takes the code atomically and creates a temporary read-only personal token with the authorizing token's current scope. 20 requests a minute per client address, 3000 in total;400 INVALID_CODE,429 TEMPORARY_TOKEN_LIMIT(20 unexpired temporary tokens per user),503 SITE_FROZENon a frozen site.temporary,read_only,parent_id,max_expires_aton the API key):/authsession routes and site settings, answers403 TOKEN_READ_ONLY;PATCHanswers400 TEMPORARY_TOKEN_SCOPE_FIXED);host: derived from the configured origin (or the matched extra public site) plus the external route prefix. A request forwarded by another site'sforward_hostsreports the caller's address only when a trusted proxy delivered it and the address is one of thesitesinconfig/deployment.json(new optionalaliasesfield read by the server).GetAPIKey,CountTemporaryAPIKeys,ExtendAPIKey(never shortens),PutCLICode/TakeCLICode(single use: one serializable transaction on SQL,FindOneAndDeleteon MongoDB), cascade deletion and scope refusal. MongoDB gets a TTL index onmax_expires_at(ordinary keys lack the field); SQL rows carryexpires_atand are removed by the existing hourly prune, codes included. No SQL schema change: keys and codes are documents insourcegraph_documents.sourcegraph tokensshows akindcolumn; version 0.6.0. Skill anddocs/interfaces.mddescribe the flow.GET /v1/capabilitieslistscli-authorization.Risks
X-Forwarded-Hostamong the deployment's own configured site addresses for its own response; arbitrary hosts and untrusted peers are ignored.Compatibility
Existing tokens have none of the new fields and behave as before; CLI sign-in and existing routes and tools are unchanged. MCP gains one tool. Upgrading needs no migration (verified on Postgres by upgrading a deployment of current
main).Testing
gofmt,go vet ./...,go test -p 1 ./...,go test -raceonsso,httpserver,mcpserver,storagecontract;bun run check;scripts/audit-public.py.internal/storagecontract) with the new temporary-key and code cases on SQLite, PostgreSQL 17 and MongoDB 8 (replica set): pass.main:authorize_cli→ exchange → read allowed (repos, capabilities, availability, search) / denied matrix (TOKEN_READ_ONLY) → temporary MCP tool list → code reuseINVALID_CODE→ listing fields → fixed scope → delete parent →401: all pass; existing remote-read e2e on the upgraded deployment passes.🤖 Generated with Claude Code