C++ MCP Server: libclang AST Code Analysis
Analyze C++ code with an MCP server using libclang AST parsing to find classes, navigate inheritance, trace calls, and explore code relationships.
npx -y @kandrwmrtn/cplusplus_mcpOverview
This project implements an MCP (Model Context Protocol) server for C++ code analysis using libclang’s AST. It parses C++ codebases to extract structural information—classes, methods, inheritance relationships, call sites—and exposes that information to MCP-aware clients. The server is intended for use in code-understanding integrations, refactoring tools, visualization UIs, or any automated workflow that needs an up-to-date, queryable representation of a C++ project.
Using libclang gives robust, compiler-aware parsing (headers, templates, macros) and produces an abstract syntax tree (AST) that the server walks to build indexes and relationship graphs. The MCP server provides back-end services for searches, navigation (e.g., find subclasses or callers), and trace queries across translation units without requiring a full language server or IDE integration.
Features
- AST-based analysis using libclang to accurately reflect compiler semantics
- Class and method discovery across translation units
- Inheritance navigation: find base classes, derived classes, virtual overrides
- Call tracing: call graph traversal to find callers and callees
- Cross-file relationship exploration (fields, typedefs, template instantiations)
- Incremental indexing and re-scan support for active development
- Exposes analysis results over the MCP (Model Context Protocol) for client integrations
Installation / Configuration
Prerequisites (example for Debian/Ubuntu):
Clone and build:
&&
Common runtime configuration (example):
- LIBCLANG path: point the server to your libclang shared library if it’s not in a standard location (e.g., /usr/lib/llvm-12/lib/libclang.so).
- Source root: the top-level directory of the C++ project to analyze.
- Compilation database: a compile_commands.json is recommended so libclang can parse files with the right flags.
Example run command (adjust flags to your build):
# Example: run the MCP server, serving the given source root
Environment variable option:
If your project uses a custom build system, generate compile_commands.json (CMake: -DCMAKE_EXPORT_COMPILE_COMMANDS=ON, or use Bear for other builds).
Available Tools / Resources
The server exposes several built-in analysis tools (available as MCP tools/resources). Typical tools include:
| Tool name | Purpose | Input | Output |
|---|---|---|---|
| class_search | Find classes by name or pattern | name or regex | list of class symbols with locations |
| inheritance | Navigate base/derived relationships | class symbol | base/derived class lists |
| call_tracer | Trace callers or callees | function symbol, depth limit | call graph nodes/edges |
| ast_dump | Dump AST nodes for a file or symbol | file path or symbol | structured AST fragment |
| indexer | Build/update project index | source root | internal indexes for queries |
These tools are typically invoked by sending MCP requests (JSON-based messages) to the server and receiving structured JSON responses. The exact MCP message formats follow the project’s protocol schema; clients should consult the server’s protocol documentation or example client implementations in the repository.
Use Cases
- Find all subclasses of a base class before a refactor:
- Use the inheritance tool to enumerate derived classes, including indirect derivations across translation units.
- Trace who calls a particular function or method:
- Use call_tracer to find direct callers; increase depth to see callers-of-callers when evaluating downstream impacts.
- Locate class usages and relationships for design reviews:
- Use class_search + ast_dump to inspect field types, template instantiations, and typedefs referencing a class.
- Integrate with visualization tools:
- Use the server as a back-end for graph visualizers to display inheritance trees or call graphs for large codebases.
- Automated audits and change impact analysis:
- Programmatically query the MCP server to gather lists of affected symbols for CI checks or security scanning.
Quick Examples
Conceptual examples (MCP messages are JSON over TCP/HTTP depending on deployment):
Find classes matching “Manager”:
Trace callers of MyNamespace::doWork with depth 2:
The server will return structured JSON containing symbol identifiers, source locations, and relationship edges suitable for client-side presentation.
If you are integrating this server into tooling, start by building the project, ensuring libclang is reachable, provide a compile_commands.json for accurate parsing, and use the example client messages in the repository to explore available MCP interactions.