Sponsored by Deepsite.site

CodeGraphContext

Created By
Shashankss12052 months ago
An MCP server that indexes local code into a graph database to provide context to AI assistants.
Content

CodeGraphContext

Stars Forks Open Issues Open PRs Closed PRs Contributors Languages Build Status Build Status PyPI version PyPI downloads License Website Docs YouTube Discord

An MCP server that indexes local code into a graph database to provide context to AI assistants.

Indexing a codebase

Indexing using an MCP client

Using the MCP server

Using the MCP server

Project Details

Star History

Star History Chart

Features

  • Code Indexing: Analyzes code and builds a knowledge graph of its components.
  • Relationship Analysis: Query for callers, callees, class hierarchies, call chains and more.
  • Live Updates: Watches local files for changes and automatically updates the graph.
  • Interactive Setup: A user-friendly command-line wizard for easy setup.

Used By

CodeGraphContext is already being explored by developers and projects for:

  • Static code analysis in AI assistants
  • Graph-based visualization of projects
  • Dead code and complexity detection

If you’re using CodeGraphContext in your project, feel free to open a PR and add it here! 🚀

Dependencies

  • neo4j>=5.15.0
  • watchdog>=3.0.0
  • requests>=2.31.0
  • stdlibs>=2023.11.18
  • typer[all]>=0.9.0
  • rich>=13.7.0
  • inquirerpy>=0.3.4
  • python-dotenv>=1.0.0
  • tree-sitter==0.20.4
  • tree-sitter-languages==1.10.2

Getting Started

  1. Install: pip install codegraphcontext

  2. Setup: cgc setup This interactive command guides you through configuring your Neo4j database connection and automatically setting up your IDE.

    ⚙️ Troubleshooting: In case, command cgc not found

    If you encounter "cgc: command not found" after installation, run the PATH fix script:

    Linux/Mac:

    # Download the fix script
    curl -O https://raw.githubusercontent.com/Shashankss1205/CodeGraphContext/main/scripts/post_install_fix.sh
    
    # Make it executable
    chmod +x post_install_fix.sh
    
    # Run the script
    ./post_install_fix.sh
    
    # Restart your terminal or reload shell config
    source ~/.bashrc  # or ~/.zshrc for zsh users
    

    Windows (PowerShell):

    # Download the fix script
    curl -O https://raw.githubusercontent.com/Shashankss1205/CodeGraphContext/main/scripts/post_install_fix.sh
    
    # Run with bash (requires Git Bash or WSL)
    bash post_install_fix.sh
    
    # Restart PowerShell or reload profile
    . $PROFILE
    

    Database Configuration:

    • Local Setup (Docker Recommended): Helps you set up a local Neo4j instance using Docker. Requires Docker and Docker Compose to be installed.
    • Local Setup (Linux Binary): For Debian-based Linux systems (like Ubuntu), cgc setup can automate the installation of Neo4j. Requires sudo privileges.
    • Hosted Setup: Allows you to connect to an existing remote Neo4j database (e.g., Neo4j AuraDB).

    IDE/CLI Configuration: After setting up your database, the wizard will ask to configure your development environment. It can automatically detect and configure the following:

    • VS Code
    • Cursor
    • Windsurf
    • Claude
    • Gemini CLI
    • ChatGPT Codex
    • Cline
    • RooCode
    • Amazon Q Developer

    Upon successful configuration, cgc setup will generate and place the necessary configuration files:

    • It creates an mcp.json file in your current directory for reference.
    • It stores your Neo4j credentials securely in ~/.codegraphcontext/.env.
    • It updates the settings file of your chosen IDE/CLI (e.g., .claude.json or VS Code's settings.json).
  3. Start: cgc start

Ignoring Files (.cgcignore)

You can tell CodeGraphContext to ignore specific files and directories by creating a .cgcignore file in the root of your project. This file uses the same syntax as .gitignore.

Example .cgcignore file:

# Ignore build artifacts
/build/
/dist/

# Ignore dependencies
/node_modules/
/vendor/

# Ignore logs
*.log

MCP Client Configuration

The cgc setup command attempts to automatically configure your IDE/CLI. If you choose not to use the automatic setup, or if your tool is not supported, you can configure it manually.

Add the following server configuration to your client's settings file (e.g., VS Code's settings.json or .claude.json):

{
  "mcpServers": {
    "CodeGraphContext": {
      "command": "cgc",
      "args": [
        "start"
      ],
      "env": {
        "NEO4J_URI": "YOUR_NEO4J_URI",
        "NEO4J_USERNAME": "YOUR_NEO4J_USERNAME",
        "NEO4J_PASSWORD": "YOUR_NEO4J_PASSWORD"
      },
      "tools": {
        "alwaysAllow": [
          "add_code_to_graph",
          "add_package_to_graph",
          "check_job_status",
          "list_jobs",
          "find_code",
          "analyze_code_relationships",
          "watch_directory",
          "find_dead_code",
          "execute_cypher_query",
          "calculate_cyclomatic_complexity",
          "find_most_complex_functions",
          "list_indexed_repositories",
          "delete_repository",
          "visualize_graph_query",
          "list_watched_paths",
          "unwatch_directory"
        ],
        "disabled": false
      },
      "disabled": false,
      "alwaysAllow": []
    }
  }
}

Natural Language Interaction Examples

Once the server is running, you can interact with it through your AI assistant using plain English. Here are some examples of what you can say:

Indexing and Watching Files

  • To index a new project:

    • "Please index the code in the /path/to/my-project directory." OR
    • "Add the project at ~/dev/my-other-project to the code graph."
  • To start watching a directory for live changes:

    • "Watch the /path/to/my-active-project directory for changes." OR
    • "Keep the code graph updated for the project I'm working on at ~/dev/main-app."

    When you ask to watch a directory, the system performs two actions at once:

    1. It kicks off a full scan to index all the code in that directory. This process runs in the background, and you'll receive a job_id to track its progress.
    2. It begins watching the directory for any file changes to keep the graph updated in real-time.

    This means you can start by simply telling the system to watch a directory, and it will handle both the initial indexing and the continuous updates automatically.

Querying and Understanding Code

  • Finding where code is defined:

    • "Where is the process_payment function?"
    • "Find the User class for me."
    • "Show me any code related to 'database connection'."
  • Analyzing relationships and impact:

    • "What other functions call the get_user_by_id function?"
    • "If I change the calculate_tax function, what other parts of the code will be affected?"
    • "Show me the inheritance hierarchy for the BaseController class."
    • "What methods does the Order class have?"
  • Exploring dependencies:

    • "Which files import the requests library?"
    • "Find all implementations of the render method."
  • Advanced Call Chain and Dependency Tracking (Spanning Hundreds of Files): The CodeGraphContext excels at tracing complex execution flows and dependencies across vast codebases. Leveraging the power of graph databases, it can identify direct and indirect callers and callees, even when a function is called through multiple layers of abstraction or across numerous files. This is invaluable for:

    • Impact Analysis: Understand the full ripple effect of a change to a core function.

    • Debugging: Trace the path of execution from an entry point to a specific bug.

    • Code Comprehension: Grasp how different parts of a large system interact.

    • "Show me the full call chain from the main function to process_data."

    • "Find all functions that directly or indirectly call validate_input."

    • "What are all the functions that initialize_system eventually calls?"

    • "Trace the dependencies of the DatabaseManager module."

  • Code Quality and Maintenance:

    • "Is there any dead or unused code in this project?"
    • "Calculate the cyclomatic complexity of the process_data function in src/utils.py."
    • "Find the 5 most complex functions in the codebase."
  • Repository Management:

    • "List all currently indexed repositories."
    • "Delete the indexed repository at /path/to/old-project."

Contributing

Contributions are welcome! 🎉
Please see our CONTRIBUTING.md for detailed guidelines. If you have ideas for new features, integrations, or improvements, open an issue or submit a Pull Request.

Join discussions and help shape the future of CodeGraphContext.

Server Config

{
  "mcpServers": {
    "CodeGraphContext": {
      "command": "cgc",
      "args": [
        "start"
      ],
      "env": {
        "NEO4J_URI": "YOUR_NEO4J_URI",
        "NEO4J_USERNAME": "YOUR_NEO4J_USERNAME",
        "NEO4J_PASSWORD": "YOUR_NEO4J_PASSWORD"
      },
      "tools": {
        "alwaysAllow": [
          "add_code_to_graph",
          "add_package_to_graph",
          "check_job_status",
          "list_jobs",
          "find_code",
          "analyze_code_relationships",
          "watch_directory",
          "find_dead_code",
          "execute_cypher_query",
          "calculate_cyclomatic_complexity",
          "find_most_complex_functions",
          "list_indexed_repositories",
          "delete_repository",
          "visualize_graph_query",
          "list_watched_paths",
          "unwatch_directory"
        ],
        "disabled": false
      },
      "disabled": false,
      "alwaysAllow": []
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
ChatWiseThe second fastest AI chatbot™
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Tavily Mcp
WindsurfThe new purpose-built IDE to harness magic
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Serper MCP ServerA Serper MCP Server
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
DeepChatYour AI Partner on Desktop
Playwright McpPlaywright MCP server
CursorThe AI Code Editor
Amap Maps高德地图官方 MCP Server
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors