Skip to content

feat(mcp): expose knowledge-base tools to all MCP clients - #295

Merged
Helweg merged 1 commit into
Helweg:mainfrom
dkhokhlov:feat/mcp-knowledge-base-tools
Aug 16, 2026
Merged

feat(mcp): expose knowledge-base tools to all MCP clients#295
Helweg merged 1 commit into
Helweg:mainfrom
dkhokhlov:feat/mcp-knowledge-base-tools

Conversation

@dkhokhlov

@dkhokhlov dkhokhlov commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Registers add_knowledge_base, list_knowledge_bases, and remove_knowledge_base as MCP server tools so every MCP client (Codex, Claude Code, Jcode, and other MCP hosts) can manage knowledge bases.
Previously these operations were exposed only to the OpenCode plugin and the Pi extension (under host-native names); MCP clients had no way to add, list, or remove knowledge bases through the MCP
server.

Implements #298

Background

The knowledge-base operations (addKnowledgeBase, listKnowledgeBase, removeKnowledgeBase in src/tools/operations.ts) were deliberately scoped to OpenCode and Pi, not overlooked: KB management
mutates persistent per-host config and rebuilds the shared indexer, and OpenCode/Pi pass a per-call worktree so KB edits land in worktree-local config. The MCP server, however, runs at a fixed
project root (process.cwd() / --project) for its lifetime, and all existing MCP tools already operate on that fixed root. Exposing KB tools on the startup root is therefore consistent with the
existing MCP model, not a new behavior.

Changes

  • src/tools/tool-names.ts — MCP_TOOL_NAMES is no longer a bare alias of PORTABLE_TOOL_NAMES; it is now PORTABLE_TOOL_NAMES + the three KB tools. PORTABLE_TOOL_NAMES is left untouched because it
    is a Pi contract (tests/pi-package.test.ts asserts Pi exposes every portable name, and Pi exposes KB tools only under its knowledge_base_* aliases).
  • src/adapters/mcp/register-tools.ts — registers the three KB tools after code_communities, calling the existing operation functions with runtime.projectRoot + runtime.host. Returns are wrapped
    so a response whose text starts with "Error: " is reported with MCP isError: true; informational returns ("already configured", "not found") are not flagged as errors.
  • docs/tools.md — recomputes the host surface matrix (portable core 15; MCP 18 tools + 5 prompts; OpenCode 19; Pi 18), adds codebase_edit_context and code_communities to the portable list,
    reclassifies the KB tools as shared by MCP + OpenCode, and documents the worktree, multi-host, config-layering, and repo-only-blame semantics.
  • README.md — notes that MCP clients can add/list/remove knowledge bases via the exposed tools.
  • tests/mcp-server.test.ts — updates the stale tool-count test name to 18.
  • tests/mcp-knowledge-bases.test.ts (new) — MCP-to-operation integration tests: real temp project + MockIndexer + in-memory client; verifies add persists to the host-specific config path
    (.claude/codebase-index.json for claude, .codebase-index/config.json for codex) and refreshes the indexer; list shows Exists; remove clears it; non-existent path returns isError: true with
    "Error: Directory does not exist" and writes no config / triggers no refresh; remove of an unconfigured path returns "Knowledge base not found" without isError; missing required path is rejected
    by Zod with isError: true.

Behavior notes

  • add_knowledge_base writes the path to the project-local host config of the MCP server (e.g. /.claude/codebase-index.json for the claude host, /.codebase-index/config.json for
    codex/jcode/pi) and refreshes the index. It is not a user-global change.
  • The index is the union of the project code and the configured knowledge-base folders (getScopedRoots() = {projectRoot} ∪ knowledgeBases). Knowledge-base source/text files are searchable by all
    retrieval tools.
  • list_knowledge_bases shows the union of project-local and user-global knowledge bases. remove_knowledge_base edits only the project-local layer, so a knowledge base inherited from a user-global
    config returns "Knowledge base not found" and is not removed.
  • Git blame metadata is collected only for files in the project git repository. Knowledge-base files outside the repository remain searchable by content but have no blame, so
    blameAuthor/blameSha/blameSince filters match only in-repo files.
  • The MCP server runs at a fixed project root for its lifetime; KB tools are not per-call worktree-aware. For worktree-local KB management, use OpenCode or Pi.

Tests

  • npx vitest run tests/mcp-server.test.ts tests/mcp-knowledge-bases.test.ts — green.
  • npx vitest run tests/pi-package.test.ts tests/tools-knowledge-bases.test.ts — Pi contract and existing operation tests unaffected.
  • Full npx vitest run — green (pre-existing failures on main unchanged; none caused by this change).

Manual verification

Installed and driven over stdio with raw JSON-RPC (--project --host claude, embeddings via OLLAMA_HOST → nomic-embed-text):

  1. tools/list → 18 tools, KB trio present.
  2. add_knowledge_base (sibling repo json-c) → Total knowledge bases: 1, wrote project-local .claude/codebase-index.json (only knowledgeBases; no inherited provider setting leaked).
  3. list_knowledge_bases → Status: Exists.
  4. index_codebase → 350 files processed, 6391 new chunks embedded (union of repo + json-c).
  5. Retrieval after rebuild: codebase_search "json_object_from_file" → json-c definitions with full source (json_util.c:68-98, score 0.99); implementation_lookup → authoritative def in json-c;
    codebase_peek → json-c lh_table_lookup/json_object_object_add alongside repo hits (union confirmed), with repo hits carrying git blame and json-c hits carrying none — matching the documented
    blame semantics.
  6. remove_knowledge_base → Removed; list_knowledge_bases → No knowledge bases configured.

Checklist

  • Exposes add_knowledge_base / list_knowledge_bases / remove_knowledge_base to all MCP clients
  • Preserves the PORTABLE_TOOL_NAMES Pi contract
  • Maps "Error:"-prefixed returns to MCP isError: true
  • Adds MCP integration unit tests
  • Updates docs and README
  • Verified end-to-end on the published package, including KB-content retrieval after rebuild

Register add_knowledge_base, list_knowledge_bases, and remove_knowledge_base
as MCP tools so codex, claude, jcode, and other MCP hosts can manage knowledge
bases. The operations already exist for the OpenCode plugin and the Pi
extension; this wires them into the MCP server.

- tool-names: define MCP_TOOL_NAMES as its own array extending
  PORTABLE_TOOL_NAMES with the three KB tools. KB tools stay out of
  PORTABLE_TOOL_NAMES because that set is a Pi contract
  (tests/pi-package.test.ts asserts Pi exposes every portable name; Pi
  uses the knowledge_base_* aliases).
- register-tools: append three server.tool registrations that call
  addKnowledgeBase/listKnowledgeBases/removeKnowledgeBase with the MCP
  runtime project root and host. Returns prefixed with "Error:" are
  surfaced with isError so clients can distinguish refusals (missing dir,
  blocked sensitive dir) from success; informational results such as
  "already configured" and "not found" stay successful.
- docs: reclassify the KB tools from OpenCode-only to shared by MCP and
  OpenCode (Pi uses aliases); correct the portable-tool count to 15 and
  the host totals; document the worktree limitation, multi-host config
  divergence, and KB indexing/config semantics (project-local write,
  union list, repo-only blame).
- tests: update the MCP tool-count test to 18; add
  tests/mcp-knowledge-bases.test.ts covering add/list/remove through the
  MCP client, host-specific config persistence (claude, codex), indexer
  refresh, the missing-path isError failure path, the not-found success
  case, and missing-required-arg schema rejection.
@Helweg Helweg added the feature New feature or capability label Aug 16, 2026
@Helweg
Helweg merged commit 6b83404 into Helweg:main Aug 16, 2026
7 of 8 checks passed
@dkhokhlov
dkhokhlov deleted the feat/mcp-knowledge-base-tools branch August 18, 2026 04:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or capability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants