Skip to main content
Glama
MCP Codebase Inspector

MCP Codebase Inspector

by crizmo
README.md
# MCP Codebase Inspector

A tiny **read-only MCP server** that gives an LLM safe, on-demand access to a local codebase. The model discovers the tools itself, picks the right ones, and answers from real files instead of guesses.

Built to demonstrate the Model Context Protocol end to end: stdio transport, tool and prompt discovery, runtime path confinement, and a real model driving the loop (Big Pickle via OpenCode). No API key is needed.

## Why this exists

Most MCP tutorials call hardcoded functions and call it an agent. This one doesn't:

- The server registers capabilities; the client **discovers** them at runtime.
- The model chooses tools and arguments — no keyword matching.
- Every tool routes through one shared path guard, so "read-only" is enforced, not promised.
- The whole thing is ~150 lines of server code and uses only the `mcp` SDK plus the standard library.

## What it exposes

Four tools:

| Tool | Purpose |
|---|---|
| `project_summary(path)` | File/line counts, languages, largest files |
| `list_directory(path)` | One directory level, with sizes |
| `read_file(path, start_line, max_lines)` | Numbered lines from any offset, capped |
| `search_code(query, path, file_glob, max_results)` | Regex search → `file:line:match` |

One prompt: `codebase_report(path)` tells the model how to chain the tools into a short report.

Vendor directories and files over 2 MB are skipped. Paths are confined to `MCP_ROOT` (default: launch directory); traversal and symlinks pointing outside are rejected.

## Quickstart

```bash
git clone https://github.com/crizmo/mcp-codebase-inspector.git
cd mcp-codebase-inspector
python3 -m pip install -e .
opencode mcp add codebase -- "$(python3 -c 'import sys;print(sys.executable)')" mcp_server.py
opencode run --model opencode/big-pickle --auto \
  "Use the codebase MCP tools to analyze this project. What does it do, main components, and two improvements?"
```

Requires Python 3.10+ and [`opencode`](https://opencode.ai). `opencode mcp add` writes a local `opencode.json` with this machine's Python path (the file is gitignored, so the repo stays portable).

To inspect a different directory, set `MCP_ROOT` in `opencode.json`:

```json
"environment": { "MCP_ROOT": "/path/to/project" }
```

## Example output

```
> build · big-pickle
⚙ execute {"code":"const [summary, root] = await Promise.all([
    tools.codebase.project_summary({ path: \".\" }),
    tools.codebase.list_directory({ path: \".\" })]); return { summary, root };"}

## What it does
mcp-codebase-inspector is a tiny read-only MCP server that gives an LLM safe, on-demand
access to a local codebase...

## Two concrete improvements
1. Cap read_file inputs.
2. Return a readable error on path escape.
```

Those two suggestions were real bugs. Both were fixed — this is the loop actually working.

## Other commands

```bash
python3 mcp_inspect.py                              # raw MCP protocol
python3 -m unittest discover -s tests -t . -v       # 4 trust-boundary tests
```

`mcp_inspect.py` is the fastest way to learn MCP: it prints the exact requests and responses the model sees.

**VS Code:** open the folder → Terminal → **Run Task** → `Big Pickle: analyze this repo`, `MCP protocol inspector`, or `Run tests`.

## Layout

```
mcp_server.py          server: tools, prompt, path guard
mcp_inspect.py         tiny stdio client that prints the protocol flow
tests/                 unittest checks for traversal, symlinks, line and result caps
pyproject.toml         package metadata and console entry points
opencode.json          generated by `opencode mcp add` (gitignored)
.github/workflows/     CI: install + unit tests on Python 3.10 and 3.13
```

## Design decisions

- **Read-only by construction.** No write, delete, or execute tools.
- **One guard, all callers.** `_path()` and `_files()` are the only places that resolve paths.
- **Stdlib first.** Only the MCP SDK is a dependency.
- **No framework.** `unittest` is enough.

MIT licensed.