|
| 1 | +import { calculateObjectSize } from "bson"; |
| 2 | +import type { AggregationCursor, FindCursor } from "mongodb"; |
| 3 | + |
| 4 | +export function getResponseBytesLimit( |
| 5 | + toolResponseBytesLimit: number | undefined | null, |
| 6 | + configuredMaxBytesPerQuery: unknown |
| 7 | +): { |
| 8 | + cappedBy: "config.maxBytesPerQuery" | "tool.responseBytesLimit" | undefined; |
| 9 | + limit: number; |
| 10 | +} { |
| 11 | + const configuredLimit: number = parseInt(String(configuredMaxBytesPerQuery), 10); |
| 12 | + |
| 13 | + // Setting configured maxBytesPerQuery to negative, zero or nullish is |
| 14 | + // equivalent to disabling the max limit applied on documents |
| 15 | + const configuredLimitIsNotApplicable = Number.isNaN(configuredLimit) || configuredLimit <= 0; |
| 16 | + |
| 17 | + // It's possible to have tool parameter responseBytesLimit as null or |
| 18 | + // negative values in which case we consider that no limit is to be |
| 19 | + // applied from tool call perspective unless we have a maxBytesPerQuery |
| 20 | + // configured. |
| 21 | + const toolResponseLimitIsNotApplicable = typeof toolResponseBytesLimit !== "number" || toolResponseBytesLimit <= 0; |
| 22 | + |
| 23 | + if (configuredLimitIsNotApplicable) { |
| 24 | + return { |
| 25 | + cappedBy: toolResponseLimitIsNotApplicable ? undefined : "tool.responseBytesLimit", |
| 26 | + limit: toolResponseLimitIsNotApplicable ? 0 : toolResponseBytesLimit, |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + if (toolResponseLimitIsNotApplicable) { |
| 31 | + return { cappedBy: "config.maxBytesPerQuery", limit: configuredLimit }; |
| 32 | + } |
| 33 | + |
| 34 | + return { |
| 35 | + cappedBy: configuredLimit < toolResponseBytesLimit ? "config.maxBytesPerQuery" : "tool.responseBytesLimit", |
| 36 | + limit: Math.min(toolResponseBytesLimit, configuredLimit), |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * This function attempts to put a guard rail against accidental memory overflow |
| 42 | + * on the MCP server. |
| 43 | + * |
| 44 | + * The cursor is iterated until we can predict that fetching next doc won't |
| 45 | + * exceed the derived limit on number of bytes for the tool call. The derived |
| 46 | + * limit takes into account the limit provided from the Tool's interface and the |
| 47 | + * configured maxBytesPerQuery for the server. |
| 48 | + */ |
| 49 | +export async function collectCursorUntilMaxBytesLimit<T = unknown>({ |
| 50 | + cursor, |
| 51 | + toolResponseBytesLimit, |
| 52 | + configuredMaxBytesPerQuery, |
| 53 | + abortSignal, |
| 54 | +}: { |
| 55 | + cursor: FindCursor<T> | AggregationCursor<T>; |
| 56 | + toolResponseBytesLimit: number | undefined | null; |
| 57 | + configuredMaxBytesPerQuery: unknown; |
| 58 | + abortSignal?: AbortSignal; |
| 59 | +}): Promise<{ cappedBy: "config.maxBytesPerQuery" | "tool.responseBytesLimit" | undefined; documents: T[] }> { |
| 60 | + const { limit: maxBytesPerQuery, cappedBy } = getResponseBytesLimit( |
| 61 | + toolResponseBytesLimit, |
| 62 | + configuredMaxBytesPerQuery |
| 63 | + ); |
| 64 | + |
| 65 | + // It's possible to have no limit on the cursor response by setting both the |
| 66 | + // config.maxBytesPerQuery and tool.responseBytesLimit to nullish or |
| 67 | + // negative values. |
| 68 | + if (maxBytesPerQuery <= 0) { |
| 69 | + return { |
| 70 | + cappedBy, |
| 71 | + documents: await cursor.toArray(), |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + let wasCapped: boolean = false; |
| 76 | + let totalBytes = 0; |
| 77 | + const bufferedDocuments: T[] = []; |
| 78 | + while (true) { |
| 79 | + if (abortSignal?.aborted) { |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + // If the cursor is empty then there is nothing for us to do anymore. |
| 84 | + const nextDocument = await cursor.tryNext(); |
| 85 | + if (!nextDocument) { |
| 86 | + break; |
| 87 | + } |
| 88 | + |
| 89 | + const nextDocumentSize = calculateObjectSize(nextDocument); |
| 90 | + if (totalBytes + nextDocumentSize >= maxBytesPerQuery) { |
| 91 | + wasCapped = true; |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + totalBytes += nextDocumentSize; |
| 96 | + bufferedDocuments.push(nextDocument); |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + cappedBy: wasCapped ? cappedBy : undefined, |
| 101 | + documents: bufferedDocuments, |
| 102 | + }; |
| 103 | +} |
0 commit comments