Skip to content

Commit bf5fbd1

Browse files
committed
Simplify back-compat for form elicitation requests
1 parent d0990a4 commit bf5fbd1

File tree

5 files changed

+75
-36
lines changed

5 files changed

+75
-36
lines changed

src/client/index.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
CallToolRequestSchema,
1515
CreateMessageRequestSchema,
1616
ElicitRequestSchema,
17+
ElicitResultSchema,
1718
ListRootsRequestSchema,
1819
ErrorCode
1920
} from '../types.js';
@@ -917,6 +918,64 @@ test('should reject form-mode elicitation when client only supports URL mode', a
917918
await client.close();
918919
});
919920

921+
test('should reject missing-mode elicitation when client only supports URL mode', async () => {
922+
const server = new Server(
923+
{
924+
name: 'test server',
925+
version: '1.0'
926+
},
927+
{
928+
capabilities: {}
929+
}
930+
);
931+
932+
const client = new Client(
933+
{
934+
name: 'test client',
935+
version: '1.0'
936+
},
937+
{
938+
capabilities: {
939+
elicitation: {
940+
url: {}
941+
}
942+
}
943+
}
944+
);
945+
946+
const handler = vi.fn().mockResolvedValue({
947+
action: 'cancel'
948+
});
949+
client.setRequestHandler(ElicitRequestSchema, handler);
950+
951+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
952+
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);
953+
954+
await expect(
955+
server.request(
956+
{
957+
method: 'elicitation/create',
958+
params: {
959+
message: 'Please provide data',
960+
requestedSchema: {
961+
type: 'object',
962+
properties: {
963+
username: {
964+
type: 'string'
965+
}
966+
}
967+
}
968+
}
969+
},
970+
ElicitResultSchema
971+
)
972+
).rejects.toThrow('Client does not support form-mode elicitation requests');
973+
974+
expect(handler).not.toHaveBeenCalled();
975+
976+
await Promise.all([client.close(), server.close()]);
977+
});
978+
920979
test('should reject URL-mode elicitation when client only supports form mode', async () => {
921980
const client = new Client(
922981
{

src/client/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,14 @@ export class Client<
267267
}
268268

269269
const { params } = validatedRequest.data;
270+
const mode = params.mode ?? 'form';
270271
const { supportsFormMode, supportsUrlMode } = getSupportedElicitationModes(this._capabilities.elicitation);
271272

272-
if (params.mode === 'form' && !supportsFormMode) {
273+
if (mode === 'form' && !supportsFormMode) {
273274
throw new McpError(ErrorCode.InvalidParams, 'Client does not support form-mode elicitation requests');
274275
}
275276

276-
if (params.mode === 'url' && !supportsUrlMode) {
277+
if (mode === 'url' && !supportsUrlMode) {
277278
throw new McpError(ErrorCode.InvalidParams, 'Client does not support URL-mode elicitation requests');
278279
}
279280

@@ -288,9 +289,9 @@ export class Client<
288289
}
289290

290291
const validatedResult = validationResult.data;
291-
const requestedSchema = params.mode === 'form' ? (params.requestedSchema as JsonSchemaType) : undefined;
292+
const requestedSchema = mode === 'form' ? (params.requestedSchema as JsonSchemaType) : undefined;
292293

293-
if (params.mode === 'form' && validatedResult.action === 'accept' && validatedResult.content && requestedSchema) {
294+
if (mode === 'form' && validatedResult.action === 'accept' && validatedResult.content && requestedSchema) {
294295
if (this._capabilities.elicitation?.form?.applyDefaults) {
295296
try {
296297
applyElicitationDefaults(requestedSchema, validatedResult.content);

src/server/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ test('should include form mode when sending elicitation form requests', async ()
700700

701701
const receivedModes: string[] = [];
702702
client.setRequestHandler(ElicitRequestSchema, request => {
703-
receivedModes.push(request.params.mode);
703+
receivedModes.push(request.params.mode ?? '');
704704
return {
705705
action: 'accept',
706706
content: {
@@ -764,7 +764,7 @@ test('should include url mode when sending elicitation URL requests', async () =
764764
const receivedModes: string[] = [];
765765
const receivedIds: string[] = [];
766766
client.setRequestHandler(ElicitRequestSchema, request => {
767-
receivedModes.push(request.params.mode);
767+
receivedModes.push(request.params.mode ?? '');
768768
if (request.params.mode === 'url') {
769769
receivedIds.push(request.params.elicitationId);
770770
}

src/server/index.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ import {
3535
import { AjvJsonSchemaValidator } from '../validation/ajv-provider.js';
3636
import type { JsonSchemaType, jsonSchemaValidator } from '../validation/types.js';
3737

38-
type LegacyElicitRequestFormParams = Omit<ElicitRequestFormParams, 'mode'>;
39-
4038
export type ServerOptions = ProtocolOptions & {
4139
/**
4240
* Capabilities to advertise as being supported by this server.
@@ -331,33 +329,13 @@ export class Server<
331329

332330
/**
333331
* Creates an elicitation request for the given parameters.
334-
* @param params The parameters for the form elicitation request (explicit mode: 'form').
332+
* For backwards compatibility, `mode` may be omitted for form requests and will default to `'form'`.
333+
* @param params The parameters for the elicitation request.
335334
* @param options Optional request options.
336335
* @returns The result of the elicitation request.
337336
*/
338-
async elicitInput(params: ElicitRequestFormParams, options?: RequestOptions): Promise<ElicitResult>;
339-
/**
340-
* Creates an elicitation request for the given parameters.
341-
* @param params The parameters for the URL elicitation request (with url and elicitationId).
342-
* @param options Optional request options.
343-
* @returns The result of the elicitation request.
344-
*/
345-
async elicitInput(params: ElicitRequestURLParams, options?: RequestOptions): Promise<ElicitResult>;
346-
/**
347-
* Creates an elicitation request for the given parameters.
348-
* @deprecated Use the overloads with explicit `mode: 'form' | 'url'` instead.
349-
* @param params The parameters for the form elicitation request (legacy signature without mode).
350-
* @param options Optional request options.
351-
* @returns The result of the elicitation request.
352-
*/
353-
async elicitInput(params: LegacyElicitRequestFormParams, options?: RequestOptions): Promise<ElicitResult>;
354-
355-
// Implementation (not visible to callers)
356-
async elicitInput(
357-
params: LegacyElicitRequestFormParams | ElicitRequestFormParams | ElicitRequestURLParams,
358-
options?: RequestOptions
359-
): Promise<ElicitResult> {
360-
const mode = ('mode' in params ? params.mode : 'form') as 'form' | 'url';
337+
async elicitInput(params: ElicitRequestFormParams | ElicitRequestURLParams, options?: RequestOptions): Promise<ElicitResult> {
338+
const mode = (params.mode ?? 'form') as 'form' | 'url';
361339

362340
switch (mode) {
363341
case 'url': {
@@ -372,10 +350,9 @@ export class Server<
372350
if (!this._clientCapabilities?.elicitation?.form) {
373351
throw new Error('Client does not support form elicitation.');
374352
}
353+
375354
const formParams: ElicitRequestFormParams =
376-
'mode' in params
377-
? (params as ElicitRequestFormParams)
378-
: ({ ...(params as LegacyElicitRequestFormParams), mode: 'form' } as ElicitRequestFormParams);
355+
params.mode === 'form' ? (params as ElicitRequestFormParams) : { ...(params as ElicitRequestFormParams), mode: 'form' };
379356

380357
const result = await this.request({ method: 'elicitation/create', params: formParams }, ElicitResultSchema, options);
381358

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1479,8 +1479,10 @@ export const PrimitiveSchemaDefinitionSchema = z.union([EnumSchemaSchema, Boolea
14791479
export const ElicitRequestFormParamsSchema = BaseRequestParamsSchema.extend({
14801480
/**
14811481
* The elicitation mode.
1482+
*
1483+
* Optional for backward compatibility. Clients MUST treat missing mode as "form".
14821484
*/
1483-
mode: z.literal('form'),
1485+
mode: z.literal('form').optional(),
14841486
/**
14851487
* The message to present to the user describing what information is being requested.
14861488
*/

0 commit comments

Comments
 (0)