# CodeGraphContext — Graph Index for Code + MCP > CodeGraphContext indexes a repo into a code graph so developers and agents can query call chains, dependencies, and architecture via CLI or MCP mode. ## Install Merge the JSON below into your `.mcp.json`: ## Quick Use 1. Install: ```bash pip install codegraphcontext ``` 2. If the CLI isn’t on PATH: ```bash curl -sSL https://raw.githubusercontent.com/CodeGraphContext/CodeGraphContext/main/scripts/post_install_fix.sh | bash ``` 3. Initialize and index a repo: ```bash cd /path/to/repo codegraphcontext init codegraphcontext index ``` ## Intro CodeGraphContext indexes a repo into a code graph so developers and agents can query call chains, dependencies, and architecture via CLI or MCP mode. - **Best for:** large codebases where agents need reliable call graphs and dependency queries - **Works with:** Python 3.10+; CLI plus MCP mode for IDE/agent integration - **Setup time:** 20–45 minutes ## Practical Notes - Index once, then query call chains/dependencies instead of re-reading files repeatedly. - Start with a small subfolder first to validate performance before indexing monorepos. ## Two modes: CLI for humans, MCP for agents You can use CodeGraphContext as: - a **CLI** to ask structural questions (callers/callees, dependencies), - an **MCP server** so your assistant can query structure via tools. ## Practical workflow for better agent context ### 1) Index your repo ```bash codegraphcontext index ``` ### 2) Ask “structure” questions first Good first questions: - “What calls this function?” - “Where does this module get instantiated?” - “Show the call chain from HTTP handler to DB write.” ### 3) Feed the query results to your agent Instead of dumping full files, give the agent: - the top 10 nodes in the call chain, - the 3–5 files it must open, - constraints (touch only X modules, avoid breaking API). ## Operational advice - Use file watching mode during refactors only when needed (it updates the graph continuously). - Treat the graph DB as a cache: rebuild when your repo changes dramatically. ### FAQ **Q: Do I need MCP to get value?** A: No. The CLI alone helps answer structural questions quickly. **Q: When should I re-index?** A: After large refactors or when results look stale; treat the graph as a cache. **Q: How does this help agents?** A: It gives reliable call/dependency context so agents stop guessing where logic lives. ## Source & Thanks > Source: https://github.com/CodeGraphContext/CodeGraphContext > License: MIT > GitHub stars: 3,218 · forks: 573 --- ## 快速使用 1. 安装: ```bash pip install codegraphcontext ``` 2. 如果命令找不到(PATH 问题): ```bash curl -sSL https://raw.githubusercontent.com/CodeGraphContext/CodeGraphContext/main/scripts/post_install_fix.sh | bash ``` 3. 初始化并索引仓库: ```bash cd /path/to/repo codegraphcontext init codegraphcontext index ``` ## 简介 CodeGraphContext 会把仓库索引成代码图谱,开发者与 agent 可用 CLI 或 MCP 方式查询调用链、依赖关系与架构视图,从而减少“靠猜”与重复读文件导致的上下文噪音与返工。 - **适合谁:** 代码规模大、agent 需要稳定的调用图/依赖查询能力的工程 - **可搭配:** Python 3.10+;既提供 CLI,也支持 MCP 模式用于 IDE/agent 集成 - **准备时间:** 20–45 分钟 ## 实战建议 - 先索引一次,再用调用链/依赖查询替代反复读文件。 - 单体仓库/monorepo 建议先索引子目录验证性能,再全量扩展。 ## 两种模式:给人用的 CLI,给 agent 用的 MCP CodeGraphContext 可以当作: - **CLI 工具**:做结构化查询(调用者/被调用者、依赖关系等), - **MCP Server**:让你的助手通过工具接口查询结构。 ## 用于增强 agent 上下文的实战流程 ### 1)先索引仓库 ```bash codegraphcontext index ``` ### 2)优先问“结构问题” 最有价值的起手问题: - “谁调用了这个函数?” - “这个模块在哪里被实例化?” - “从 HTTP handler 到 DB 写入的调用链是什么?” ### 3)把查询结果喂给 agent,而不是丢整库 给 agent 的最好输入通常是: - 调用链上的关键节点(top 10), - 必须打开的 3–5 个文件, - 约束条件(只改哪些模块、不要破坏哪些接口)。 ## 运维建议 - refactor 期间按需打开 watch 模式(持续更新图谱)。 - 把图数据库当缓存:当代码结构大幅变化时重建索引即可。 ### FAQ **一定要用 MCP 才有价值吗?** 答:不需要。只用 CLI 做结构查询也能大幅提效。 **什么时候需要重建索引?** 答:大重构后或结果明显过期时;把图谱当缓存即可。 **它怎么帮助 agent?** 答:提供可靠的调用/依赖上下文,减少 agent “猜逻辑在哪”的成本。 ## 来源与感谢 > Source: https://github.com/CodeGraphContext/CodeGraphContext > License: MIT > GitHub stars: 3,218 · forks: 573 --- Source: https://tokrepo.com/en/workflows/codegraphcontext-graph-index-for-code-mcp Author: MCP Hub