server
code_context_agent.mcp.server ¶
FastMCP server exposing code-context-agent's core differentiators.
This server exposes capabilities that coding agents (Claude Code, Cursor, etc.) cannot get from commodity MCP tools:
- Full 10-phase analysis pipeline (start_analysis / check_analysis)
- Code graph algorithms (query_code_graph)
- Progressive graph exploration (explore_code_graph)
- Analysis artifact access (resources)
Tools that already exist in the MCP marketplace (ripgrep search, LSP symbols, git history, AST-grep) are intentionally NOT exposed here.
The analysis pipeline is exposed as a kickoff/poll pair to avoid MCP client timeouts. Graph query and exploration tools operate on persisted artifacts and return in sub-second time.
start_analysis async ¶
Kick off full codebase analysis. Returns immediately with a job_id for polling.
USE THIS WHEN: You need to analyze a codebase that hasn't been analyzed yet (no .code-context/ directory exists). This is a one-time batch operation.
DO NOT USE IF: .code-context/code_graph.json already exists — go straight to query_code_graph or explore_code_graph instead.
The analysis runs in the background (5-20 min) and produces: - .code-context/CONTEXT.md — narrated architecture overview - .code-context/code_graph.json — structural graph for algorithm queries - .code-context/CONTEXT.signatures.md — compressed Tree-sitter signatures - .code-context/CONTEXT.bundle.md — curated source code bundle - .code-context/analysis_result.json — structured analysis metadata
NEXT STEP: Poll check_analysis(job_id) every 30 seconds until status is "completed", then use query_code_graph or explore_code_graph.
Returns:
| Type | Description |
|---|---|
dict | { "job_id": "a1b2c3d4e5f6", "status": "starting", "repo_path": "/Users/me/projects/myapp", "output_dir": "/Users/me/projects/myapp/.code-context", "message": "Analysis started. Poll check_analysis(job_id) for progress." |
dict | } |
Source code in src/code_context_agent/mcp/server.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
check_analysis ¶
Poll the status of a running analysis job. Call every 30 seconds until done.
USE THIS AFTER: calling start_analysis. Keep polling until status is "completed" or "error".
POLLING PATTERN: 1. Call check_analysis(job_id) 2. If status is "starting" or "running", wait 30 seconds, repeat 3. If status is "completed", artifacts are ready — use query_code_graph 4. If status is "error", check the error field 5. If status is "stopped", partial artifacts may exist (hit time/turn limit)
Returns (while running):
Returns (when complete): { "job_id": "a1b2c3d4e5f6", "status": "completed", "repo_path": "/Users/me/projects/myapp", "output_dir": "/Users/me/projects/myapp/.code-context", "result": {"status": "completed", "turn_count": 87, "duration_seconds": 542.3, ...}, "artifacts": { "context": true, "graph": true, "manifest": true, "signatures": true, "bundle": true, "result": true } }
Source code in src/code_context_agent/mcp/server.py
query_code_graph ¶
query_code_graph(
repo_path,
algorithm,
top_k=10,
node_a="",
node_b="",
resolution=1.0,
category="",
)
Run graph algorithms on a pre-built code graph to find structural insights.
USE THIS WHEN: You need to understand which code is most important, how code is organized, or how components relate to each other.
PREREQUISITE: .code-context/code_graph.json must exist (from start_analysis or 'code-context-agent analyze' CLI). Check for the file first.
ALGORITHMS AND WHEN TO USE EACH:
Finding important code: - "hotspots" — Betweenness centrality. Finds bottleneck/integration code that many paths go through. Use for: risk assessment, refactoring targets. - "foundations" — PageRank. Finds core infrastructure that important code depends on. Use for: understanding what's foundational, documentation priority. - "trust" — TrustRank (PageRank seeded from entry points). More noise-resistant than foundations. Use for: identifying truly important production code. - "entry_points" — Nodes with no incoming edges. Use for: finding where execution starts, understanding app structure.
Finding structure: - "modules" — Louvain community detection. Groups densely connected code into logical clusters. Use for: architecture diagrams, understanding layers.
Analyzing relationships (require node_a and/or node_b): - "coupling" — How tightly two nodes are connected. Use for: change impact, refactoring decisions. Requires node_a AND node_b. - "similar" — Personalized PageRank from a node. Finds related code. Use for: understanding a component's neighborhood. Requires node_a. - "dependencies" — BFS traversal from a node. Shows transitive dependencies. Use for: understanding what a component needs. Requires node_a. - "triangles" — Tightly-coupled triads. Use for: finding clusters of interdependent code that should be refactored together.
Filtering: - "category" — All nodes in a business logic category (from AST-grep analysis). Use for: finding all database operations, auth logic, etc. Requires category.
NODE ID FORMAT: Node IDs come from explore_code_graph or previous query results. Format is "filepath:symbol_name", e.g. "src/services/auth.py:AuthService" or "src/api/routes.ts:handleRequest".
Returns example (for hotspots): { "algorithm": "hotspots", "results": [ {"id": "src/core/engine.py:process", "name": "process", "score": 0.85, "node_type": "function", "file_path": "src/core/engine.py"}, ... ] }
Returns example (for modules): { "algorithm": "modules", "module_count": 5, "results": [ {"module_id": 0, "size": 15, "key_nodes": [...], "cohesion": 0.8}, ... ] }
Source code in src/code_context_agent/mcp/server.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
explore_code_graph ¶
explore_code_graph(
repo_path,
action,
node_id="",
module_id=-1,
target_node="",
depth=1,
category="",
)
Progressively explore a code graph, starting broad and drilling down.
USE THIS WHEN: You want to understand a codebase step by step, starting with a high-level overview and drilling into specific areas of interest.
PREREQUISITE: .code-context/code_graph.json must exist (from start_analysis or 'code-context-agent analyze' CLI). Check for the file first.
RECOMMENDED EXPLORATION FLOW: 1. Start with action="overview" — returns entry points, hotspots, modules, and foundation code. This gives you node IDs and module IDs for drill-down. 2. Pick interesting nodes from the overview results. 3. Use action="expand_node" with a node_id to see its neighbors and relationships. 4. Use action="expand_module" with a module_id to see a cluster's internals. 5. Use action="path" with node_id + target_node to trace how two components connect. 6. Use action="category" to see all code in a business logic category (db, auth, etc.).
GETTING NODE IDs: Node IDs appear in results from overview (entry_points, hotspots, foundations), expand_node, and query_code_graph. They look like "src/services/auth.py:AuthService" or "src/api/handler.ts:processRequest".
Returns example (for overview): { "action": "overview", "total_nodes": 342, "total_edges": 891, "entry_points": [{"id": "src/main.py:main", "name": "main", ...}], "hotspots": [{"id": "src/core/engine.py:process", "score": 0.85, ...}], "modules": [{"module_id": 0, "size": 45, "key_nodes": [...]}], "foundations": [{"id": "src/db/connection.py:get_pool", "score": 0.72, ...}], "explored_count": 25 }
Returns example (for expand_node): { "action": "expand_node", "center": "src/core/engine.py:process", "discovered_nodes": [{"id": "...", "name": "...", "edge_type": "calls"}], "edges": [...], "suggested_next": ["src/core/pipeline.py:Pipeline"], "explored_count": 40 }
Source code in src/code_context_agent/mcp/server.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
get_graph_stats ¶
Get summary statistics about a repository's code graph.
USE THIS WHEN: You want a quick check of whether analysis was successful and what the graph contains before running algorithms.
Returns node and edge counts broken down by type. A healthy graph from a medium codebase typically has 100-500 nodes and 200-2000 edges.
Returns:
| Type | Description |
|---|---|
dict | { "node_count": 342, "edge_count": 891, "node_types": {"function": 180, "class": 45, "method": 90, "pattern_match": 27}, "edge_types": {"calls": 400, "references": 250, "imports": 150, "tests": 91}, "density": 0.0076 |
dict | } |
Source code in src/code_context_agent/mcp/server.py
read_context ¶
Read the CONTEXT.md narrated architecture overview for a repository.
This is the primary human-readable output of the analysis — a <=300 line markdown document covering architecture, business logic, key components, and risks. Best artifact to read first for codebase understanding.
Source code in src/code_context_agent/mcp/server.py
read_graph ¶
Read the raw code_graph.json (NetworkX node-link format).
Contains all nodes (functions, classes, methods, pattern matches) and edges (calls, references, imports, inherits, tests, cochanges). This is the data that query_code_graph and explore_code_graph operate on.
Source code in src/code_context_agent/mcp/server.py
read_manifest ¶
Read the files.all.txt complete file listing (one path per line).
Every file in the repository, respecting .gitignore. Useful for understanding repository size and finding specific file paths.
Source code in src/code_context_agent/mcp/server.py
read_signatures ¶
Read compressed Tree-sitter signatures (function/class signatures only, bodies stripped).
Compact view of the codebase API surface. Useful for understanding the public interface of modules without reading full source code.
Source code in src/code_context_agent/mcp/server.py
read_bundle ¶
Read the curated source code bundle (full source of key files).
Contains the actual source code of the most important files identified during analysis. Larger than signatures but includes implementation details.
Source code in src/code_context_agent/mcp/server.py
read_result ¶
Read the structured AnalysisResult JSON with ranked business logic and risks.
Machine-readable analysis output containing: status, summary, ranked business_logic_items (with scores), architectural risks (with severity), generated file list, and graph statistics.