Repository navigation
fix(mcp): add pagination envelopes and structured resource errors (#819) - #897
Conversation
- Add MCPPaginatedQueryResultPayload/MCPPaginatedSearchResultPayload with items/hits, total, limit, offset, and optional diagnostics. - Update conversation_query_result_payload() and conversation_search_result_payload() to produce paginated envelopes. - Wire total via spec.count() in list_conversations and search tools. - Add is_error: Literal[True] to MCPErrorPayload for machine-readable error detection. - Add code="not_found" to conversation and messages resource not-found error returns. - Wrap 5 resource handlers with try/except returning structured internal_error payloads (stats, conversations, tags, session-tree, provider/recent). Ref #819 #852. Verification: deferred to wave batch per #852 policy. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Warning Rate limit exceeded
To continue reviewing without waiting, purchase usage credits in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 95289cd596
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Pull request overview
This PR updates the MCP surface to support client-side pagination for conversation listing and search results, and to standardize machine-readable error payloads across MCP resources.
Changes:
- Wrap
list_conversationsandsearchtool responses in paginated envelopes includingtotal,limit, andoffset. - Add
is_error: TruetoMCPErrorPayloadand introduce paginated payload models for MCP query/search results. - Add structured
internal_errorhandling (andnot_foundcodes) to multiple MCP resource handlers.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
polylogue/mcp/server_tools.py |
Clamp limit/offset, compute totals, and return paginated envelopes for search and list_conversations. |
polylogue/mcp/server_resources.py |
Add structured error payloads for resource failures and not_found codes for missing conversations/messages. |
polylogue/mcp/payloads.py |
Extend error payload schema and add paginated response payload models for MCP list/search tools. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from polylogue.mcp.context_pack import ( | ||
| ContextPackActionSummary as MCPContextPackActionSummary, | ||
| ContextPackConversation as MCPContextPackConversation, | ||
| ContextPackDateRange as MCPContextPackDateRange, | ||
| ContextPackMessage as MCPContextPackMessage, | ||
| ContextPackPayload as MCPContextPackPayload, | ||
| ContextPackProject as MCPContextPackProject, | ||
| ContextPackProvenance as MCPContextPackProvenance, | ||
| ContextPackQueryContext as MCPContextPackQueryContext, | ||
| ContextPackUnresolvedWork as MCPContextPackUnresolvedWork, | ||
| ) |
| "MCPArchiveStatsPayload", | ||
| "MCPContextPackActionSummary", | ||
| "MCPContextPackConversation", | ||
| "MCPContextPackDateRange", | ||
| "MCPContextPackMessage", | ||
| "MCPContextPackPayload", | ||
| "MCPContextPackProject", | ||
| "MCPContextPackProvenance", | ||
| "MCPContextPackQueryContext", | ||
| "MCPContextPackUnresolvedWork", | ||
| "MCPContextPackActionSummary", | ||
| "MCPContextPackConversation", | ||
| "MCPContextPackDateRange", | ||
| "MCPContextPackMessage", | ||
| "MCPContextPackPayload", | ||
| "MCPContextPackProject", | ||
| "MCPContextPackProvenance", | ||
| "MCPContextPackQueryContext", | ||
| "MCPContextPackUnresolvedWork", |
| message_type=message_type, | ||
| offset=offset, | ||
| offset=clamped_offset, | ||
| ).build_spec(hooks.clamp_limit) | ||
| results = await ops.search_conversation_hits(spec) | ||
| total = await spec.count(hooks.get_query_store()) | ||
| diagnostics = await ops.diagnose_query_miss(spec) if not results else None |
| results = await ops.search_conversation_hits(spec) | ||
| total = await spec.count(hooks.get_query_store()) | ||
| diagnostics = await ops.diagnose_query_miss(spec) if not results else None | ||
| return hooks.json_payload(conversation_search_result_payload(results, diagnostics=diagnostics)) | ||
| return hooks.json_payload( | ||
| conversation_search_result_payload( | ||
| results, | ||
| total=total, | ||
| limit=clamped_limit, | ||
| offset=clamped_offset, | ||
| diagnostics=diagnostics, | ||
| ) | ||
| ) |
| conversations = await ops.query_conversations(spec) | ||
| total = await spec.count(hooks.get_query_store()) | ||
| diagnostics = None | ||
| if not conversations: | ||
| try: | ||
| diagnostics = await ops.diagnose_query_miss(spec) | ||
| except ConfigError: | ||
| diagnostics = None | ||
| return hooks.json_payload(conversation_query_result_payload(conversations, diagnostics=diagnostics)) | ||
| return hooks.json_payload( | ||
| conversation_query_result_payload( | ||
| conversations, | ||
| total=total, | ||
| limit=clamped_limit, | ||
| offset=clamped_offset, | ||
| diagnostics=diagnostics, | ||
| ) | ||
| ) |
| try: | ||
| archive_stats = await hooks.get_archive_ops().storage_stats() | ||
| except Exception as exc: | ||
| return hooks.error_json( | ||
| f"Failed to retrieve archive stats: {exc}", | ||
| code="internal_error", | ||
| detail=type(exc).__name__, | ||
| ) |
| try: | ||
| convs = await MCPConversationQueryRequest().build_spec(hooks.clamp_limit).list(hooks.get_query_store()) | ||
| except Exception as exc: | ||
| return hooks.error_json( | ||
| f"Failed to list conversations: {exc}", | ||
| code="internal_error", | ||
| detail=type(exc).__name__, | ||
| ) |
| try: | ||
| tags = await hooks.get_tag_store().list_tags() | ||
| except Exception as exc: | ||
| return hooks.error_json( | ||
| f"Failed to list tags: {exc}", | ||
| code="internal_error", | ||
| detail=type(exc).__name__, | ||
| ) |
| try: | ||
| tree = await hooks.get_archive_ops().get_session_tree(conv_id) | ||
| except Exception as exc: | ||
| return hooks.error_json( | ||
| f"Failed to get session tree for {conv_id}: {exc}", | ||
| code="internal_error", | ||
| detail=type(exc).__name__, | ||
| ) |
| try: | ||
| spec = MCPConversationQueryRequest(provider=name, sort="date", limit=10).build_spec(hooks.clamp_limit) | ||
| convs = await spec.list(hooks.get_query_store()) | ||
| except Exception as exc: | ||
| return hooks.error_json( | ||
| f"Failed to list recent conversations for provider {name}: {exc}", | ||
| code="internal_error", | ||
| detail=type(exc).__name__, | ||
| ) |
Closes the narrowed scope of #819. `list_conversations` and `search` returned bare JSON arrays with no `total`/`limit`/ `offset` metadata, blocking client-side pagination; resource handlers had inconsistent error patterns and `MCPErrorPayload` had no explicit `is_error` field. - `polylogue/mcp/payloads.py`: adds `is_error: Literal[True]` to `MCPErrorPayload`. Adds `MCPPaginatedQueryResultPayload` and `MCPPaginatedSearchResultPayload` with `items` / `hits`, `total`, `limit`, `offset`, `diagnostics`. Updates `conversation_query_result_payload()` and `conversation_search_result_payload()` to produce the paginated envelopes. - `polylogue/mcp/server_tools.py`: `search` and `list_conversations` clamp limit/offset, call `spec.count()` for total, and wrap results in the new envelopes. - `polylogue/mcp/server_resources.py`: adds `code="not_found"` to conversation/messages not-found paths; wraps stats / conversations / tags / session-tree / provider/recent handlers with try/except returning structured `internal_error` payloads. Refs #819
- #819 (MCP envelopes): `polylogue/mcp/payloads.py` returns a populated `next_offset` cursor on paginated responses so MCP clients can iterate without recomputing offsets, completing the pagination contract introduced in PR #897. - #820 (storage integrity): `storage/sqlite/schema_ddl_archive.py` switches the message-FK policy from implicit to explicit `ON DELETE NO ACTION`, matching the documented invariant in the schema docstrings rather than relying on SQLite's default. Refs #819, #820
Closes the narrowed scope of #819. `list_conversations` and `search` returned bare JSON arrays with no `total`/`limit`/ `offset` metadata, blocking client-side pagination; resource handlers had inconsistent error patterns and `MCPErrorPayload` had no explicit `is_error` field. - `polylogue/mcp/payloads.py`: adds `is_error: Literal[True]` to `MCPErrorPayload`. Adds `MCPPaginatedQueryResultPayload` and `MCPPaginatedSearchResultPayload` with `items` / `hits`, `total`, `limit`, `offset`, `diagnostics`. Updates `conversation_query_result_payload()` and `conversation_search_result_payload()` to produce the paginated envelopes. - `polylogue/mcp/server_tools.py`: `search` and `list_conversations` clamp limit/offset, call `spec.count()` for total, and wrap results in the new envelopes. - `polylogue/mcp/server_resources.py`: adds `code="not_found"` to conversation/messages not-found paths; wraps stats / conversations / tags / session-tree / provider/recent handlers with try/except returning structured `internal_error` payloads. Refs #819
- #819 (MCP envelopes): `polylogue/mcp/payloads.py` returns a populated `next_offset` cursor on paginated responses so MCP clients can iterate without recomputing offsets, completing the pagination contract introduced in PR #897. - #820 (storage integrity): `storage/sqlite/schema_ddl_archive.py` switches the message-FK policy from implicit to explicit `ON DELETE NO ACTION`, matching the documented invariant in the schema docstrings rather than relying on SQLite's default. Refs #819, #820
|
Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.
Only the rows listed above are dispositioned. Held findings remain open. |
|
Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.
Only the rows listed above are dispositioned. Held findings remain open. |
Summary
Add pagination envelopes to
list_conversationsandsearchMCP tools, and standardize structured error handling across all resource handlers.Problem
list_conversationsandsearchreturned bare JSON arrays with nototal/limit/offsetmetadata, making client-side pagination impossible.MCPErrorPayloadlacked an explicitis_errorfield.Solution
Payloads (
polylogue/mcp/payloads.py):is_error: Literal[True]toMCPErrorPayload.MCPPaginatedQueryResultPayload(items, total, limit, offset, diagnostics) andMCPPaginatedSearchResultPayload(hits, total, limit, offset, diagnostics).conversation_query_result_payload()andconversation_search_result_payload()to produce paginated envelopes with total/limit/offset.Tools (
polylogue/mcp/server_tools.py):searchandlist_conversationsnow compute clamped limit/offset, callspec.count()for total, and wrap results in paginated envelopes.Resources (
polylogue/mcp/server_resources.py):code="not_found"to conversation and messages resource not-found error returns.internal_errorpayloads.Verification
Deferred to wave batch per #852 policy.
Refs #819 #852.