opencode MCP server
Lets Claude Code offload cheap, mechanical tasks to opencode's free models for codebase summaries, exploration, web research, and bulk edits, saving paid tokens.
README
opencode MCP server
A small, dependency-light MCP (stdio) server that lets Claude Code delegate cheap, mechanical work to opencode's free models — codebase summaries, exploration, web research, boilerplate, first drafts, contained bulk edits — so paid tokens go where they matter.
Python 3.10+, stdlib + the official mcp SDK. Runs on Linux and Windows.
Tools
| Tool | Purpose |
|---|---|
delegate |
Run one task on a free model. mode:"read" (default) is enforced read-only; mode:"write" may edit files under dir. Supports named sessions (multi-turn), file attachments, per-call model/timeout, and opencode_config to hand the worker extra MCP servers. |
list_sessions |
Named sessions with opencode ids, dirs, turn counts, token/cost tallies. |
forget_session |
Drop a name from local bookkeeping (opencode's server-side session survives). |
ping |
Liveness + doctor: resolves the binary, reports version, round-trips one prompt. |
models |
List model ids (free only by default — free ids rotate). |
Safety model
The worker is a cheap model; the guardrails assume it will occasionally do something dumb.
- Read-only by default.
mode:"read"maps toopencode --agent plan. Verified behavior: file writes and bash mutations are refused (nothing ever hit disk in testing); read-only shell commands and network (websearch) still work — read mode is not offline. - Write mode is confined.
dirmust be an absolute, existing directory; the filesystem root, the home directory itself, and system paths (/etc,/usr,/var, …,C:\Windows,C:\Program Files*) are refused, including their subtrees. Optionally setOPENCODE_MCP_ALLOWED_DIRS(pathsep-separated roots) to allowlist where write mode may operate. - Never
--auto. opencode's own permission prompts stay denied in non-interactive runs. - Everything is reported.
files_changedandtools_usedcome back on every call; point write mode at git-tracked dirs so you can diff/revert. - Hard timeout with tree-kill (opencode spawns child processes): POSIX
process-group kill, Windows
taskkill /T /F. Cancelling the MCP call kills the worker too. Timeouts return partial results and keep the session resumable. - One call per session name at a time; parallel calls on different sessions are fine.
Install & register
cd /root/agent-mcp
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
# register with Claude Code (user scope):
claude mcp add -s user opencode -- /root/agent-mcp/.venv/bin/python /root/agent-mcp/server.py
Windows (PowerShell):
cd C:\path\to\agent-mcp
py -3 -m venv .venv
.venv\Scripts\pip install -r requirements.txt
claude mcp add -s user opencode -- C:\path\to\agent-mcp\.venv\Scripts\python.exe C:\path\to\agent-mcp\server.py
Windows smoke check (3 commands):
opencode --version # CLI reachable?
.venv\Scripts\python -m unittest discover -s tests # parser/guards/kill-path
.venv\Scripts\python tests\e2e_client.py # live free-model round-trip
Configuration (env vars, all optional)
| Var | Default | Meaning |
|---|---|---|
OPENCODE_MCP_BIN |
auto-detect | Path to the opencode executable. Auto-detection: PATH, then the real opencode.exe behind npm .cmd shims on Windows, then ~/.opencode/bin/opencode. |
OPENCODE_MCP_MODEL |
opencode/deepseek-v4-flash-free |
Default model id. |
OPENCODE_MCP_STATE |
~/.local/state/opencode-mcp / %LOCALAPPDATA%\opencode-mcp |
Where sessions.json lives. |
OPENCODE_MCP_TIMEOUT |
180 |
Default per-call timeout (s); per-call timeout_sec clamps to 10–570. |
OPENCODE_MCP_MAX_TEXT |
40000 |
Truncate the worker's answer beyond this many chars (truncated: true). |
OPENCODE_MCP_ALLOWED_DIRS |
unset | If set, write-mode dir must live under one of these roots. |
Giving the worker MCP tools
Pass opencode_config (absolute path) to delegate, pointing at e.g.:
{
"mcp": {
"gbk-fs": {
"type": "local",
"command": ["/root/gbk-fs/.venv/bin/python3", "-m", "gbk_fs"],
"enabled": true
}
}
}
Verified live: the free model discovers and calls such tools (they surface to
it as gbk-fs_list_files, etc.).
Tests
.venv/bin/python -m unittest discover -s tests # offline: real captured fixtures
.venv/bin/python tests/e2e_client.py # live: needs network, ~1-2 min, $0
Design notes / gotchas (hard-won)
- The task prompt is piped via stdin (write, close). Never pass it in argv:
a
--separator makes opencode drop the message and hang waiting on stdin, and Windows caps argv at 32 KB. - Resume sessions with
-s <ses_id>(never--continue). opencode run --format jsonemits JSONL events; the answer is the concatenation oftextparts, tokens/cost arrive instep-finishparts, file writes are completedtoolparts (write/edit/patch/multiedit) withstate.input.filePath.- Pipes are forced to UTF-8 (
errors="replace") — Windows would otherwise default to cp125x. stderr goes to a temp file to avoid two-pipe deadlock. - Free model ids rotate; the
modelstool exists so the orchestrator can self-correct when the default disappears.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
Neon Database
MCP server for interacting with Neon Management API and databases
E2B
Using MCP to run code via e2b.
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.