Ollama MCP
by EugenioTesio
README.md
# Ollama MCP — local-first development
**A Python MCP server for Cursor, Claude Desktop and any other stdio MCP client.**
It exposes three read-only tools over **stdio**:
| MCP tool | Input | Work done on your Mac |
| --- | --- | --- |
| `search_code` | Query + path (+ optional `workspace_root`) | ripgrep search (no LLM) |
| `summarize_files` | List of file paths (+ optional `workspace_root`) | Reads source files and summarizes with Ollama |
| `analyze_logs` | Path to a log file (+ optional `workspace_root`) | Deduplicates errors and diagnoses with Ollama |
The token-saving detail is that the client sends **file paths** as MCP arguments instead of whole source files or build logs, and receives compact tool results. The client's own model decides when to call the tools.
One installed server works for **any number of projects**: each call says which project it is about. Any project you name works.
## 1. Install once (global installation)
Requirements: Python 3.11+, [Ollama](https://ollama.com/download/mac), and ripgrep.
```bash
brew install ripgrep
ollama pull qwen3.5:9b-q4_K_M
```
**Option A — from PyPI (recommended).** Install the `ollama-mcp` command into its own isolated environment with [pipx](https://pipx.pypa.io) or [uv](https://docs.astral.sh/uv/):
```bash
pipx install ollama-mcp # or: uv tool install ollama-mcp
which ollama-mcp # e.g. /Users/me/.local/bin/ollama-mcp
```
MCP clients then start the server with that absolute path and no arguments:
```json
"command": "/Users/me/.local/bin/ollama-mcp",
"args": []
```
**Option B — from source.**
```bash
cd /absolute/path/to/ollama-mcp
python3 -m venv .venv
source .venv/bin/activate
pip install -e .
```
The server is then started by any MCP client with either:
- `/absolute/path/to/ollama-mcp/.venv/bin/ollama-mcp`, or
- `/absolute/path/to/ollama-mcp/.venv/bin/python -m ollama_mcp.server`.
The configuration examples below use the Option B form; with Option A, replace `command` and `args` as shown above.
Always use an **absolute path** to the command; GUI apps such as Claude Desktop do not inherit your shell's `PATH` or activated virtualenv. For the same reason the server also looks for ripgrep in `/opt/homebrew/bin` and `/usr/local/bin`; set `RIPGREP_PATH` if yours is elsewhere.
Check Ollama with `ollama run qwen3.5:9b-q4_K_M`. The **6.6 GB** model size listed in Ollama is primarily download/disk size, **not** full runtime RAM usage; expect additional memory for the KV cache and backend. Start with an 8K-token context.
The package pins the **MCP Python SDK v1 maintenance series** (`mcp>=1.28,<2`) for compatibility with its tested `FastMCP` API.
## 2. How paths are resolved
Every tool accepts an optional `workspace_root` argument (absolute project directory, `~` allowed).
| Path given | Resolved against |
| --- | --- |
| Absolute (`/Users/me/code/app/lib/main.dart`) | Used as is; no workspace needed |
| Relative + `workspace_root` in the call | `workspace_root` |
| Relative, no `workspace_root` | `WORKSPACE_ROOT` environment variable, if set and valid |
| Relative, neither | Clear error asking for `workspace_root` or an absolute path |
The server **never** uses its current working directory as "the project": Claude Desktop, for example, starts servers with an unrelated working directory.
### `WORKSPACE_ROOT` is optional
- **Unset:** the server starts normally; clients pass `workspace_root` or absolute paths.
- **Set to an absolute directory:** it is the default for relative paths (the previous behaviour, so existing Cursor per-project configs keep working). A per-call `workspace_root` overrides it.
- **Set but unusable** (relative path, missing directory, `/`, your home directory, or an unexpanded `${workspaceFolder}`): the server logs a warning on stderr and runs as if it were unset.
## 3. Configure Claude Desktop
Edit `~/Library/Application Support/Claude/claude_desktop_config.json` (Claude Desktop → Settings → Developer → Edit Config) and merge this entry into any existing `mcpServers`:
```json
{
"mcpServers": {
"ollama-local": {
"command": "/absolute/path/to/ollama-mcp/.venv/bin/python",
"args": ["-m", "ollama_mcp.server"],
"env": {
"OLLAMA_BASE_URL": "http://127.0.0.1:11434",
"OLLAMA_MODEL": "qwen3.5:9b-q4_K_M",
"OLLAMA_NUM_CTX": "8192"
}
}
}
}
```
Leave `WORKSPACE_ROOT` out of this config: Claude Desktop has no notion of an open project, so name the project in your prompt (see examples below). Fully quit and reopen Claude Desktop after editing.
Ready-made files are in the source repository under [`templates/claude-desktop/`](templates/claude-desktop/) (they are not part of the PyPI package): this config entry, instructions to paste into a Claude Project (so it always passes the right `workspace_root`), and an optional skill. See [`templates/README.md`](templates/README.md).
## 4. Configure Cursor
**Option A — global (one entry for every project).** Put this in `~/.cursor/mcp.json`:
```json
{
"mcpServers": {
"ollama-local": {
"type": "stdio",
"command": "/absolute/path/to/ollama-mcp/.venv/bin/python",
"args": ["-m", "ollama_mcp.server"],
"env": {
"WORKSPACE_ROOT": "${workspaceFolder}",
"OLLAMA_BASE_URL": "http://127.0.0.1:11434",
"OLLAMA_MODEL": "qwen3.5:9b-q4_K_M",
"OLLAMA_NUM_CTX": "8192"
}
}
}
}
```
When Cursor expands `${workspaceFolder}`, relative paths work against the open project. If it is not expanded (for example when no folder is open), the server ignores it and the agent passes `workspace_root` or absolute paths. The server is a single process, so with several Cursor windows open, passing `workspace_root` explicitly is the reliable choice; the included rule asks the agent to do that.
**Option B — per project (previous setup, unchanged).** Copy `templates/cursor/.cursor/` into a repository's `.cursor/` directory and replace `/ABSOLUTE/PATH/TO/ollama-mcp/.venv/bin/python` in `.cursor/mcp.json` with your real path. This creates:
```text
your-project/
└── .cursor/
├── mcp.json
├── rules/prefer-local-mcp.mdc
└── skills/local-first-development/SKILL.md
```
Avoid overwriting an existing `.cursor/mcp.json` without merging its other `mcpServers` entries. The rule and skill are instructions that guide the agent toward the tools.
Open Cursor Settings → MCP and verify the server is available. Restart/reload Cursor if necessary.
## 5. Try it — different projects, one server
Claude Desktop (or Cursor without a default workspace):
```text
Use ollama-local search_code to find AccountBloc in /Users/me/code/banking-app.
```
```text
Summarize lib/features/accounts/account_bloc.dart and lib/features/accounts/account_repository.dart
in the project /Users/me/code/banking-app.
```
```text
Analyze the log /Users/me/code/api-server/logs/test.log.
```
```text
In /Users/me/code/api-server, search for "def create_order".
```
The resulting tool calls look like:
```json
{"query": "AccountBloc", "path": ".", "workspace_root": "/Users/me/code/banking-app"}
{"paths": ["src/orders.py"], "workspace_root": "/Users/me/code/api-server"}
{"log_path": "/Users/me/code/api-server/logs/test.log"}
```
Cursor with `WORKSPACE_ROOT` set:
```text
Use the local MCP to find references to AccountBloc in this repository.
```
To analyze logs, save them to disk first, e.g. `flutter analyze > logs/analyze.log 2>&1`, then ask for `logs/analyze.log`.
To verify registration independently, use the MCP Inspector or the Python SDK client. The server speaks stdio and all diagnostics go to stderr.
## 6. Safety and limits
- Uses the local Ollama URL `127.0.0.1:11434` by default. All processing stays on your machine.
- **Read-only.** The tools search and read files; they edit nothing and run no commands.
- **Scoped reads.** Any project you name works. `/` and your home directory are refused as a workspace or search directory (too broad).
- **Symlinks:** a path must stay inside its workspace after resolution; for a standalone absolute file, inside its own directory. Symlinks that point elsewhere are refused, and recursive search never follows symlinks. `..` is refused. A symlinked `workspace_root` itself is fine.
- **Never readable:** private keys and keystores (`*.pem`, `*.key`, `*.p12`, `*.pfx`, `*.jks`, `*.keystore`, `*.ppk`, `id_rsa`, `id_ed25519`, …) and anything under `.ssh`, `.gnupg`, `.aws`, `.azure`, `.kube`, `.docker`.
- **Needs explicit intent:** `.env`, `.env.*`, `.envrc`, `.npmrc`, `.pypirc`, `.netrc`, `.git-credentials`, `credentials.json`, `service-account.json`, and files under `.git` or `.config` are refused unless the call passes `allow_sensitive: true`, which the tool descriptions reserve for when the user explicitly asked for that file. Clients that show tool arguments for approval will display this flag. `search_code` never searches these files.
- A symlink is checked under both its own name and its target's, so `notes.txt -> .env` is still treated as sensitive.
- Generated folders (`node_modules`, `build`, `.venv`, `venv`, `.dart_tool`, `__pycache__`) are skipped by search but can be read when named explicitly (e.g. `build/test.log`).
- Input files are limited to 65 KB each for summarization, 72K characters combined, and 2 MB for a log. Large summaries are processed in chunks (up to 10).
- `search_code` returns up to 50 matches (default 25); it uses literal search, not semantic RAG.
- Secret redaction is **best effort**.
- Tool responses should be treated as hypotheses, especially for failures and architectural conclusions.
- Minimal local token/latency metadata is stored at `~/.ollama-mcp/metrics.sqlite3` (override with `MCP_METRICS_DB`); code, paths and logs are not stored there.
### Environment variables
| Variable | Default | Purpose |
| --- | --- | --- |
| `WORKSPACE_ROOT` | unset | Optional default project for relative paths |
| `OLLAMA_BASE_URL` | `http://127.0.0.1:11434` | Local Ollama endpoint |
| `OLLAMA_MODEL` | `qwen3.5:9b-q4_K_M` | Model used for summaries and log analysis |
| `OLLAMA_NUM_CTX` | `8192` | Context window |
| `OLLAMA_TIMEOUT_SECONDS` | `120` | Request timeout |
| `MCP_METRICS_DB` | `~/.ollama-mcp/metrics.sqlite3` | Metrics database |
| `RIPGREP_PATH` | auto-detected | Explicit path to `rg` |
## 7. Test
```bash
python -m unittest discover -s tests -v
```
Tests use a fake local model, so they run without Ollama. One test starts the real server over stdio without `WORKSPACE_ROOT` (as Claude Desktop does) and calls it with an MCP client. A real Cursor/Claude Desktop + Ollama acceptance test still needs to be run on your Mac.
## 8. Next optimization
Use 10-20 real tasks to compare the client with and without this MCP: total cloud tokens, tool latency, failures, and human corrections. A small always-on rule adds a little prompt overhead, so actual net savings must be measured.