HTTP and MCP
All routes live under the /sourcegraph prefix of the instance's origin. Query routes are under /sourcegraph/v1/ (search is /sourcegraph/api/search), management routes under /sourcegraph/api/admin/, and the MCP endpoint is /sourcegraph/mcp. Both surfaces call the same service, so revisions, limits and error codes are identical. For how to choose a credential see Connecting Agents, Scripts and the CLI to Source Graph.
Optional service. Context does not require Source Graph or its MCP server. When neither is installed or configured, Context's query skill still reads code evidence locally through a shallow, sparse partial clone and grep; in its default
automode it prefers Source Graph only when an authorized instance is available.
Request conventions
- Repository — routes that act on one repository take it as the
repoquery parameter,owner/repository. - Query parameters — only
repoandafterare accepted, plusrevisionon/v1/repo/plugins,branchon/api/admin/v1/repo/commit-options, andkind,query,author,title,since,until,sortandfirston/v1/repo/refs. Unknown or repeated parameters return400 INVALID_QUERY_PARAMETERS. - Bodies — JSON objects of at most 64 KiB. Unknown fields,
nullvalues, duplicate keys and trailing data are rejected. Query-route fields use capitalized names (Revision,Pattern); management routes use lowercase names (name,policy). - Credentials —
Authorization: Bearer <service token>orBearer sgk_…for a personal key; otherwise a browser session or anonymous access. An invalid credential returns401and never falls back to anonymous access. - Time — each request has a 30-second processing budget; a request that exceeds it returns
503 QUERY_DEADLINE_EXCEEDED, and an unexpected dependency failure returns503 DEPENDENCY_UNAVAILABLE.
Query routes
These are available anonymously when public reading is enabled, and to every credential, for the repositories that caller can see: anonymous callers see public repositories only, and private repositories are visible to their creator, managers, administrators signed in on the website and the service token. A private repository the caller cannot see answers 404 REPOSITORY_NOT_FOUND, exactly like an unregistered name.
| Method | Path | Body | Returns |
|---|---|---|---|
| GET | /v1/capabilities | — | Protocol and supported operations: search, read, list, diff, resolve, refs, availability, commits and prepare |
| GET | /v1/repos | — (after for the next page) | Repositories visible to the caller, 100 per page; signed-in callers also get each repository's managers |
| GET | /v1/repo/availability | — | One repository: retained versions, heads, groups and jobs, its code_provider and the code_host pinned at registration, with an ETag |
| GET | /v1/repo/commits | — | Same as availability |
| GET | /v1/repo/plugins | — (optional revision) | Repository plugins found at that commit, without running them |
| GET | /v1/repo/refs | — (optional kind, query, author, title, since, until, sort, first, after) | One page of the repository's branches or tags; see Branches, tags and resolve |
| POST | /v1/repo/resolve | {"Revision"} | The fixed commit for a branch, tag or full SHA, how it was found and whether it is indexed |
| POST | /api/search | {"Revision","Pattern","FixedStrings","IgnoreCase","Glob","Paths","Output"} | Search hits and Meta; see Query Syntax |
| POST | /v1/repo/read | {"Revision","Path","StartLine","EndLine"} | One file range |
| POST | /v1/repo/list | {"Revision","Path","First","After"} | Direct children of one directory |
| POST | /v1/repo/diff | {"Base","Head","Paths","First","After"} | Changed files and patches between two commits |
Revision is optional everywhere except diff, where Base and Head are required. Only resolve accepts branch and tag names that are not monitored; every other route takes a monitored branch or a full SHA, so resolve any other branch or tag first and use the returned SHA. read_many has no HTTP route; it exists only as an MCP tool.
Branches, tags and resolve
resolve — the MCP tool or POST /v1/repo/resolve — turns a revision into one full commit. It never downloads code or builds an index.
revision | Resolution | source |
|---|---|---|
Omitted, or a monitored branch (also as refs/heads/<name>) | The last synchronized head. The remote is not asked, so it may lag behind the remote's latest commit; a bare name equal to a monitored branch is not checked for a same-named tag, so pass refs/tags/<name> for the tag | observed |
| A full 40-character lowercase SHA | An observed or retained version of this repository; otherwise REVISION_NOT_ELIGIBLE | observed |
Any other branch or tag name, refs/heads/<name> or refs/tags/<name> | The repository's ref catalog when it is fresh and not truncated; otherwise an exact lookup on the remote, which downloads only that ref's commit and tag objects, without file trees, into a scratch directory, confirms the object type and commit details, and deletes it | catalog or remote |
A short SHA, HEAD, a revision expression, another ref namespace or a name with characters the service does not accept, such as spaces or parentheses | Rejected with SHORT_SHA_UNSUPPORTED or INVALID_REVISION; such names still appear in refs | — |
| Output field | Meaning |
|---|---|
commit, indexed, committer_time | The full SHA, whether it has a search index, and its committer time |
kind | branch, tag or commit (a SHA was passed) |
source | observed, catalog or remote, as in the table above |
monitored | Whether the branch is one of the repository's monitored branches |
ref | The full ref name, such as refs/heads/main, when known |
created_at, author, title | When known: the commit time, author and title; for an annotated tag, the tagging time, the tagger and the first line of the tag message, while committer_time stays the commit's |
hint | Present when the commit has no index: this caller may prepare the full SHA, or a repository manager can |
A bare name that is both a branch and a tag returns REF_AMBIGUOUS listing both; pass the full form. A missing name returns REF_NOT_FOUND, and a tag that ends at a tree or blob returns REF_NOT_COMMIT. Resolution uses the ref as actually downloaded, so a branch that moves between listing and download resolves to the downloaded commit; an object that cannot be verified returns a retryable REMOTE_UNAVAILABLE. Identical concurrent remote lookups share one request, and their answers, including "not found", are cached briefly (see Limits). Visibility is checked first, so a private repository the caller cannot see answers 404 REPOSITORY_NOT_FOUND without asking the remote. Over HTTP the response carries the same fields together with the rest of the stored version record.
refs — the MCP tool or GET /v1/repo/refs?repo= — lists one page of a registered repository's branches or tags. Listing never indexes.
| Parameter | Values | Default |
|---|---|---|
kind | branch, tag or all | branch |
query | Case-insensitive substring of the branch or tag name | — |
author | Case-insensitive substring of the author's name or email; the tagger for annotated tags | — |
title | Case-insensitive substring of the commit title or the first line of an annotated tag's message | — |
since, until | RFC 3339 or YYYY-MM-DD, compared with created_at | — |
sort | created (newest first) or name | created; name for a names-only catalog |
first | 1–100 | 30 |
after | The previous page's next_cursor, with the other parameters unchanged | — |
Invalid values return 400 with INVALID_KIND, INVALID_SORT, INVALID_LIMIT, INVALID_CURSOR or INVALID_TIME.
| Response field | Meaning |
|---|---|
repo | The repository |
catalog | state, reason, refreshed_at, total and truncated, described below |
sort | The order applied: created or name |
refs | Entries with name, ref, kind, monitored and indexed. Entries verified to end at a commit add commit, created_at, committer_time, author and title; annotated tags add annotated; tags of trees, blobs or unverifiable objects carry object and target_type instead of commit |
next_cursor, has_more | Pass next_cursor as after while has_more is true |
hint | Guidance, for example that a truncated list covers only part of the repository |
created_at is the commit time for branches and lightweight tags and the tagging time for annotated tags.
catalog.state | Meaning |
|---|---|
ready | A fresh catalog with commit details |
stale | The catalog expired or its refresh failed; the last successful catalog is returned with its refreshed_at while a refresh runs in the background |
names_only | Names without commit details. catalog.reason is filter_unsupported (the remote cannot send commit objects without trees), ref_limit (more than 200,000 branches or tags) or size_limit (the download exceeded 256 MiB). Sorting defaults to name; an explicit author, title, since, until or sort=created returns REFS_FILTER_UNAVAILABLE |
catalog.truncated: true means only the first 200,000 names of a kind are listed and query searches only those; resolve with a complete name does not depend on the catalog. The cursor is an offset, so a refresh between pages can repeat or skip a few entries.
Catalogs live in service memory. A refresh lists the remote first, so the ref limit applies before anything is downloaded, then downloads branch and tag commit and tag objects, without file trees, into a scratch directory that is parsed and deleted. A catalog stays fresh for 5 minutes or three times its last refresh duration, whichever is longer; an expired catalog is served while one background refresh per repository runs. Without any catalog, a request waits for the first refresh within its remaining deadline less 2 seconds and otherwise returns REFS_CATALOG_PREPARING with Retry-After while the refresh continues; resolving a complete name works meanwhile. A failed refresh without an earlier catalog returns the remote error; with one, the stale catalog is returned.
Management routes
These need the service token or an administrator's browser session. A repository's managers can also use every ?repo= route on that repository, and others get 403 REPOSITORY_MANAGEMENT_DENIED. When the site lets signed-in users register repositories, any signed-in user can register one and becomes its manager; otherwise registration returns 403 REGISTRATION_DISABLED. Users, site settings, global code-source tokens and other site-wide routes stay administrator-only. Personal API keys can call only POST /api/admin/v1/repo/prepare here, for repositories their owner manages or, when the owner is an administrator, any repository the key can see; every other management route returns 403 ACCESS_DENIED. A few read routes — GET /api/admin/v1/repos, GET /api/admin/v1/repo and GET /api/admin/v1/statistics — are also open to anonymous reading, limited to what the caller can see; statistics leave out the names of repositories the caller cannot see.
| Method | Path | Body or headers | Purpose |
|---|---|---|---|
| GET | /api/admin/v1/statistics | — | Request and job statistics, and the latest index collection round |
| GET | /api/admin/v1/instances | — | Instance heartbeats |
| GET | /api/admin/v1/repos | — | Repository catalog |
| POST | /api/admin/v1/repos | Registration fields; optional Idempotency-Key | Register a repository (202); a name that is already registered returns 409 REPOSITORY_EXISTS with the registrant in creator |
| GET | /api/admin/v1/repo | — | Repository with ETag |
| PATCH | /api/admin/v1/repo | Update fields; If-Match revision | Change branches, window, visibility, interval, path groups, pinned snapshots and storage budget; enable, disable or delete |
| POST | /api/admin/v1/repo/preview | Update fields | Preview an update |
| POST | /api/admin/v1/repo/prepare | {"kind","commit","group"}; optional Idempotency-Key | Queue sync or index work (202) |
| GET | /api/admin/v1/repo/commit-options | — (optional branch) | Recent synchronized commits to choose from |
| POST | /api/admin/v1/repo/index/preview, /api/admin/v1/repo/index | {"branch","commit"}; the second also preview and If-Match | Add an index for one commit |
| POST | /api/admin/v1/repo/cleanup/preview, /api/admin/v1/repo/cleanup | {"preview"} and If-Match for the second | Remove old indexes, keeping branch-head and pinned ones |
| POST | /api/admin/v1/repo/delete/preview, /api/admin/v1/repo/delete | {"preview"} and If-Match for the second | Permanently delete repository data |
| POST | /api/admin/v1/repo/jobs/cancel | {"job_id"} | Cancel a job |
| POST | /api/admin/v1/repo/purge | If-Match revision | Purge a disabled, deleted repository |
| POST | /api/admin/v1/repo/managers | {"add":"<user id>"} or {"remove":"<user id>"}; If-Match revision | Add or remove a manager (at most 50); an unknown or disabled user returns 404 USER_NOT_FOUND |
| GET | /auth/users/search?q= | — | Signed-in users find up to 20 enabled users by name or email to choose a manager |
Registration and PATCH also accept git_timeout_minutes (0 inherits the site default; 15, 30, 45 or 60 override it) and public_read (false makes the repository private). PATCH also accepts pinned_commits, full SHAs that neither retention nor capacity release ever removes (at most 4096, otherwise PIN_LIMIT); pins are added only here, never by preparation. It also accepts max_storage_bytes, the repository's storage budget from 1 GiB to 10 TiB (default 100 GiB); when usage reaches it, the oldest unprotected snapshots are released first. Repository details add can_manage, and for signed-in callers creator and managers; GET /access-policy adds user_repositories. Registration fields are listed in Registering and Managing Repositories.
Manual preparation is paced per repository across the website, HTTP and MCP. A request identical to a queued or running job (same group, kind and revision) returns that job and adds no queue load. Any other new manual request within 30 seconds of the repository's last accepted one returns 429 RATE_LIMITED with Retry-After; one request that queues every monorepo group counts once, and scheduled work is not paced.
Health, sign-in and settings routes
| Path | Access | Purpose |
|---|---|---|
GET /healthz | Open | Liveness: {"status":"alive"} |
GET /readyz | Open | Readiness; 503 when the catalog cannot be read |
GET /access-policy | Open | public_read, public_prepare (always false) and sso_enabled |
GET /site-settings | Open | Public repository defaults |
GET /v1/code-sources | Open | The configured code sources in display order — id, name, host, username, anonymous, editable, and locked for fields set by the startup file; never tokens |
/auth/… | Browser | Providers, sign-in and callback, current user, sign-out, api-keys, user administration |
/auth/code-credentials | Signed-in session | Your own GitHub and custom Git tokens |
/api/admin/settings/… | Administrator session only | Site settings sections, including code_source (PATCH with revision and values name, host, username), plus code-credentials, query-errors, jobs/clear, storage |
Site administration under /api/admin/settings refuses any request that carries an Authorization header — it is reachable only from a signed-in administrator's browser.
MCP endpoint
/sourcegraph/mcp speaks stateless Streamable HTTP with JSON responses; the server identifies itself as context-sourcegraph. Every tool works only on repositories the credential can see. The toolset depends on the credential:
| Credential | Tools |
|---|---|
| Anonymous (public reading on) | Read-only: every tool except prepare |
| Personal key whose owner is an administrator or manages at least one repository | Read-only tools plus prepare, for repositories the owner manages or, for an administrator, any repository the key can see; rate-limited to 10 per key and 60 in total per minute |
| Other personal keys | Read-only: every tool except prepare |
| Service token | All tools |
Whether a key's owner manages a repository is cached for up to a minute, so a newly added manager may see prepare after a short delay. Each prepare call is still checked against the current role and manager list.
The optional connection parameter ?plugins= sets the default plugin selection for read and read_many: auto, off, or up to 16 comma-separated names. Invalid values return 400 INVALID_PLUGINS.
MCP tools
| Tool | Required | Optional (default) |
|---|---|---|
repositories | — | after |
availability | repo | job_id, include_plugins, revision |
resolve | repo | revision (a branch or tag name, refs/heads/<name>, refs/tags/<name> or a full SHA) |
refs | repo | kind (branch), query, author, title, since, until, sort (created), first (30, range 1–100), after |
search | repo, pattern | revision, fixed_strings, ignore_case, glob, paths, output (content or files), max_lines_per_file (10, range 1–1000) |
read | repo, path | revision, start_line (1), end_line (start + 499), plugins |
read_many | repo, files (1–10 items of path, start_line, end_line) | revision (resolved once for the batch), plugins |
list | repo, first (1–1000) | revision, path, after |
diff | repo, base, head, first (1–100) | paths, after |
prepare | repo, idempotency_key (8–128 bytes) | revision, group |
resolve and refs work as described in Branches, tags and resolve. The other tools accept a monitored branch or a full SHA and return BRANCH_NOT_MONITORED for any other branch: find it with refs if needed, resolve it, and pass the returned SHA. When indexed is false, someone allowed to prepare that repository prepares the SHA first.
plugins is a list such as [{"name":"context-evidence"}]; [] disables enhancements for that call, and a connection opened with plugins=off always disables them. prepare accepts an omitted revision (default branch), a monitored branch or a full 40-character SHA. A SHA that has not been synchronized is fetched as an ordinary snapshot: preparation does not pin it, so retention and the storage budget release it in order like any other snapshot, and only pinned_commits keeps it permanently, as described in Registering and Managing Repositories. It is asynchronous: a repeated identical request returns the queued or running job, a different new request within 30 seconds returns RATE_LIMITED with the seconds to wait, and you poll availability afterwards.
Treat the repository, the original full SHA and the path as one source identity: do not reconstruct a SHA or reuse it in another repository. Group read_many requests by repository and revision, use paths returned by search or list, and give end_line as an inclusive absolute line number, not a line count.
availability over HTTP also returns job_failures, a bounded history of failed task summaries whose retention is described in Ops: Day-to-Day Operations. Over MCP, availability with a specific job_id returns job_failure and job_lookup.state=archived_failure when the live task has been pruned but its failure summary remains. This is historical evidence, not current readiness or a task that can be resumed, and a missing history does not mean the task succeeded.
MCP output
search, read and read_many return readable text rather than raw JSON:
- search starts with
Repository,Commit,Status,Partial,Truncated, aCoverageline, registeredPathsandGroups, and the returned and total file counts, then lists eachFilewith numbered matching lines. - read starts with
Repository, thenPath,CommitandLines: start-end, continuation fields when more remains (Truncated,HasMore,NextStartLine), notes for symlinks, submodules and LFS pointers, then numbered linesN: text. - read_many returns one text block per item, each with
Item N; RequestedLines, anErrorline if that item failed, and the same file header and lines.
Plugin output is appended after the item it belongs to; plugin diagnostics come in a separate block and never replace the original text. The other tools return compact JSON; resolve returns commit, indexed, committer_time, kind, source and monitored plus ref, created_at, author, title and hint when present, and refs returns the page described in Branches, tags and resolve. Errors come back as a tool result marked as an error, whose text is {"code","message"}, with a recovery hint for not-ready states.
Limits
| Limit | Value |
|---|---|
| Catalog page | 100 repositories |
| Search pattern | 8,192 bytes; cannot match a newline or the empty string |
| Search globs | 64, each up to 1,024 bytes |
| Search or diff paths | 64 directories |
| Search page | 100 files and 1,000 matches with 2 context lines, then smaller fallbacks; 2 MiB response; 5-second deadline |
| Engine match cap | 10,000 matches |
| Read | 500 lines and 256 KiB per call |
read_many | 1–10 items, 1,000 lines, 256 KiB and 30 seconds shared |
| List page | 1–1,000 entries |
| Diff page | 1–100 files; patches 64 KiB per file and 256 KiB total |
| Refs page | 1–100 entries, default 30 |
| Ref catalog size | 200,000 branches and 200,000 tags with details; beyond that, names only and truncated |
| Ref catalogs in memory | 1,000,000 refs across repositories; the least recently used catalogs are dropped beyond that |
| Ref catalog freshness | 5 minutes or three times the last refresh duration, whichever is longer; one refresh per repository and 4 per instance at a time |
| Ref scratch downloads | 256 MiB per catalog refresh, 16 MiB per resolve lookup, 4 GiB across all downloads, always within the instance free-space reserve |
| Remote ref lookups | 2 per repository and 8 per instance at a time, 64 running or waiting; answers, including "not found", cached for 30 seconds (10,000 entries); backoff after failures from 10 seconds to 5 minutes per repository |
| Snapshots | 4,096 per path group; the oldest unprotected snapshot is released to make room |
| Pinned snapshots | 4,096 per repository |
| Request body | 64 KiB |
Error format
HTTP errors return a JSON body with an Error message and Meta containing Protocol, RequestID, Status, Code and Retryable; a refusal with a known earliest retry time, such as paced preparation or a busy remote, also sets Retry-After. Transport-level codes include NOT_FOUND, METHOD_NOT_ALLOWED, INVALID_QUERY_PARAMETERS, INVALID_REQUEST, AUTHENTICATION_REQUIRED, ACCESS_DENIED, RATE_LIMITED, PRECONDITION_REQUIRED, QUERY_DEADLINE_EXCEEDED and DEPENDENCY_UNAVAILABLE. Operation codes such as INDEX_NOT_READY, REVISION_NOT_ELIGIBLE, INVALID_PATTERN or READ_BUDGET_EXCEEDED carry their own status, as do the management codes REPOSITORY_MANAGEMENT_DENIED, REGISTRATION_DISABLED, REPOSITORY_EXISTS and USER_NOT_FOUND, the pin and budget validation codes PIN_LIMIT and INVALID_STORAGE_BUDGET, and the Git fetch codes such as GIT_REPOSITORY_UNAVAILABLE, which covers both a missing repository and one the credential cannot see. REPOSITORY_NOT_FOUND is returned both for an unregistered name and for a private repository the caller cannot see.
Branch, tag, remote and capacity codes:
| Code | Status | Meaning |
|---|---|---|
REF_NOT_FOUND | 404 | No branch or tag with that name exists on the remote; names are case-sensitive, so search with refs |
REF_AMBIGUOUS | 409 | A branch and a tag share the name; pass refs/heads/<name> or refs/tags/<name> |
REF_NOT_COMMIT | 409 | The tag ends at a tree or blob, so it cannot be used to read code |
REFS_CATALOG_PREPARING | 503 | The first listing is still being prepared; retry after Retry-After, or resolve a complete name meanwhile |
REFS_FILTER_UNAVAILABLE | 409 | The catalog has names only; filter with query and sort by name |
BRANCH_NOT_MONITORED | 404 | An operation other than resolve received a branch that is not monitored; resolve it to a SHA first |
REMOTE_ACCESS_DENIED | 502 | The code-source token this repository uses cannot read its remote; a repository manager should check it. The caller's own key is not the cause |
REMOTE_UNAVAILABLE | 502 or 503 | The remote is unavailable, timed out or backing off, too many lookups are waiting, or a ref's object could not be verified; retryable, with Retry-After |
INSUFFICIENT_STORAGE | 503 or 507 | The instance's free disk space is below its reserve; this is not the repository budget |
RECLAIM_FAILED | 503 | Older snapshots could not be released safely, or released snapshots are not collected yet; collection continues in the background, so retry shortly |
REPOSITORY_STORAGE_LIMIT | 409 | Protected snapshots — branch heads, pins and snapshots of active jobs — alone fill the storage budget, or a running preparation exceeds 120% of it; remove pins you no longer need or raise the budget |
SNAPSHOT_LIMIT | 409 | Protected snapshots alone fill a path group's 4,096 versions; remove pins you no longer need |
Unprotected snapshots never cause SNAPSHOT_LIMIT or REPOSITORY_STORAGE_LIMIT: they are released oldest first to make room. Ops: Day-to-Day Operations explains the common codes and what to do about them.
HTTP 与 MCP 接口
所有路由都位于实例源地址的 /sourcegraph 前缀下。查询路由在 /sourcegraph/v1/ 下(搜索为 /sourcegraph/api/search),管理路由在 /sourcegraph/api/admin/ 下,MCP 端点为 /sourcegraph/mcp。两类接口调用的是同一个服务,因此版本、限制和错误码完全一致。如何选择凭据,见 连接 Agent、脚本和命令行使用 Source Graph。
可选服务。 Context 并不依赖 Source Graph 或它的 MCP 服务。两者都没有安装或配置时,Context 的查询技能仍会通过浅层、稀疏的部分克隆加 grep 在本地读取代码证据;默认的
auto模式只在有可用的已授权实例时才优先使用 Source Graph。
请求约定
- 仓库:作用于单个仓库的路由通过
repo查询参数指定仓库,格式为owner/repository。 - 查询参数:只接受
repo和after,另外/v1/repo/plugins接受revision,/api/admin/v1/repo/commit-options接受branch,/v1/repo/refs接受kind、query、author、title、since、until、sort和first。未知或重复的参数返回400 INVALID_QUERY_PARAMETERS。 - 请求体:JSON 对象,最大 64 KiB。未知字段、
null值、重复键和尾随数据都会被拒绝。查询路由的字段名首字母大写(Revision、Pattern);管理路由使用小写字段名(name、policy)。 - 凭据:使用
Authorization: Bearer <service token>,个人 Key 则为Bearer sgk_…;否则使用浏览器会话或匿名访问。无效凭据返回401,绝不会回退为匿名访问。 - 时间:每个请求有 30 秒的处理预算;超时的请求返回
503 QUERY_DEADLINE_EXCEEDED,意外的依赖故障返回503 DEPENDENCY_UNAVAILABLE。
查询路由
开启公开读取时,匿名访客可以使用这些路由;所有凭据也都可以使用,范围限于调用方能看到的仓库:匿名调用方只能看到公开仓库,私有仓库只对其创建者、管理者、在网站上登录的管理员和服务令牌可见。调用方看不到的私有仓库会返回 404 REPOSITORY_NOT_FOUND,与未登记的名称完全相同。
| 方法 | 路径 | 请求体 | 返回 |
|---|---|---|---|
| GET | /v1/capabilities | — | 协议与支持的操作:search、read、list、diff、resolve、refs、availability、commits 和 prepare |
| GET | /v1/repos | —(用 after 翻到下一页) | 调用方可见的仓库,每页 100 个;已登录调用方还会得到每个仓库的 managers |
| GET | /v1/repo/availability | — | 单个仓库:保留的版本、分支最新提交、路径组和任务,以及它的 code_provider 和登记时固定的 code_host,附带 ETag |
| GET | /v1/repo/commits | — | 与 availability 相同 |
| GET | /v1/repo/plugins | —(可选 revision) | 该提交中发现的仓库插件,不会运行它们 |
| GET | /v1/repo/refs | —(可选 kind、query、author、title、since、until、sort、first、after) | 仓库分支或标签的一页列表;见 分支、标签与 resolve |
| POST | /v1/repo/resolve | {"Revision"} | 分支、标签或完整 SHA 对应的固定提交、它的查找方式及其是否已建索引 |
| POST | /api/search | {"Revision","Pattern","FixedStrings","IgnoreCase","Glob","Paths","Output"} | 搜索命中和 Meta;见 搜索语法 |
| POST | /v1/repo/read | {"Revision","Path","StartLine","EndLine"} | 一个文件的指定行范围 |
| POST | /v1/repo/list | {"Revision","Path","First","After"} | 单个目录的直接子项 |
| POST | /v1/repo/diff | {"Base","Head","Paths","First","After"} | 两个提交之间变更的文件与补丁 |
除 diff 外,Revision 均为可选;diff 必须提供 Base 和 Head。只有 resolve 接受未被监控的分支名和标签名;其他路由都只接受监控分支或完整 SHA,因此其他分支或标签要先解析,再使用返回的 SHA。read_many 没有 HTTP 路由,只以 MCP 工具的形式提供。
分支、标签与 resolve
resolve(MCP 工具或 POST /v1/repo/resolve)把一个版本转换为一个完整提交。它从不下载代码,也不建索引。
revision | 解析方式 | source |
|---|---|---|
省略,或一个监控分支(也可写作 refs/heads/<name>) | 最近一次同步的最新提交。不会询问远端,因此可能落后于远端的最新提交;与监控分支同名的裸名称不会检查是否存在同名标签,要使用标签请传 refs/tags/<name> | observed |
| 40 位小写完整 SHA | 该仓库已观察到或已保留的版本;否则返回 REVISION_NOT_ELIGIBLE | observed |
其他任意分支名或标签名、refs/heads/<name> 或 refs/tags/<name> | 引用目录新鲜且未截断时使用仓库的引用目录;否则在远端精确查找:只把该引用的提交对象和标签对象(不含文件树)下载到临时目录,确认对象类型和提交详情后删除该目录 | catalog 或 remote |
短 SHA、HEAD、版本表达式、其他引用命名空间,或包含服务不接受字符(如空格或括号)的名称 | 拒绝,返回 SHORT_SHA_UNSUPPORTED 或 INVALID_REVISION;这类名称仍会出现在 refs 中 | — |
| 输出字段 | 含义 |
|---|---|
commit、indexed、committer_time | 完整 SHA、是否已有搜索索引,以及提交者时间 |
kind | branch、tag 或 commit(传入的是 SHA) |
source | observed、catalog 或 remote,见上表 |
monitored | 该分支是否是仓库的监控分支之一 |
ref | 已知时为完整引用名,例如 refs/heads/main |
created_at、author、title | 已知时为提交时间、作者和标题;对于附注标签,则是打标签的时间、打标签者和标签消息的第一行,而 committer_time 仍是提交的时间 |
hint | 提交没有索引时出现:说明当前调用方可以准备该完整 SHA,或者需要由仓库管理者准备 |
同时是分支和标签的裸名称返回 REF_AMBIGUOUS 并列出两者;请传完整形式。不存在的名称返回 REF_NOT_FOUND,最终指向树或 blob 的标签返回 REF_NOT_COMMIT。解析以实际下载到的引用为准,因此在列出与下载之间移动过的分支会解析到实际下载的提交;无法验证的对象返回可重试的 REMOTE_UNAVAILABLE。相同的并发远端查询共用一个请求,其结果(包括“未找到”)会短暂缓存(见 限制)。可见性最先检查,因此调用方看不到的私有仓库会直接返回 404 REPOSITORY_NOT_FOUND,不会询问远端。通过 HTTP 调用时,响应包含相同的字段,以及已存储版本记录的其余字段。
refs(MCP 工具或 GET /v1/repo/refs?repo=)列出已登记仓库的分支或标签的一页。列出操作从不建索引。
| 参数 | 取值 | 默认值 |
|---|---|---|
kind | branch、tag 或 all | branch |
query | 分支名或标签名的子串,不区分大小写 | — |
author | 作者姓名或邮箱的子串,不区分大小写;附注标签匹配打标签者 | — |
title | 提交标题或附注标签消息第一行的子串,不区分大小写 | — |
since、until | RFC 3339 或 YYYY-MM-DD,与 created_at 比较 | — |
sort | created(最新在前)或 name | created;只有名称的目录为 name |
first | 1–100 | 30 |
after | 上一页的 next_cursor,其他参数保持不变 | — |
无效值返回 400,错误码为 INVALID_KIND、INVALID_SORT、INVALID_LIMIT、INVALID_CURSOR 或 INVALID_TIME。
| 响应字段 | 含义 |
|---|---|
repo | 仓库 |
catalog | state、reason、refreshed_at、total 和 truncated,说明见下文 |
sort | 实际采用的排序:created 或 name |
refs | 条目,包含 name、ref、kind、monitored 和 indexed。已验证最终指向提交的条目还包含 commit、created_at、committer_time、author 和 title;附注标签还包含 annotated;指向树、blob 或无法验证对象的标签以 object 和 target_type 代替 commit |
next_cursor、has_more | has_more 为 true 时,把 next_cursor 作为 after 传入 |
hint | 指引信息,例如说明截断的列表只覆盖了仓库的一部分 |
对于分支和轻量标签,created_at 是提交时间;对于附注标签,则是打标签的时间。
catalog.state | 含义 |
|---|---|
ready | 新鲜的目录,含提交详情 |
stale | 目录已过期或刷新失败;返回上一次成功的目录及其 refreshed_at,同时在后台刷新 |
names_only | 只有名称,没有提交详情。catalog.reason 为 filter_unsupported(远端无法只发送不含文件树的提交对象)、ref_limit(分支或标签超过 200,000 个)或 size_limit(下载超过 256 MiB)。排序默认为 name;显式传入 author、title、since、until 或 sort=created 会返回 REFS_FILTER_UNAVAILABLE |
catalog.truncated: true 表示某一类引用只列出了前 200,000 个名称,query 也只在这些名称中搜索;用完整名称调用 resolve 不依赖目录。游标是一个偏移量,因此翻页期间如果发生刷新,可能会重复或跳过少量条目。
目录保存在服务内存中。刷新时先列出远端引用,因此在下载任何内容之前就会应用引用数量上限;随后把分支和标签的提交对象与标签对象(不含文件树)下载到临时目录,解析后即删除。目录在 5 分钟与上次刷新耗时的三倍中较长的时间内保持新鲜;目录过期后仍会继续提供,同时每个仓库在后台运行一次刷新。如果还没有任何目录,请求会在剩余截止时间减去 2 秒的时间内等待首次刷新,否则返回 REFS_CATALOG_PREPARING 并带 Retry-After,刷新继续进行;在此期间解析完整名称不受影响。刷新失败时,如果之前没有目录,则返回远端错误;如果有,则返回过期的目录。
管理路由
这些路由需要服务令牌或管理员的浏览器会话。仓库的管理者也可以对该仓库使用所有带 ?repo= 的路由,其他人会得到 403 REPOSITORY_MANAGEMENT_DENIED。站点允许已登录用户登记仓库时,任何已登录用户都可以登记仓库并成为其管理者;否则登记会返回 403 REGISTRATION_DISABLED。用户、站点设置、全局代码源令牌和其他站点级路由仍仅限管理员。个人 API Key 在这里只能调用 POST /api/admin/v1/repo/prepare,范围是其所有者管理的仓库;若所有者是管理员,则为该 Key 能看到的任何仓库。其他所有管理路由都返回 403 ACCESS_DENIED。少数读取路由(GET /api/admin/v1/repos、GET /api/admin/v1/repo 和 GET /api/admin/v1/statistics)也对匿名读取开放,但仅限调用方能看到的内容;统计信息会略去调用方看不到的仓库名称。
| 方法 | 路径 | 请求体或请求头 | 用途 |
|---|---|---|---|
| GET | /api/admin/v1/statistics | — | 请求与任务统计,以及最近一轮索引回收 |
| GET | /api/admin/v1/instances | — | 实例心跳 |
| GET | /api/admin/v1/repos | — | 仓库目录 |
| POST | /api/admin/v1/repos | 登记字段;可选 Idempotency-Key | 登记仓库(202);名称已被登记时返回 409 REPOSITORY_EXISTS,并在 creator 中给出登记人 |
| GET | /api/admin/v1/repo | — | 仓库信息,附带 ETag |
| PATCH | /api/admin/v1/repo | 更新字段;If-Match 版本 | 修改分支、保留窗口、可见性、同步间隔、路径组、固定快照和存储预算;启用、停用或删除 |
| POST | /api/admin/v1/repo/preview | 更新字段 | 预览一次更新 |
| POST | /api/admin/v1/repo/prepare | {"kind","commit","group"};可选 Idempotency-Key | 排队执行 sync 或 index 工作(202) |
| GET | /api/admin/v1/repo/commit-options | —(可选 branch) | 可供选择的最近已同步提交 |
| POST | /api/admin/v1/repo/index/preview、/api/admin/v1/repo/index | {"branch","commit"};后者还需 preview 和 If-Match | 为某个提交添加索引 |
| POST | /api/admin/v1/repo/cleanup/preview、/api/admin/v1/repo/cleanup | {"preview"},后者还需 If-Match | 删除旧索引,保留分支最新提交和已固定快照的索引 |
| POST | /api/admin/v1/repo/delete/preview、/api/admin/v1/repo/delete | {"preview"},后者还需 If-Match | 永久删除仓库数据 |
| POST | /api/admin/v1/repo/jobs/cancel | {"job_id"} | 取消任务 |
| POST | /api/admin/v1/repo/purge | If-Match 版本 | 清除已停用且已删除的仓库 |
| POST | /api/admin/v1/repo/managers | {"add":"<user id>"} 或 {"remove":"<user id>"};If-Match 版本 | 添加或移除管理者(最多 50 位);用户不存在或已停用时返回 404 USER_NOT_FOUND |
| GET | /auth/users/search?q= | — | 已登录用户按姓名或邮箱查找最多 20 位启用中的用户,用于选择管理者 |
登记和 PATCH 还接受 git_timeout_minutes(0 表示继承站点默认值;15、30、45 或 60 表示覆盖)和 public_read(false 表示将仓库设为私有)。PATCH 还接受 pinned_commits,即保留策略和容量释放都不会移除的完整 SHA(最多 4096 个,否则返回 PIN_LIMIT);固定只能在这里添加,准备不会添加固定。它还接受 max_storage_bytes,即仓库的存储预算,范围 1 GiB 到 10 TiB(默认 100 GiB);用量达到预算时,会先释放最旧的未受保护快照。仓库详情还包含 can_manage,对已登录调用方还会返回 creator 和 managers;GET /access-policy 还返回 user_repositories。登记字段见 登记和管理仓库。
手动准备在网站、HTTP 和 MCP 之间按仓库统一限速。与已排队或正在运行的任务完全相同(同一路径组、类型和版本)的请求会直接返回该任务,不增加队列负载。距该仓库上一次被接受的请求不足 30 秒的其他新手动请求,会返回 429 RATE_LIMITED 并附带 Retry-After;一次为 Monorepo 所有路径组排队的请求只计一次,定时任务不受限速。
健康检查、登录与设置路由
| 路径 | 访问权限 | 用途 |
|---|---|---|
GET /healthz | 开放 | 存活检查:{"status":"alive"} |
GET /readyz | 开放 | 就绪检查;无法读取仓库目录时返回 503 |
GET /access-policy | 开放 | public_read、public_prepare(始终为 false)和 sso_enabled |
GET /site-settings | 开放 | 公开的仓库默认值 |
GET /v1/code-sources | 开放 | 按显示顺序列出已配置的代码源——id、name、host、username、anonymous、editable,以及列出启动文件所设字段的 locked;从不返回令牌 |
/auth/… | 浏览器 | 登录提供方、登录与回调、当前用户、退出登录、api-keys、用户管理 |
/auth/code-credentials | 已登录会话 | 你自己的 GitHub 和自定义 Git 令牌 |
/api/admin/settings/… | 仅限管理员会话 | 站点设置各分区,包括 code_source(用 PATCH 提交 revision 和 values 中的 name、host、username),以及 code-credentials、query-errors、jobs/clear、storage |
/api/admin/settings 下的站点管理会拒绝任何携带 Authorization 请求头的请求,只能从已登录管理员的浏览器访问。
MCP 端点
/sourcegraph/mcp 使用无状态的 Streamable HTTP,响应为 JSON;服务端标识自己为 context-sourcegraph。每个工具都只作用于该凭据能看到的仓库。可用的工具集取决于凭据:
| 凭据 | 工具 |
|---|---|
| 匿名(已开启公开读取) | 只读:除 prepare 外的全部工具 |
| 所有者是管理员或至少管理一个仓库的个人 Key | 只读工具加上 prepare,范围是所有者管理的仓库;若所有者是管理员,则为该 Key 能看到的任何仓库;限流为每个 Key 每分钟 10 次、总计每分钟 60 次 |
| 其他个人 Key | 只读:除 prepare 外的全部工具 |
| 服务令牌 | 全部工具 |
Key 的所有者是否管理某个仓库最多缓存一分钟,因此新添加的管理者可能要稍等片刻才能看到 prepare。每次 prepare 调用仍会按当前的角色和管理者名单进行检查。
可选的连接参数 ?plugins= 用于设置 read 和 read_many 的默认插件选择:auto、off,或最多 16 个以逗号分隔的名称。无效值返回 400 INVALID_PLUGINS。
MCP 工具
| 工具 | 必填 | 可选(默认值) |
|---|---|---|
repositories | — | after |
availability | repo | job_id、include_plugins、revision |
resolve | repo | revision(分支名或标签名、refs/heads/<name>、refs/tags/<name> 或完整 SHA) |
refs | repo | kind(branch)、query、author、title、since、until、sort(created)、first(30,范围 1–100)、after |
search | repo、pattern | revision、fixed_strings、ignore_case、glob、paths、output(content 或 files)、max_lines_per_file(10,范围 1–1000) |
read | repo、path | revision、start_line(1)、end_line(起始行 + 499)、plugins |
read_many | repo、files(1–10 项,每项含 path、start_line、end_line) | revision(整批只解析一次)、plugins |
list | repo、first(1–1000) | revision、path、after |
diff | repo、base、head、first(1–100) | paths、after |
prepare | repo、idempotency_key(8–128 字节) | revision、group |
resolve 和 refs 的行为见 分支、标签与 resolve。其他工具接受监控分支或完整 SHA,对其他分支返回 BRANCH_NOT_MONITORED:需要时先用 refs 找到它,再用 resolve 解析,然后传入返回的 SHA。indexed 为 false 时,由有权准备该仓库的人先准备该 SHA。
plugins 是一个列表,例如 [{"name":"context-evidence"}];传 [] 会在本次调用中关闭增强,而以 plugins=off 打开的连接始终关闭增强。prepare 接受省略版本(默认分支)、监控分支,或 40 位完整 SHA。尚未同步的 SHA 会作为普通快照拉取:准备不会固定它,因此保留策略和存储预算会像对待其他快照一样按顺序释放它,只有 pinned_commits 才能永久保留它,详见 登记和管理仓库。它是异步的:重复的相同请求会返回已排队或正在运行的任务,30 秒内的其他新请求会返回 RATE_LIMITED 及需要等待的秒数,之后请轮询 availability。
把仓库、原始完整 SHA 和路径作为一个整体的来源标识:不要自行重建 SHA,也不要在另一个仓库中复用它。按仓库和版本对 read_many 请求分组,使用 search 或 list 返回的路径,并把 end_line 作为包含在内的绝对行号,而不是行数。
通过 HTTP 调用 availability 时还会返回 job_failures,即失败任务摘要的有限历史,其保留规则见 运维:日常运维与排障。通过 MCP 用指定的 job_id 调用 availability 时,如果实时任务已被清理但失败摘要仍在,会返回 job_failure 和 job_lookup.state=archived_failure。这只是历史证据,不代表当前已就绪,也不是可以继续执行的任务;没有历史记录也不意味着任务成功。
MCP 输出
search、read 和 read_many 返回可读文本,而不是原始 JSON:
- search 依次以
Repository、Commit、Status、Partial、Truncated、一行Coverage、已登记的Paths和Groups,以及返回文件数与总文件数开头,然后列出每个File及其带行号的匹配行。 - read 以
Repository开头,接着是Path、Commit和Lines: start-end;还有剩余内容时给出续读字段(Truncated、HasMore、NextStartLine),并附上符号链接、子模块和 LFS 指针的说明,最后是N: text格式的带行号内容。 - read_many 为每一项返回一个文本块,每块包含
Item N; RequestedLines,该项失败时有一行Error,其余为相同的文件头和内容行。
插件输出追加在所属项之后;插件诊断信息放在单独的块中,绝不会替换原始文本。其他工具返回紧凑的 JSON;resolve 返回 commit、indexed、committer_time、kind、source 和 monitored,存在时还返回 ref、created_at、author、title 和 hint;refs 返回 分支、标签与 resolve 中描述的分页结果。错误以标记为错误的工具结果返回,其文本为 {"code","message"},未就绪状态会附带恢复提示。
限制
| 限制项 | 值 |
|---|---|
| 仓库目录分页 | 100 个仓库 |
| 搜索模式 | 8,192 字节;不能匹配换行符或空字符串 |
| 搜索 glob | 64 个,每个最多 1,024 字节 |
| 搜索或差异路径 | 64 个目录 |
| 搜索分页 | 100 个文件、1,000 条匹配,带 2 行上下文,超出时逐级缩小;响应 2 MiB;截止时间 5 秒 |
| 引擎匹配上限 | 10,000 条匹配 |
| 读取 | 每次调用 500 行、256 KiB |
read_many | 1–10 项,共享 1,000 行、256 KiB 和 30 秒 |
| 列目录分页 | 1–1,000 个条目 |
| 差异分页 | 1–100 个文件;补丁每个文件 64 KiB,总计 256 KiB |
| 引用分页 | 1–100 个条目,默认 30 |
| 引用目录大小 | 200,000 个分支和 200,000 个标签(含详情);超出部分只保留名称,并标记 truncated |
| 内存中的引用目录 | 所有仓库合计 1,000,000 个引用;超出时丢弃最近最少使用的目录 |
| 引用目录新鲜度 | 5 分钟与上次刷新耗时的三倍,取较长者;每个仓库同时 1 个刷新,每个实例同时 4 个 |
| 引用临时下载 | 每次目录刷新 256 MiB,每次 resolve 查询 16 MiB,所有下载合计 4 GiB,始终遵守实例的空闲空间预留 |
| 远端引用查询 | 每个仓库同时 2 个,每个实例同时 8 个,运行中与等待中合计 64 个;结果(包括“未找到”)缓存 30 秒(10,000 条);失败后按仓库退避,从 10 秒到 5 分钟 |
| 快照 | 每个路径组 4,096 个;释放最旧的未受保护快照以腾出空间 |
| 固定快照 | 每个仓库 4,096 个 |
| 请求体 | 64 KiB |
错误格式
HTTP 错误返回一个 JSON 体,其中包含 Error 消息,以及含有 Protocol、RequestID、Status、Code 和 Retryable 的 Meta;如果拒绝时已知最早可重试的时间(例如受限速的准备请求或繁忙的远端),还会设置 Retry-After。传输层错误码包括 NOT_FOUND、METHOD_NOT_ALLOWED、INVALID_QUERY_PARAMETERS、INVALID_REQUEST、AUTHENTICATION_REQUIRED、ACCESS_DENIED、RATE_LIMITED、PRECONDITION_REQUIRED、QUERY_DEADLINE_EXCEEDED 和 DEPENDENCY_UNAVAILABLE。INDEX_NOT_READY、REVISION_NOT_ELIGIBLE、INVALID_PATTERN 或 READ_BUDGET_EXCEEDED 等操作错误码有各自的状态码;管理类错误码 REPOSITORY_MANAGEMENT_DENIED、REGISTRATION_DISABLED、REPOSITORY_EXISTS、USER_NOT_FOUND,固定与预算校验错误码 PIN_LIMIT、INVALID_STORAGE_BUDGET,以及 GIT_REPOSITORY_UNAVAILABLE 等 Git 拉取错误码也是如此,其中 GIT_REPOSITORY_UNAVAILABLE 同时涵盖仓库不存在和凭据看不到该仓库两种情况。对于未登记的名称和调用方看不到的私有仓库,都会返回 REPOSITORY_NOT_FOUND。
分支、标签、远端与容量相关错误码:
| 错误码 | 状态码 | 含义 |
|---|---|---|
REF_NOT_FOUND | 404 | 远端不存在该名称的分支或标签;名称区分大小写,请用 refs 搜索 |
REF_AMBIGUOUS | 409 | 分支和标签同名;请传 refs/heads/<name> 或 refs/tags/<name> |
REF_NOT_COMMIT | 409 | 该标签最终指向树或 blob,无法用于读取代码 |
REFS_CATALOG_PREPARING | 503 | 首次列出仍在准备中;请在 Retry-After 之后重试,或在此期间解析完整名称 |
REFS_FILTER_UNAVAILABLE | 409 | 目录只有名称;请用 query 过滤并按名称排序 |
BRANCH_NOT_MONITORED | 404 | resolve 以外的操作收到了未被监控的分支;请先把它解析为 SHA |
REMOTE_ACCESS_DENIED | 502 | 该仓库使用的代码源令牌无法读取其远端,应由仓库管理者检查。原因不在调用方自己的 Key |
REMOTE_UNAVAILABLE | 502 或 503 | 远端不可用、超时或正在退避,等待中的查询过多,或某个引用的对象无法验证;可重试,并带 Retry-After |
INSUFFICIENT_STORAGE | 503 或 507 | 实例的磁盘空闲空间低于预留值;这不是仓库的存储预算 |
RECLAIM_FAILED | 503 | 无法安全释放较早的快照,或已释放的快照尚未回收;回收会在后台继续,请稍后重试 |
REPOSITORY_STORAGE_LIMIT | 409 | 受保护的快照(分支最新提交、固定快照和活动任务所用的快照)本身就占满了存储预算,或正在进行的准备超出预算的 120%;请移除不再需要的固定或提高预算 |
SNAPSHOT_LIMIT | 409 | 受保护的快照本身就占满了某个路径组的 4,096 个版本;请移除不再需要的固定 |
未受保护的快照从不导致 SNAPSHOT_LIMIT 或 REPOSITORY_STORAGE_LIMIT:它们会按从旧到新的顺序被释放以腾出空间。运维:日常运维与排障 解释了常见错误码及处理方法。