Skip to content

Level: beginnerTime: 15 minutesPath: MCP

Give OpenCode a local knowledge base ​

Make a folder of your own documents searchable from every OpenCode session, without sending anything to a cloud retrieval service. OpenCode is an MCP client and duckdb-vectors mcp is an MCP server over stdio. One entry in OpenCode's global config gives the agent search and report tools over your indexed folder, in the desktop app and in the terminal alike.

The index is a single DuckDB file on your disk, and the MCP server runs as a local subprocess that OpenCode spawns. There is no network on the query path. This page uses the engineering-brain corpus as its example, and was checked against OpenCode v2.4.2.

Step 1: pick the folder and the database path ​

Choose the folder to index and where to keep the database. Indexing dispatches each file by type: Markdown is split on headings, PDFs are read per page; convert other formats to Markdown first.

Keep the database outside any repository, so it never shows up in a git status. Next to the OpenCode config works well:

shell
$ mkdir -p ~/.config/opencode/knowledge

Note both paths as absolute paths. This page uses /Users/you/Public/engineering-brain/documentation and /Users/you/.config/opencode/knowledge/engineering-brain.duckdb.

Step 2: add the MCP server to the global config ​

Open ~/.config/opencode/opencode.json and add an entry under mcp.servers, next to any settings already there:

json
{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "servers": {
      "engineering-brain": {
        "type": "local",
        "command": [
          "uvx",
          "duckdb-vectors",
          "mcp",
          "--index",
          "/Users/you/Public/engineering-brain/documentation",
          "--recursive",
          "--database",
          "/Users/you/.config/opencode/knowledge/engineering-brain.duckdb"
        ],
        "cwd": "/Users/you/.config/opencode/knowledge",
        "pool": true,
        "environment": {
          "DUCKDB_VECTORS_TAG_FIELDS": "context: $.retrieval.context",
          "DUCKDB_VECTORS_TAG_BM25": "true"
        }
      }
    }
  }
}

uvx fetches and runs the latest duckdb-vectors on demand, so there is no install step. With --index, the server indexes the folder on startup, then serves. The environment block projects the contextual tags that engineering-brain keeps in its sidematter files; drop it for a corpus without them, and see the tagged retrieval workbook for what these settings do.

A few things to get right:

  • Use mcp.servers. OpenCode v2 reads server names under mcp.servers, not directly under mcp, and not under the mcpServers key that Kilo Code and Kiro use. A snippet copied from another client will not register. OpenCode's MCP documentation owns the full list of fields.
  • Set cwd and pool to run one server. OpenCode connects each open project directory to every MCP server on its own. Without pooling, three open directories gave three duckdb-vectors processes on the same database file. pool: true shares one connection, but only for a local server with a fixed cwd; without it, the OpenCode log says mcp pool ignored: local server has no fixed cwd, so each directory runs its own. With both fields set, three directories shared one process.
  • Environment values are literal. OpenCode expands {env:NAME} in these strings but not shell expressions such as $NAME, so the JSONPath $.retrieval.context reaches the server unchanged.
  • Global or project scope. The global file serves every session, which suits a personal library. To scope a knowledge base to one repository instead, put the same entry in that repository's opencode.json; a project config replaces a global server of the same name entirely.
  • From the command line instead, opencode mcp add engineering-brain --global --env NAME=value -- uvx duckdb-vectors mcp ... writes the entry. Add cwd and pool by hand afterwards.
  • uvx on PATH. The config assumes uvx is on the PATH OpenCode inherits. If the server fails to start, set the first command element to the absolute path (which uvx).

Saving the file is enough: the running OpenCode service picked up the change and reconnected the server, without a restart.

Step 3: confirm it works ​

List the servers and their status:

shell
$ opencode mcp list
✓ engineering-brain  connected

duckdb-vectors mcp registers two query-only tools:

  • search(query, top_k=5) — hybrid semantic and exact-token search over the knowledge base; returns a JSON array of ranked chunks (chunk_id, text, source, title, metadata, score).
  • report() — meta-information about the knowledge base (database path, backend, dimensions, chunk count, and sources), for judging what it contains.

Then ask a question your documents answer, and check that the agent calls the tools and cites the returned source:

shell
$ opencode run "Using the engineering-brain knowledge base, what is our convention for git commit messages? Cite the source document."

With the default Code Mode, the agent does not call search as a separate tool. It runs an execute step with code such as tools["engineering-brain"].search({ query: "git commit message convention" }), then answers. In the run above it called report and search, and quoted the rules of the git-workbook.md section "How to add messages to your commits?". Set codemode: false on the server only if you prefer the tools exposed directly; it was not needed here.

You can also check the index without OpenCode:

shell
$ uvx duckdb-vectors status --database /Users/you/.config/opencode/knowledge/engineering-brain.duckdb

First start ​

The first launch downloads the package, the default local embedding model, and two DuckDB extensions, then indexes the whole folder. In the run behind this page, the first connection attempt failed while that first index was running, and opencode mcp list showed failed with a message about the protocol handshake. The next connection, once the index existed, succeeded with the default protocol setting. If you see this on a first start, wait for the index to complete and list the servers again. The Kiro procedure and the performance workbook give measured indexing times for this corpus.

Asking without naming the knowledge base ​

In the checks above, the prompt named the knowledge base, and the agent used it. To have the agent reach for it on questions such as "how do we name AWS resources?" without being told, add a skill that describes when to search. OpenCode loads global skills from ~/.config/opencode/skills/. The search-engineering-brain skill in the Kiro procedure is a starting point. This page has not measured how reliably such a skill triggers in OpenCode.

Keeping the index fresh ​

The index is built when the server starts, not live. After editing the documents, reconnect the server: saving opencode.json reconnects it, and so does restarting OpenCode. Indexing is incremental, so only changed, added, and deleted files are processed.

Next steps ​