The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): show redacted, size-capped arguments on untrusted MCP write approvals
fangliquanflq2b6f15b4a1e3
diff --git a/tests/tools/test_mcp_trust_gating.py b/tests/tools/test_mcp_trust_gating.pyindex e07f370ce02..ec82cf2d313 100644--- a/tests/tools/test_mcp_trust_gating.py+++ b/tests/tools/test_mcp_trust_gating.py@@ -106,6 +106,30 @@ class TestTrustGateAtCallTime:assert json.loads(raw) == {"result": "ok"}fake_session.call_tool.assert_awaited_once()+ def test_approval_card_shows_redacted_bounded_arguments(self, fake_session):+ """The approver sees the exact call payload without exposed secrets."""+ _set_trust("srv", "untrusted")+ handler = _mcp_handlers._make_tool_handler("srv", "trigger_build", 30.0)+ args = {+ "job": "release",+ "api_key": "sk-supersecretcredential123456",+ "notes": "x" * 5_000,+ }++ with patch(+ "tools.approval_prompt.request_elicitation_consent",+ return_value="decline",+ ) as consent:+ handler(args)++ message = consent.call_args.args[0]+ assert "```json" in message+ assert '"job": "release"' in message+ assert "sk-supersecretcredential123456" not in message+ assert "truncated" in message+ assert len(message) < 3_000+ fake_session.call_tool.assert_not_awaited()+def test_denied_approval_blocks_rpc(self, fake_session):"""'decline' blocks the call — the RPC must never fire."""_set_trust("srv", "untrusted")diff --git a/tools/mcp_tool_handlers.py b/tools/mcp_tool_handlers.pyindex ddeca179a8b..15f16da394f 100644--- a/tools/mcp_tool_handlers.py+++ b/tools/mcp_tool_handlers.py@@ -73,7 +73,30 @@ def _trust_granted(server_name: str, tool_name: str) -> bool:_trust_approval_key(server_name, tool_name))-def _trust_gate_check(server_name: str, tool_name: str, booked: Optional[list] = None) -> Optional[str]:+_TRUST_GATE_ARGUMENTS_MAX_CHARS = 2_048+_TRUST_GATE_ARGUMENTS_TRUNCATION_MARKER = "\n... [arguments truncated]"+++def _format_trust_gate_arguments(args: dict) -> str:+ """Return redacted, bounded JSON for the user-facing approval card."""+ from agent.redact import redact_sensitive_text++ rendered = json.dumps(args, ensure_ascii=False, indent=2, default=str)+ rendered = redact_sensitive_text(rendered)+ if len(rendered) > _TRUST_GATE_ARGUMENTS_MAX_CHARS:+ prefix_length = (+ _TRUST_GATE_ARGUMENTS_MAX_CHARS+ - len(_TRUST_GATE_ARGUMENTS_TRUNCATION_MARKER)+ )+ rendered = (+ rendered[:prefix_length].rstrip()+ + _TRUST_GATE_ARGUMENTS_TRUNCATION_MARKER+ )+ return rendered+++def _trust_gate_check(server_name: str, tool_name: str, booked: Optional[list] = None,+ args: Optional[dict] = None) -> Optional[str]:"""Approval gate for write-capable tools on ``trust: untrusted`` servers, plus the declared spendlimit on any server. None to proceed, else a ``tool_error``. Fail-closed: approval-system errorsblock. An admitted costed call is charged to the chat's tally and appended to *booked* so the@@ -90,17 +113,17 @@ def _trust_gate_check(server_name: str, tool_name: str, booked: Optional[list] =from tools.approval_context import get_current_session_keycharge = _costs.pending_charge(key, server_name, tool_name, get_current_session_key())if charge is None:- return _ask_for_call(server_name, tool_name, None, booked) if gated else None+ return _ask_for_call(server_name, tool_name, None, booked, args=args) if gated else Noneif not gated and charge.reserve_within_limit()[0]:if booked is not None:booked.append(charge)return Nonewith charge.asking(): # re-read under the slot: a call that asked first may have booked meanwhile- return _ask_for_call(server_name, tool_name, charge, booked, gated=gated)+ return _ask_for_call(server_name, tool_name, charge, booked, gated=gated, args=args)def _ask_for_call(server_name: str, tool_name: str, charge, booked: Optional[list], *,- gated: bool = True) -> Optional[str]:+ gated: bool = True, args: Optional[dict] = None) -> Optional[str]:"""Ask the user about one gated and/or costed call; book the charge only on an explicit yes."""spent, over = 0.0, Falseif charge is not None:@@ -120,6 +143,8 @@ def _ask_for_call(server_name: str, tool_name: str, charge, booked: Optional[listail = "This tool is write-capable (no readOnlyHint=true annotation) and may modify external state."# The limit notice leads: the messaging gateway's text fallback keeps only the first ~200 characters.message = " ".join([head, *(["This call goes past the chat's spend limit."] if over else []), *extra, tail])+ display_args = _format_trust_gate_arguments(args or {})+ message += f"\n\nArguments:\n```json\n{display_args}\n```"# Over the limit the answer covers this call only: a remembered trust grant must not# pre-approve the next over-limit call.remember_key = None if over else _trust_approval_key(server_name, tool_name)@@ -649,7 +674,7 @@ def _make_tool_handler(server_name: str, tool_name: str, tool_timeout: float, *,def _handler(args: dict, **kwargs) -> Any:# Security boundary: untrusted-server write tools need approval before ANY transport work (incl. lazy spawn).booked: list = [] # a costed call's charge; refunded when the call never reaches the server- error = _trust_gate_check(server_name, tool_name, booked)+ error = _trust_gate_check(server_name, tool_name, booked, args)if error is not None:return errorserver, error = None, _check_circuit_breaker(server_name)