Skip to content

The agent that grows with you

fix(mcp): show the instructions and destination when approving a subscription

emozilla86573f380e47
diff --git a/tests/tools/test_mcp_events.py b/tests/tools/test_mcp_events.py
index 99fe0bdd2e7..eefa15e2fb2 100644
--- a/tests/tools/test_mcp_events.py
+++ b/tests/tools/test_mcp_events.py
@@ -134,6 +134,24 @@ class TestSubscribe:
assert "public_url" in _subscribe(session)["error"]
assert session.sent == [] and _routes(home) == {}
+ def test_the_approval_shows_the_instructions_and_the_destination(self, monkeypatch):
+ shown = {}
+
+ def consent(message, description, **kwargs):
+ shown.update(message=message, description=description)
+ return "accept"
+ monkeypatch.setattr("tools.approval_prompt.request_elicitation_consent", consent)
+ args = {"name": "feed.emission.created", "arguments": ARGS, "instructions": "Post a two-line summary.",
+ "deliver": "telegram", "deliver_chat_id": "-100123"}
+ assert events._consent_error("ambush", args) is None
+ text = f"{shown['message']}\n{shown['description']}"
+ assert "Post a two-line summary." in text and "telegram" in text and "-100123" in text
+
+ def test_overlong_instructions_are_refused_before_asking(self):
+ assert events._subscribe_args_error({"name": "x", "instructions": "a" * events._MAX_INSTRUCTIONS_CHARS}) is None
+ assert events._subscribe_args_error({"name": "x", "instructions": "a" * (events._MAX_INSTRUCTIONS_CHARS + 1)})
+ assert events._subscribe_args_error({"name": "x", "deliver_chat_id": 123})
+
def test_a_disabled_subscription_is_not_turned_back_on(self, home):
_subscribe(FakeSession(_grant()))
data = _routes(home)
diff --git a/tools/mcp_tool_events.py b/tools/mcp_tool_events.py
index 5fbaaa39f9b..120e33df462 100644
--- a/tools/mcp_tool_events.py
+++ b/tools/mcp_tool_events.py
@@ -40,6 +40,8 @@ _FORBIDDEN, _NOT_FOUND, _UNSUPPORTED, _INVALID_PARAMS = -32024, -32023, -32026,
# A refresh refused with one of these can never succeed as stored: access was revoked, the event type is
# gone, its arguments no longer validate, or the server no longer offers what the subscription asks for.
_TERMINAL_REFRESH_CODES = frozenset({_FORBIDDEN, _NOT_FOUND, _UNSUPPORTED, _INVALID_PARAMS})
+# The approval prompt shows the instructions in full, so they are bounded.
+_MAX_INSTRUCTIONS_CHARS = 2000
# Refresh loops by server name, each as (its event loop, its wake-up event). A new subscription wakes them
# so its first refresh is scheduled from its own grant, not after the loop's current sleep (up to an hour).
_refresh_wakers: dict[str, set[tuple[asyncio.AbstractEventLoop, asyncio.Event]]] = {}
@@ -396,14 +398,31 @@ async def _unsubscribe_event(session, args: dict, server_name: str) -> dict:
return {"unsubscribed": {"name": args["name"], "arguments": arguments}}
+def _subscribe_args_error(args: dict) -> Optional[str]:
+ """Validation for ``subscribe_event`` that runs before the user is asked to approve it."""
+ instructions = args.get("instructions")
+ if instructions is not None and not isinstance(instructions, str):
+ return "'instructions' must be a string"
+ if isinstance(instructions, str) and len(instructions) > _MAX_INSTRUCTIONS_CHARS:
+ return f"'instructions' is limited to {_MAX_INSTRUCTIONS_CHARS} characters"
+ for key in ("deliver", "deliver_chat_id"):
+ if args.get(key) is not None and not isinstance(args[key], str):
+ return f"'{key}' must be a string"
+ return None
+
+
def _consent_error(server_name: str, args: dict) -> Optional[str]:
"""Subscribing installs a standing trigger that starts unattended agent runs: ask the user
- every time (fail closed), whatever the server's trust tier."""
+ every time (fail closed), whatever the server's trust tier. The prompt shows the whole standing
+ contract: the event, its arguments, the instructions each run follows, and where results go."""
+ deliver = args.get("deliver") or "log"
+ target = f"'{deliver}' chat {args['deliver_chat_id']}" if args.get("deliver_chat_id") else f"'{deliver}'"
try:
from tools.approval_prompt import request_elicitation_consent
answer = request_elicitation_consent(
- f"Subscribe to MCP event '{args['name']}' from server '{server_name}'? Each event starts an agent run "
- f"whose result goes to '{args.get('deliver') or 'log'}'.",
+ f"Subscribe to MCP event '{args['name']}' from server '{server_name}'? Each event starts an "
+ f"unattended agent run that follows the instructions below and sends its result to {target}.",
+ f"Instructions: {args.get('instructions') or 'Summarize what happened.'}\n"
f"Arguments: {json.dumps(args.get('arguments') or {})[:500]}",
surface=f"mcp-events/{server_name}", title=f"MCP server '{server_name}' event subscription")
except Exception as exc:
@@ -427,8 +446,11 @@ def _make_events_handler(op: str, label: str, rpc, needs_consent: bool = False):
return tool_error("Missing required parameter 'name'")
if args.get("arguments") is not None and not isinstance(args["arguments"], dict):
return tool_error("'arguments' must be an object")
- if needs_consent and (error := _consent_error(server_name, args)):
- return error
+ if needs_consent:
+ if invalid := _subscribe_args_error(args):
+ return tool_error(invalid)
+ if error := _consent_error(server_name, args):
+ return error
async def _call():
async with server._rpc_lock:
diff --git a/tools/mcp_tool_schema.py b/tools/mcp_tool_schema.py
index 0a669210361..e3679494f6b 100644
--- a/tools/mcp_tool_schema.py
+++ b/tools/mcp_tool_schema.py
@@ -221,7 +221,8 @@ _UTILITY_TOOL_SPECS = (
"description": "Subscription arguments matching the event's inputSchema",
"properties": {},
"additionalProperties": True},
- "instructions": {"type": "string", "description": "What to do each time the event arrives"},
+ "instructions": {"type": "string",
+ "description": "What to do each time the event arrives (up to 2000 characters)"},
"deliver": {"type": "string",
"description": "Where each run's result goes: log, telegram, discord, slack, ..."},
"deliver_chat_id": {"type": "string", "description": "Optional chat id on the deliver platform"},
diff --git a/website/docs/user-guide/features/mcp.md b/website/docs/user-guide/features/mcp.md
index 9e1ed8da99e..923b30ca529 100644
--- a/website/docs/user-guide/features/mcp.md
+++ b/website/docs/user-guide/features/mcp.md
@@ -1026,8 +1026,9 @@ The server then gets three more utility tools:
- `list_events`: the event types the server offers, and your current subscriptions to them
- `subscribe_event`: subscribe with the event's arguments, what to do when it arrives
- (`instructions`) and where each run's result goes (`deliver`, `deliver_chat_id`); you are asked to
- approve every subscription, since it starts unattended agent runs
+ (`instructions`, up to 2000 characters) and where each run's result goes (`deliver`,
+ `deliver_chat_id`). You are asked to approve every subscription, since it starts unattended agent
+ runs; the approval shows the instructions and the destination in full.
- `unsubscribe_event`: cancel a subscription
Each subscription is stored as a dynamic webhook route (`hermes webhook list` shows it) with its own