rulesync icon indicating copy to clipboard operation
rulesync copied to clipboard

Follow-ups for OpenCode MCP tool filter key sanitization (PR #3693)

Open dyoshikawa opened this issue 1 month ago • 3 comments

Background

PR #3693 (fix(mcp): sanitize OpenCode tool filter keys) makes the OpenCode MCP adapter (OpenCode V1, V2 and MiMo Code share the code path) generate and import tool filter keys in the sanitized form that OpenCode itself uses, reusing the helpers shared with Kilo. It addresses the remaining MCP tools-map item in #2592. The review found no high or critical issues, so the PR was merged. The low-severity findings below are tracked here as follow-ups.

Details

All locations are in src/features/mcp/opencode-mcp.ts and src/features/mcp/opencode-mcp.test.ts.

  1. Key collisions can flip a deny into an allow. In convertToOpencodeFormat, server a.b with disabledTools: ["x"] and server a_b with enabledTools: ["x"] both produce a_b_x, and the later entry overwrites the earlier one. Within a single server (read.file vs read_file) deny wins, which is the safe direction.
  2. Per-call allocation. findOpencodeToolOwner creates builtinKeys: new Set<string>() on every call. It should be a module-level constant with a comment explaining why built-ins are intentionally empty (the OpenCode tools map only holds MCP toggles, unlike Kilo).
  3. Duplicated orphan filtering. convertFromOpencodeFormat pre-filters tools with findOpencodeToolOwner, while orphanMcpToolFiltersToRulesync in mcp-transport.ts has its own raw-prefix check. The latter is redundant for this caller but still needed by Kilo.
  4. Lossy normalisation on round trip. read.file becomes read_file after import, so it no longer matches the real tool name on other targets (e.g. Claude Code). This matches OpenCode behaviour but is not documented.
  5. Colliding sanitized server names. For servers a.b and a_b, findOpencodeMcpToolKeyOwner keeps the first server, so the second server's disabledTools are re-attributed on import. First-wins behaviour is neither tested nor documented.
  6. Test gaps.
    • Orphan server names containing . or -.
    • An explicit V2 (mcp.servers envelope) case with sanitized keys.
    • Regeneration over an existing file that still has legacy raw keys such as my.server_delete.file (confirm applySharedConfigPatch replaces tools wholesale).
    • Use toEqual instead of toMatchObject for the imported filter fields.

Solution / Next Steps

  • (1) Merge colliding keys with "false wins" semantics, or emit a warning when two entries collapse to one key.
  • (2) Hoist the empty set to a module-level ReadonlySet constant and add the explanatory comment.
  • (3) Add a comment on the two-layer filtering, or let the shared helper take an owner predicate so the logic lives in one place.
  • (4) Document the dot-to-underscore normalisation under docs/reference, or record it as known behaviour.
  • (5) Add a test or comment that documents first-wins ownership for colliding server names.
  • (6) Add the missing tests listed above and tighten the assertions.

dyoshikawa avatar Oct 30 '26 12:10 dyoshikawa

Status (2026-10-31, autonomous run): item 1 (colliding keys flipping a deny into an allow) is covered by the external PR #3699, which is waiting for a maintainer review. Items 2–6 touch the same two files (src/features/mcp/opencode-mcp.ts and its test), so this run leaves them until #3699 lands rather than racing a conflicting PR against it.

dyoshikawa avatar Oct 30 '26 16:10 dyoshikawa

Status (2026-10-31): item 1 landed with #3699 (an existing deny now survives a later allow when server names sanitize to the same key). Items 2–6 are still open and no longer blocked.

dyoshikawa avatar Oct 31 '26 01:10 dyoshikawa

I'd like to pick up items 2–6 in one PR on top of #3699. I'll open it shortly.

Oscar-Williams avatar Nov 01 '26 13:11 Oscar-Williams