The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): stop listing event types after 20 pages
emozillab9fe74584db3
diff --git a/tests/tools/test_mcp_events.py b/tests/tools/test_mcp_events.pyindex eefa15e2fb2..cc15bcc90d5 100644--- a/tests/tools/test_mcp_events.py+++ b/tests/tools/test_mcp_events.py@@ -85,6 +85,21 @@ class TestUtilitySelection:assert set(events.EVENT_UTILITY_KEYS) <= self._keys({"events": True})+class TestList:+ def test_listing_stops_at_the_page_cap(self, home):+ class Endless:+ calls = 0++ async def send_request(self, request, result_type):+ Endless.calls += 1+ await asyncio.sleep(0)+ return {"events": [{"name": f"e{Endless.calls}"}], "nextCursor": f"c{Endless.calls}"}++ result = asyncio.run(asyncio.wait_for(events._list_events(Endless(), {}, "ambush"), timeout=5))+ assert len(result["events"]) == Endless.calls == events._MAX_LIST_PAGES+ assert "truncated" in result++class TestSubscribe:def test_subscribe_registers_a_signed_route_then_the_subscription(self, home):session = FakeSession(_grant())diff --git a/tools/mcp_tool_events.py b/tools/mcp_tool_events.pyindex 120e33df462..84e72a3b30b 100644--- a/tools/mcp_tool_events.py+++ b/tools/mcp_tool_events.py@@ -40,6 +40,7 @@ _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})+_MAX_LIST_PAGES = 20# 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@@ -317,12 +318,13 @@ def start_refresh_task(server) -> Optional[asyncio.Task]:# ---- utility tools ----async def _list_events(session, args: dict, server_name: str) -> dict:- events, cursor = [], None+ events, cursor, pages = [], None, 0while True:result = await _send(session, "events/list", {"cursor": cursor} if cursor else {})+ pages += 1events.extend(result.get("events") or [])cursor = result.get("nextCursor")- if not cursor:+ if not cursor or pages >= _MAX_LIST_PAGES:breakroutes = await asyncio.to_thread(_load_routes, server_name)states = await asyncio.to_thread(_load_state)@@ -330,7 +332,11 @@ async def _list_events(session, args: dict, server_name: str) -> dict:"deliver": r.get("deliver", "log"), "refreshBefore": states.get(n, {}).get("refresh_before"),**({"enabled": False} if r.get("enabled", True) is False else {})}for n, r in routes.items()]- return {"events": events, "subscriptions": subscribed}+ listed = {"events": events, "subscriptions": subscribed}+ if cursor:+ listed["truncated"] = (f"The server has more than {_MAX_LIST_PAGES} pages of event types; "+ f"these are the first {len(events)}.")+ return listedasync def _subscribe_event(session, args: dict, server_name: str) -> dict: