Skip to content

feat: accept a bounded batch of document ids on get_document_by_id - #380

Draft
celticht32 wants to merge 1 commit into
couchbase:mainfrom
celticht32:feature/kv-bulk-read-parameter
Draft

celticht32 wants to merge 1 commit into
couchbase:mainfrom
celticht32:feature/kv-bulk-read-parameter

Conversation

@celticht32

@celticht32 celticht32 commented Nov 23, 2026 •

Copy link
Copy Markdown

Related Issue

Resolves: CBSE-24143 (https://couchbasecloud.atlassian.net/browse/CBSE-24143)

Related: #274 is the earlier shape of this request, a separate get_documents_by_ids tool. This pull request is the zero-new-tools shape. If it is accepted, #274 can be closed.

Server(s) Affected

  • Operational (operational)
  • Operational Insights (operational-insights)
  • Shared machinery (all servers)

What does this change do?

get_document_by_id gains an optional document_ids parameter. Pass document_id for one document, as today, or document_ids for up to 50. The tool list does not grow, and the single form is unchanged, including raising when the document is not found.

The concern raised on CBSE-23739 and CBSE-23737 is that a bulk read can fill the agent's context window, because the documents come back into the context and stay there. The change is built around bounding that.

Guardrail Behaviour
Count At most 50 ids per call, rejected before the cluster is touched
Size Returned documents together are limited to 64 KiB by default. CB_MCP_BULK_READ_MAX_BYTES can lower it, or raise it up to a hard ceiling of 128 KiB. A bad value falls back to the default
Truncation is never silent Once the next document would pass the limit, it and every later document read successfully are listed in not_returned, in request order, with truncated, bytes_returned and limit_bytes, and a line telling the agent to call again with not_returned
Nothing cut mid-document A document larger than the whole limit is listed in oversize with its size, to be read alone with document_id. It does not stop the documents after it
Per-id accounting Every requested id is in exactly one of documents, errors, oversize and not_returned
Errors capped Per-id error text goes through the shared error formatter
Read scope only No write scope is needed

Why is this change needed?

An agent reading fifty documents one call at a time spends fifty model turns and accumulates fifty results. One bounded call costs one of each, and says what happened to every id. SQL++ USE KEYS is not an equivalent for a key-value principal: Data Reader does not grant the ability to run SQL++ queries, USE KEYS has no cap on keys or bytes returned, and an absent key simply produces no row.

Evidence of Testing

Built on upstream main at 45e8b32 and on nothing else, 1 commit. It applies to a clean main by itself and does not depend on any other pull request.

# Linux
pytest tests/unit -q
2296 passed

ruff format --check src/ tests/     clean
ruff check src/ tests/              All checks passed

Unit tests cover the 50-id cap enforced before the cluster is touched, deferral to not_returned in request order, bytes and limit reported, disjoint accounting across all four buckets, an oversize document never cut and not blocking later ones, error text capped, the environment variable lowering the limit, raising it only to the ceiling and falling back on bad values, and the limits and result fields appearing in the tool description. A mutation check confirmed the truncation test fails if the deferral rule is removed.

Three accuracy cases are written and committed but not executed, consistent with the repository's accuracy workflow: several ids in one call, one id staying on the single form, and a truncated result followed by a second call for not_returned.

Integration evidence, run on the pushed commit 7bda242 (checkout verified unchanged for the whole of each run, nothing else using the repository).

  • Couchbase Server 8.0.1 Enterprise Edition (Docker, 2 nodes): the whole operational suite (the scope CI runs): 203 passed, 8 skipped, 0 failed. This includes the change's own integration tests (truncation with a follow-up call, an oversize document read alone, the single-document read unchanged), the existing KV and read-only tests, and the Search tests. The 8 skips are 4 performance-tool tests and 4 vector-search tests that need query history and an embedding provider, which this cluster does not have. Unmodified main on the same cluster: the Search test file gives 31 passed, 0 failed.
  • Couchbase Capella (AWS us-east-1): the change's own integration tests plus the existing KV and read-only integration tests: 44 passed. Full operational suite: 174 passed, 4 failed, 33 skipped. The 4 failures are the Search index create and drop round trips, and the 33 skips are Search, vector and REST-only tests. This Capella cluster has no Search service, and the same 4 fail on unmodified main (Search test file on main: 4 failed, 12 passed, 15 skipped).
  • Search behaviour is verified on the Enterprise Edition cluster only. The earlier Search failures on that cluster came from its Search memory quota (256 MB, too small to index the 31,591-document test collection); with the quota raised to 1024 MB the Search tests pass, on this branch and on unmodified main.

Compatibility

The single form is unchanged. The new parameter is optional. No existing tool name, parameter or return shape changes. One new optional environment variable, CB_MCP_BULK_READ_MAX_BYTES, with a safe default and a hard ceiling.

Read-only mode

Works in read-only mode, since it is a read. No write scope is needed.

Checklist

  • Standalone: no dependency on any other pull request
  • Unit tests added and passing on Linux
  • Integration tests added
  • Integration tests run on Capella
  • Integration tests run on self-managed
  • README row updated
  • No new dependencies

🤖 Generated with Claude Code

@celticht32

Copy link
Copy Markdown
Author

Design note: performance, document size constraints, backward compatibility

This answers the three points raised on CBSE-23737 / CBSE-24143 for v1.1. It covers this PR (bulk read) and the related bulk write branch, and it states what has been measured and what has not.

Summary

Both bulk shapes add no tools and leave every existing single-document call unchanged, and both cap the size of a batch and of a result so one call cannot flood an agent's context window.

  • Reads (this PR, CBSE-24143): an optional document_ids parameter on get_document_by_id. Capped at 50 ids per call and 64 KiB of returned documents by default, with a hard ceiling of 128 KiB.
  • Writes (CBSE-23737, branch feature/kv-bulk-write-parameter): an optional documents parameter on the four existing write tools (upsert, insert, replace, delete). Capped at 100 documents and 1 MiB serialized, both checked before anything is written.

No performance measurements exist yet, and this note says so rather than implying otherwise.

Performance

The gain from bulk is fewer agent turns and a smaller context, not fewer network round trips, and no timing comparison has been run yet.

What the SDK does. The Python SDK's *_multi calls build one request per key and hand the list to the C++ core, which dispatches them concurrently. There is no batched KV opcode. An earlier claim of "one network round trip rather than one per document" was wrong, was corrected in the docstring and PR text, and is not repeated here.

Where the cost actually is. In an MCP server the unit of cost is a tool call, and every call is a full model turn: the model generates the call, waits, and the result stays in its context for the rest of the session. Correcting fifty records through the single-document tools costs fifty turns and fifty near-identical success results. One bulk call costs one of each. This holds even if the SDK looped serially underneath.

Concurrency. The server limits tool-call concurrency to 40 per process, so a bulk call occupies one slot where fifty single calls would compete for fifty.

Not measured. There are no latency or throughput figures for bulk against single-document calls, and no token counts for either. A benchmark on the Enterprise Edition test cluster is planned, run at the real caps (100 documents for writes, 50 ids for reads). It will report wall-clock time per batch, the number of model turns avoided, and the size of the returned result in bytes and approximate tokens. Until it is run, this section makes a structural argument, not a measured one.

Document size constraints

Reads are the risky side, so their limits are tighter: a read puts documents into the agent's context, while a write sends documents the agent already holds.

Bulk write Bulk read
Count cap 100 documents 50 ids
Size cap 1 MiB serialized batch 64 KiB of returned documents (default)
Adjustable No Lower freely with CB_MCP_BULK_READ_MAX_BYTES; raise only up to a 128 KiB ceiling
Checked Before anything is written Count before the cluster is touched; size as results are added
When exceeded Whole batch refused with a message naming the limit, so the agent can split it Remaining documents go in not_returned, in order, with truncated: true, bytes_returned and limit_bytes
Single oversize document Counts toward the 1 MiB cap Goes in oversize with its size; read it alone with document_id; later documents are still returned

Result accounting. Every requested id appears in exactly one bucket: for writes succeeded, errors or unknown_outcome; for reads documents, errors, oversize or not_returned. A missing key is named as not found, so absence is an answer. A write the server returns no outcome for is unknown_outcome, with one line of guidance (check the document's state before retrying), said once per result.

Bounded output. Per-document error text is capped. The limits and the full result shape are written into each tool description, so the model sees them before it calls.

Why 64 KiB. At roughly four bytes per token, 64 KiB is about 16,000 tokens. An earlier bound of 1 MiB would be on the order of 250,000 tokens, which fills a context window, so it was replaced.

Environment variable handling. A value that is not a positive integer is ignored, and a value above the ceiling is held at the ceiling, so a misconfiguration cannot turn the limit off.

Backward compatibility

No existing tool is removed, renamed or given a new required argument, and a caller that passes one document gets exactly today's behaviour.

  • Tool list unchanged. The batch is an optional parameter on tools that already exist, so the catalogue grows by zero tools. There is no second tool to choose between, which also removes the confusion between a bulk tool and its single-document twin.
  • Single-document form unchanged. For reads, get_document_by_id with document_id behaves as before, including raising when the document is not found. For writes, the single-document arguments are untouched.
  • Scopes unchanged. Bulk reads need read scope only and are offered to a reader credential. Bulk writes need write scope, so they are never offered to a read-only credential.
  • Confirmation prompts. A batch confirmation names the document count and up to five ids (each at most 60 characters, then "and N more"), so a person approving a bulk delete sees what will happen.
  • Existing tests. The integration runs include the existing KV and read-only integration tests, and all of them pass on Capella and on Enterprise Edition.
  • Superseded shape. PR feat: add get_documents_by_ids for bulk document reads #274 proposed a separate get_documents_by_ids tool. It is closed in favour of the parameter shape.

One cost to state. The limits and result shape are written into the tool descriptions, so those descriptions get longer and every session that loads the tool pays for it. The size of that increase has not been measured. It is the price of the model knowing the limits before it calls.

Evidence and open decisions

Both branches pass the whole operational integration suite on Couchbase Server Enterprise Edition with no failures.

Branch Enterprise Edition 8.0.1 (Docker, 2 nodes) Capella (AWS us-east-1)
Write (feature/kv-bulk-write-parameter, 04e928a) 201 passed, 8 skipped, 0 failed 42 passed on own and KV tests; full suite 172 passed, 4 failed, 33 skipped
Read (this PR, cebdb9b) 203 passed, 8 skipped, 0 failed 44 passed on own and KV tests; full suite 174 passed, 4 failed, 33 skipped

On Capella the 4 failures are Search index tests. That cluster has no Search service, and the same 4 fail on unmodified main. The 8 skips on Enterprise Edition are 4 performance-tool tests and 4 vector-search tests that need query history and an embedding provider. Search is verified on Enterprise Edition only, after raising its Search memory quota from 256 MB to 1024 MB, which the 31,591-document test collection needed.

Decisions needed

  1. For reads: the parameter shape on get_document_by_id, the limits (50 ids, 64 KiB default, 128 KiB ceiling) and the truncation behaviour. A different limit, with a reason, is as useful as agreement.
  2. For writes: whether the batch is accepted as an optional documents parameter on the four existing write tools, adding zero tools. PR CBSE-23737: Accept a batch of documents on the KV write tools #370 was closed on 20 November with "Rejected for now"; the work stays on its branch.
  3. Whether a deployment switch to ship bulk off by default is wanted. It was not built, because hiding a parameter needs new option plumbing in the shared core, and refusing at call time would still advertise the parameter at the same token cost.

Still to do on this side. The performance benchmark described above, and a measurement of how much the longer tool descriptions add to a session's context.

The read side of the same problem as the batched writes: fetching fifty documents costs fifty round trips and fifty tool calls when the SDK can fetch them in one.

This adds an optional document_ids parameter, capped at MAX_BULK_READ_DOCS. The single document_id parameter is untouched, so every existing caller is unaffected.

As with the batched writes, a missing document is an ordinary outcome rather than a failed call, so the result reports per-document outcomes and the multi operation runs with return_exceptions.
@celticht32
celticht32 force-pushed the feature/kv-bulk-read-parameter branch from cebdb9b to 7bda242 Compare November 30, 2026 04:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant