Skip to content

Commit 1fbdd4e

Browse files
cliffhallclaude
andcommitted
fix(everything): count tasks/update answers in UTF-8 bytes
The MAX_ANSWER_BYTES guard compared JSON.stringify(answered).length, which counts UTF-16 code units, so a non-ASCII answer could exceed the 64 KiB byte budget by up to 3x. Measure with Buffer.byteLength(..., "utf8") and pin it with a protocol-level test whose answer is under the limit by string length and over it in bytes. Closes #5419 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 281b167 commit 1fbdd4e

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-everything": patch
3+
---
4+
5+
`tasks/update` now measures the answers against its 64 KiB limit in UTF-8 bytes rather than string length, so non-ASCII answers can no longer exceed the byte budget.

‎src/everything/__tests__/tasks.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,37 @@ describe("the extension", () => {
210210
expect(Object.keys(task.inputRequests as object)).toEqual(["sampling-1"]);
211211
});
212212

213+
it("counts the answers in UTF-8 bytes, not string length (#5419)", async () => {
214+
peer = await rawClient({ ...TASKS, sampling: {} });
215+
const created = await peer.call("trigger-sampling-request-async", {
216+
prompt: "p",
217+
});
218+
// Each "é" is one UTF-16 code unit but two UTF-8 bytes, so these answers
219+
// are under the limit by string length and over it in bytes.
220+
const inputResponses = {
221+
"sampling-1": {
222+
role: "assistant",
223+
model: "m",
224+
content: { type: "text", text: "é".repeat(MAX_ANSWER_BYTES / 2) },
225+
},
226+
};
227+
const serialized = JSON.stringify(inputResponses);
228+
const bytes = Buffer.byteLength(serialized, "utf8");
229+
expect(serialized.length).toBeLessThan(MAX_ANSWER_BYTES);
230+
expect(bytes).toBeGreaterThan(MAX_ANSWER_BYTES);
231+
const response = await peer.request("tasks/update", {
232+
taskId: created.taskId,
233+
inputResponses,
234+
});
235+
expect(response.error).toMatchObject({
236+
code: -32602,
237+
message: `Invalid params for tasks/update: the answers are ${bytes} bytes, over the ${MAX_ANSWER_BYTES}-byte limit; the task is unchanged`,
238+
});
239+
const task = await peer.get(created.taskId as string);
240+
expect(task.status).toBe("input_required");
241+
expect(Object.keys(task.inputRequests as object)).toEqual(["sampling-1"]);
242+
});
243+
213244
it("refuses tasks/update without inputResponses with -32602", async () => {
214245
peer = await rawClient(TASKS);
215246
const created = await peer.call("simulate-research-query", { topic: "t" });

‎src/everything/docs/features.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,6 @@ Logging is deprecated as of 2026-07-28 (SEP-2577) and remains available on both
9999
- `tasks/update`: the client's `inputResponses` to the outstanding input requests;
100100
- `tasks/cancel`: stop a task that has not finished.
101101

102-
A task tool called by a client whose request does not declare the extension, or the capability its task asks for input with (`sampling` for `trigger-sampling-request-async`, form-mode `elicitation` for `trigger-elicitation-request-async`), is refused with `-32021` (missing required client capability), naming what is missing in `data.requiredCapabilities`; so is a `tasks/*` request from a client without the extension. An unknown or expired task, and a `tasks/update` whose answers exceed 64 KiB serialized, are refused with `-32602`. A `tasks/cancel` is acknowledged whatever the task's status (cancellation is cooperative), and a finished task stays as it ended. An update's answers to requests that are not outstanding (never issued, already answered, or sent while the task is not waiting for input) are ignored, as the extension asks. Each input request a task makes has its own key, never reused once answered, so a request asked again after an answer that does not fit gets a new key (`form-2`, `sampling-2`). The process keeps at most 100 tasks, each until its TTL; at that limit a new task is refused with a tool error. The task id is a server-minted UUID, so a task lives across requests (each 2026-07-28 HTTP request is served by a fresh server instance) and is reached only through its handle. A task's progress is computed from the clock when it is read, so nothing runs in the background.
102+
A task tool called by a client whose request does not declare the extension, or the capability its task asks for input with (`sampling` for `trigger-sampling-request-async`, form-mode `elicitation` for `trigger-elicitation-request-async`), is refused with `-32021` (missing required client capability), naming what is missing in `data.requiredCapabilities`; so is a `tasks/*` request from a client without the extension. An unknown or expired task, and a `tasks/update` whose answers exceed 64 KiB serialized (counted in UTF-8 bytes), are refused with `-32602`. A `tasks/cancel` is acknowledged whatever the task's status (cancellation is cooperative), and a finished task stays as it ended. An update's answers to requests that are not outstanding (never issued, already answered, or sent while the task is not waiting for input) are ignored, as the extension asks. Each input request a task makes has its own key, never reused once answered, so a request asked again after an answer that does not fit gets a new key (`form-2`, `sampling-2`). The process keeps at most 100 tasks, each until its TTL; at that limit a new task is refused with a tool error. The task id is a server-minted UUID, so a task lives across requests (each 2026-07-28 HTTP request is served by a fresh server instance) and is reached only through its handle. A task's progress is computed from the clock when it is read, so nothing runs in the background.
103103

104104
The 2025-11-25 core task methods (`tasks/result`, `tasks/list`, task-augmented requests) are not served: SDK v2 removed the experimental layer they were built on.

‎src/everything/server/tasks.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ const tasks = new Map<string, StoredTask>();
106106
export const MAX_TASKS = 100;
107107

108108
/**
109-
* The most bytes of answers, serialized, one `tasks/update` may hand a task.
109+
* The most bytes of answers, serialized as JSON and counted in UTF-8, one
110+
* `tasks/update` may hand a task.
110111
* A task keeps its answers until its TTL, and a transport accepts messages
111112
* far larger than any of these tasks needs (10 MiB on stdio), so with
112113
* `MAX_TASKS` this bounds what the store holds.
@@ -413,7 +414,10 @@ export function registerTasksExtension(server: McpServer): void {
413414
outstanding.includes(key),
414415
),
415416
);
416-
const size = JSON.stringify(answered).length;
417+
// The limit is in bytes, so count the UTF-8 encoding of the serialized
418+
// answers: a string's length counts UTF-16 code units, which under-counts
419+
// any non-ASCII text by up to 3x.
420+
const size = Buffer.byteLength(JSON.stringify(answered), "utf8");
417421
if (size > MAX_ANSWER_BYTES) {
418422
throw new ProtocolError(
419423
ProtocolErrorCode.InvalidParams,

0 commit comments

Comments
 (0)