Repository navigation
feat: accept a bounded batch of document ids on get_document_by_id - #380
celticht32 wants to merge 1 commit into
Conversation
|
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. SummaryBoth 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.
No performance measurements exist yet, and this note says so rather than implying otherwise. PerformanceThe 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 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 constraintsReads 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.
Result accounting. Every requested id appears in exactly one bucket: for writes 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 compatibilityNo existing tool is removed, renamed or given a new required argument, and a caller that passes one document gets exactly today's behaviour.
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 decisionsBoth branches pass the whole operational integration suite on Couchbase Server Enterprise Edition with no failures.
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
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.
cebdb9b to
7bda242
Compare
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_idstool. This pull request is the zero-new-tools shape. If it is accepted, #274 can be closed.Server(s) Affected
operational)operational-insights)What does this change do?
get_document_by_idgains an optionaldocument_idsparameter. Passdocument_idfor one document, as today, ordocument_idsfor 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.
CB_MCP_BULK_READ_MAX_BYTEScan lower it, or raise it up to a hard ceiling of 128 KiB. A bad value falls back to the defaultnot_returned, in request order, withtruncated,bytes_returnedandlimit_bytes, and a line telling the agent to call again withnot_returnedoversizewith its size, to be read alone withdocument_id. It does not stop the documents after itdocuments,errors,oversizeandnot_returnedWhy 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 KEYSis not an equivalent for a key-value principal: Data Reader does not grant the ability to run SQL++ queries,USE KEYShas no cap on keys or bytes returned, and an absent key simply produces no row.Evidence of Testing
Built on upstream
mainat45e8b32and on nothing else, 1 commit. It applies to a cleanmainby itself and does not depend on any other pull request.Unit tests cover the 50-id cap enforced before the cluster is touched, deferral to
not_returnedin 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).
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
🤖 Generated with Claude Code