Skip to content

Commit 2ffc55c

Browse files
authored
Merge pull request #5034 from modelcontextprotocol/v2/fix/4792-everything-conditional-tools
fix(everything): instructions qualify capability-gated tools; gate trigger-elicitation-request on form mode
2 parents 8696907 + 88ae87f commit 2ffc55c

8 files changed

Lines changed: 223 additions & 48 deletions
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+
Server instructions no longer tell every client to use tools that are registered only for clients declaring a capability; they list those tools and the capability each needs (#4792, ported from #4835). `trigger-elicitation-request` and `trigger-elicitation-request-async` are now registered only for clients that support form-mode elicitation, so a client that declared only URL-mode elicitation no longer sees tools it cannot answer (#4985).

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

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe("Registration Index Files", () => {
5858
server: {
5959
getClientCapabilities: vi.fn(() => ({
6060
roots: {},
61-
elicitation: { url: {} },
61+
elicitation: { form: {}, url: {} },
6262
sampling: {},
6363
})),
6464
},
@@ -128,6 +128,32 @@ describe("Registration Index Files", () => {
128128
// Should not register any capability-gated tools when capabilities are missing
129129
expect(mockServerNoCapabilities.registerTool).not.toHaveBeenCalled();
130130
});
131+
132+
it("should register no form-mode elicitation tool for a URL-only client, even with task support", () => {
133+
// Both elicitation tools send form-mode requests, which a client that
134+
// declared only `elicitation.url` cannot answer (#4985).
135+
const mockServerUrlOnly = {
136+
registerTool: vi.fn(),
137+
server: {
138+
getClientCapabilities: vi.fn(() => ({
139+
elicitation: { url: {} },
140+
tasks: { requests: { elicitation: { create: {} } } },
141+
})),
142+
},
143+
experimental: {
144+
tasks: {
145+
registerToolTask: vi.fn(),
146+
},
147+
},
148+
} as unknown as McpServer; // partial mock: McpServer's private members rule out a structural literal
149+
150+
registerConditionalTools(mockServerUrlOnly);
151+
152+
const registeredTools = vi
153+
.mocked(mockServerUrlOnly.registerTool)
154+
.mock.calls.map((call) => call[0]);
155+
expect(registeredTools).toEqual(["trigger-url-elicitation"]);
156+
});
131157
});
132158

133159
describe("prompts/index.ts", () => {
@@ -149,6 +175,104 @@ describe("Registration Index Files", () => {
149175
});
150176
});
151177

178+
describe("instructions vs. capability-gated tools", () => {
179+
// The instructions are read once in the server factory and handed to the
180+
// McpServer constructor, before `oninitialized` runs and client capabilities
181+
// are known. They are therefore the same string for every client, while the
182+
// tools registered by registerConditionalTools are not. These tests pin the
183+
// two together.
184+
185+
// Every capability the conditional tools gate on, so the "all capabilities"
186+
// registration below is the full set.
187+
const allCapabilities = {
188+
roots: {},
189+
sampling: {},
190+
elicitation: { form: {}, url: {} },
191+
tasks: {
192+
requests: {
193+
sampling: { createMessage: {} },
194+
elicitation: { create: {} },
195+
},
196+
},
197+
};
198+
199+
const registeredWith = (capabilities: object): string[] => {
200+
const mockServer = {
201+
registerTool: vi.fn(),
202+
server: {
203+
getClientCapabilities: vi.fn(() => capabilities),
204+
},
205+
experimental: {
206+
tasks: {
207+
registerToolTask: vi.fn(),
208+
},
209+
},
210+
} as unknown as McpServer; // partial mock: McpServer's private members rule out a structural literal
211+
212+
registerConditionalTools(mockServer);
213+
214+
const viaRegisterTool = vi
215+
.mocked(mockServer.registerTool)
216+
.mock.calls.map((call) => call[0]);
217+
const viaRegisterToolTask = vi
218+
.mocked(mockServer.experimental.tasks.registerToolTask)
219+
.mock.calls.map((call) => call[0]);
220+
return [...viaRegisterTool, ...viaRegisterToolTask];
221+
};
222+
223+
// A tool is capability-gated if declaring the capabilities makes it appear.
224+
// Deriving it as a difference rather than hard-coding a list means a tool
225+
// that stops being gated drops out of these assertions on its own.
226+
const gatedTools = (): string[] => {
227+
const withAll = registeredWith(allCapabilities);
228+
const withNone = registeredWith({});
229+
return withAll.filter((name) => !withNone.includes(name)).sort();
230+
};
231+
232+
// The entries of the "Capability-Gated Tools" list, by tool name.
233+
const documentedTools = (instructions: string): string[] => {
234+
const section = instructions
235+
.split(/^## /m)
236+
.find((part) => part.startsWith("Capability-Gated Tools"));
237+
expect(
238+
section,
239+
'instructions.md has no "Capability-Gated Tools" section',
240+
).toBeDefined();
241+
return [...(section ?? "").matchAll(/^- `([a-z0-9-]+)`:/gm)]
242+
.map((match) => match[1])
243+
.sort();
244+
};
245+
246+
it("documents exactly the tools that client capabilities gate", () => {
247+
// A tool registered only when a capability is declared is absent from
248+
// tools/list for every other client, so an agent told to use it has
249+
// nothing to call. The instructions must name the same set the gates do.
250+
expect(documentedTools(readInstructions())).toEqual(gatedTools());
251+
});
252+
253+
it("never tells an agent to use a capability-gated tool unconditionally", () => {
254+
const instructions = readInstructions();
255+
const gated = gatedTools();
256+
257+
// Lines in the gated section are already qualified by the section itself.
258+
const sections = instructions.split(/^## /m);
259+
const otherLines = sections
260+
.filter((part) => !part.startsWith("Capability-Gated Tools"))
261+
.flatMap((part) => part.split("\n"));
262+
263+
const unconditional = otherLines.filter(
264+
(line) =>
265+
gated.some((name) => line.includes(`\`${name}\``)) &&
266+
!/\bif\b/i.test(line),
267+
);
268+
269+
expect(
270+
unconditional,
271+
"mention a gated tool without saying it may be absent",
272+
).toEqual([]);
273+
});
274+
});
275+
152276
describe("resources/index.ts", () => {
153277
it("should register resource templates", () => {
154278
const mockServer = createMockServer();

‎src/everything/__tests__/sampling-elicitation.test.ts‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -388,27 +388,18 @@ describe("trigger-elicitation-request", () => {
388388
expect(texts[0]).toBe("⚠️ User cancelled the elicitation dialog.");
389389
});
390390

391-
// KNOWN BUG #4985: trigger-elicitation-request is listed for a URL-only elicitation client, whose call then fails; the fix changes this assertion.
392-
it("is listed for a URL-only client, which then cannot answer its form request", async () => {
393-
// Characterization: the tool is gated on `elicitation` alone, so a
394-
// client that declared only URL mode sees it and the call fails.
395-
const { s } = await connectElicitation(() => ({ action: "cancel" }), {
396-
elicitation: { url: {} },
397-
});
391+
it("is not listed for a URL-only client, which cannot answer a form request (#4985)", async () => {
392+
const { s, asked } = await connectElicitation(
393+
() => ({ action: "cancel" }),
394+
{ elicitation: { url: {} } },
395+
);
398396
session = s;
399-
const result = await s.client.callTool({
400-
name: "trigger-elicitation-request",
401-
arguments: {},
402-
});
403-
expect(result).toEqual({
404-
isError: true,
405-
content: [
406-
{
407-
type: "text",
408-
text: "MCP error -32602: MCP error -32602: Client does not support form-mode elicitation requests",
409-
},
410-
],
411-
});
397+
const { tools } = await s.client.listTools();
398+
expect(tools.map((t) => t.name)).not.toContain(
399+
"trigger-elicitation-request",
400+
);
401+
expect(tools.map((t) => t.name)).toContain("trigger-url-elicitation");
402+
expect(asked).toEqual([]);
412403
});
413404
});
414405

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

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,41 @@ describe("initialize", () => {
6363
);
6464
});
6565

66-
// KNOWN BUG #4792: pins current (wrong) behavior; the fix changes this assertion.
67-
it("names capability-gated tools in its instructions even for a client that cannot see them (#4792)", async () => {
68-
// Characterization of #4792: the instructions are static, so a client
69-
// that declares no sampling, elicitation or roots capability is told
70-
// about tools that are not in its tools/list. The fix changes this test.
66+
it("qualifies every capability-gated tool it names, for a client that cannot see them (#4792)", async () => {
67+
// The instructions are fixed before client capabilities are known, so a
68+
// client that declares no sampling, elicitation or roots capability still
69+
// receives them. Each gated tool it names may appear only in the
70+
// "Capability-Gated Tools" section or on a line that makes it conditional.
7171
session = await connect();
7272
const instructions = session.client.getInstructions() ?? "";
7373
const { tools } = await session.client.listTools();
7474
const names = tools.map((t) => t.name);
75-
76-
for (const gated of [
77-
"trigger-sampling-request",
78-
"trigger-elicitation-request",
75+
const gated = [
7976
"get-roots-list",
80-
]) {
81-
expect(instructions).toContain(`\`${gated}\``);
82-
expect(names).not.toContain(gated);
77+
"trigger-elicitation-request",
78+
"trigger-elicitation-request-async",
79+
"trigger-sampling-request",
80+
"trigger-sampling-request-async",
81+
"trigger-url-elicitation",
82+
];
83+
84+
const sections = instructions.split(/^## /m);
85+
const gatedSection =
86+
sections.find((part) => part.startsWith("Capability-Gated Tools")) ?? "";
87+
const unconditional = sections
88+
.filter((part) => part !== gatedSection)
89+
.flatMap((part) => part.split("\n"))
90+
.filter(
91+
(line) =>
92+
gated.some((tool) => line.includes(`\`${tool}\``)) &&
93+
!/\bif\b/i.test(line),
94+
);
95+
96+
for (const tool of gated) {
97+
expect(names).not.toContain(tool);
98+
expect(gatedSection).toContain(`\`${tool}\``);
8399
}
100+
expect(unconditional).toEqual([]);
84101
});
85102
});
86103

@@ -111,8 +128,11 @@ describe("tools/list", () => {
111128
[{ roots: {} }, ["get-roots-list"]],
112129
[{ sampling: {} }, ["trigger-sampling-request"]],
113130
[{ elicitation: {} }, ["trigger-elicitation-request"]],
131+
[{ elicitation: { form: {} } }, ["trigger-elicitation-request"]],
132+
// A URL-only client cannot answer the form request (#4985).
133+
[{ elicitation: { url: {} } }, ["trigger-url-elicitation"]],
114134
[
115-
{ elicitation: { url: {} } },
135+
{ elicitation: { form: {}, url: {} } },
116136
["trigger-elicitation-request", "trigger-url-elicitation"],
117137
],
118138
[
@@ -126,6 +146,22 @@ describe("tools/list", () => {
126146
{ elicitation: {}, tasks: { requests: { elicitation: { create: {} } } } },
127147
["trigger-elicitation-request", "trigger-elicitation-request-async"],
128148
],
149+
[
150+
{
151+
elicitation: { form: {} },
152+
tasks: { requests: { elicitation: { create: {} } } },
153+
},
154+
["trigger-elicitation-request", "trigger-elicitation-request-async"],
155+
],
156+
// The async tool's request is form mode too, so a URL-only client does
157+
// not get it even with task support (#4985).
158+
[
159+
{
160+
elicitation: { url: {} },
161+
tasks: { requests: { elicitation: { create: {} } } },
162+
},
163+
["trigger-url-elicitation"],
164+
],
129165
// The async tools need the base capability as well as the task one.
130166
[{ tasks: { requests: { sampling: { createMessage: {} } } } }, []],
131167
[{ tasks: { requests: { elicitation: { create: {} } } } }, []],

‎src/everything/docs/features.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
- `trigger-long-running-operation` (tools/trigger-long-running-operation.ts): Simulates a multi-step operation over a given `duration` and number of `steps`; reports progress via `notifications/progress` when a `progressToken` is provided by the client.
2323
- `toggle-simulated-logging` (tools/toggle-simulated-logging.ts): Starts or stops simulated, random‑leveled logging for the invoking session. Respects the client’s selected minimum logging level.
2424
- `toggle-subscriber-updates` (tools/toggle-subscriber-updates.ts): Starts or stops simulated resource update notifications for URIs the invoking session has subscribed to.
25-
- `trigger-elicitation-request` (tools/trigger-elicitation-request.ts): Issues an `elicitation/create` request using form-mode fields (strings, numbers, booleans, enums, and format validation) and returns the resulting action/content.
25+
- `trigger-elicitation-request` (tools/trigger-elicitation-request.ts): Issues an `elicitation/create` request using form-mode fields (strings, numbers, booleans, enums, and format validation) and returns the resulting action/content. Requires client form-mode elicitation: `elicitation.form`, or an `elicitation` object that declares neither mode.
2626
- `trigger-url-elicitation` (tools/trigger-url-elicitation.ts): Issues an `elicitation/create` request in URL mode (`mode: "url"`) with an `elicitationId`, or throws MCP error `-32042` (`UrlElicitationRequiredError`) when `errorPath=true`. On the error path the prerequisite elicitation it returns points at a different URL than the failing request (`https://modelcontextprotocol.io`); when the client satisfies it and retries the same call, the retry ignores `errorPath` and proceeds via the request path, so the client does not loop on the same error. The retry marker is one-shot per `(session, url, elicitationId)`: it is cleared on the recognized retry, so re-running the error path with identical arguments without an intervening prerequisite is treated as a retry and proceeds. Requires client capability `elicitation.url`.
2727
- `trigger-sampling-request` (tools/trigger-sampling-request.ts): Issues a `sampling/createMessage` request to the client/LLM using provided `prompt` and optional generation controls; returns the LLM's response payload.
2828
- `simulate-research-query` (tools/simulate-research-query.ts): Demonstrates MCP Tasks (SEP-1686) with a simulated multi-stage research operation. Accepts `topic` and `ambiguous` parameters. Returns a task that progresses through stages with status updates. If `ambiguous` is true and client supports elicitation, sends an elicitation request directly to gather clarification before completing.
2929
- `trigger-sampling-request-async` (tools/trigger-sampling-request-async.ts): Demonstrates bidirectional tasks where the server sends a sampling request that the client executes as a background task. Server polls for status and retrieves the LLM result when complete. Requires client to support `tasks.requests.sampling.createMessage`.
30-
- `trigger-elicitation-request-async` (tools/trigger-elicitation-request-async.ts): Demonstrates bidirectional tasks where the server sends an elicitation request that the client executes as a background task. Server polls while waiting for user input. Requires client to support `tasks.requests.elicitation.create`.
30+
- `trigger-elicitation-request-async` (tools/trigger-elicitation-request-async.ts): Demonstrates bidirectional tasks where the server sends an elicitation request that the client executes as a background task. Server polls while waiting for user input. Requires client form-mode elicitation (as for `trigger-elicitation-request`) and `tasks.requests.elicitation.create`.
3131

3232
## Prompts
3333

@@ -85,7 +85,7 @@ The server advertises support for MCP Tasks, enabling long-running operations wi
8585
Use the `simulate-research-query` tool to exercise the full task lifecycle. Set `ambiguous: true` to trigger elicitation - the server will send an `elicitation/create` request directly and await the response before completing.
8686

8787
**Client-side tasks (server calls client):**
88-
Use `trigger-sampling-request-async` or `trigger-elicitation-request-async` to demonstrate bidirectional tasks where the server sends requests that the client executes as background tasks. These require the client to advertise `tasks.requests.sampling.createMessage` or `tasks.requests.elicitation.create` capabilities respectively.
88+
Use `trigger-sampling-request-async` or `trigger-elicitation-request-async` to demonstrate bidirectional tasks where the server sends requests that the client executes as background tasks. These require the client to advertise `sampling` with `tasks.requests.sampling.createMessage`, or form-mode `elicitation` with `tasks.requests.elicitation.create`, respectively.
8989

9090
### Bidirectional Task Flow
9191

‎src/everything/docs/instructions.md‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Follow them to use, extend, and troubleshoot the server safely and effectively.
55

66
## Cross-Feature Relationships
77

8-
- Use `get-roots-list` to see client workspace roots before file operations
8+
- If `get-roots-list` is in your tool list, use it to see client workspace roots before file operations
99
- `gzip-file-as-resource` creates session-scoped resources accessible only during the current session
1010
- Enable `toggle-simulated-logging` before debugging to see server log messages
1111
- Enable `toggle-subscriber-updates` to receive periodic resource update notifications
@@ -14,14 +14,23 @@ Follow them to use, extend, and troubleshoot the server safely and effectively.
1414

1515
- `gzip-file-as-resource`: Max fetch size controlled by `GZIP_MAX_FETCH_SIZE` (default 10MB), timeout by `GZIP_MAX_FETCH_TIME_MILLIS` (default 30s), allowed domains by `GZIP_ALLOWED_DOMAINS`
1616
- Session resources are ephemeral and lost when the session ends
17-
- Sampling requests (`trigger-sampling-request`) require client sampling capability
18-
- Elicitation requests (`trigger-elicitation-request`) require client elicitation capability
17+
18+
## Capability-Gated Tools
19+
20+
Registered only if your client declared the capability shown. Otherwise they are absent from `tools/list`, so do not call them:
21+
22+
- `get-roots-list`: `roots`
23+
- `trigger-sampling-request`: `sampling`
24+
- `trigger-elicitation-request`: `elicitation.form`, or `elicitation` with no mode
25+
- `trigger-url-elicitation`: `elicitation.url`
26+
- `trigger-sampling-request-async`: `sampling` and `tasks.requests.sampling.createMessage`
27+
- `trigger-elicitation-request-async`: form-mode `elicitation` and `tasks.requests.elicitation.create`
1928

2029
## Operational Patterns
2130

2231
- For long operations, use `trigger-long-running-operation` which sends progress notifications
2332
- Prefer reading resources before calling mutating tools
24-
- Check `get-roots-list` output to understand the client's workspace context
33+
- If `get-roots-list` is in your tool list, check its output to understand the client's workspace context
2534

2635
## Easter Egg
2736

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ export const registerTriggerElicitationRequestAsyncTool = (
4343
// Check client capabilities
4444
const clientCapabilities = server.server.getClientCapabilities() || {};
4545

46-
// Client must support elicitation AND tasks.requests.elicitation
46+
// Client must support form-mode elicitation AND tasks.requests.elicitation.
47+
// The request is form mode, so a URL-only client cannot answer it (#4985):
48+
// form mode is `elicitation.form`, or `elicitation` with neither mode.
49+
const elicitation = clientCapabilities.elicitation;
4750
const clientSupportsElicitation =
48-
clientCapabilities.elicitation !== undefined;
51+
elicitation !== undefined &&
52+
(elicitation.form !== undefined || elicitation.url === undefined);
4953
const clientTasksCapability = clientCapabilities.tasks as
5054
| {
5155
requests?: { elicitation?: { create?: object } };

0 commit comments

Comments
 (0)