Skip to main content
Glama
mcp-codebase-server

mcp-codebase-server

by codeyrk
README.md
# mcp-codebase-server

A **security-first MCP (Model Context Protocol) server** for C++ monorepos.
Gives Claude Desktop and VS Code deep codebase awareness — symbol search, impact analysis, branch diff review, linting, and format checking — **without ever sending raw source code to external AI**.

---

## Security Guarantees

| Guarantee | How |
|---|---|
| **Zero code egress** | Raw source code never leaves your machine |
| **Pre-index secret scrubbing** | Known secret patterns redacted before SQLite index |
| **Structured metadata only** | AI receives symbol graphs, signatures, dependency maps — never file contents |
| **Output validation** | Every tool response is scanned before returning — defence in depth |
| **LDAP identity binding** | Every tool call stamped with developer identity (or OS username in degraded mode) |
| **stdio transport** | Zero inbound network ports |

---

## Prerequisites

| Requirement | Notes |
|---|---|
| Node.js 20+ | Required for `--env-file` support |
| C++ monorepo on disk | Set `REPO_PATH` in `.env` |
| Docker (optional) | Required only for `run_linter` (clang-tidy) |
| clang-format (optional) | Required only for `format_check` — `brew install clang-format` |

---

## Quick Start

```bash
# 1. Install dependencies
npm install

# 2. Copy and edit the environment file
cp .env.example .env
# Edit .env — at minimum set REPO_PATH=/absolute/path/to/your/monorepo

# 3. Build
npm run build

# 4. Index the codebase (first time, and after large changes)
npm run index

# 5. Start the server
npm start
# Should print: [server] mcp-codebase-server v0.1.0 ready
```

---

## Environment Configuration (`.env`)

```bash
# REQUIRED
REPO_PATH=/absolute/path/to/your/monorepo

# Optional overrides
# INDEX_DB_PATH=$HOME/.mcp-server/index.db
# AUDIT_LOG_DIR=$HOME/.mcp-server/audit
# LOG_LEVEL=info

# Docker — required for run_linter (clang-tidy)
DOCKER_CONTAINER=your-build-container-name
COMPILE_COMMANDS_PATH=/container/path/to/compile_commands.json
CONTAINER_REPO_PATH=/container/path/to/repo
CLANG_TIDY_BIN=/usr/lib/llvm-17/bin/clang-tidy   # full path if not on container PATH

# Host clang-format — required for format_check
CLANG_FORMAT_BIN=/opt/homebrew/bin/clang-format   # brew install clang-format

# LDAP / Active Directory (leave blank for OS-username fallback)
# LDAP_URL=ldap://ldap.yourcompany.com:389
# LDAP_BASE_DN=dc=yourcompany,dc=com
# LDAP_BIND_DN=cn=mcp-service,ou=serviceaccounts,dc=yourcompany,dc=com
# LDAP_BIND_PASSWORD=
```

---

## Connecting to Claude Desktop

Edit `~/Library/Application Support/Claude/claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "codebase": {
      "command": "node",
      "args": [
        "--env-file=/absolute/path/to/mcp-codebase-server/.env",
        "/absolute/path/to/mcp-codebase-server/dist/index.js"
      ]
    }
  }
}
```

Restart Claude Desktop. The 8 codebase tools will appear in the tool list.

---

## Connecting to VS Code (Cline / Continue)

Add to `.vscode/mcp.json` in your workspace:

```json
{
  "servers": {
    "codebase": {
      "type": "stdio",
      "command": "node",
      "args": [
        "--env-file=/path/to/mcp-codebase-server/.env",
        "/path/to/mcp-codebase-server/dist/index.js"
      ]
    }
  }
}
```

---

## Available Tools (8 total)

### Navigation & Search

| Tool | What it returns |
|---|---|
| `search_code` | Ranked list of symbols matching a name, concept, or keyword. Never raw code. |
| `get_symbol` | Full structural profile: location, signature, includes, sibling symbols, complexity. |
| `explain_file` | Everything a file defines + what it includes + git change history + architectural role. |
| `index_stats` | Index health: symbol count, file count, CMake targets, include edges. Call first if results seem incomplete. |

### Impact Analysis

| Tool | What it returns |
|---|---|
| `blast_radius` | Given a file or symbol: all files that need recompilation, affected CMake targets, risk level (low/medium/high/critical), test suites to run. |
| `diff_review` | Given a branch name: all changed files with their symbols, aggregate blast radius, risk level, and test suites. Essential before code review or merge. |

### Code Quality

| Tool | What it returns |
|---|---|
| `run_linter` | Runs clang-tidy inside the build container. Returns structured findings (file, line, severity, rule, message). Never raw code. Requires Docker. |
| `format_check` | Runs clang-format `--dry-run` on host. Returns structured violations (file, line, col). Never raw diffs. Requires `clang-format` binary. |

---

## Typical Workflow

```
1. diff_review branch=feature/my-branch
      → Lists changed files + symbols + risk level

2. blast_radius file=src/core/MyChangedHeader.hpp
      → Shows full recompilation impact

3. run_linter files=[changed files from step 1]
      → Checks for clang-tidy issues

4. format_check files=[changed files from step 1]
      → Confirms formatting is clean before merge
```

---

## Indexer

The indexer uses **tree-sitter** for AST-accurate C++ symbol extraction:
- Functions, methods (including qualified `Foo::bar()`), constructors, destructors
- Operator overloads (`operator==`, `operator[]`)
- Template functions and class templates
- Classes, structs, enums
- Namespace context preserved

Run the indexer:
```bash
npm run index          # indexes REPO_PATH from .env
```

Rebuild after large file additions/deletions:
```bash
rm ~/.mcp-server/index.db && npm run index
```

The index also builds:
- **Include graph** — transitive `#include` dependency map (used by `blast_radius`)
- **CMake target map** — which targets own which files (used by `blast_radius` and `diff_review`)

---

## Secret Scrubbing

Secrets are redacted **before indexing** — they never enter the SQLite database.

Redacted patterns include:
- PEM private keys
- API keys and auth tokens
- Hardcoded passwords
- Internal IP addresses
- Flask / Django secret keys
- Base64-encoded credentials

All redacted values are replaced with `[REDACTED:<type>]` in signatures.

---

## Audit Logs

Every tool call is logged to `~/.mcp-server/audit/YYYY-MM-DD.jsonl`.

Each entry contains: timestamp, session ID, developer identity, tool name, query, files accessed, result count, duration.

Logs **never** contain: raw code, secret values, or file contents.

---

## Project Structure

```
mcp-codebase-server/
├── src/
│   ├── index.ts                  # Entry point — boots server, registers all 8 tools
│   ├── config/
│   │   └── config.ts             # Zod-validated config from env vars + ~/.mcp-server/config.json
│   ├── audit/
│   │   └── audit-logger.ts       # Structured JSONL audit logging (never logs code)
│   ├── security/
│   │   ├── secret-scrubber.ts    # Pre-index secret redaction pipeline
│   │   ├── output-validator.ts   # Post-response secret pattern scanner
│   │   └── ldap-identity.ts      # LDAP developer identity (OS username fallback)
│   ├── indexer/
│   │   ├── cpp-indexer.ts        # tree-sitter AST C++ parser → SQLite symbol index
│   │   ├── include-graph.ts      # #include transitive dependency graph
│   │   ├── cmake-parser.ts       # CMakeLists.txt target → file mapping
│   │   └── index-runner.ts       # CLI entry point for npm run index
│   └── tools/
│       ├── search-code.ts        # search_code
│       ├── get-symbol.ts         # get_symbol
│       ├── explain-file.ts       # explain_file
│       ├── index-stats.ts        # index_stats
│       ├── blast-radius.ts       # blast_radius
│       ├── diff-review.ts        # diff_review
│       ├── run-linter.ts         # run_linter (clang-tidy via Docker)
│       └── format-check.ts       # format_check (clang-format on host)
├── scripts/
│   ├── verify-blast-radius.ts
│   ├── verify-diff-review.ts
│   ├── verify-run-linter.ts
│   ├── verify-run-linter-live.ts
│   └── verify-format-check.ts
├── .env.example                  # Template — copy to .env and fill in
├── package.json
└── tsconfig.json
```

---

## Roadmap

| Phase | Status | Items |
|---|---|---|
| **Phase 1** | ✅ Complete | Symbol index, code search, file explanation, secret scrubbing, LDAP audit |
| **Phase 2** | ✅ Complete | tree-sitter parser, include graph, CMake map, blast_radius, diff_review, run_linter, format_check |
| **Phase 3** | Planned | Mermaid diagram generation (include graph, CMake targets, C4 architecture) |
| **Phase 4** | Planned | Convention registry, project memory, git history context |