Skip to content

Commit 3a32d10

Browse files
evalstatecliffhallclaude
authored
Add URL Elicitation cases (#3334)
* url elicitation * single tool for normal and error path * Address review feedback on URL elicitation tool - Remove src/everything/pnpm-lock.yaml (monorepo uses npm workspaces) - Drop redundant TriggerUrlElicitationSchema.parse(args) in the handler; destructure directly from args like the other tools, since the SDK validates against the registered schema before invoking the handler - Add explicit accept/decline/cancel messaging in the request path, mirroring trigger-elicitation-request.ts - Clarify the registerTool count comment in registrations.test.ts (task-based tools register via registerToolTask, counted separately) - Add tests covering the no-url / undefined-capability registration guards, the randomUUID() elicitationId fallback, and the decline/cancel response paths (100% coverage of the tool) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Add tool annotations to trigger-url-elicitation Match the annotations block on sibling elicitation tools and the CLAUDE.md guidance. Uses openWorldHint: true since the tool drives an external browser flow. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: cliffhall <cliff@futurescale.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 64b1cb0 commit 3a32d10

6 files changed

Lines changed: 410 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Registration Index Files', () => {
5454
server: {
5555
getClientCapabilities: vi.fn(() => ({
5656
roots: {},
57-
elicitation: {},
57+
elicitation: { url: {} },
5858
sampling: {},
5959
})),
6060
},
@@ -67,14 +67,17 @@ describe('Registration Index Files', () => {
6767

6868
registerConditionalTools(mockServerWithCapabilities);
6969

70-
// Should register 3 conditional tools + 3 task-based tools when all capabilities present
71-
expect(mockServerWithCapabilities.registerTool).toHaveBeenCalledTimes(3);
70+
// Should register 4 conditional tools via registerTool when all capabilities
71+
// are present. Task-based tools register via registerToolTask (counted separately),
72+
// so they are not included in this registerTool count.
73+
expect(mockServerWithCapabilities.registerTool).toHaveBeenCalledTimes(4);
7274

7375
const registeredTools = (
7476
mockServerWithCapabilities.registerTool as any
7577
).mock.calls.map((call: any[]) => call[0]);
7678
expect(registeredTools).toContain('get-roots-list');
7779
expect(registeredTools).toContain('trigger-elicitation-request');
80+
expect(registeredTools).toContain('trigger-url-elicitation');
7881
expect(registeredTools).toContain('trigger-sampling-request');
7982

8083
// Task-based tools are registered via experimental.tasks.registerToolTask

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

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { registerToggleSimulatedLoggingTool } from '../tools/toggle-simulated-lo
1313
import { registerToggleSubscriberUpdatesTool } from '../tools/toggle-subscriber-updates.js';
1414
import { registerTriggerSamplingRequestTool } from '../tools/trigger-sampling-request.js';
1515
import { registerTriggerElicitationRequestTool } from '../tools/trigger-elicitation-request.js';
16+
import { registerTriggerUrlElicitationTool } from '../tools/trigger-url-elicitation.js';
1617
import { registerGetRootsListTool } from '../tools/get-roots-list.js';
1718
import { registerGZipFileAsResourceTool } from '../tools/gzip-file-as-resource.js';
1819

@@ -706,6 +707,251 @@ describe('Tools', () => {
706707
});
707708
});
708709

710+
describe('trigger-url-elicitation', () => {
711+
it('should not register when client does not support URL elicitation', () => {
712+
const handlers: Map<string, Function> = new Map();
713+
const mockServer = {
714+
registerTool: vi.fn((name: string, config: any, handler: Function) => {
715+
handlers.set(name, handler);
716+
}),
717+
server: {
718+
getClientCapabilities: vi.fn(() => ({ elicitation: { form: {} } })),
719+
},
720+
} as unknown as McpServer;
721+
722+
registerTriggerUrlElicitationTool(mockServer);
723+
724+
expect(mockServer.registerTool).not.toHaveBeenCalled();
725+
});
726+
727+
it('should register when client supports URL elicitation', () => {
728+
const handlers: Map<string, Function> = new Map();
729+
const mockServer = {
730+
registerTool: vi.fn((name: string, config: any, handler: Function) => {
731+
handlers.set(name, handler);
732+
}),
733+
server: {
734+
getClientCapabilities: vi.fn(() => ({ elicitation: { url: {} } })),
735+
},
736+
} as unknown as McpServer;
737+
738+
registerTriggerUrlElicitationTool(mockServer);
739+
740+
expect(mockServer.registerTool).toHaveBeenCalledWith(
741+
'trigger-url-elicitation',
742+
expect.objectContaining({
743+
title: 'Trigger URL Elicitation Tool',
744+
description: expect.stringContaining('URL elicitation'),
745+
}),
746+
expect.any(Function)
747+
);
748+
});
749+
750+
it('should send URL-mode elicitation request when errorPath is false', async () => {
751+
const handlers: Map<string, Function> = new Map();
752+
const mockSendRequest = vi.fn().mockResolvedValue({
753+
action: 'accept',
754+
});
755+
756+
const mockServer = {
757+
registerTool: vi.fn((name: string, config: any, handler: Function) => {
758+
handlers.set(name, handler);
759+
}),
760+
server: {
761+
getClientCapabilities: vi.fn(() => ({ elicitation: { url: {} } })),
762+
},
763+
} as unknown as McpServer;
764+
765+
registerTriggerUrlElicitationTool(mockServer);
766+
767+
const handler = handlers.get('trigger-url-elicitation')!;
768+
const result = await handler(
769+
{
770+
url: 'https://example.com/verify',
771+
message: 'Open this page to verify your identity',
772+
elicitationId: 'elicitation-123',
773+
errorPath: false,
774+
},
775+
{ sendRequest: mockSendRequest }
776+
);
777+
778+
expect(mockSendRequest).toHaveBeenCalledWith(
779+
expect.objectContaining({
780+
method: 'elicitation/create',
781+
params: expect.objectContaining({
782+
mode: 'url',
783+
url: 'https://example.com/verify',
784+
message: 'Open this page to verify your identity',
785+
elicitationId: 'elicitation-123',
786+
}),
787+
}),
788+
expect.anything(),
789+
expect.anything()
790+
);
791+
792+
expect(result.content[0].text).toContain(
793+
'✅ User completed the URL elicitation flow.'
794+
);
795+
});
796+
797+
it('should not register when client has no elicitation capability at all', () => {
798+
const mockServer = {
799+
registerTool: vi.fn(),
800+
server: {
801+
getClientCapabilities: vi.fn(() => ({})),
802+
},
803+
} as unknown as McpServer;
804+
805+
registerTriggerUrlElicitationTool(mockServer);
806+
807+
expect(mockServer.registerTool).not.toHaveBeenCalled();
808+
});
809+
810+
it('should not register when client capabilities are undefined', () => {
811+
const mockServer = {
812+
registerTool: vi.fn(),
813+
server: {
814+
getClientCapabilities: vi.fn(() => undefined),
815+
},
816+
} as unknown as McpServer;
817+
818+
registerTriggerUrlElicitationTool(mockServer);
819+
820+
expect(mockServer.registerTool).not.toHaveBeenCalled();
821+
});
822+
823+
it('should default the elicitationId to a random UUID when omitted', async () => {
824+
const handlers: Map<string, Function> = new Map();
825+
const mockSendRequest = vi.fn().mockResolvedValue({
826+
action: 'accept',
827+
});
828+
829+
const mockServer = {
830+
registerTool: vi.fn((name: string, config: any, handler: Function) => {
831+
handlers.set(name, handler);
832+
}),
833+
server: {
834+
getClientCapabilities: vi.fn(() => ({ elicitation: { url: {} } })),
835+
},
836+
} as unknown as McpServer;
837+
838+
registerTriggerUrlElicitationTool(mockServer);
839+
840+
const handler = handlers.get('trigger-url-elicitation')!;
841+
await handler(
842+
{
843+
url: 'https://example.com/verify',
844+
message: 'Open this page to verify your identity',
845+
errorPath: false,
846+
},
847+
{ sendRequest: mockSendRequest }
848+
);
849+
850+
const sentParams = mockSendRequest.mock.calls[0][0].params;
851+
expect(sentParams.elicitationId).toMatch(
852+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
853+
);
854+
});
855+
856+
it('should report a declined URL elicitation', async () => {
857+
const handlers: Map<string, Function> = new Map();
858+
const mockSendRequest = vi.fn().mockResolvedValue({ action: 'decline' });
859+
860+
const mockServer = {
861+
registerTool: vi.fn((name: string, config: any, handler: Function) => {
862+
handlers.set(name, handler);
863+
}),
864+
server: {
865+
getClientCapabilities: vi.fn(() => ({ elicitation: { url: {} } })),
866+
},
867+
} as unknown as McpServer;
868+
869+
registerTriggerUrlElicitationTool(mockServer);
870+
871+
const handler = handlers.get('trigger-url-elicitation')!;
872+
const result = await handler(
873+
{
874+
url: 'https://example.com/verify',
875+
message: 'Open this page to verify your identity',
876+
elicitationId: 'elicitation-123',
877+
errorPath: false,
878+
},
879+
{ sendRequest: mockSendRequest }
880+
);
881+
882+
expect(result.content[0].text).toContain('❌ User declined to open the URL');
883+
});
884+
885+
it('should report a cancelled URL elicitation', async () => {
886+
const handlers: Map<string, Function> = new Map();
887+
const mockSendRequest = vi.fn().mockResolvedValue({ action: 'cancel' });
888+
889+
const mockServer = {
890+
registerTool: vi.fn((name: string, config: any, handler: Function) => {
891+
handlers.set(name, handler);
892+
}),
893+
server: {
894+
getClientCapabilities: vi.fn(() => ({ elicitation: { url: {} } })),
895+
},
896+
} as unknown as McpServer;
897+
898+
registerTriggerUrlElicitationTool(mockServer);
899+
900+
const handler = handlers.get('trigger-url-elicitation')!;
901+
const result = await handler(
902+
{
903+
url: 'https://example.com/verify',
904+
message: 'Open this page to verify your identity',
905+
elicitationId: 'elicitation-123',
906+
errorPath: false,
907+
},
908+
{ sendRequest: mockSendRequest }
909+
);
910+
911+
expect(result.content[0].text).toContain(
912+
'⚠️ User cancelled the URL elicitation'
913+
);
914+
});
915+
916+
it('should throw MCP error -32042 with required URL elicitation data when errorPath is true', async () => {
917+
const handlers: Map<string, Function> = new Map();
918+
const mockServer = {
919+
registerTool: vi.fn((name: string, config: any, handler: Function) => {
920+
handlers.set(name, handler);
921+
}),
922+
server: {
923+
getClientCapabilities: vi.fn(() => ({ elicitation: { url: {} } })),
924+
},
925+
} as unknown as McpServer;
926+
927+
registerTriggerUrlElicitationTool(mockServer);
928+
929+
const handler = handlers.get('trigger-url-elicitation')!;
930+
931+
expect.assertions(2);
932+
933+
try {
934+
await handler(
935+
{
936+
url: 'https://example.com/connect',
937+
message: 'Authorization is required to continue.',
938+
elicitationId: 'elicitation-xyz',
939+
errorPath: true,
940+
},
941+
{}
942+
);
943+
} catch (error: any) {
944+
expect(error.code).toBe(-32042);
945+
expect(error.data.elicitations[0]).toEqual({
946+
mode: 'url',
947+
url: 'https://example.com/connect',
948+
message: 'Authorization is required to continue.',
949+
elicitationId: 'elicitation-xyz',
950+
});
951+
}
952+
});
953+
});
954+
709955
describe('get-roots-list', () => {
710956
it('should not register when client does not support roots', () => {
711957
const { mockServer } = createMockServer();

‎src/everything/docs/features.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
- `get-structured-content` (tools/get-structured-content.ts): Demonstrates structured responses. Accepts `location` input and returns both backward‑compatible `content` (a `text` block containing JSON) and `structuredContent` validated by an `outputSchema` (temperature, conditions, humidity).
2020
- `get-sum` (tools/get-sum.ts): For two numbers `a` and `b` calculates and returns their sum. Uses Zod to validate inputs.
2121
- `get-tiny-image` (tools/get-tiny-image.ts): Returns a tiny PNG MCP logo as an `image` content item with brief descriptive text before and after.
22-
- `trigger-long-running-operation` (tools/trigger-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.
22+
- `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.
26+
- `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`. Requires client capability `elicitation.url`.
2527
- `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.
2628
- `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.
2729
- `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`.

‎src/everything/docs/structure.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ src/everything
5555
│ ├── trigger-elicitation-request-async.ts
5656
│ ├── trigger-long-running-operation.ts
5757
│ ├── trigger-sampling-request.ts
58-
│ └── trigger-sampling-request-async.ts
58+
│ ├── trigger-sampling-request-async.ts
59+
│ └── trigger-url-elicitation.ts
5960
└── transports
6061
├── sse.ts
6162
├── stdio.ts
@@ -85,8 +86,8 @@ src/everything
8586

8687
- `architecture.md`
8788
- This document.
88-
- `server-instructions.md`
89-
- Human‑readable instructions intended to be passed to the client/LLM as for guidance on server use. Loaded by the server at startup and returned in the "initialize" exchange.
89+
- `instructions.md`
90+
- Human‑readable instructions intended to be passed to the client/LLM as guidance on server use. Loaded by the server at startup and returned in the initialize exchange.
9091

9192
### `prompts/`
9293

@@ -154,6 +155,8 @@ src/everything
154155
- Registers a `simulate-research-query` task-based tool that demonstrates the MCP Tasks feature (SEP-1686). Simulates a multi-stage research operation with progress updates. If the query is marked as ambiguous and the client supports elicitation, it pauses mid-execution to request clarification via `elicitation/create`. Uses `server.experimental.tasks.registerToolTask()` with `execution: { taskSupport: "required" }`.
155156
- `trigger-elicitation-request.ts`
156157
- Registers a `trigger-elicitation-request` tool that sends an `elicitation/create` request to the client/LLM and returns the elicitation result.
158+
- `trigger-url-elicitation.ts`
159+
- Registers a `trigger-url-elicitation` tool that either sends an out-of-band URL-mode `elicitation/create` request (`mode: "url"`) including an `elicitationId` (request path) or throws `UrlElicitationRequiredError` (`-32042`) for client-handled URL elicitation (error path).
157160
- `trigger-elicitation-request-async.ts`
158161
- Registers a `trigger-elicitation-request-async` tool that demonstrates bidirectional MCP tasks for elicitation. Sends an elicitation request with task metadata, then polls the client's `tasks/get` endpoint for completion status before fetching the final result.
159162
- `trigger-sampling-request.ts`

‎src/everything/tools/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { registerTriggerSamplingRequestTool } from "./trigger-sampling-request.j
1717
import { registerTriggerSamplingRequestAsyncTool } from "./trigger-sampling-request-async.js";
1818
import { registerTriggerElicitationRequestAsyncTool } from "./trigger-elicitation-request-async.js";
1919
import { registerSimulateResearchQueryTool } from "./simulate-research-query.js";
20+
import { registerTriggerUrlElicitationTool } from "./trigger-url-elicitation.js";
2021

2122
/**
2223
* Register the tools with the MCP server.
@@ -44,6 +45,7 @@ export const registerTools = (server: McpServer) => {
4445
export const registerConditionalTools = (server: McpServer) => {
4546
registerGetRootsListTool(server);
4647
registerTriggerElicitationRequestTool(server);
48+
registerTriggerUrlElicitationTool(server);
4749
registerTriggerSamplingRequestTool(server);
4850
// Task-based research tool (uses experimental tasks API)
4951
registerSimulateResearchQueryTool(server);

0 commit comments

Comments
 (0)