Skip to content

Commit d9ce091

Browse files
cliffhallclaude
andcommitted
fix(everything): bound the task store; document the async task tools' needs
Copilot round 4 on #5131: - The process keeps at most 100 tasks, dropping the oldest, and the task tools bound the strings a task keeps (topic 1,000 characters, prompt 10,000), so a burst of task-tool calls cannot exhaust memory before the TTL frees anything. - The instructions name the sampling and form-elicitation capabilities trigger-sampling-request-async and trigger-elicitation-request-async need beyond the tasks extension, and the -32021 a call without them gets. Part of #4852 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 4416643 commit d9ce091

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* `tasks/result` and `tasks/list`, which the extension does not define.
2323
*/
2424
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
25-
import { clearTasks, TASKS_EXTENSION } from "../server/tasks.js";
25+
import { clearTasks, MAX_TASKS, TASKS_EXTENSION } from "../server/tasks.js";
2626
import { STAGE_DURATION, STAGES } from "../tools/simulate-research-query.js";
2727
import { connectRaw as rawClient, type RawSession } from "./harness.js";
2828

@@ -148,6 +148,26 @@ describe("the extension", () => {
148148
},
149149
);
150150

151+
it("keeps at most MAX_TASKS, dropping the oldest", async () => {
152+
peer = await rawClient(TASKS);
153+
const first = await peer.call("simulate-research-query", { topic: "t" });
154+
for (let i = 1; i < MAX_TASKS; i++) {
155+
await peer.call("simulate-research-query", { topic: "t" });
156+
}
157+
expect((await peer.get(first.taskId as string)).status).toBe("working");
158+
await peer.call("simulate-research-query", { topic: "t" });
159+
const response = await peer.request("tasks/get", { taskId: first.taskId });
160+
expect(response.error).toMatchObject({ code: -32602 });
161+
});
162+
163+
it("refuses a topic longer than it will keep", async () => {
164+
peer = await rawClient(TASKS);
165+
const result = await peer.call("simulate-research-query", {
166+
topic: "x".repeat(1001),
167+
});
168+
expect(result.isError).toBe(true);
169+
});
170+
151171
it("drops a task once its TTL has passed", async () => {
152172
peer = await rawClient(TASKS);
153173
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
@@ -94,6 +94,6 @@ Logging is deprecated as of 2026-07-28 (SEP-2577) and remains available on both
9494
- `tasks/update`: the client's `inputResponses` to the outstanding input requests;
9595
- `tasks/cancel`: stop a task that has not finished.
9696

97-
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 cancel of a finished task are refused with `-32602`. 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 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.
97+
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 cancel of a finished task are refused with `-32602`. 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, dropping the oldest first. 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.
9898

9999
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/docs/instructions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Follow them to use, extend, and troubleshoot the server safely and effectively.
77

88
The server serves two protocol eras. A client on protocol version 2026-07-28 connects with `server/discover` and sends its capabilities with each request; a 2025-era client connects with `initialize`. Some tools exist on one era only, so trust your `tools/list`:
99

10-
- 2026-07-28 only: `simulate-research-query`, `trigger-sampling-request-async` and `trigger-elicitation-request-async`, which run as tasks on the `io.modelcontextprotocol/tasks` extension and need a client that declares it
10+
- 2026-07-28 only: `simulate-research-query`, `trigger-sampling-request-async` and `trigger-elicitation-request-async`, which run as tasks on the `io.modelcontextprotocol/tasks` extension and need a client that declares it; `trigger-sampling-request-async` also needs `sampling`, and `trigger-elicitation-request-async` form-mode `elicitation`. A call without them fails with `-32021`
1111
- 2025-era only (deprecated): `toggle-simulated-logging` and `toggle-subscriber-updates`
1212

1313
## Cross-Feature Relationships

‎src/everything/server/tasks.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ type StoredTask = {
9595
/** Every live task, by id, across every server instance in the process. */
9696
const tasks = new Map<string, StoredTask>();
9797

98+
/**
99+
* The most tasks the process keeps. A task holds its tool's arguments (each
100+
* task tool bounds their length) and the client's answers (bounded by the
101+
* transports' request size) until its TTL, so without a cap a burst of calls
102+
* could exhaust memory first; past it, the oldest task is dropped.
103+
*/
104+
export const MAX_TASKS = 100;
105+
98106
/** Drop every task past its TTL. */
99107
function sweep(now: number): void {
100108
for (const [taskId, task] of tasks) {
@@ -167,6 +175,11 @@ export function createTask(
167175
): CallToolResult {
168176
const now = Date.now();
169177
sweep(now);
178+
// The map keeps insertion order, so the first entries are the oldest.
179+
for (const taskId of tasks.keys()) {
180+
if (tasks.size < MAX_TASKS) break;
181+
tasks.delete(taskId);
182+
}
170183
const task: StoredTask = {
171184
taskId: randomUUID(),
172185
createdAt: now,

‎src/everything/tools/simulate-research-query.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ import {
3131

3232
// Tool input schema
3333
const SimulateResearchQuerySchema = z.object({
34-
topic: z.string().describe("The research topic to investigate"),
34+
topic: z
35+
.string()
36+
.max(1000)
37+
.describe(
38+
"The research topic to investigate (at most 1,000 characters; the task keeps it until it expires)",
39+
),
3540
ambiguous: z
3641
.boolean()
3742
.default(false)

‎src/everything/tools/trigger-sampling-request-async.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import { createTask, type TaskState } from "../server/tasks.js";
2222

2323
// Tool input schema
2424
const TriggerSamplingRequestAsyncSchema = z.object({
25-
prompt: z.string().describe("The prompt to send to the LLM"),
25+
prompt: z
26+
.string()
27+
.max(10000)
28+
.describe(
29+
"The prompt to send to the LLM (at most 10,000 characters; the task keeps it until it expires)",
30+
),
2631
maxTokens: z
2732
.number()
2833
.default(100)

0 commit comments

Comments
 (0)