Skip to content

Add bounded, resumable pagination for MCP query and ls - #204

Merged
Vsevolod Kukol (sevoku) merged 8 commits into
mainfrom
dev/mkrueger/mcp-pagination
Sep 2, 2026
Merged

Vsevolod Kukol (sevoku) merged 8 commits into
mainfrom
dev/mkrueger/mcp-pagination

Conversation

@mkrueger

@mkrueger Mike Krüger (mkrueger) commented Sep 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • return one bounded Cosmos DB page per MCP query and container-item ls call
  • apply a safe default cap of 100 items when max is omitted
  • surface an opaque top-level continuationToken, including null at exhaustion
  • accept continuation as an MCP-only argument without exposing or echoing it in the interactive shell
  • keep database/container name listings complete and omit continuation metadata because their ARM/data-plane abstraction is not resumable
  • document the MCP payload and resume flow

Implementation notes

IPagedCommand is the single source of truth used for MCP schema generation, default caps, and continuation binding. Interactive and scripted shell behavior remains unchanged and continues to aggregate pages normally.

The shared max help text retains shell semantics; MCP tool schemas add a paging-specific clarification that callers must use continuationToken to detect exhaustion.

Validation

  • application and test projects build with 0 warnings and 0 errors
  • full test suite: 2,273 passed, 0 failed, 98 emulator-dependent tests skipped

Closes #161

MCP calls to query and container-item ls now return one Cosmos DB page per
call, capped at 100 items by default, with the page's continuation token
surfaced as a top-level continuationToken in the structured result. A null
token signals exhaustion. Agents resume by passing the token back as the
continuation argument.

The continuation argument is MCP-only: it is injected into the tool schema
and bound explicitly rather than declared as a CosmosOption, so it never
appears in shell help, parsing, or docs, and the token is kept out of the
echoed command line. IPagedCommand is the single source of truth for which
commands paginate, driving the schema, the default cap, and binding.

Interactive and scripted behavior is unchanged: those paths keep their
multi-page aggregation and the SELECT TOP rewrite, which cannot coexist
with a continuation token because tokens are only settable through MCP.

The shared max help text keeps its whole-result wording for the shell; the
single-page clarification is appended only to the MCP schema.

Closes #161
Database and container name listings enumerate their complete result sets and do not expose resumable continuation tokens. Only container-item listings are marked as paged, and the MCP payload documentation now states that scope explicitly.
@github-code-quality

github-code-quality Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: C#

C# / code-coverage/dotnet

The overall line coverage in commit 902a466 in the dev/mkrueger/mcp-pag... branch remains at 61%, unchanged from commit 4691b41 in the main branch.

Show a line coverage summary of the most impacted files.
File main 4691b41 dev/mkrueger/mcp-pag... 902a466 +/-
D:\a\CosmosDBSh...QueryCommand.cs 41% 40% -1%
D:\a\CosmosDBSh...ponseFactory.cs 92% 92% 0%
D:\a\CosmosDBSh...mmandFactory.cs 93% 93% 0%
D:\a\CosmosDBSh...\ResultLimit.cs 100% 100% 0%
D:\a\CosmosDBSh...olOperations.cs 89% 90% +1%
D:\a\CosmosDBSh...CommandState.cs 91% 92% +1%
D:\a\CosmosDBSh...\ListCommand.cs 16% 18% +2%

Updated September 02, 2026 10:29 UTC

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds bounded, resumable pagination semantics specifically for MCP tool calls (query and container-item ls) to prevent unbounded result floods while keeping interactive/scripting shell behavior unchanged (still aggregates pages normally).

Changes:

  • Introduces an IPagedCommand contract and MCP wiring to (a) apply a safe default max of 100 for paged MCP tools and (b) accept an MCP-only continuation argument.
  • Surfaces a top-level continuationToken in MCP structured responses for paged results (including null at exhaustion).
  • Documents the MCP paging contract and adds tests covering schema/defaults and continuation token output.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
README.md Notes MCP paging behavior and links to detailed pagination docs.
docs/mcp.md Documents continuationToken and the MCP resume flow for paged tools.
docs/commands.md Clarifies MCP-only paging semantics for query and container-item ls.
CosmosDBShell/Azure.Data.Cosmos.Shell.Mcp/ToolOperations.cs Adds MCP schema/default handling for paging and binds MCP-only continuation.
CosmosDBShell/Azure.Data.Cosmos.Shell.Mcp/McpResponseFactory.cs Adds continuationToken to structured MCP success payloads for paged results.
CosmosDBShell/Azure.Data.Cosmos.Shell.Core/CommandState.cs Adds paging metadata fields (IsPage, ContinuationToken) to command results.
CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/QueryCommand.cs Implements IPagedCommand, binds continuation into query iterator, and returns single page under MCP.
CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/ListCommand.cs Implements IPagedCommand, binds continuation into item listing iterator, and returns single page under MCP.
CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/IPagedCommand.cs New interface defining MCP-only paging inputs for eligible commands.
CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/CommandFactory.cs Exposes IsPaged discovery for MCP schema generation/binding.
CosmosDBShell.Tests/ToolOperationsTests.cs Adds coverage for paging schema defaults/descriptions and continuation exposure/binding.
CosmosDBShell.Tests/McpResponseFactoryTests.cs Adds coverage for continuationToken being present (including null) for paged results.
Suppressed comments (1)

CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/ListCommand.cs:270

  • For MCP ls item listings, result.limitReached currently only becomes true when the returned item count hits max. Cosmos DB can return fewer than MaxItemCount and still provide a non-null continuation token, so callers could see continuationToken but limitReached=false, contradicting the documented MCP paging contract.
        var reachedLimit = limitReached && effectiveMaxItemCount.HasValue;
        returnState.Result = new ShellJson(JsonSerializer.SerializeToElement(new { type = "item", values = list, limitReached = reachedLimit }));

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Return a sanitized MCP error when `continuation` is not a string or null instead of silently restarting pagination. Add direct binder coverage and an end-to-end CallTool test.
Copilot AI review requested due to automatic review settings September 1, 2026 10:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Comment thread docs/mcp.md Resolved
Keep the continuation schema and binder consistent by accepting only non-null strings. Clarify that null continuation output means paging is complete, and add direct and end-to-end null-input coverage.
Copilot AI review requested due to automatic review settings September 1, 2026 10:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Derive MCP `limitReached` from continuation-token presence so short Cosmos pages correctly signal that more results remain. Preserve existing shell semantics and add focused regression coverage.
Copilot AI review requested due to automatic review settings September 1, 2026 10:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Normalize omitted and non-positive MCP max values to the safe 100-item default for query and ls while preserving shell unlimited semantics. Advertise a positive schema minimum and update tests and documentation.
Copilot AI review requested due to automatic review settings September 1, 2026 10:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.

Resolve pagination conflicts with request charge tracking
Copilot AI review requested due to automatic review settings September 2, 2026 10:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The paging contract is consistently implemented across schema, binding, command execution, responses, documentation, and has targeted regression coverage for the key edge cases.

Review details
  • Files reviewed: 16/16 changed files
  • Comments generated: 0 new
  • Review effort level: Lite
@sevoku
Vsevolod Kukol (sevoku) merged commit f0ece29 into main Sep 2, 2026
10 checks passed
@sevoku
Vsevolod Kukol (sevoku) deleted the dev/mkrueger/mcp-pagination branch September 2, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants