examples: demonstrate pre-tool approval for MCP calls - #723
examples: demonstrate pre-tool approval for MCP calls#723jiezeng2004-design wants to merge 3 commits into
Conversation
📝 WalkthroughWalkthroughAdds an MCP filesystem tool approval example with fail-closed policy decisions, optional interactive approval, SHA-256 argument digests, JSONL audit logging, configuration, documentation, and tests covering allowed, approved, declined, blocked, redacted, and permission-restricted outcomes. ChangesMCP tool approval flow
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Agent
participant ApprovalHook
participant ApprovalGate
participant filesystem_policy
participant JsonlAuditSink
Agent->>ApprovalHook: pre_tool_call tool request
ApprovalHook->>ApprovalGate: authorize request
ApprovalGate->>filesystem_policy: evaluate normalized operation
filesystem_policy-->>ApprovalGate: policy decision
ApprovalGate->>JsonlAuditSink: append audit event
ApprovalGate-->>ApprovalHook: allow or reject
ApprovalHook-->>Agent: original request or False
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@examples/basic/mcp_tool_approval/mcp_agent.config.yaml`:
- Around line 14-15: Update the MCP filesystem server command in the
configuration by pinning `@modelcontextprotocol/server-filesystem` to a specific
tested version in the args value, rather than resolving the latest release
dynamically. Keep the existing npx invocation and approval configuration
unchanged.
In `@examples/basic/mcp_tool_approval/policy_gateway.py`:
- Around line 145-148: Update the audit log append logic in _append to create
newly opened log files with owner-only permissions (0600), and explicitly
validate or repair existing files that have looser permissions before writing.
Preserve the current append behavior and JSON serialization while ensuring no
group or other-user access remains.
In `@examples/basic/mcp_tool_approval/README.md`:
- Around line 21-25: Update the Run prerequisites in the README to state that
Node.js/npm must be installed and npx must be available on PATH before executing
the example. Keep the existing Python/uv setup and command flow unchanged.
In `@examples/basic/mcp_tool_approval/requirements.txt`:
- Line 1: Update the mcp-agent dependency specifier in requirements.txt to use a
supported relative filesystem path, such as ../.., instead of the file:// URI
containing .. in its authority.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 94541e4c-9ad1-4265-9956-413168eae9f7
📒 Files selected for processing (7)
examples/basic/mcp_tool_approval/README.mdexamples/basic/mcp_tool_approval/main.pyexamples/basic/mcp_tool_approval/mcp_agent.config.yamlexamples/basic/mcp_tool_approval/mcp_agent.secrets.yaml.exampleexamples/basic/mcp_tool_approval/policy_gateway.pyexamples/basic/mcp_tool_approval/requirements.txttests/examples/test_mcp_tool_approval.py
|
@coderabbitai review |
✅ Action performedReview finished.
|
Summary
AugmentedLLM.pre_tool_callhookWhy
Tool filtering controls which tools the model sees, but applications may also
need a decision immediately before dispatch. This example shows how a subclass
can delegate that decision to injected approval and audit components.
The example is intentionally limited to the configured filesystem MCP server.
It does not attempt to parse shell commands or provide a general security policy
engine, and it does not add a new mcp-agent public API.
Enforcement boundary
The hook covers calls dispatched through this augmented LLM instance. Direct
Agent.call_toolcalls, another LLM instance, or a client connected directlyto the MCP server bypass the example. Server-initiated
sampling/createMessageis a separate protocol path.The sensitive-path check is illustrative, not workspace confinement or a
sandbox. The filesystem server should be rooted at the intended workspace, and
OS or container isolation remains the containment boundary.
Validation
ruff check examples/basic/mcp_tool_approval tests/examples/test_mcp_tool_approval.pyruff format --check examples/basic/mcp_tool_approval tests/examples/test_mcp_tool_approval.pypytest tests/examples/test_mcp_tool_approval.py -q(8 passed)python -m compileallfor the example and focused testsgit diff --check@modelcontextprotocol/server-filesystem@2026.7.10The focused tests use fake approval and audit components and do not start an MCP
server or call an LLM.
Summary by CodeRabbit
New Features
Tests