Skip to content

mcp: add per-server tool allow and deny lists - #2020

Merged
bbondy merged 6 commits into
brave:mainfrom
winshaurya:parity-mcp-tool-filter
Oct 13, 2026
Merged

bbondy merged 6 commits into
brave:mainfrom
winshaurya:parity-mcp-tool-filter

Conversation

@winshaurya

@winshaurya winshaurya commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

User impact: Users can now filter tools exposed by MCP servers using allow lists (--tools) and deny lists (--exclude-tools), preventing unwanted or noisy tools from entering the model's context while maintaining clear UI visibility over withheld tools.

Hi @bbondy and reviewers,

This PR implements per-server tool allow lists (tools) and deny lists (exclude_tools) for MCP servers, resolves #1399.

The problem

General-purpose MCP servers often provide dozens of tools, many of which may be irrelevant, noisy, or undesirable in a given project. Previously, all tools reported by an MCP server during its handshake were either accepted into the tool list offered to the model or caused the entire server's tools to be declined. There was no way to allow a subset of tools or exclude specific unwanted tools on a per-server basis.

Reproduce

  1. Connect an MCP server that advertises multiple tools (e.g., weather advertising get_alerts, get_forecast, get_radar).
  2. There were no declaration fields or CLI flags to allow only get_forecast or to exclude get_radar.
  3. An approval of the server's tool list offered every tool to the planner, consuming prompt context on every turn even for tools that should never be invoked.

The fix

We implement tool filtering across five modular, signed commits:

  1. Declarations and Digest Compatibility (crates/config/src/mcp.rs):

    • Added tools: Vec<String> and exclude_tools: Vec<String> to Declaration::Stdio and Declaration::Http.
    • Validated tool names per is_tool_filter_name(), accepting both bare identifiers (get_forecast) and alias-qualified identifiers (weather:get_forecast).
    • Included tool filter sets in Declaration::digest() only when non-empty, guaranteeing that existing declarations without filters retain their exact existing SHA-256 digests.
    • Tracked differences across declaration changes under Field::Tools and Field::ExcludeTools.
    • Handled Problem::Tools for invalid types and Problem::Tool for invalid identifiers.
  2. Tool Filtering, Protocol Handling, and UI Visibility (crates/agent, crates/mcp, crates/approval, crates/tui, crates/ui-bridge):

    • Filtered tool lists during the MCP handshake before offering tools to the model or writing approval vouches.
    • Enforced strict precedence: deny lists (exclude_tools) always take precedence over allow lists (tools). If a tool matches any entry in exclude_tools, it is withheld; if tools is non-empty and the tool is not in tools, it is withheld.
    • Counted withheld tools under withheld: usize, keeping them distinct from tools refused by format or protocol violations.
    • Enhanced the user interface across TUI (crates/tui/src/confirm.rs), plain CLI (crates/approval/src/lib.rs), and wire protocol (crates/ui-bridge/src/wire.rs, acp.rs) to report withheld tools in the confirmation card when non-zero (e.g., "(1 tool withheld by filter)").
    • Added localized messages (mcp-tools-withheld) in en-US.ftl and fr.ftl.
  3. CLI Options and Validation (crates/cli/src/mcp.rs, crates/agent/src/servers.rs):

    • Added --tools <list> and --exclude-tools <list> flags to bravebot mcp add for both stdio and HTTP servers.
    • Supported comma-separated (--tools foo,bar) and space-separated argument formats as well as flag-joined formats (--tools=foo,bar).
    • Validated that empty or whitespace-only argument values are rejected with informative errors (mcp-tools-needs-a-list, mcp-exclude-tools-needs-a-list).
    • Rendered declared tool filters in the interactive confirmation prompt (tools: ... and exclude tools: ...) and in server inspect views.
  4. Specifications and Formal Verification (docs/specs):

    • Added normative clause SERVERS-17 to docs/specs/mcp-servers.md, specifying precedence, qualification matching, withheld counting, and digest requirements.
    • Incremented the SERVERS clause count in docs/specs/README.md from 16 to 17.
    • Updated the Labelled::new call site count in docs/specs/labels.md to account for the filtered listing constructor.
    • Passed mechanical spec validation (check-spec.py --mechanical-only) with 0 errors across 1037 clauses.
  5. Review Feedback Resolutions (crates/agent, crates/config, docs/specs):

    • Drove integration test through real servers::reach with declarations on disk rather than a synthetic helper.
    • Asserted withheld tools are completely omitted from the model request payload.
    • Asserted that attempting to call a withheld tool is refused without reaching the MCP server.
    • Added unit test in bravebot-config asserting Declarations::parse produces Problem::Tools and Problem::Tool for invalid declarations.
    • Added unit test in bravebot-agent asserting handshake_local filters tools by allow and deny lists.

Trying it by hand

  1. Build the CLI from the PR branch (BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 cargo build -p bravebot-cli) and use a scratch directory or project so real declarations are untouched.
  2. Run bravebot mcp add srv --tools fetch,search --exclude-tools delete -- /opt/srv and answer n at the question. Observe lines tools: fetch, search and exclude tools: delete above the digest line.
  3. Check the flag forms: --tools=fetch,search, --tools fetch search, and an --http https://mcp.example.com/mcp declaration with the same flags are each accepted.
  4. Run bravebot mcp add srv --tools -- /opt/srv, then with --tools= and with --tools ''. Each is refused with "needs a tool name or a comma-separated list". The same holds for --exclude-tools.
  5. Run bravebot mcp add srv --tools 'not a valid tool name' -- /opt/srv. Observe refusal saying the name is not a tool name.
  6. Connect an MCP server that advertises multiple tools (such as weather) with --exclude-tools get_forecast, start a session and approve it. The tool confirmation card omits that tool and reports "one tool was withheld by the server's allow or deny list".
  7. Declare the same server with --tools get_alerts --exclude-tools get_alerts. Observe that no tools are offered and all tools are counted as withheld because deny wins.
  8. Edit the filter on an already approved server in mcp.json. Observe that the server is asked about again on next session start, with tools or exclude_tools named as changed.

Test plan

  1. Mechanical spec verification: python3 agents/skills/check-spec/check-spec.py --mechanical-only (passes with 0 errors across 80 specs and 1037 clauses).
  2. Locale verification: python3 contrib/check-locales.py (passes with 0 new gaps).
  3. Code formatting: cargo fmt --all -- --check (passes cleanly).
  4. Clippy checks: BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 cargo clippy -p bravebot-cli -p bravebot-agent -p bravebot-config -p bravebot-mcp -p bravebot-approval -p bravebot-tui -p bravebot-ui-bridge --all-targets --all-features -- -D warnings (passes with 0 warnings).
  5. Unit and integration tests:
    • BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 cargo test -p bravebot-config (554 tests pass).
    • BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 cargo test -p bravebot-mcp (20 tests pass).
    • BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 cargo test -p bravebot-approval (8 tests pass).
    • BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 cargo test -p bravebot-agent (tests pass, including handshake_local_filters_tools_by_allow_and_deny_lists and withheld_mcp_tools_are_not_offered_and_reported_in_confirmation).
    • BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 cargo test -p bravebot-cli --bin bravebot mcp (45 tests pass).

Thank you very much!

@netzenbot-reviewer netzenbot-reviewer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation: request changes

What this pull request does

This adds per-server tool allow (tools) and deny (exclude_tools) lists to MCP server declarations, for both stdio and HTTP servers. Names may be bare (get_forecast) or alias-qualified (weather:get_forecast), are validated, deduplicated, accepted in the declarations JSON file, and set with new --tools and --exclude-tools flags on bravebot mcp add (comma-separated, space-separated or =-joined). After the handshake, Listing::filtered drops any tool matching a deny entry, and any tool not matching an allow entry when the allow list is non-empty, so deny wins; removed tools are counted separately from tools refused for format reasons. The filters are folded into the declaration digest only when non-empty, so existing digests are unchanged and editing a filter requires re-approval. The count is carried through the TUI card, the plain prompt, the ui-bridge wire/ACP payloads and both locale files as a 'tool(s) withheld' line, and the add prompt and inspect output show tools: and exclude tools: lines. A new spec clause, SERVERS-17, documents this.

Trying it by hand

The description does not say how to try this change. These steps would:

  1. Build the CLI from the PR branch (e.g. BRAVEBOT_ALLOW_UNCONFIGURED_BUILD=1 cargo build -p bravebot-cli) and use a scratch HOME or project so no real declarations are touched.
  2. Run bravebot mcp add srv --tools fetch,search --exclude-tools delete -- /opt/srv and answer n at the question. Expect lines tools: fetch, search and exclude tools: delete above the digest line.
  3. Check the flag forms: --tools=fetch,search, --tools fetch search and an --http https://mcp.example.com/mcp declaration with the same flags should each be accepted.
  4. Run bravebot mcp add srv --tools -- /opt/srv, then with --tools= and with --tools ''. Each should be refused with 'needs a tool name or a comma-separated list'. Repeat with --exclude-tools.
  5. Run bravebot mcp add srv --tools 'not a valid tool name' -- /opt/srv. Expect a refusal saying the name is not a tool name.
  6. Point an MCP server that advertises several tools (e.g. the weather server from docs/specs/mcp-servers.md) at a declaration with --exclude-tools get_forecast, start a session and approve it. The tool-list card should omit that tool and show 'one tool was withheld by the server's allow or deny list'.
  7. Declare the same server with --tools get_alerts --exclude-tools get_alerts. Expect no tools offered and every tool counted as withheld, because deny wins.
  8. Edit the filter on an already approved server (for example in the declarations JSON file). Expect the server to be asked about again, with tools or exclude_tools named as changed. Check this step against the real UI, since I did not confirm the exact wording.
How this review reached its recommendation

The review read 20 changed files.

What it checked:

  • Written best practices. The changes were compared with 16 rules from this project's best-practice documents (compatibility, dependencies, paths, portability, shared-implementation, specs, tests, writing).
  • Bugs. The changes were read for mistakes that would make the code misbehave or stop it building.
  • This project's own criteria. The changes were checked against these questions:
    • Whether the change agrees with the project's specs, and whether a change to behaviour a spec describes also updates that spec and its tests.
    • Whether untrusted content (text from the web, files or tools) can influence a decision, or be given a better trust label than it came with, anywhere other than the places built for that.
    • Whether the tests would fail if the behaviour broke, rather than covering only the allowed case or passing with the bug put back.
    • Whether the change does what its title says and nothing more, with no unrelated refactor and no new dependency, permission or network destination the title does not explain.

The checks flagged 8 possible problems. 4 of them went to a second reader, who checked each against the full source code rather than only the diff, and kept 2.

Comment thread crates/agent/tests/mcp.rs Resolved
Comment thread crates/config/src/mcp.rs Resolved
@winshaurya

winshaurya commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

new changes

  1. Integration Test via Real servers::reach with Declarations on Disk (crates/agent/tests/mcp.rs):

    • Replaced the synthetic helper with the real servers::reach handshake pipeline reading declarations and approvals from disk.
    • Asserted that withheld tools are omitted from the model request payload (rounds(&chat)[0]["tools"]).
    • Asserted that an attempted call to a withheld tool is refused as an unknown tool without reaching the MCP server.
  2. Declaration Parser Error Handling Unit Tests (crates/config/src/mcp.rs):

    • Added tools_and_exclude_tools_require_lists_of_valid_tool_names covering Problem::Tools for non-array and non-string inputs and Problem::Tool for invalid identifiers under Declarations::parse.
  3. Stdio Server Handshake Filtering Unit Test (crates/agent/src/servers.rs):

    • Added unit test handshake_local_filters_tools_by_allow_and_deny_lists using a mock stdio server under confinement, validating that both allow and deny lists are applied and withheld tools are counted.
  4. Linked Tests in Spec (docs/specs/mcp-servers.md):

    • Linked the new unit tests to clause SERVERS-17. python3 agents/skills/check-spec/check-spec.py --mechanical-only passes with 0 errors.
  5. Trying It by Hand:

    • Added the reproduction and verification steps to the PR description.
  6. All Commits Signed:

    • All commits on the branch are signed with my verified SSH key.

Thank you! @bbondy

A declaration may declare an allow list (tools) and a deny list
(exclude_tools) of tool names. When exclude_tools names a tool, it is
denied; when tools is non-empty and does not name a tool, it is denied.

Tool names must be valid identifiers, optionally prefixed with an
alias. Both stdio and http declarations support filtering. When tool
filters are specified, they are included in the declaration digest so
changes to filtering require approval. Declarations with no tool
filters produce the exact same digest as before.

Signed-off-by: winshaurya <[email protected]>
Servers list their tools during handshake. When a server declaration
specifies an allow list (tools) or deny list (exclude_tools), the tools
are filtered before they are stored or offered to the model.

Deny lists take precedence over allow lists. Tools withheld by filters
are counted under withheld, kept distinct from tools refused by
protocol or validation rules. The number of withheld tools is presented
to the user in confirmation prompts across TUI, plain text, and wire
interfaces.

Signed-off-by: winshaurya <[email protected]>
- Drive integration test through real servers::reach with declarations on disk
- Assert withheld tools are omitted from model request payload
- Assert that attempting to call a withheld tool is refused
- Add unit test verifying Problem::Tools and Problem::Tool on Declarations::parse
- Add unit test verifying handshake_local filters tools by allow and deny lists
- Link new tests to SERVERS-17 in docs/specs/mcp-servers.md

Signed-off-by: winshaurya <[email protected]>
@winshaurya
winshaurya force-pushed the parity-mcp-tool-filter branch from 4fe825b to 40d0829 Compare October 11, 2026 21:50

@netzenbot-reviewer netzenbot-reviewer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation: approve

1 earlier comment was addressed and resolved.

What this pull request does

This adds optional tools (allow) and exclude_tools (deny) lists to stdio and HTTP MCP server declarations, with matching --tools and --exclude-tools flags on bravebot mcp add. After the handshake, Listing::filtered drops any tool named in the deny list, and any tool not in a non-empty allow list, so deny wins. Names may be bare (get_forecast) or alias-qualified (weather:get_forecast), and are validated when parsed from mcp.json or from the flags. The two lists enter the declaration digest only when non-empty, so existing digests are unchanged, and editing a filter on an approved server triggers a fresh approval that names tools or exclude_tools as changed. Withheld tools are counted separately from refused ones, and the count is shown in the tool-list confirmation in the TUI, the plain prompt and the wire/ACP payload, with en-US and fr strings and a new SERVERS-17 spec clause. The description and diff agree. One detail the description does not mention: the declaration prompt and inspect view print tools: and exclude tools: lines.

How this review reached its recommendation

The review read 5 of the 20 changed files: only those that changed since the bot last reviewed this pull request.

What it checked:

  • Written best practices. The changes were compared with 16 rules from this project's best-practice documents (compatibility, dependencies, paths, portability, shared-implementation, specs, tests, writing).
  • Bugs. The changes were read for mistakes that would make the code misbehave or stop it building.
  • This project's own criteria. The changes were checked against these questions:
    • Whether the change agrees with the project's specs, and whether a change to behaviour a spec describes also updates that spec and its tests.
    • Whether untrusted content (text from the web, files or tools) can influence a decision, or be given a better trust label than it came with, anywhere other than the places built for that.
    • Whether the tests would fail if the behaviour broke, rather than covering only the allowed case or passing with the bug put back.
    • Whether the change does what its title says and nothing more, with no unrelated refactor and no new dependency, permission or network destination the title does not explain.

The checks flagged 2 possible problems. 2 of them went to a second reader, who checked each against the full source code rather than only the diff, and found none of them to be a real problem introduced by this change.

@netzenbot-reviewer netzenbot-reviewer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation: approve

What this pull request does

This adds per-server tool filters to MCP declarations: an allow list (tools) and a deny list (exclude_tools), each entry either a bare tool name or alias:tool. bravebot mcp add gains --tools and --exclude-tools (comma-separated, space-separated or = forms, for stdio and HTTP servers), and the same keys are read from and written to the declarations file with validation. After the handshake, Listing::filtered drops any tool that matches a deny entry, or that misses a non-empty allow list, so deny wins. It counts those tools as withheld, separately from tools refused for format reasons, so the model is never offered them and calls to them are refused. The filters are folded into the declaration digest only when non-empty, so existing approvals keep their digests, and editing a filter triggers a new approval that names the changed field. The approval card (TUI, plain CLI, wire and ACP) shows a 'tools withheld' line, the mcp add question prints tools: and exclude tools: lines, and a new spec clause SERVERS-17 and en-US and fr strings are added. The diff matches the description.

How this review reached its recommendation

The review read 0 of the 20 changed files: only those that changed since the bot last reviewed this pull request.

None of these checks flagged anything, so there was nothing to double-check.

@bbondy
bbondy merged commit 811c7b5 into brave:main Oct 13, 2026
25 checks passed
@winshaurya
winshaurya deleted the parity-mcp-tool-filter branch November 21, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a per-server tool allow list and deny list so unwanted MCP tools are never offered to the model

3 participants