-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathelicitation-handler.ts
More file actions
353 lines (320 loc) · 11.7 KB
/
elicitation-handler.ts
File metadata and controls
353 lines (320 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import type { ExtensionUIContext } from "@earendil-works/pi-coding-agent";
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
import {
ElicitRequestSchema,
type ElicitRequest,
type ElicitRequestFormParams,
type ElicitRequestURLParams,
type ElicitResult,
} from "@modelcontextprotocol/sdk/types.js";
import open from "open";
export type ExtensionUIFormValue = string | number | boolean | string[] | undefined;
export interface ExtensionUIFormSelectOption {
value: string;
label?: string;
description?: string;
}
export type ExtensionUIFormField =
| {
type: "text";
name: string;
label: string;
description?: string;
placeholder?: string;
required?: boolean;
defaultValue?: string;
minLength?: number;
maxLength?: number;
pattern?: string;
}
| {
type: "number" | "integer";
name: string;
label: string;
description?: string;
required?: boolean;
defaultValue?: number;
minimum?: number;
maximum?: number;
}
| {
type: "boolean";
name: string;
label: string;
description?: string;
defaultValue?: boolean;
}
| {
type: "select";
name: string;
label: string;
description?: string;
required?: boolean;
options: ExtensionUIFormSelectOption[];
defaultValue?: string;
}
| {
type: "multiSelect";
name: string;
label: string;
description?: string;
required?: boolean;
options: ExtensionUIFormSelectOption[];
defaultValue?: string[];
};
export interface ExtensionUIFormRequest {
title: string;
message?: string;
fields: ExtensionUIFormField[];
submitLabel?: string;
secondaryLabel?: string;
cancelLabel?: string;
}
export type ExtensionUIFormResult =
| { action: "submit"; values: Record<string, ExtensionUIFormValue> }
| { action: "secondary" }
| { action: "cancel" };
export interface ElicitationUIContext extends ExtensionUIContext {
form(request: ExtensionUIFormRequest): Promise<ExtensionUIFormResult>;
}
export interface ElicitationHandlerOptions {
serverName: string;
ui: ElicitationUIContext;
autoOpenUrls: boolean;
}
export type ServerElicitationConfig = Omit<ElicitationHandlerOptions, "serverName">;
export function registerElicitationHandler(client: Client, options: ElicitationHandlerOptions): void {
client.setRequestHandler(ElicitRequestSchema, (request) => {
return handleElicitationRequest(options, request as ElicitRequest);
});
}
export async function handleElicitationRequest(
options: ElicitationHandlerOptions,
request: ElicitRequest,
): Promise<ElicitResult> {
const params = request.params;
if (params.mode === "url") {
return handleUrlElicitation(options, params);
}
return handleFormElicitation(options, params);
}
export async function handleFormElicitation(
options: ElicitationHandlerOptions,
params: ElicitRequestFormParams,
): Promise<ElicitResult> {
const form = convertMcpSchemaToPiForm(options.serverName, params);
const result = await options.ui.form(form);
if (result.action !== "submit") {
return convertPiFormResultToMcpResult(result);
}
return {
action: "accept",
content: coerceAndValidateFormValues(params, result.values),
};
}
export async function handleUrlElicitation(
options: ElicitationHandlerOptions,
params: ElicitRequestURLParams,
): Promise<ElicitResult> {
const browserUrl = getBrowserElicitationUrl(params.url);
if (!options.autoOpenUrls) {
const result = await options.ui.form({
title: "MCP Browser Request",
message: [
`Server: ${options.serverName}`,
"",
params.message,
"",
`Domain: ${browserUrl.host}`,
`URL: ${browserUrl.toString()}`,
"",
"Open this URL in your browser?",
].join("\n"),
fields: [],
submitLabel: "Open",
secondaryLabel: "Decline",
cancelLabel: "Cancel",
});
if (result.action === "secondary") return { action: "decline" };
if (result.action === "cancel") return { action: "cancel" };
}
await open(browserUrl.toString());
options.ui.notify("Opened browser for MCP elicitation.", "info");
return { action: "accept" };
}
export function convertMcpSchemaToPiForm(
serverName: string,
params: ElicitRequestFormParams,
): ExtensionUIFormRequest {
const required = new Set(params.requestedSchema.required ?? []);
return {
title: "MCP Input Request",
message: `Server: ${serverName}\n\n${params.message}`,
submitLabel: "Submit",
secondaryLabel: "Decline",
cancelLabel: "Cancel",
fields: Object.entries(params.requestedSchema.properties).map(([name, schema]): ExtensionUIFormField => {
const label = schema.title ?? humanizeName(name);
const base = {
name,
label,
description: schema.description,
required: required.has(name),
};
if (schema.type === "string" && "oneOf" in schema && Array.isArray(schema.oneOf)) {
return omitUndefined({
...base,
type: "select" as const,
options: schema.oneOf.map((option) => ({ value: option.const, label: option.title })),
defaultValue: schema.default,
});
}
if (schema.type === "string" && "enum" in schema && Array.isArray(schema.enum)) {
const enumNames = "enumNames" in schema && Array.isArray(schema.enumNames) ? schema.enumNames : undefined;
return omitUndefined({
...base,
type: "select" as const,
options: schema.enum.map((value, index) => omitUndefined({ value, label: enumNames?.[index] })),
defaultValue: schema.default,
});
}
if (schema.type === "array") {
return omitUndefined({
...base,
type: "multiSelect" as const,
options: extractMultiSelectOptions(schema),
defaultValue: schema.default,
});
}
if (schema.type === "number" || schema.type === "integer") {
return omitUndefined({
...base,
type: schema.type,
defaultValue: schema.default,
minimum: schema.minimum,
maximum: schema.maximum,
});
}
if (schema.type === "boolean") {
return omitUndefined({
type: "boolean" as const,
name,
label,
description: schema.description,
defaultValue: schema.default,
});
}
const stringSchema = schema as { default?: string; minLength?: number; maxLength?: number };
return omitUndefined({
...base,
type: "text" as const,
defaultValue: stringSchema.default,
minLength: stringSchema.minLength,
maxLength: stringSchema.maxLength,
});
}),
};
}
export function convertPiFormResultToMcpResult(result: ExtensionUIFormResult): ElicitResult {
if (result.action === "secondary") return { action: "decline" };
if (result.action === "cancel") return { action: "cancel" };
return { action: "accept", content: stripUndefined(result.values) as ElicitResult["content"] };
}
export function coerceAndValidateFormValues(
params: ElicitRequestFormParams,
values: Record<string, ExtensionUIFormValue>,
): Record<string, string | number | boolean | string[]> {
const output: Record<string, string | number | boolean | string[]> = {};
const required = new Set(params.requestedSchema.required ?? []);
for (const [name, schema] of Object.entries(params.requestedSchema.properties)) {
const raw = values[name] ?? schema.default;
if (raw === undefined || (raw === "" && schema.type !== "string")) {
if (required.has(name)) throw new Error(`Missing required elicitation field: ${name}`);
continue;
}
if (schema.type === "string") {
const stringSchema = schema as { minLength?: number; maxLength?: number };
const value = String(raw);
if (stringSchema.minLength !== undefined && value.length < stringSchema.minLength) {
throw new Error(`Elicitation field ${name} is shorter than minimum length ${stringSchema.minLength}`);
}
if (stringSchema.maxLength !== undefined && value.length > stringSchema.maxLength) {
throw new Error(`Elicitation field ${name} is longer than maximum length ${stringSchema.maxLength}`);
}
if ("enum" in schema && Array.isArray(schema.enum) && !schema.enum.includes(value)) {
throw new Error(`Elicitation field ${name} is not an allowed value`);
}
if ("oneOf" in schema && Array.isArray(schema.oneOf) && !schema.oneOf.some((option) => option.const === value)) {
throw new Error(`Elicitation field ${name} is not an allowed value`);
}
output[name] = value;
continue;
}
if (schema.type === "number" || schema.type === "integer") {
const value = typeof raw === "number" ? raw : Number(raw);
if (!Number.isFinite(value)) throw new Error(`Elicitation field ${name} must be a number`);
if (schema.type === "integer" && !Number.isInteger(value)) throw new Error(`Elicitation field ${name} must be an integer`);
if (schema.minimum !== undefined && value < schema.minimum) {
throw new Error(`Elicitation field ${name} is below minimum ${schema.minimum}`);
}
if (schema.maximum !== undefined && value > schema.maximum) {
throw new Error(`Elicitation field ${name} is above maximum ${schema.maximum}`);
}
output[name] = value;
continue;
}
if (schema.type === "boolean") {
output[name] = typeof raw === "boolean" ? raw : raw === "true";
continue;
}
if (schema.type === "array") {
if (!Array.isArray(raw)) throw new Error(`Elicitation field ${name} must be a list`);
const allowed = new Set(extractMultiSelectOptions(schema).map((option) => option.value));
const value = raw.map(String);
if (schema.minItems !== undefined && value.length < schema.minItems) {
throw new Error(`Elicitation field ${name} has fewer than ${schema.minItems} selections`);
}
if (schema.maxItems !== undefined && value.length > schema.maxItems) {
throw new Error(`Elicitation field ${name} has more than ${schema.maxItems} selections`);
}
for (const item of value) {
if (!allowed.has(item)) throw new Error(`Elicitation field ${name} contains an invalid selection`);
}
output[name] = value;
}
}
return output;
}
function extractMultiSelectOptions(schema: Extract<ElicitRequestFormParams["requestedSchema"]["properties"][string], { type: "array" }>): ExtensionUIFormSelectOption[] {
const items = schema.items as { enum?: string[]; anyOf?: Array<{ const: string; title: string }> };
if (Array.isArray(items.anyOf)) {
return items.anyOf.map((option) => ({ value: option.const, label: option.title }));
}
return (items.enum ?? []).map((value) => ({ value }));
}
function humanizeName(name: string): string {
return name
.replace(/[_-]+/g, " ")
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
.replace(/^./, (char) => char.toUpperCase());
}
function getBrowserElicitationUrl(url: string): URL {
const parsed = new URL(url);
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
throw new Error(`MCP URL elicitation only supports http/https URLs: ${parsed.protocol}`);
}
return parsed;
}
function stripUndefined(values: Record<string, ExtensionUIFormValue>): Record<string, string | number | boolean | string[]> {
const output: Record<string, string | number | boolean | string[]> = {};
for (const [key, value] of Object.entries(values)) {
if (value !== undefined) output[key] = value;
}
return output;
}
function omitUndefined<T extends Record<string, unknown>>(value: T): T {
for (const key of Object.keys(value)) {
if (value[key] === undefined) delete value[key];
}
return value;
}