Skip to content

feat: add MCP_REDIS_ALLOWED_TOOLS to expose only the named tools - #161

Merged
vchomakov merged 2 commits into
redis:mainfrom
cyb3ralbert:allowed-tools
Nov 23, 2026
Merged

vchomakov merged 2 commits into
redis:mainfrom
cyb3ralbert:allowed-tools

Conversation

@cyb3ralbert

@cyb3ralbert cyb3ralbert commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Every tool is registered unconditionally. load_tools() imports each module under src/tools/, the @mcp.tool() decorators run, and all 53 tools land in tools/list — including delete, expire, rename, json_del, hdel, xdel, srem, zrem, lrem, xgroup_destroy and the rest of the write surface. There is no way to hand an agent a subset.

The control the README currently points at is Redis ACL:

ACL SETUSER readonlyuser on >mypassword ~* +@read -@write

That restricts what may be executed, not what is offered. Two consequences:

  1. The tool stays in the inventory. The agent sees delete, plans a step around it, calls it, and gets Error deleting key foo: NOPERM ... back as a tool result. The refusal arrives after the plan was made, not before.
  2. ACL granularity is over Redis commands, not MCP tools, and it is unavailable to anyone who does not administer the Redis server — managed and shared instances included.

Change

MCP_REDIS_ALLOWED_TOOLS takes a comma-separated list of tool names and exposes only those:

MCP_REDIS_ALLOWED_TOOLS="get,set,hget,hgetall,dbsize" redis-mcp-server --url redis://localhost:6379/0
tools/list = 53  →  tools/list = 5

Three properties, in the order they matter:

Unregistered, not refused. Tools outside the list are removed via the public FastMCP.remove_tool, so they are absent from tools/list rather than failing on call. A tool an agent cannot see is a tool it cannot plan around.

Allowlist, not denylist. A tool added in a future release stays unavailable until it is named. A denylist would ship new write tools to existing users on upgrade.

Unknown names abort startup. A misspelled name would otherwise be dropped silently, leaving fewer tools than configured — indistinguishable, from the outside, from the agent simply not using the tool. The error names the offending entries and lists what is available:

ValueError: MCP_REDIS_ALLOWED_TOOLS names unknown tool: gett.
Available tools: client_list, create_vector_index_hash, dbsize, delete, ...

Leaving the variable unset changes nothing — all 53 tools stay available, so this is inert for existing users.

Tests

tests/test_tool_allowlist.py, 16 cases across three groups: parsing (unset vs. empty vs. whitespace vs. stray commas), resolution against the registered set (subset, full set, empty, one typo, several typos sorted, error contents), and application to a real FastMCP instance asserted through list_tools() — including that an unknown name raises and leaves the server untouched.

Verified locally against the checks in .github/workflows/ci.yml: full suite 361 passed, coverage 88.46% (gate 80%), ruff format --check clean, bandit -r src/ clean, and the startup smoke test passes. End-to-end runs confirm 53 → 3 tool inventories and the abort-on-typo path through src/main.py.

Notes

Two things I would rather you decide than assume:

apply_tool_allowlist reads server._tool_manager.list_tools() to enumerate what is registered. FastMCP exposes no synchronous accessor — list_tools() is a coroutine and this runs at import time. Happy to switch to asyncio.run or to track names at registration if you prefer to avoid the private attribute.

The failure on an unknown name is a ValueError raised at import, so it surfaces as a traceback rather than a click error, because src/common/server.py is imported before cli() runs. If you would rather it print through click.echo(..., err=True) and sys.exit(1) like the URI parsing error in main.py, that needs the import restructured and I did not want to do that unasked.

A read-only baseline keyword (read-only plus named write tools, rather than naming every read tool) is the natural follow-up and is how I implemented the equivalent in chigwell/telegram-mcp#168. It needs each of the 53 tools classified, which is a judgement call per tool, so I left it out of this PR rather than bundle it. Glad to add it if you want it.


Note

Medium Risk
Misconfigured allowlists fail at startup; correct configs change which tools agents can discover and invoke, which affects operational safety but is opt-in.

Overview
Adds MCP_REDIS_ALLOWED_TOOLS, a comma-separated allowlist so only named MCP tools are exposed. After load_tools(), the server unregisters everything else via FastMCP.remove_tool, so omitted tools never appear in tools/list (unlike Redis ACL, which only blocks execution).

Strict validation: empty/blank allowlists and unknown tool names raise ValueError at import/startup with available tools listed; unset env leaves all tools unchanged.

README documents the feature, contrasts it with Redis ACL, and adds the env var to the configuration table. New tests/test_tool_allowlist.py covers parsing, resolution, and apply behavior.

Reviewed by Cursor Bugbot for commit 7d63c75. Bugbot is set up for automated code reviews on this repo. Configure here.

@cyb3ralbert

Copy link
Copy Markdown
Contributor Author

CI on this PR has been sitting at action_required since 28 July, so I ran the full matrix on my fork instead, to save a maintainer the round trip. The branch is this PR's commit rebased onto current main (3d7ca68), with no other changes:

Run 30610245762 — 18 of 19 jobs green.

  • test-ubuntu — Python 3.10, 3.11, 3.12, 3.13, 3.14 ✅
  • test-other-os (macos-latest) — 3.10, 3.11, 3.12, 3.13, 3.14 ✅
  • test-other-os (windows-latest) — 3.10, 3.11, 3.12, 3.13, 3.14 ✅
  • lint-and-format, security-scan, docker-test ✅
  • build-test ❌

The one red job is not caused by this PR. build-test fails on ⚙️ Test package installation because mcp 2.0.0 removed mcp.server.fastmcp, which breaks any install resolved without uv.lock — the nightly builds on main have been red for the same reason since 29 July. I filed that separately as #163, with a one-line fix in #164; with that pin in place this job passes too.

Happy to rebase or split this PR if that helps review.

@codecov-commenter

codecov-commenter commented Aug 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.47%. Comparing base (faeb09f) to head (7d63c75).
⚠️ Report is 15 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #161      +/-   ##
==========================================
+ Coverage   88.20%   88.47%   +0.27%     
==========================================
  Files          19       19              
  Lines        1060     1085      +25     
==========================================
+ Hits          935      960      +25     
  Misses        125      125              
Flag Coverage Δ
unittests 88.47% <100.00%> (+0.27%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vchomakov

Copy link
Copy Markdown
Member

@cyb3ralbert Thanks for digging into this!
The mcp pin (#164) is merged now, so if you rebase onto main I'll kick off CI right after.

@cyb3ralbert

Copy link
Copy Markdown
Contributor Author

@vchomakov Rebased onto main — thanks for merging the pin.

Verified on the rebased branch that the previously failing build-test step should now pass: a clean pip install . resolves mcp 1.29.0 and redis-mcp-server --help runs fine.

The PR itself is unchanged — same three files, no conflicts with the eight commits that landed in between.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit b8b3049. Configure here.

Comment thread src/common/server.py Resolved
Jenya Vlasov and others added 2 commits September 27, 2026 18:11
The server registers all 53 tools unconditionally, so an agent is always
offered `delete`, `expire`, `rename` and the rest of the write surface.
The only control the README can point at is Redis ACL, which restricts
what may be executed rather than what is offered: a denied command still
comes back as a tool error once the agent has decided to call it, and
ACL granularity is over Redis commands, not MCP tools.

Add an allowlist of tool names. Tools outside it are unregistered, so
they are absent from tools/list rather than refused on call — a visible
tool can be planned around even when every call to it would fail.

Being an allowlist rather than a denylist, a tool added in a future
release stays unavailable until it is named. An unknown name aborts
startup instead of being dropped, which would otherwise expose fewer
tools than configured while looking like a working configuration.

Leaving the variable unset keeps every tool available.

Co-Authored-By: Claude Opus 5 <[email protected]>
@cyb3ralbert

cyb3ralbert commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@vchomakov Fixed in the latest commit: an allowlist that is set but names no tools (empty, blank, or only commas) now aborts startup with a clear error, the same way an unknown tool name does. Rebased onto current main as well; 363 tests pass locally.

@vchomakov vchomakov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fix and for your patience on this one. LGTM.

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.

3 participants