What Is Source Graph
Context Source Graph lets people, scripts and AI Agents search and read code at an exact Git commit without cloning the repository. Answers come back in milliseconds from a prepared index, and every result names the commit it was read from, so the same question gets the same evidence.
It is open source and self-hosted. A public, read-only live demo serves this site's own Context knowledge workspace, context4ai/portal, together with the source code of context4ai/context, context4ai/sourcegraph and context4ai/agent-graph, so you can try both code search and knowledge-first questions right away. The guides in this section cover connecting to an instance, using it with Context, and — for administrators — registering repositories, self-hosting, securing and operating it.
The problem it solves
An Agent answering a question about code usually needs a handful of exact lines, not a whole repository. Its work is many small, precise rounds of search, then read against code it does not have checked out. Cloning every repository into every Agent sandbox at the right revision is slow, and running rg or git grep over a checkout repeats the same full scan on every call.
Source Graph moves that work ahead of the question: it keeps each registered repository mirrored and indexed in the background, so a query reads a prepared index instead of scanning a working tree. The gap widens as repositories grow.
What you get
- Fast search. Indexed search typically completes in under 5 ms on the server; in the same region as its callers, a full request including the network stays well under 100 ms. On the demo, a literal search that takes
rgabout 61 ms over a local checkout returns in about 1 ms. Pinned Search and Reads explains where the speed comes from. - Reproducible reads. Any branch or tag of a registered repository resolves to one full commit before anything is read, so a search hit, the file it points to and a later diff describe the same snapshot. Branches and tags can be listed and searched by name, author, title or date, newest first, so an Agent can find the version a question is about.
- Ready for Agents. The same operations — list repositories, find branches and tags, resolve a version, search, read one range or several, list a directory, diff two commits — are available on a website, an HTTP API, an MCP server and a command-line client. Any MCP client that speaks Streamable HTTP can connect, and anonymous access is read-only and limited to public repositories. Repositories can also be private, visible only to their creator, managers and administrators.
- Snapshot retention. Shallow synchronization of branch heads, snapshots of any commit prepared on demand and a storage budget per repository keep disk use predictable. When a repository reaches its capacity, its oldest snapshots are released first, while current branch heads and versions pinned in its settings are kept.
- Small footprint. One process manages the Git mirrors, the search engine and background indexing. SQLite is the default database; PostgreSQL and MongoDB are supported.
A faster option for Context
By default, Context reads the code behind an answer from a local repository or from a sparse partial clone of the cited commit, which takes time. When Source Graph is available, it reads the same code at the same commit much faster. Source Graph is a progressive enhancement for performance only: answering without it does not change answer quality. How to connect it and choose the reading mode is in Connecting Agents, Scripts and the CLI to Source Graph.
Source Graph also works without Context. When an instance holds a team's knowledge repository as well as its code, any MCP client can answer from both with nothing but a retrieval prompt; see Querying Through MCP in Your Agent.
When it is worth connecting
Connect Source Graph when Agents or scripts regularly need code from repositories they do not have locally, when answers must cite an exact commit, or when the same repositories are queried many times a day. A local checkout remains simpler for code you are editing right now.
Source Graph is a text search and reading service at fixed revisions. Each instance owns its mirrors and indexes on local disk; it scales by giving instances disjoint sets of repositories, not by running replicas over the same data.
Start in three steps
- Try the live demo. Open https://context4ai.org/sourcegraph/ and search or read code in the browser; no account is needed.
- Connect your Agent. Open the site's Connect page and copy the configuration for your Agent host. Connecting Agents, Scripts and the CLI to Source Graph covers MCP, HTTP, the CLI and credentials. To ask questions of this site's knowledge straight away, follow Querying Through MCP in Your Agent.
- Run your own instance for your repositories. Start it with Docker, add Google or GitHub sign-in, and the first verified user becomes the administrator who registers repositories. Ops: Self-Hosting Source Graph and Registering and Managing Repositories walk through both.
Where to go next
| To… | Read |
|---|---|
| Connect your Agent, a script or the CLI | Connecting Agents, Scripts and the CLI to Source Graph |
| Let Context read cited code without cloning | Choose how Context reads code |
| Answer from a team's knowledge and code in any MCP client | Querying Through MCP in Your Agent |
| Add repositories and monorepo path groups | Registering and Managing Repositories |
| Deploy and configure your own instance | Ops: Self-Hosting Source Graph |
| Set up sign-in and decide who can read | Ops: Sign-in and Permissions |
| Watch health and jobs, and fix common errors | Ops: Day-to-Day Operations |
| Understand how the service is built | Architecture Overview and Pinned Search and Reads |
| Look up routes, tools, flags and settings | HTTP and MCP, CLI, Query Syntax and Configuration |
Source Graph 是什么
Context Source Graph 让人、脚本和 AI Agent 无需克隆仓库,就能在确定的 Git 提交上搜索和阅读代码。 结果来自预先建好的索引,毫秒级返回;每个结果都标明读取所用的提交,同一个问题总能拿到同样的证据。
它开源、可自托管。公开的只读 在线演示 同时提供本文档站自己的 Context 知识工作区 context4ai/portal,以及 context4ai/context、context4ai/sourcegraph 和 context4ai/agent-graph 的源码,所以你马上就能试用代码搜索,也能试试“先读知识”的问答。本节的指南介绍如何接入实例、如何与 Context 配合使用,以及面向管理员的登记仓库、自托管、安全保护和运维。
它解决什么问题
Agent 回答代码问题时,通常只需要少量确切的代码行,而不是整个仓库。它的工作是针对并未检出的代码,进行很多轮小而精确的“先搜索、再阅读”。把每个仓库都按正确版本克隆到每个 Agent 沙箱里很慢;在检出目录上运行 rg 或 git grep,每次调用又要把同样的内容完整扫描一遍。
Source Graph 把这部分工作挪到提问之前:它在后台为每个已登记仓库保持镜像和索引,查询读取的是预先建好的索引,而不是扫描工作树。仓库越大,差距越明显。
你能得到什么
- 快速搜索。 服务端的索引搜索通常在 5 ms 内完成;与调用方部署在同一区域时,含网络在内的完整请求也远低于 100 ms。在演示实例上,一次在本地检出目录里
rg约需 61 ms 的字面量搜索,大约 1 ms 就能返回。速度从何而来,见 固定版本的搜索与阅读。 - 可复现的读取。 已登记仓库的任何分支或标签,在读取任何内容之前都先解析为一个完整提交,因此搜索命中、它指向的文件和之后的差异,描述的是同一个快照。分支和标签可以按名称、作者、标题或日期列出和搜索,最新的在前,Agent 因此能找到问题所涉及的版本。
- 为 Agent 而生。 同样的操作(列出仓库、查找分支和标签、解析版本、搜索、读取一段或多段代码、列出目录、比较两个提交)在网站、HTTP API、MCP 服务器和命令行客户端上都可用。任何支持 Streamable HTTP 的 MCP 客户端都能接入;匿名访问为只读,且仅限公开仓库。仓库也可以设为私有,只对其创建者、管理者和管理员可见。
- 快照保留。 只浅层同步分支最新提交、按需准备任意提交的快照,并为每个仓库设置存储预算,让磁盘占用可预期。仓库用满容量时,最旧的快照会最先被释放,而当前的分支最新提交和在仓库设置中固定的版本会被保留。
- 占用小。 一个进程同时管理 Git 镜像、搜索引擎和后台建索引。默认数据库为 SQLite,也支持 PostgreSQL 和 MongoDB。
为 Context 加快读取代码
默认情况下,Context 从本地仓库或对所引用提交的稀疏部分克隆中读取答案背后的代码,这需要一些时间。有 Source Graph 时,它能在同一提交上更快地读取同样的代码。Source Graph 是一项只为性能服务的渐进增强:不使用它,答案质量也不会改变。如何接入以及如何选择读取方式,见 连接 Agent、脚本和命令行使用 Source Graph。
反过来,Source Graph 也可以脱离 Context 使用。只要实例上既有团队的知识仓库,也有它的代码,任何 MCP 客户端只凭一段检索提示词就能基于两者作答;参见 在 Agent 中通过 MCP 查询。
什么时候值得接入
当 Agent 或脚本经常需要本地没有的仓库代码、回答必须引用确切的提交,或者同一批仓库每天被反复查询时,就值得接入 Source Graph。对于你正在修改的代码,本地检出仍然更简单。
Source Graph 是一个在固定版本上做文本搜索与阅读的服务。每个实例在本地磁盘上持有自己的镜像和索引;扩容的方式是让多个实例分别负责互不重叠的仓库,而不是在同一份数据上运行多个副本。
三步开始
- 试用在线演示。 打开 https://context4ai.org/sourcegraph/,直接在浏览器中搜索和阅读代码,无需账号。
- 接入你的 Agent。 打开站点的 Connect(接入)页面,复制适用于你的 Agent 宿主的配置。MCP、HTTP、CLI 和凭据的用法见 连接 Agent、脚本和命令行使用 Source Graph。想马上向本站的知识提问,按 在 Agent 中通过 MCP 查询 操作即可。
- 为自己的仓库运行实例。 用 Docker 启动,配置 Google 或 GitHub 登录,第一个完成验证的用户会成为管理员并登记仓库。具体步骤见 运维:自行部署 Source Graph 和 登记和管理仓库。
下一步
| 如果你想… | 阅读 |
|---|---|
| 接入你的 Agent、脚本或 CLI | 连接 Agent、脚本和命令行使用 Source Graph |
| 让 Context 无需克隆即可读取所引用的代码 | 选择 Context 读取代码的方式 |
| 在任意 MCP 客户端中基于团队的知识和代码作答 | 在 Agent 中通过 MCP 查询 |
| 添加仓库和 Monorepo 路径组 | 登记和管理仓库 |
| 部署并配置自己的实例 | 运维:自行部署 Source Graph |
| 设置登录并决定谁可以读取 | 运维:登录与权限 |
| 监控健康状态和任务,并修复常见错误 | 运维:日常运维与排障 |
| 了解服务是如何构建的 | 架构概览 和 固定版本的搜索与阅读 |
| 查阅路由、工具、参数和设置 | HTTP 与 MCP 接口、CLI、搜索语法 和 配置 |