From ebdc40280b3cf47b6bb1a52d5a16db1c296094e6 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 6 Oct 2026 10:09:31 +0000 Subject: [PATCH] fix(mcp): cap the page offset of list_recordings, list_entities and list_topics search and get_entity already stop at 10,000; the three list tools took any offset, so a huge one could reach the database as a long scan or an error. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01W42z4FbekWr33b6791svQQ --- fastapi_backend/app/api/mcp_tools.py | 6 +++--- fastapi_backend/tests/api/test_mcp.py | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/fastapi_backend/app/api/mcp_tools.py b/fastapi_backend/app/api/mcp_tools.py index 3fab90fb1..4869db1fa 100644 --- a/fastapi_backend/app/api/mcp_tools.py +++ b/fastapi_backend/app/api/mcp_tools.py @@ -407,7 +407,7 @@ def list_namespaces(ctx: Context) -> dict[str, Any]: Arg("media", "string", "only this kind", enum=MEDIA), Arg("sort", "string", "order: date, title or duration; - first for descending", default="-date", enum=SORTS), Arg("limit", "integer", "how many", default=20, minimum=1, maximum=100), - Arg("offset", "integer", "skip this many (for the next page)", default=0, minimum=0), + Arg("offset", "integer", "skip this many (for the next page)", default=0, minimum=0, maximum=10000), ) def list_recordings( ctx: Context, @@ -716,7 +716,7 @@ def _entity_row(e: dict[str, Any]) -> dict[str, Any]: Arg("recording_id", "integer", "only those mentioned in this recording"), Arg("sort", "string", "order", default="mentions", enum=("mentions", "recordings", "recent", "rising", "name")), Arg("limit", "integer", "how many", default=25, minimum=1, maximum=100), - Arg("offset", "integer", "skip this many (for the next page)", default=0, minimum=0), + Arg("offset", "integer", "skip this many (for the next page)", default=0, minimum=0, maximum=10000), ) def list_entities( ctx: Context, @@ -1058,7 +1058,7 @@ def graph_paths(ctx: Context, from_node: str, to_node: str, max_depth: int, shor Arg("top", "boolean", "only topics with no broader topic", default=False), Arg("broader_id", "integer", "only the narrower topics of this one"), Arg("limit", "integer", "how many", default=50, minimum=1, maximum=200), - Arg("offset", "integer", "skip this many (for the next page)", default=0, minimum=0), + Arg("offset", "integer", "skip this many (for the next page)", default=0, minimum=0, maximum=10000), ) def list_topics( ctx: Context, query: str | None, namespace: str | None, top: bool, broader_id: int | None, limit: int, offset: int diff --git a/fastapi_backend/tests/api/test_mcp.py b/fastapi_backend/tests/api/test_mcp.py index 6b5b27f2d..7b84e4ea6 100644 --- a/fastapi_backend/tests/api/test_mcp.py +++ b/fastapi_backend/tests/api/test_mcp.py @@ -261,6 +261,8 @@ def test_entities_and_the_graph(client, env, monkeypatch): e = tool(client, h, "get_entity", entity_id=dyno["entity_id"], mentions=2) assert e["name"] == "Dyno Therapeutics" and e["mentions"] == 4 and e["next_mentions_offset"] == 2 assert tool_error(client, h, "get_entity", entity_id=dyno["entity_id"], mentions_offset=10001).startswith("mentions_offset") + for name in ("list_recordings", "list_entities", "list_topics"): # every page offset has the same cap as search's + assert tool_error(client, h, name, offset=10001).startswith("offset"), name assert {m["name"] for m in e["mentioned_most_by"]} == {"Alice", "Bob", "Carol"} line = e["lines"][0] assert line["recording_id"] in (ep1, ep2) and "Dyno Therapeutics" in line["text"]