CodingOpen SourceFreeActiveMachine-verified· intermediate · ~10 min setup

OpenCode: add an MCP tool, then lock it to one agent

Plug in an MCP server (live docs via Context7) but disable it globally and switch it on only for the one agent that needs it, so it does not burn tokens every turn.

by Shilpa Mitra· verified today· v1.0.0

Run this workflow

CI-verified, 2/2 fixtures passing.

Build this with your agent

One copy-paste hands Claude Code, Codex, or Cursor the full recipe, steps included, nothing to fetch.

Intended Use

Anyone adding MCP tools to OpenCode without bloating every session. CI parses opencode.json and asserts the context7 server is a remote MCP with a url, disabled globally via tools (context7*: false), and re-enabled only for the docs agent (context7*: true). No key, no network. The model using the tool is fenced.

Not for

  • Enabling every MCP server globally, that adds their tools to every turn and burns tokens
  • Expecting CI to call the MCP server, only that the scoping config is well-formed

The Stack

Tested Against

opencode@1.17.4opencode.ai/docs (2026-06)node@20.x

Side effects & data flow

Network
none, local only
Writes
./opencode.json
Credentials
none required

Prerequisites

  • OpenCode installed
  • A provider logged in to actually run it

Steps

  1. 1

    Add the MCP server, disable it globally, scope it to one agent

    Declare the Context7 remote server, set tools context7* to false globally, and re-enable it only under the docs agent. Local servers work the same with type: local and a command array. CI parses opencode.json and asserts the scoping is exactly this.

    cat > opencode.json <<'JSON'
    {
      "$schema": "https://opencode.ai/config.json",
      "mcp": {
        "context7": { "type": "remote", "url": "https://mcp.context7.com/mcp" }
      },
      "tools": { "context7*": false },
      "agent": {
        "docs": { "mode": "primary", "model": "provider/your-model", "tools": { "context7*": true } }
      }
    }
    JSON
    node -e 'const c=JSON.parse(require("fs").readFileSync("opencode.json","utf8"));const m=c.mcp.context7;const globalOff=c.tools["context7*"]===false;const agentOn=c.agent.docs.tools["context7*"]===true;if(m&&m.type==="remote"&&typeof m.url==="string"&&m.url.length>0&&globalOff&&agentOn){console.log("config OK: context7 remote MCP, disabled globally, re-enabled only for the docs agent")}else{console.log("BAD");process.exit(1)}'
  2. 2

    Use the docs agent (the model step, not checked by CI)

    Switch to the docs agent and the Context7 tools are available there only; everywhere else they stay off, saving context. The model using the tool is fenced, so CI never claims it.

Eval, 2 fixtures

Last passed: verified today
  • mcp-scopedcontainstimeout 30s · max $0

    Expected: config OK: context7 remote MCP, disabled globally, re-enabled only for the docs agent

  • clean-exitexit_codetimeout 30s · max $0

    Expected: 0

Results

Every MCP server adds tools to the context on every turn, which burns tokens fast. Scoping a server to a single agent is the fix; heavy servers (the GitHub one is notorious) can blow past your context limit alone.

Did this work for you?

Our CI checks the setup runs. You tell us if the whole thing worked. Tell us straight.

Liked this workflow?

Get new verified workflows in WebAfterAI, three issues a week (Tue, Thu, Sat).