nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] MCP tools bypass development-mode policy: execute in plan mode, auto-denied in headless

Open addyCooks opened this issue 3 months ago • 1 comments

Description

MCP tools do not participate in the development-mode policy that governs every other tool. mcp-client.ts hand-rolls its own approval predicate instead of routing through the central mode system, and MODE_EXCLUDED_TOOLS only contains built-in tool names so MCP tools slip past mode filtering entirely.

This fails in both directions from a single root cause:

Mode Expected Actual
plan Blocked — plan mode is documented as "show tool calls but don't execute" An alwaysAllow-ed MCP tool executes immediately, with no approval prompt
headless Runs unattended, like execute_bash and the file tools do Auto-denied with "Tool execution was denied by the user." — no user is present

Root cause

source/mcp/mcp-client.ts:444-446 builds its own policy:

const isAutoApproved = this.isToolAutoApproved(toolName, serverName);
const approval: ToolApprovalPolicy = isAutoApproved
    ? false
    : (_args, mode) => mode !== 'auto-accept';
  • isAutoApproved → approval: false → resolveToolApproval rule #3 (boolean short-circuit) returns "no approval" in every mode, including plan.
  • Otherwise → 'headless' !== 'auto-accept' → true → approval required in headless, where no approval handler exists (tool-approval-queue.ts:23 defaults to () => false), so the call is silently denied.

Two supporting gaps let this reach the user:

  1. source/tools/tool-manager.ts:42-55 — MODE_EXCLUDED_TOOLS.plan lists only built-ins (write_file, execute_bash, git_commit, …). MCP tool names are never in it.
  2. source/tools/tool-manager.ts:194-203 — the plan/headless filter only consults this.customTools; if (!meta) return true waves MCP tools straight through.

Compare source/tools/execute-bash.tsx:179 and source/utils/tool-approval.ts:18 both explicitly special-case headless. MCP was never updated to match.

Environment

  • OS: Windows 11 (platform-independent, this is pure policy logic)
  • Node version: v22.22.3
  • Nanocoder version: 1.30.0 (current main, tag v1.30.0)
  • Provider: Any, reproduces regardless of provider
  • Model: Any, reproduces regardless of model

Steps to Reproduce

A. Plan mode executes a mutation (no MCP server needed for the unit repro)

  1. Configure any MCP server in .mcp.json with a mutating tool in its alwaysAllow list the format documented in source/commands/mcp.tsx:66:
    { "mcpServers": { "github": { "command": "...", "alwaysAllow": ["create_issue"] } } }
    
  2. Start nanocoder and switch to plan mode (Shift+Tab).
  3. Ask the model to do something that calls create_issue.
  4. The issue is created. No approval prompt appears.

B. Headless denies an ordinary MCP tool

  1. Configure any MCP server without the tool in alwaysAllow.
  2. Add a skill with a subscribe: block on file.changed whose subagent uses that MCP tool.
  3. Start the daemon: nanocoder daemon start.
  4. Touch a watched file.
  5. The run returns Tool execution was denied by the user. no user was ever prompted.

Unit-level repro (drop into source/tools/, no MCP server or daemon required):

import test from 'ava';
import {resolveToolApproval} from './approval-policy.js';

// Mirrors exactly what mcp-client.ts:444-446 constructs.
const autoApproved = {approval: false as const};
const ordinary = {approval: (_a: unknown, mode: string) => mode !== 'auto-accept'};

test('plan mode must not execute an alwaysAllow-ed MCP tool', async t => {
    t.true(await resolveToolApproval('create_issue', autoApproved, {}, {mode: 'plan'}));
});

test('headless must not require approval for an ordinary MCP tool', async t => {
    t.false(await resolveToolApproval('create_issue', ordinary as any, {}, {mode: 'headless'}));
});

Both assertions fail on current main.

Expected Behavior

  • Plan mode: no MCP tool that mutates state is reachable, and no alwaysAllow entry can override that. Plan mode is the safety mode users switch into precisely to inspect an untrusted model's intentions without side effects.
  • Headless mode: MCP tools behave like execute_bash and the file tools they run unattended, since headless exists specifically for daemon-triggered runs with no foreground prompt available.

Actual Behavior

  • Plan mode: alwaysAllow-ed MCP tools execute silently. The documented contract ("Show tool calls but don't execute") is violated with no warning to the user.
  • Headless mode: ordinary MCP tools are denied with a message claiming a user declined, when no user was present. Any triggered skill that depends on MCP silently fails, and the error message actively misdirects debugging.

Logs/Screenshots

Output from the unit repro above against v1.30.0:

✔ PLAN MODE: alwaysAllow-ed MCP tool executes with NO approval
✘ [fail]: HEADLESS: ordinary MCP tool is auto-DENIED
  Value is not `false`:
  true

Additional Context

Suggested fix make MCP entries first-class participants in the existing policy rather than special-casing them:

  1. Have getToolEntries() return readOnly alongside approval, derived from the MCP tool's own annotations (readOnlyHint where the server provides it), so resolveToolApproval rule #5 works for MCP the way it already does for built-ins.
  2. Replace the hand-rolled predicate with the shared helper, so headless is handled once: mode !== 'auto-accept' && mode !== 'headless' matching createFileToolApproval in source/utils/tool-approval.ts:18.
  3. Make plan-mode filtering in tool-manager.ts deny-by-default for any non-read-only tool instead of enumerating built-in names, so future tool sources cannot silently inherit this bug.
  4. Consider whether a server-level alwaysAllow should be able to override a mode at all currently it short-circuits ahead of every other rule.

Happy to open a PR with the fix plus the regression tests above.

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder

addyCooks avatar Aug 27 '26 12:08 addyCooks

Hey @akramcodez ! I’ve already started going through this issue and looking into the policy flow I’d be happy to take it up and work on the fix.

addyCooks avatar Aug 27 '26 12:08 addyCooks