Sourcegraph CLI
$ src Official Med RiskOfficial Sourcegraph CLI (src) enables cross-repository code search with -json output, raw GraphQL queries via src api, and large-scale Batch Changes automation. Headless agents authenticate cleanly via SRC_ENDPOINT and SRC_ACCESS_TOKEN.
brew install sourcegraph/src-cli-v2/src-cli src search -json and src api cover global code search and GraphQL queries without standing up an MCP server, while src batch preview validates multi-repo changesets.
Agent-Readiness Checklist
Static evaluation (not yet probed on this host). Run npm run data:probe locally to capture live verification.
FCRS-1.0 6-Dimension Breakdown
Supports src search -json, src api JSON output, SRC_ACCESS_TOKEN headless auth, and src batch preview.
src search -jsonandsrc apiemit machine-parseable JSONSRC_ENDPOINT+SRC_ACCESS_TOKENenv vars avoid interactive login
Token-based auth and src batch preview dry-run checks are strong, though src batch apply opens remote PRs and --get-curl leaks tokens to stdout.
src batch previewvalidates changesets before remote PR creation--get-curlprints raw Authorization headers
Single static Go binary available via custom Homebrew tap sourcegraph/src-cli-v2/src-cli, go install, and GitHub releases.
- Cross-platform Go binary for macOS, Linux, and Windows
Searches across hundreds of remote repositories in one call and automates cross-repo Batch Changes.
- Combines search, GraphQL
src api, and Batch Changes
Official first-party CLI maintained by Sourcegraph (sourcegraph/src-cli) under Apache-2.0.
- Official Sourcegraph repository under Apache-2.0
Official CLI reference docs at sourcegraph.com/docs/cli and per-subcommand -h help.
- Structured reference documentation online and via
-h
Security, Auth & Guardrails
Reads SRC_ENDPOINT and SRC_ACCESS_TOKEN env vars, or stores credentials from src login in ~/.config/sourcegraph (or OS keychain).
CLI is Apache-2.0 open-source; connecting to private Sourcegraph instances requires an account/license.
# Sourcegraph CLI (src) Agent Rules - Authenticate via `SRC_ENDPOINT` + `SRC_ACCESS_TOKEN` and always pass `-json` on `src search`. - Run `src batch preview -f <spec>` before `src batch apply`; never pass `--get-curl` in shared logs (it prints raw tokens).
Agent-Ready Command Recipes
Safe Read-Only Queries
src search -json 'repo:^github.com/my-org/ lang:go context.Background'
Queries a Sourcegraph instance and outputs structured JSON match payloads.
src api -query='query { currentUser { username } }' Executes a read-only GraphQL query returning JSON for scripting or auth verification.
Controlled Writes & Previews
src batch preview -f batch.yaml
Validates and previews multi-repo changesets on Sourcegraph without opening pull requests.
Strengths for Agents
- +
src search -jsonandsrc apiprovide clean JSON across all indexed remote repositories. - +
src batch previewenables safe validation before executing multi-repo changesets.
Caveats & Pitfalls
- !
src searchuses single-dash-jsonrather than--json. - ! Debug flag
--get-curlprints the rawSRC_ACCESS_TOKENto stdout.