Skip to content

Architecture Overview ​

This page describes how Context Source Graph works: the operations it shares across its four surfaces, how one instance is put together and who can do what. It is a self-hosted Go application built on Git and the Zoekt trigram index; every request resolves to one exact commit before anything is read. For why it exists, what it is good for and how to start, read What Is Source Graph.

mermaid
flowchart LR
  callers["People, scripts<br/>and AI Agents"] --> surfaces["Website · HTTP · MCP · CLI"]
  surfaces --> service["repo-service<br/>one shared contract"]
  service --> git["Git mirrors<br/>per repository"]
  service --> engine["Zoekt index<br/>child process"]
  service --> db["Application database<br/>SQLite, PostgreSQL or MongoDB"]
  git -->|background sync and indexing| engine

The source code is at github.com/context4ai/sourcegraph. The name does not imply any affiliation with the Sourcegraph platform.

One contract, four surfaces ​

The website, HTTP, MCP and the CLI share these operations; read_many is available through MCP only, and refs through MCP and HTTP:

OperationWhat it does
repositoriesList registered repositories and their observed branches
availabilityShow a repository's retained versions and indexing status
refsFind a repository's branches and tags, with each one's commit, time, author and title and whether that commit is indexed
resolveTurn a branch, a tag or an eligible full SHA into the fixed commit to use next, without downloading code
searchSearch file contents at one commit, shaped like rg
readRead a line range of one file at one commit
read_manyRead up to ten known ranges from one repository at one commit in a single MCP call
listList one directory at one commit
diffCompare two eligible commits
prepareAsk for a commit to be synchronized and indexed (authorized callers only)

Operations that read code take a monitored branch or a full SHA; only resolve also accepts other branches and tags, so a caller working from one of those resolves it first and carries the returned SHA. The website lives under /sourcegraph/, the MCP endpoint at /sourcegraph/mcp, and the HTTP API under /sourcegraph/v1/ and /sourcegraph/api/. The command-line client sourcegraph-cli calls the same HTTP routes. Exact routes, tool inputs and limits are in the HTTP and MCP and CLI references.

How an instance is put together ​

A Source Graph instance is a single repo-service process. It starts the Zoekt search server as a child process listening only on the loopback interface, keeps a bare Git mirror for each registered repository on its data volume, and runs background workers that synchronize snapshots of monitored branch heads, index new heads and collect expired indexes. Each repository has a storage budget for its Git objects and indexes; when it fills, the oldest snapshots that are not branch heads, pins or in use by a job are released rather than new work being refused. A separate background pass finishes the Git collection of released snapshots, so space is recovered even after an interrupted job.

Branches and tags beyond the monitored ones are discovered without touching the mirrors. For refs and resolve, the service keeps an in-memory catalog of each repository's branches and tags, built by listing the remote and downloading only commit and tag objects, without file trees, into a scratch directory that is deleted as soon as it is parsed. A single name that is not in a fresh, complete catalog is looked up on the remote the same way. Discovery never indexes or adds versions; a commit becomes searchable only when it is prepared.

The application database stores repositories, jobs, identities and settings, and also the instance's application secrets: code-source tokens with their encryption key, the service API credential and the Google/GitHub sign-in configuration, all encrypted. The data volume therefore holds only Git mirrors and indexes, and a replacement host needs the database rather than an old key file. SQLite is the default; PostgreSQL and MongoDB are supported for larger or sharded deployments. Git mirrors and indexes always stay on the instance's local disk — search speed comes from that locality — so capacity is planned around disk rather than CPU. To grow, run several instances that each own a disjoint set of repositories; never run two instances over the same repositories or data directory. Ops: Self-Hosting Source Graph covers both topologies.

Repositories can also ship WebAssembly plugins named <name>.sourcegraph.wasm that add text to read and read_many results. Context uses this to attach each knowledge section's sources, described in Evidence for Context.

Who can do what ​

Without any sign-in provider configured, an instance is an anonymous, read-only site: visitors can search and read public repositories, subject to per-minute rate limits. Adding Google or GitHub sign-in enables administration, and the first verified user becomes the administrator. Each repository is public or private: a private repository is visible only to its creator, its managers, administrators signed in on the website and the service token, and to everyone else it looks unregistered. Signed-in users can create personal API keys for scripts and Agents, which read as their owner, and a service token covers automation. Registering repositories, indexing older commits and other changes require an administrator or the service token, unless the site lets signed-in users register repositories: each such user then manages the repositories they register and can add other managers. Ops: Sign-in and Permissions lists the full matrix.

Where to go next ​

To…Read
Learn why Source Graph exists and how to startWhat Is Source Graph
Understand pinned search and the speed comparisonPinned Search and Reads
See how Context knowledge and Source Graph combineEvidence for Context
Connect a browser, MCP client, script or the CLIConnecting Agents, Scripts and the CLI to Source Graph
Add repositories and monorepo path groupsRegistering and Managing Repositories
Deploy your own instanceOps: Self-Hosting Source Graph
Configure sign-in and API keysOps: Sign-in and Permissions
Run, monitor and troubleshoot itOps: Day-to-Day Operations
Look up routes, tools, flags and settingsHTTP and MCP, CLI, Query Syntax, Configuration

架构概览 ​

本页说明 Context Source Graph 如何工作:四个入口共享的操作、单个实例如何组成,以及谁能做什么。它是一个基于 Git 和 Zoekt 三元组(trigram)索引、可自部署的 Go 应用;每个请求在读取任何内容之前都会先解析到一个确定的提交。它为什么存在、适合做什么以及如何开始,请阅读 Source Graph 是什么。

mermaid
flowchart LR
  callers["人、脚本<br/>和 AI Agent"] --> surfaces["网站 · HTTP · MCP · CLI"]
  surfaces --> service["repo-service<br/>统一的接口约定"]
  service --> git["Git 镜像<br/>每个仓库一份"]
  service --> engine["Zoekt 索引<br/>子进程"]
  service --> db["应用数据库<br/>SQLite、PostgreSQL 或 MongoDB"]
  git -->|"后台同步与建索引"| engine

源码位于 github.com/context4ai/sourcegraph。名称并不代表它与 Sourcegraph 平台有任何关联。

一套接口约定,四种入口 ​

网站、HTTP、MCP 和 CLI 共享以下操作,其中 read_many 只能通过 MCP 使用,refs 可通过 MCP 和 HTTP 使用:

操作作用
repositories列出已登记的仓库及其观察到的分支
availability显示仓库保留的版本和建索引状态
refs查找仓库的分支和标签,列出每个分支或标签的提交、时间、作者、标题,以及该提交是否已建索引
resolve把分支、标签或符合条件的完整 SHA 转换为后续要使用的固定提交,不下载代码
search在某个提交上搜索文件内容,形式与 rg 类似
read读取某个提交上一个文件的指定行范围
read_many在一次 MCP 调用中读取同一仓库、同一提交上最多十个已知范围
list列出某个提交上的一个目录
diff比较两个符合条件的提交
prepare请求同步某个提交并为其建索引(仅限已授权的调用方)

读取代码的操作接受监控分支或完整 SHA;只有 resolve 还接受其他分支和标签,因此从这类分支或标签出发的调用方要先解析,再沿用返回的 SHA。网站位于 /sourcegraph/ 下,MCP 端点为 /sourcegraph/mcp,HTTP API 位于 /sourcegraph/v1/ 和 /sourcegraph/api/ 下。命令行客户端 sourcegraph-cli 调用的也是这些 HTTP 路由。具体路由、工具输入和限制见 HTTP 与 MCP 接口 和 CLI 参考文档。

实例的组成 ​

一个 Source Graph 实例就是一个 repo-service 进程。它以子进程方式启动 Zoekt 搜索服务器(只监听回环接口),在数据卷上为每个已登记仓库保存一份裸 Git 镜像,并运行后台 worker 来同步监控分支最新提交的快照、为新的最新提交建索引,并回收已过期的索引。每个仓库的 Git 对象和索引都有各自的存储预算;预算用满时,服务会释放最旧的、既不是分支最新提交、未被固定、也未被任务占用的快照,而不是拒绝新的工作。另有一个后台流程负责完成已释放快照的 Git 回收,因此即使任务中断,空间也能回收。

监控分支以外的分支和标签无需触碰镜像即可发现。为支持 refs 和 resolve,服务在内存中为每个仓库维护一份分支和标签目录:它列出远端引用,只把提交对象和标签对象(不含文件树)下载到一个临时目录,解析完成后立即删除该目录。如果某个名称不在一份新鲜且完整的目录中,服务会以同样方式到远端单独查找。发现过程从不建索引,也不增加版本;提交只有在准备(prepare)之后才能被搜索。

应用数据库存储仓库、任务、身份和设置,也加密存储实例的应用密钥:代码源令牌及其加密密钥、服务 API 凭据,以及 Google/GitHub 登录配置。因此数据卷只保存 Git 镜像和索引,替换主机时需要的是数据库,而不是旧的密钥文件。默认使用 SQLite;对于规模更大或需要分片的部署,也支持 PostgreSQL 和 MongoDB。Git 镜像和索引始终保存在实例的本地磁盘上——搜索速度正来自这种本地性——因此容量规划围绕磁盘而非 CPU 展开。需要扩容时,运行多个实例,各自负责互不重叠的一组仓库;切勿让两个实例处理同一批仓库或同一个数据目录。运维:自行部署 Source Graph 介绍了这两种拓扑。

仓库还可以附带名为 <name>.sourcegraph.wasm 的 WebAssembly 插件,为 read 和 read_many 的结果追加文本。Context 借此为每个知识章节附上其来源,详见 与 Context 协同。

权限划分 ​

未配置任何登录提供方时,实例是一个匿名只读站点:访客可以搜索和阅读公开仓库,并受每分钟限流约束。添加 Google 或 GitHub 登录后即可启用管理功能,第一个通过验证的用户会成为管理员。每个仓库要么公开,要么私有:私有仓库只对其创建者、其管理者、在网站上登录的管理员和服务令牌可见,对其他所有人而言它就像未登记一样。已登录用户可以为脚本和 Agent 创建个人 API Key,这些 Key 以其所有者的身份读取,自动化场景则使用服务令牌。登记仓库、为较早的提交建索引以及其他变更操作,需要管理员或服务令牌;除非站点允许已登录用户登记仓库,此时每位这样的用户管理自己登记的仓库,并可以添加其他管理者。完整的权限矩阵见 运维:登录与权限。

下一步 ​

如果你想…阅读
了解 Source Graph 为什么存在以及如何开始Source Graph 是什么
了解固定版本搜索和速度对比固定版本的搜索与阅读
了解 Context 知识与 Source Graph 如何结合与 Context 协同
接入浏览器、MCP 客户端、脚本或 CLI连接 Agent、脚本和命令行使用 Source Graph
添加仓库和 Monorepo 路径组登记和管理仓库
部署你自己的实例运维:自行部署 Source Graph
配置登录和 API Key运维:登录与权限
运行、监控和排查问题运维:日常运维与排障
查阅路由、工具、参数和设置HTTP 与 MCP 接口、CLI、搜索语法、配置