Skip to content

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 indexes context4ai/context and context4ai/sourcegraph.

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 rg about 61 ms over a local checkout returns in about 1 ms. Pinned Search and Reads explains where the speed comes from.
  • Reproducible reads. Branches and tags resolve to one commit before anything is read, so a search hit, the file it points to and a later diff describe the same snapshot.
  • Ready for Agents. The same operations — list repositories, search, read a range, list a directory, diff two commits — are available on a website, an HTTP API, an MCP server and a command-line client, and MCP can also read several ranges in one call. Any MCP client that speaks Streamable HTTP can connect, and anonymous access is read-only and limited to public repositories.
  • Small footprint. One process manages the Git mirrors, the search engine and background indexing. SQLite is the default database; PostgreSQL and MongoDB are supported.

How it works with Context ​

Context knowledge pages point back to the code that supports each statement. When an Agent answers with Context, its query Skill reads that original code to confirm the answer. In the default auto mode it uses an authorized context-sourcegraph MCP server when one is configured, and otherwise falls back to local retrieval: a shallow, sparse partial clone of the recorded commit searched with grep or rg.

So Source Graph is optional for Context. Connect it when you want Agents to read pinned code remotely, without local checkouts or clone time. Repositories can also ship reading plugins (<name>.sourcegraph.wasm) that add information to read results; Context uses one so that reading a knowledge page through Source Graph also returns the sources each section was written from — see Evidence for Context.

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 ​

  1. Try the live demo. Open https://context4ai.org/sourcegraph/ and search or read code in the browser; no account is needed.
  2. Connect your Agent. Open the site's Connect page and copy the configuration for your Agent host, or point any Streamable HTTP MCP client at https://context4ai.org/sourcegraph/mcp. Connecting covers MCP, HTTP, the CLI and credentials.
  3. 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. Self-Hosting and Registering Repositories walk through both.

Where to go next ​

To…Read
Connect an MCP client, script or the CLIConnecting
Add repositories and monorepo path groupsRegistering Repositories
Understand how the service is builtArchitecture Overview and Pinned Search and Reads
Deploy, secure and operate your own instanceSelf-Hosting, Deployment Recommendations, Authentication and Access Control and Operations
Look up routes, tools, flags and settingsHTTP and MCP, CLI, Query Syntax and Configuration

Source Graph 是什么 ​

Context Source Graph 让人、脚本和 AI Agent 无需克隆仓库,就能在确定的 Git 提交上搜索和阅读代码。 结果来自预先建好的索引,毫秒级返回;每个结果都标明读取所用的提交,同一个问题总能拿到同样的证据。

它开源、可自部署。公开的只读在线演示索引了 context4ai/context 和 context4ai/sourcegraph。

它解决什么问题 ​

Agent 回答代码问题时,通常只需要少量确切的代码行,而不是整个仓库。它的工作是针对并未检出的代码,进行很多轮小而精确的「先搜索、再阅读」。把每个仓库都按正确版本克隆到每个 Agent 沙箱里很慢;在检出目录上运行 rg 或 git grep,每次调用又要把同样的内容完整扫描一遍。

Source Graph 把这部分工作挪到提问之前:它在后台为每个已登记仓库保持镜像和索引,查询读取的是预先建好的索引,而不是扫描工作树。仓库越大,差距越明显。

你能得到什么 ​

  • 快速搜索。 服务端的索引搜索通常在 5 ms 内完成;与调用方部署在同一区域时,含网络在内的完整请求也远低于 100 ms。在演示实例上,一次在本地检出目录里 rg 约需 61 ms 的字面量搜索,大约 1 ms 就能返回。速度从何而来,见固定版本的搜索与阅读。
  • 可复现的读取。 分支和标签在读取任何内容之前都先解析为一个提交,因此搜索命中、它指向的文件和之后的差异,描述的是同一个快照。
  • 为 Agent 而生。 列出仓库、搜索、读取一段代码、列出目录、比较两个提交,这些操作在网站、HTTP API、MCP 服务器和命令行客户端上都一样可用;MCP 还能在一次调用中读取多段代码。任何支持 Streamable HTTP 的 MCP 客户端都能接入;匿名访问为只读,且仅限公开仓库。
  • 占用小。 一个进程同时管理 Git 镜像、搜索引擎和后台建索引。默认数据库为 SQLite,也支持 PostgreSQL 和 MongoDB。

如何与 Context 配合 ​

Context 的知识页面会回指支撑每条结论的代码。Agent 用 Context 回答问题时,查询技能会去读取这些原始代码来确认答案。在默认的 auto 模式下,只要配置了已授权的 context-sourcegraph MCP 服务器,它就会使用该服务;否则回退到本地检索:对记录的提交做浅层、稀疏的部分克隆,再用 grep 或 rg 搜索。

所以 Source Graph 对 Context 是可选的。当你希望 Agent 远程读取固定版本的代码、不想在本地检出或等待克隆时,再接入它。仓库还可以附带读取插件(<name>.sourcegraph.wasm),在 read 结果中追加信息;Context 就用这样一个插件,让你通过 Source Graph 读取知识页面时,同时拿到每个章节的写作来源,见与 Context 协同。

什么时候值得接入 ​

当 Agent 或脚本经常需要本地没有的仓库代码、回答必须引用确切的提交,或者同一批仓库每天被反复查询时,就值得接入 Source Graph。对于你正在修改的代码,本地检出仍然更简单。

Source Graph 是一个在固定版本上做文本搜索与阅读的服务。每个实例在本地磁盘上持有自己的镜像和索引;扩容的方式是让多个实例分别负责互不重叠的仓库,而不是在同一份数据上运行多个副本。

三步开始 ​

  1. 试用在线演示。 打开 https://context4ai.org/sourcegraph/,直接在浏览器中搜索和阅读代码,无需账号。
  2. 接入你的 Agent。 打开站点的 Connect(接入)页面,复制适用于你的 Agent 宿主的配置;或者把任意支持 Streamable HTTP 的 MCP 客户端指向 https://context4ai.org/sourcegraph/mcp。MCP、HTTP、CLI 和凭据的用法见接入。
  3. 为自己的仓库运行实例。 用 Docker 启动,配置 Google 或 GitHub 登录,第一个完成验证的用户会成为管理员并登记仓库。具体步骤见自部署与数据库选择和登记仓库。

下一步 ​

如果你想…阅读
接入 MCP 客户端、脚本或 CLI接入
添加仓库和 Monorepo 路径组登记仓库
了解服务是如何构建的架构概览和固定版本的搜索与阅读
部署、保护并运维自己的实例自部署与数据库选择、部署建议、登录与准入和运维
查阅路由、工具、参数和设置HTTP 与 MCP 接口、CLI、搜索语法和配置