Skip to content

Commit 8ea5e05

Browse files
cliffhallclaude
andcommitted
fix(everything): keep async task tools within their client task
trigger-elicitation-request-async asked the client for a 10-minute task TTL and then polled up to 600 times one second apart, which outlasts the TTL, so a client that honors it expired the task and the tool failed on its last tasks/get. Polling now runs against a deadline 5 seconds short of the TTL, measured from before the request is sent, and times out with a report instead (modelcontextprotocol#4986). trigger-sampling-request-async read the status message only from polls, so a client task already failed when created was reported as "No message". It now starts from the CreateTaskResult's statusMessage (modelcontextprotocol#4987). The pinning tests in tasks.test.ts now assert the fixed behavior, and their KNOWN BUG markers are gone. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit 8ea5e05

4 files changed

Lines changed: 59 additions & 25 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+
Fix the async task tools: `trigger-elicitation-request-async` now stops polling before the 10-minute TTL it requests for the client's task runs out, instead of polling past it and failing on a task the client has expired (#4986); and `trigger-sampling-request-async` reports the status message of a client task that is already finished when it is created, instead of "No message" (#4987).

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -422,18 +422,17 @@ describe("trigger-sampling-request-async", () => {
422422
);
423423
});
424424

425-
// KNOWN BUG #4987: trigger-sampling-request-async drops the status message of a task that is already finished when created; the fix changes this assertion.
426-
it("does not poll a client task that is already finished when it is created", async () => {
427-
// Characterization: the status message is only read from polls, so a
428-
// task that fails before it is returned is reported with "No message".
425+
it("reports the status message of a client task that is already finished when it is created", async () => {
426+
// A task that fails before it is returned is never polled, so its status
427+
// message comes from the CreateTaskResult.
429428
const { taskStore } = await connectTaskClient((_m, taskId) => {
430429
void taskStore.updateTaskStatus(taskId!, "failed", "instant failure");
431430
});
432431
const [text] = await callAsync("trigger-sampling-request-async", {
433432
prompt: "p",
434433
});
435434
expect(text).toMatch(
436-
/^\[FAILED\] No message\n\nProgress:\nTask created: \S+$/,
435+
/^\[FAILED\] instant failure\n\nProgress:\nTask created: \S+$/,
437436
);
438437
});
439438

@@ -577,26 +576,37 @@ describe("trigger-elicitation-request-async", () => {
577576
expect(texts[0]).toMatch(/^\[FAILED\] No message\n\nProgress:\n/);
578577
});
579578

580-
it("gives up after 600 polls of a task that never finishes", async () => {
579+
/** Also fake `Date`, which the tool's polling deadline is measured with. */
580+
function fakeDate() {
581+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
582+
}
583+
584+
it("gives up 5 seconds before the 10-minute TTL of a task that never finishes", async () => {
585+
fakeDate();
581586
// A client that keeps its task longer than the 10-minute TTL asked for.
582587
await connectTaskClient(() => {}, 60_000);
583588
const texts = await callAsync("trigger-elicitation-request-async");
584589
expect(texts[0]).toMatch(
585-
/^\[TIMEOUT\] Task timed out after 600 poll attempts\n\nProgress:\n/,
590+
/^\[TIMEOUT\] Task timed out after 595 poll attempts, before its 10-minute TTL expired\n\nProgress:\n/,
586591
);
587592
});
588593

589-
// KNOWN BUG #4986: trigger-elicitation-request-async polls for longer than the 10-minute TTL it requests for its task; the fix changes this assertion.
590-
it("fails on the last poll instead when the client honors the 10-minute TTL", async () => {
591-
// Characterization: 600 one-second polls outlast the 600000 ms TTL the
592-
// tool asks for, so a client that expires the task on time answers the
593-
// last tasks/get with "not found" and the call fails before the timeout
594-
// report.
595-
await connectTaskClient(() => {});
594+
it("times out before a client that honors the 10-minute TTL expires the task", async () => {
595+
fakeDate();
596+
let created = "";
597+
const { taskStore } = await connectTaskClient((_m, taskId) => {
598+
created = taskId!;
599+
});
596600
const texts = await callAsync("trigger-elicitation-request-async");
597-
expect(texts).toEqual([
598-
"MCP error -32602: MCP error -32602: Failed to retrieve task: Task not found",
599-
]);
601+
// The last poll reached the task while the client still held it.
602+
expect(texts).toHaveLength(1);
603+
expect(texts[0]).toMatch(
604+
/^\[TIMEOUT\] Task timed out after 595 poll attempts, before its 10-minute TTL expired\n\nProgress:\nTask created: \S+\nPoll 1: working\n/,
605+
);
606+
expect(texts[0]).toMatch(/\nPoll 595: working$/);
607+
// The client still holds the task when the tool gives up, 5 s before the
608+
// 600000 ms TTL it asked for runs out.
609+
expect((await taskStore.getTask(created))?.ttl).toBe(600000);
600610
});
601611

602612
it("returns a synchronous answer from a client that ignores the task request", async () => {

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ const config = {
2222
// Poll interval in milliseconds
2323
const POLL_INTERVAL = 1000;
2424

25-
// Maximum poll attempts before timeout (10 minutes for user input)
26-
const MAX_POLL_ATTEMPTS = 600;
25+
// Time-to-live requested for the client's task (10 minutes for user input)
26+
const TASK_TTL = 600000;
27+
28+
// Stop polling this long before the TTL runs out, so the last tasks/get
29+
// reaches the client while it still holds the task
30+
const TTL_SAFETY_MARGIN = 5000;
2731

2832
/**
2933
* Registers the 'trigger-elicitation-request-async' tool.
@@ -59,13 +63,18 @@ export const registerTriggerElicitationRequestAsyncTool = (
5963
name,
6064
config,
6165
async (args, extra): Promise<CallToolResult> => {
66+
// Polling must end before the client may expire the task. The client
67+
// starts the TTL when it creates the task, after this point, so a
68+
// deadline measured from here is conservative.
69+
const pollDeadline = Date.now() + TASK_TTL - TTL_SAFETY_MARGIN;
70+
6271
// Create the elicitation request WITH task metadata
6372
// Using z.any() schema to avoid complex type matching with _meta
6473
const request = {
6574
method: "elicitation/create" as const,
6675
params: {
6776
task: {
68-
ttl: 600000, // 10 minutes (user input may take a while)
77+
ttl: TASK_TTL, // 10 minutes (user input may take a while)
6978
},
7079
message:
7180
"Please provide inputs for the following fields (async task demo):",
@@ -144,13 +153,19 @@ export const registerTriggerElicitationRequestAsyncTool = (
144153
let attempts = 0;
145154
let taskStatus = elicitResponse.task.status;
146155
let taskStatusMessage: string | undefined;
156+
let timedOut = false;
147157

148158
while (
149159
taskStatus !== "completed" &&
150160
taskStatus !== "failed" &&
151-
taskStatus !== "cancelled" &&
152-
attempts < MAX_POLL_ATTEMPTS
161+
taskStatus !== "cancelled"
153162
) {
163+
// Give up rather than poll past the TTL the task was created with
164+
if (Date.now() + POLL_INTERVAL > pollDeadline) {
165+
timedOut = true;
166+
break;
167+
}
168+
154169
// Wait before polling
155170
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL));
156171
attempts++;
@@ -185,12 +200,14 @@ export const registerTriggerElicitationRequestAsyncTool = (
185200
}
186201

187202
// Check for timeout
188-
if (attempts >= MAX_POLL_ATTEMPTS) {
203+
if (timedOut) {
189204
return {
190205
content: [
191206
{
192207
type: "text",
193-
text: `[TIMEOUT] Task timed out after ${MAX_POLL_ATTEMPTS} poll attempts\n\nProgress:\n${statusMessages.join(
208+
text: `[TIMEOUT] Task timed out after ${attempts} poll attempts, before its ${
209+
TASK_TTL / 60000
210+
}-minute TTL expired\n\nProgress:\n${statusMessages.join(
194211
"\n",
195212
)}`,
196213
},

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ export const registerTriggerSamplingRequestAsyncTool = (server: McpServer) => {
147147
// Poll for task completion
148148
let attempts = 0;
149149
let taskStatus = samplingResponse.task.status;
150-
let taskStatusMessage: string | undefined;
150+
// A task that is already finished when created is never polled, so
151+
// its status message has to come from the CreateTaskResult
152+
let taskStatusMessage = samplingResponse.task.statusMessage;
151153

152154
while (
153155
taskStatus !== "completed" &&

0 commit comments

Comments
 (0)