# What Is the Model Context Protocol (MCP) and How Does code-review-graph Use It?

> Discover the Model Context Protocol MCP, a JSON-RPC for efficient AI coding assistant context retrieval. See how code-review-graph leverages MCP for streamlined code analysis.

- Repository: [Tirth Kanani/code-review-graph](https://github.com/tirth8205/code-review-graph)
- Tags: deep-dive
- Published: 2026-08-11

---

**The Model Context Protocol (MCP) is a lightweight JSON-RPC protocol that lets AI coding assistants request structured, token-efficient context from a local server instead of sending large raw source dumps.**

In the `tirth8205/code-review-graph` project, MCP serves as the bridge between AI agents and a sophisticated code analysis engine. This article explains how MCP works, how `code-review-graph` implements it via FastMCP, and how you can leverage its 30+ tools and prompt templates for intelligent code review workflows.

## Understanding the Model Context Protocol

MCP addresses a critical problem in AI-assisted development: **context window limitations**. Traditional approaches send entire codebases to language models, wasting tokens and hitting limits. MCP flips this model by letting the AI query a specialized server for exactly the context it needs.

The protocol supports two transport mechanisms:

- **stdio** — the original FastMCP transport, used when the server spawns as a child process
- **streamable HTTP on localhost** — enabled with the `--http` flag in [`code_review_graph/main.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/main.py)【/cache/repos/github.com/tirth8205/code-review-graph/main/code_review_graph/main.py#L1-L6】

MCP messages follow JSON-RPC 2.0, with methods for tool invocation, prompt rendering, and capability negotiation. The server exposes its available tools and prompts during initialization, letting clients discover functionality dynamically.

## How code-review-graph Implements MCP

The `code-review-graph` MCP server bootstraps in [`code_review_graph/main.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/main.py) through a concise but powerful setup sequence.

### FastMCP Server Initialization

The server instantiates via `FastMCP("code-review-graph", …)` at lines 20-26, configuring logging, transport, and capability flags【/cache/repos/github.com/tirth8205/code-review-graph/main/code_review_graph/main.py#L20-L26】. This object becomes the central registry for all exposed functionality.

### Tool Registration

The `@mcp.tool()` decorator registers 30+ analysis tools. Each tool is a Python function with typed parameters and structured return values. For example, `build_or_update_graph_tool` appears at lines 99-102【/cache/repos/github.com/tirth8205/code-review-graph/main/code_review_graph/main.py#L99-L102】:

```python
@mcp.tool()
async def build_or_update_graph_tool(
    repo_root: str,
    full_rebuild: bool = False,
    postprocess: str = "full"
) -> dict:
    """Build or incrementally update the code knowledge graph."""
    ...

```

Other tools exposed include:

- **Impact radius analysis** — identifies affected code from changed files
- **Community detection** — clusters related modules using graph algorithms
- **Wiki generation** — produces markdown documentation from graph structure
- **Flow analysis** — traces data and control flow between components

### Prompt Templates

Beyond tools, the server registers 5 prompt templates via `@mcp.prompt()`. These higher-level constructs combine multiple tool calls into coherent, task-oriented workflows. The `review_changes_prompt`, for instance, orchestrates change-set gathering, impact analysis, and risk scoring into a single natural-language summary suitable for direct model consumption.

### Async Architecture

A critical implementation detail appears at lines 15-21: heavy computation runs in `asyncio.to_thread` wrappers to prevent blocking the stdio event loop【/cache/repos/github.com/tirth8205/code-review-graph/main/code_review_graph/main.py#L15-L21】. This matters particularly on Windows, where I/O handling differs from POSIX systems. The server remains responsive even during lengthy graph builds or community detection runs.

## MCP Client Integration Patterns

### Python FastMCP Client

Connect to a running server and invoke tools programmatically:

```python
from fastmcp import FastMCP

mcp = FastMCP("code-review-graph")

result = mcp.run_tool(
    "build_or_update_graph_tool",
    {
        "full_rebuild": False,
        "repo_root": "/path/to/repo",
        "postprocess": "full",
    },
)
print(result["node_count"], result["edge_count"])

```

The same client code works across stdio and HTTP transports; only the server launch command changes.

### Direct HTTP Requests

For debugging or non-Python environments, use raw JSON-RPC over HTTP:

```bash

# Terminal 1: start HTTP server

code-review-graph serve --http

# Terminal 2: request impact analysis

curl -X POST http://127.0.0.1:5555/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "get_impact_radius_tool",
    "params": {
      "changed_files": ["src/main.py"],
      "repo_root": "/path/to/repo"
    },
    "id": 1
  }'

```

The response includes selected nodes, edges, and a token-count estimate—critical information for clients managing context budgets.

### CLI as MCP Proxy

The `code-review-graph` CLI commands often forward to MCP tools internally. The `review-changes` command demonstrates this pattern:

```bash
code-review-graph review-changes --files src/main.py

```

This executes `review_changes_prompt` on the MCP server, handling the JSON-RPC plumbing transparently.

## System Architecture

The documentation's architecture diagram ([`docs/architecture.md`](https://github.com/tirth8205/code-review-graph/blob/main/docs/architecture.md), lines 5-23) positions the MCP server as the control layer between AI clients and graph infrastructure【/cache/repos/github.com/tirth8205/code-review-graph/main/docs/architecture.md#L5-L23】. The flow is:

1. **MCP client** (Claude Code, Cursor, etc.) discovers available tools
2. **MCP server** receives requests, validates parameters
3. **Background threads** execute graph operations (parsing, SQLite queries, incremental updates)
4. **Structured responses** return to client with minimal token overhead

This layering lets AI agents reason about codebases they cannot directly access, while `code-review-graph` maintains persistent, queryable knowledge graphs locally.

## Key Source Files

Understanding MCP in this codebase requires familiarity with:

| File | Purpose |
|------|---------|
| [`code_review_graph/main.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/main.py) | FastMCP bootstrap, `@mcp.tool()` and `@mcp.prompt()` registration |
| `code_review_graph/tools/*.py` | 30+ tool implementations: build, query, flows, communities, metrics |
| [`docs/architecture.md`](https://github.com/tirth8205/code-review-graph/blob/main/docs/architecture.md) | System diagram showing MCP server placement |
| [`docs/USAGE.md`](https://github.com/tirth8205/code-review-graph/blob/main/docs/USAGE.md) | MCP configuration for Claude Code, Cursor, and other clients |
| [`docs/FEATURES.md`](https://github.com/tirth8205/code-review-graph/blob/main/docs/FEATURES.md) | Current tool and prompt inventory (30 tools, 5 prompts as of main) |

## Summary

- **MCP is a JSON-RPC protocol** for structured context exchange between AI assistants and specialized servers
- **code-review-graph implements MCP** via FastMCP with stdio and HTTP transports
- **30+ tools and 5 prompts** expose graph building, impact analysis, and review workflows
- **Non-blocking architecture** uses `asyncio.to_thread` for responsiveness under load
- **Token-efficient responses** replace raw code dumps with targeted, structured context

## Frequently Asked Questions

### What makes MCP different from a REST API?

MCP is designed specifically for AI agent interaction. It includes capability discovery, prompt templating, and built-in token budgeting that generic REST lacks. The protocol also standardizes across stdio and HTTP, letting the same server work as a subprocess or network service without client code changes.

### How do I connect Claude Code to code-review-graph?

Add an MCP server configuration pointing to `code-review-graph serve` in your Claude Code settings. The [`docs/USAGE.md`](https://github.com/tirth8205/code-review-graph/blob/main/docs/USAGE.md) file provides specific JSON configurations. Once connected, Claude discovers all 30+ tools and can invoke them during conversations to analyze your repository structure.

### Does the HTTP transport support authentication?

As implemented in [`main.py`](https://github.com/tirth8205/code-review-graph/blob/main/main.py), the HTTP server binds to localhost only without built-in authentication. For multi-user or remote deployments, place a reverse proxy with TLS and token validation in front. The stdio transport avoids this entirely by running the server as a child process with inherited OS permissions.

### What happens when a tool takes too long to execute?

Long-running operations execute in `asyncio.to_thread` pools, keeping the MCP event loop responsive. The client receives progress notifications if implemented, or eventual completion. For extremely large repositories, consider incremental builds via `full_rebuild=False` to reduce latency.