Skip to content

Commit d19037a

Browse files
cliffhallclaude
andcommitted
fix(everything): refuse task tools and tasks/* without the extension with -32021
Copilot round 2 on #5131: the tasks extension requires a -32021 error, with the extension in data.requiredCapabilities, for a client that does not declare it, both on a request that cannot be served without a task handle and on tasks/get, tasks/update and tasks/cancel. McpServer reports a thrown error from a tool handler as an isError result, so guardTaskTools wraps the tools/call handler McpServer installs; the factory no longer declares the tools capability in the constructor (the first registerTool advertises the same one), so the handler is installed after the guard. The sampling and form-elicitation requirements of the async task tools are answered the same way, replacing the isError results added in round 1. Part of #4852 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 7f3b522 commit d19037a

7 files changed

Lines changed: 290 additions & 239 deletions

File tree

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

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,76 @@ describe("the extension", () => {
7878
});
7979
});
8080

81+
it.each([
82+
["simulate-research-query", {}, { extensions: { [TASKS_EXTENSION]: {} } }],
83+
[
84+
"trigger-sampling-request-async",
85+
{ sampling: {} },
86+
{ extensions: { [TASKS_EXTENSION]: {} } },
87+
],
88+
["trigger-sampling-request-async", TASKS, { sampling: {} }],
89+
[
90+
"trigger-sampling-request-async",
91+
{},
92+
{ extensions: { [TASKS_EXTENSION]: {} }, sampling: {} },
93+
],
94+
[
95+
"trigger-elicitation-request-async",
96+
{ elicitation: { form: {} } },
97+
{ extensions: { [TASKS_EXTENSION]: {} } },
98+
],
99+
["trigger-elicitation-request-async", TASKS, { elicitation: { form: {} } }],
100+
[
101+
"trigger-elicitation-request-async",
102+
{ ...TASKS, elicitation: { url: {} } },
103+
{ elicitation: { form: {} } },
104+
],
105+
] as const)(
106+
"refuses %s from a client declaring %j with -32021 naming what is missing",
107+
async (tool, capabilities, requiredCapabilities) => {
108+
peer = await rawClient(capabilities);
109+
const response = await peer.request("tools/call", {
110+
name: tool,
111+
arguments:
112+
tool === "trigger-elicitation-request-async"
113+
? {}
114+
: tool === "simulate-research-query"
115+
? { topic: "t" }
116+
: { prompt: "p" },
117+
});
118+
expect(response.error).toMatchObject({
119+
code: -32021,
120+
data: { requiredCapabilities },
121+
});
122+
},
123+
);
124+
125+
it.each([
126+
["tasks/get", {}],
127+
["tasks/update", { inputResponses: {} }],
128+
["tasks/cancel", {}],
129+
])(
130+
"refuses %s from a client without the extension with -32021",
131+
async (method, extra) => {
132+
peer = await rawClient(TASKS);
133+
const created = await peer.call("simulate-research-query", {
134+
topic: "t",
135+
});
136+
await peer.close();
137+
peer = await rawClient({});
138+
const response = await peer.request(method, {
139+
taskId: created.taskId,
140+
...extra,
141+
});
142+
expect(response.error).toMatchObject({
143+
code: -32021,
144+
data: {
145+
requiredCapabilities: { extensions: { [TASKS_EXTENSION]: {} } },
146+
},
147+
});
148+
},
149+
);
150+
81151
it("drops a task once its TTL has passed", async () => {
82152
peer = await rawClient(TASKS);
83153
const created = await peer.call("simulate-research-query", { topic: "t" });
@@ -162,22 +232,6 @@ describe("simulate-research-query", () => {
162232
expect((await peer.get(created.taskId as string)).status).toBe("completed");
163233
});
164234

165-
it("is a tool error for a client that does not declare the extension", async () => {
166-
peer = await rawClient({});
167-
const result = await peer.call("simulate-research-query", { topic: "t" });
168-
expect(result).toMatchObject({
169-
isError: true,
170-
content: [
171-
{
172-
type: "text",
173-
text: expect.stringContaining(
174-
`needs a client that declares the ${TASKS_EXTENSION} extension`,
175-
),
176-
},
177-
],
178-
});
179-
});
180-
181235
it("ignores 'ambiguous' for a client without elicitation", async () => {
182236
peer = await rawClient(TASKS);
183237
const created = await peer.call("simulate-research-query", {
@@ -407,21 +461,6 @@ describe("trigger-sampling-request-async", () => {
407461
expect(resultText(done)).toContain('"text": "hi"');
408462
});
409463

410-
it("is a tool error for a client that does not declare sampling", async () => {
411-
peer = await rawClient(TASKS);
412-
const result = await peer.call("trigger-sampling-request-async", {
413-
prompt: "p",
414-
});
415-
expect(result).toMatchObject({
416-
isError: true,
417-
content: [
418-
{
419-
text: "trigger-sampling-request-async asks the client for input, so it needs a client that declares the sampling capability.",
420-
},
421-
],
422-
});
423-
});
424-
425464
it("keeps waiting after an answer of the wrong kind", async () => {
426465
peer = await rawClient({ ...TASKS, sampling: {} });
427466
const created = await peer.call("trigger-sampling-request-async", {
@@ -435,14 +474,6 @@ describe("trigger-sampling-request-async", () => {
435474
"input_required",
436475
);
437476
});
438-
439-
it("is a tool error for a client that does not declare the extension", async () => {
440-
peer = await rawClient({ sampling: {} });
441-
const result = await peer.call("trigger-sampling-request-async", {
442-
prompt: "p",
443-
});
444-
expect(result.isError).toBe(true);
445-
});
446477
});
447478

448479
describe("trigger-elicitation-request-async", () => {
@@ -520,22 +551,6 @@ describe("trigger-elicitation-request-async", () => {
520551
},
521552
);
522553

523-
it.each([
524-
["only the tasks extension", TASKS],
525-
["URL-mode elicitation only", { ...TASKS, elicitation: { url: {} } }],
526-
])("is a tool error for a client with %s", async (_label, capabilities) => {
527-
peer = await rawClient(capabilities);
528-
const result = await peer.call("trigger-elicitation-request-async");
529-
expect(result).toMatchObject({
530-
isError: true,
531-
content: [
532-
{
533-
text: "trigger-elicitation-request-async asks the client for input, so it needs a client that declares the elicitation (form mode) capability.",
534-
},
535-
],
536-
});
537-
});
538-
539554
it.each(["decline", "cancel"])("reports a %s", async (action) => {
540555
const done = await answer({ action });
541556
expect(resultText(done)).toBe(`Elicitation ${action}.`);
@@ -552,10 +567,4 @@ describe("trigger-elicitation-request-async", () => {
552567
"input_required",
553568
);
554569
});
555-
556-
it("is a tool error for a client that does not declare the extension", async () => {
557-
peer = await rawClient({ elicitation: {} });
558-
const result = await peer.call("trigger-elicitation-request-async");
559-
expect(result.isError).toBe(true);
560-
});
561570
});

‎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-
An unknown or expired task, an update to a task that is not waiting for input or that answers a request it did not make, and a cancel of a finished task are refused with `-32602`. 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, an update to a task that is not waiting for input or that answers a request it did not make, and a cancel of a finished task are refused with `-32602`. 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/server/index.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ import {
3333
} from "../tools/index.js";
3434
import { registerResources, readInstructions } from "../resources/index.js";
3535
import { registerPrompts } from "../prompts/index.js";
36-
import { registerTasksExtension, TASKS_EXTENSION } from "./tasks.js";
36+
import {
37+
guardTaskTools,
38+
registerTasksExtension,
39+
TASKS_EXTENSION,
40+
} from "./tasks.js";
3741
import { syncRoots } from "./roots.js";
3842
import {
3943
instancePublisher,
@@ -127,9 +131,10 @@ export const createServer = (
127131
},
128132
{
129133
capabilities: {
130-
tools: {
131-
listChanged: true,
132-
},
134+
// `tools: { listChanged: true }` is advertised by the first
135+
// registerTool. Declaring it here would install McpServer's
136+
// tools/call handler in the constructor, before guardTaskTools can
137+
// wrap it.
133138
prompts: {
134139
listChanged: true,
135140
},
@@ -167,6 +172,10 @@ export const createServer = (
167172
onCleanup: (fn) => cleanups.push(fn),
168173
};
169174

175+
// A task tool refuses, with -32021, a client that has not declared what it
176+
// needs; this wraps the tools/call handler the first registration installs.
177+
if (era === "modern") guardTaskTools(server);
178+
170179
// Register the tools, resources and prompts every era gets
171180
registerTools(server, context);
172181
registerResources(server, context);

0 commit comments

Comments
 (0)