Summary In AG2 a tool's human-approval state and its execution dispatch are both keyed by ToolCallEvent.name alone
Description
In AG2 a tool's human-approval state and its execution dispatch are both keyed by ToolCallEvent.name alone. Nothing in the approval bypass nor in the stream subscription distinguishes which implementation of a name the human saw. Two different tool objects that share a name — the exact situation when a local read_file/deploy collides with an MCP server-proxied tool of the same name — are one identity as far as the guard and the stream are concerned:
- Approval generalizes across implementations by name. The human is shown a prompt containing only the tool name, answers "always" once, and every same-name implementation executes from then on with zero questions.
- One tool call is broadcast to all same-name subscribers. Every implementation registers its own executor on
stream.where(ToolCallEvent.name == name);Stream.sendwalks all matching subscribers, so oneToolCallEventexecutes both implementations (double side effects). If only one of the pair carriesapproval_required, the other executes with no approval decision at all — and it still executes when the human explicitly answers "n" (deny): the denial stops only the guarded implementation.
Registration does not stop the collision either: Toolkit rejects duplicate names, but the agent-level aggregation of self.tools + self._additional_tools + ask(tools=...) has no cross-source name check, so Agent(tools=[deploy, deploy]) is accepted silently (FunctionTool.ensure_tool deep-copies, so even passing the same tool object twice yields two same-name executors).
Affected versions
- Audited: ag2 1.0.5 (PyPI).
- Retested on the current latest ag2 1.0.6: reproduces identically (all assertions pass, exit code 0).
ag2/middleware/builtin/tools/approval.py,ag2/tools/final/function_tool.pyandag2/stream.pyare byte-identical between 1.0.5 and 1.0.6; inag2/agent.pythe aggregation statement only moved from line 1344 to 1346 (the 1.0.6 diff is an unrelated subtask-semaphore/threading change).
Code path
ag2/middleware/builtin/tools/approval.py:14(BYPASS_KEY = "ag:approval_required:always"),:56-59(bypass checkbypass_dict.get(event.name)) and:68-72(bypass_dict[event.name] = True) — the "always" grant is recorded against the name only, and the approval prompt itself contains only{tool_name}/{tool_arguments}(:10-13), so the human cannot see which implementation they are authorizing.ag2/tools/final/function_tool.py:131—stack.enter_context(context.stream.where(ToolCallEvent.name == self.schema.function.name).sub_scope(execute))— dispatch subscribes by name.ag2/stream.py:235-246—Stream.senditeratestuple(self._subscribers.values())and awaits every matching subscriber: one call event, N executions.ag2/agent.py:1344(1.0.6::1346) —all_tools = tuple(chain(self.tools, self._additional_tools, additional_tools))with no duplicate-name check across sources; the only check is inside a singleToolkit(ag2/tools/final/toolkit.py:45-46,ToolConflictError).- Downstream effect:
_execute_call(ag2/tools/executor.py:137-150) awaits only the firstToolResultEventfor the call id, so the model sees one innocent tool result while both implementations already ran.
PoC (fully offline, no source modification, side effects = local marker files in /tmp only)
/tmp/ag2-f3-poc/poc_ag2_f3.py, driven by ag2.testing.TestConfig scripted model turns (the library's own test double) + wrap_hitl-style HITL hook. Two same-name deploy tools with different implementations writing different marker files (marker_deploy_local.jsonl / marker_deploy_mcp.jsonl), plus an impl tag visible only inside the tool body (invisible to the schema, the model, and the approval prompt).
- Block 0 —
Toolkit(deploy, deploy)raisesToolConflictError;Agent(tools=[deploy, deploy])accepts both,agent.tools == ['deploy', 'deploy']. - Block 1 (scenario a) — both same-name tools guarded with
approval_required(); two scripteddeploycalls. The human is asked exactly once (deploy,{"env": "prod"}→ "always") and all 4 executions happen (local+mcp × prod+staging) with zero further questions. Theimpl=mcpexecutions are silent:bypass_at_exec={'deploy': True}at the moment they run. - Block 2 (control) — second tool renamed to
deploy_mcp:deploy_mcpdoes not run on thedeploycall and gets its own ask (2 asks for 2 names). Renaming restores the guard, isolating the root cause to name-keyed identity. - Block 3 (scenario b) — same-name pair, unguarded, one scripted
deploycall → both marker files written (double side effects), 0 asks. - Block 4 — same-name pair, only the local implementation guarded: 1 ask total across 2 calls × 2 implementations; the unguarded twin executes on every call.
- Block 5 (sharpened) — human answers "n": the guarded implementation is denied and does not run, while the same-name unguarded implementation runs anyway.
Result: VULNERABILITY CONFIRMED on 1.0.5 and on 1.0.6 (17/17 assertions, exit code 0 on both). Full transcripts: /tmp/ag2-f3-poc/run_output_1.0.5.txt, /tmp/ag2-f3-poc/run_output_1.0.6.txt; markers under /tmp/ag2-f3-poc/run-1.0.{5,6}/.
Impact
- Approval bypass across implementations: a human reviewing a prompt that says only `
deploy` believes they are approving one known implementation; their "always" silently authorizes any other implementation registered under the same name (local vs MCP-proxied vs plugin-supplied). Confidence in the human gate is misplaced — the gate's key is a string, not the code that runs. - Denial is not a stop button: with a same-name unguarded twin present, answering "n" stops only the guarded copy; the other still executes its side effects.
- Doubled side effects from one decision: one tool call fans out to every same-name implementation (double writes, double deploys, double spends) while the model sees a single normal tool result, so nothing in the transcript hints at the duplication.
- No friction at the point where it could be caught: the toolkit-level
ToolConflictErrorshows the intended invariant, but agent-level aggregation skips it, and because the second schema is a duplicate the LLM itself cannot disambiguate either.
Suggested fix
- Key approval state and execution dispatch by tool instance identity (e.g. a stable per-registration tool id) instead of
ToolCallEvent.name: the bypass record should bebypass[tool_id] = True, and the stream subscription condition should carry the tool id (e.g. a privateToolCallEvent.tool_idfield set at registration) rather thanToolCallEvent.name. - Enforce name uniqueness at the agent boundary: in the
all_toolsaggregation (ag2/agent.py:1344), raiseToolConflictErroron cross-source duplicates — or auto-disambiguate with a forced source prefix (mcp__server__read_file) so name and identity can no longer diverge silently. - Defensively,
Stream.sendcould assert a single subscriber per tool-call identity, and the schema advertised to the model should drop or flag duplicates instead of advertising the same function name twice.
---
Reported by Chengzhi Yi (GitHub @Tardfyou) — yimou@hust.edu.cn. Offline PoC against the public PyPI packages (ag2 1.0.5 and 1.0.6) using the project's own ag2.testing double; no platform, service, or live system was contacted and no package source was modified.