keryx 0.45.0 → 0.46.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/classes/Connection.ts +107 -4
- package/classes/McpUrlElicitationRequiredError.ts +32 -0
- package/classes/TypedError.ts +2 -0
- package/index.ts +6 -0
- package/package.json +1 -1
- package/util/mcpElicitation.ts +191 -0
- package/util/mcpServer.ts +30 -7
package/classes/Connection.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { randomUUID } from "crypto";
|
|
2
|
+
import type { z } from "zod";
|
|
2
3
|
import { api, logger } from "../api";
|
|
3
4
|
import { config } from "../config";
|
|
4
5
|
import type { PubSubMessage } from "../initializers/pubsub";
|
|
5
6
|
import type { SessionData } from "../initializers/session";
|
|
6
7
|
import type { RateLimitInfo } from "../middleware/rateLimit";
|
|
7
8
|
import { ansi } from "../util/ansi";
|
|
9
|
+
import {
|
|
10
|
+
completeMcpElicitation,
|
|
11
|
+
elicitMcpForm,
|
|
12
|
+
elicitMcpUrl,
|
|
13
|
+
type McpElicitationContext,
|
|
14
|
+
type McpFormElicitationResult,
|
|
15
|
+
type McpUrlElicitationResult,
|
|
16
|
+
} from "../util/mcpElicitation";
|
|
8
17
|
import { isSecret } from "../util/zodMixins";
|
|
9
18
|
import type { Action, ActionParams } from "./Action";
|
|
10
19
|
import { LogFormat } from "./Logger";
|
|
@@ -130,6 +139,10 @@ export class Connection<
|
|
|
130
139
|
metadata: Partial<TMeta>;
|
|
131
140
|
/** @internal Tracks nested `act()` depth so metadata is only reset on the outermost call. */
|
|
132
141
|
private _actDepth = 0;
|
|
142
|
+
/** @internal MCP request functions for this transient connection. */
|
|
143
|
+
private mcpElicitationContext?: McpElicitationContext;
|
|
144
|
+
/** Abort signal for the action currently executing on this connection. */
|
|
145
|
+
private actionAbortSignal?: AbortSignal;
|
|
133
146
|
|
|
134
147
|
/**
|
|
135
148
|
* Create a new connection and register it in `api.connections`.
|
|
@@ -295,6 +308,89 @@ export class Connection<
|
|
|
295
308
|
return api.session.regenerate(this);
|
|
296
309
|
}
|
|
297
310
|
|
|
311
|
+
/**
|
|
312
|
+
* Attach the current MCP request context. Framework transport code calls this before
|
|
313
|
+
* invoking an action; application code should use {@link elicitForm} and
|
|
314
|
+
* {@link elicitUrl} instead.
|
|
315
|
+
*
|
|
316
|
+
* @param context - Request-scoped MCP client capabilities and send functions.
|
|
317
|
+
* @internal
|
|
318
|
+
*/
|
|
319
|
+
setMcpElicitationContext(context: McpElicitationContext): void {
|
|
320
|
+
this.mcpElicitationContext = context;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Ask an MCP client to collect non-sensitive structured input from the user.
|
|
325
|
+
* Accepted content is validated against `schema` before it is returned.
|
|
326
|
+
*
|
|
327
|
+
* Form elicitation must never request passwords, API keys, access tokens, payment
|
|
328
|
+
* credentials, or other secrets. Use {@link elicitUrl} for sensitive input.
|
|
329
|
+
*
|
|
330
|
+
* @param options - Human-readable prompt and a flat Zod object schema containing
|
|
331
|
+
* primitive fields supported by MCP.
|
|
332
|
+
* @returns The client's accept, decline, or cancel decision.
|
|
333
|
+
* @throws {TypedError} When called outside MCP, when the client lacks form
|
|
334
|
+
* elicitation support, when the schema is incompatible, or when accepted content
|
|
335
|
+
* fails validation.
|
|
336
|
+
*/
|
|
337
|
+
async elicitForm<TSchema extends z.ZodType>(options: {
|
|
338
|
+
message: string;
|
|
339
|
+
schema: TSchema;
|
|
340
|
+
}): Promise<McpFormElicitationResult<TSchema>> {
|
|
341
|
+
return elicitMcpForm(
|
|
342
|
+
{
|
|
343
|
+
connectionType: this.type,
|
|
344
|
+
context: this.mcpElicitationContext,
|
|
345
|
+
actionAbortSignal: this.actionAbortSignal,
|
|
346
|
+
},
|
|
347
|
+
options,
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Ask an MCP client to obtain consent to open an out-of-band URL.
|
|
353
|
+
*
|
|
354
|
+
* An `accept` result only means that the user consented to navigate. The application
|
|
355
|
+
* must detect completion through its own callback, webhook, database, or Redis state,
|
|
356
|
+
* then call {@link completeElicitation}.
|
|
357
|
+
*
|
|
358
|
+
* @param options - Message, absolute URL, and optional stable elicitation id.
|
|
359
|
+
* @returns The client's decision and the elicitation id used for later completion.
|
|
360
|
+
* @throws {TypedError} When called outside MCP, when the client lacks URL
|
|
361
|
+
* elicitation support, or when `url` is invalid.
|
|
362
|
+
*/
|
|
363
|
+
async elicitUrl(options: {
|
|
364
|
+
message: string;
|
|
365
|
+
url: string | URL;
|
|
366
|
+
elicitationId?: string;
|
|
367
|
+
}): Promise<McpUrlElicitationResult> {
|
|
368
|
+
return elicitMcpUrl(
|
|
369
|
+
{
|
|
370
|
+
connectionType: this.type,
|
|
371
|
+
context: this.mcpElicitationContext,
|
|
372
|
+
actionAbortSignal: this.actionAbortSignal,
|
|
373
|
+
},
|
|
374
|
+
options,
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Notify the initiating MCP client that an accepted URL elicitation completed.
|
|
380
|
+
*
|
|
381
|
+
* @param elicitationId - The id supplied to or returned by {@link elicitUrl}.
|
|
382
|
+
* @throws {TypedError} When called outside an active MCP request.
|
|
383
|
+
*/
|
|
384
|
+
async completeElicitation(elicitationId: string): Promise<void> {
|
|
385
|
+
await completeMcpElicitation(
|
|
386
|
+
{
|
|
387
|
+
connectionType: this.type,
|
|
388
|
+
context: this.mcpElicitationContext,
|
|
389
|
+
},
|
|
390
|
+
elicitationId,
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
|
|
298
394
|
/** Add a channel to this connection's subscription set. */
|
|
299
395
|
subscribe(channel: string) {
|
|
300
396
|
this.subscriptions.add(channel);
|
|
@@ -469,11 +565,18 @@ export class Connection<
|
|
|
469
565
|
}, timeoutMs);
|
|
470
566
|
});
|
|
471
567
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
568
|
+
const previousSignal = this.actionAbortSignal;
|
|
569
|
+
this.actionAbortSignal = controller.signal;
|
|
570
|
+
try {
|
|
571
|
+
return await Promise.race([
|
|
572
|
+
action.run(params, this, controller.signal),
|
|
573
|
+
timeoutPromise,
|
|
574
|
+
]);
|
|
575
|
+
} finally {
|
|
576
|
+
this.actionAbortSignal = previousSignal;
|
|
577
|
+
}
|
|
476
578
|
}
|
|
579
|
+
|
|
477
580
|
private async formatParams(params: Record<string, unknown>, action: Action) {
|
|
478
581
|
if (!action.inputs) return {} as ActionParams<Action>;
|
|
479
582
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ElicitRequestURLParams } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import { ErrorType, TypedError } from "./TypedError";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Signals that an MCP request cannot continue until the client completes one or
|
|
6
|
+
* more URL elicitations and retries the original request.
|
|
7
|
+
*
|
|
8
|
+
* Throw this from an MCP action when work cannot remain pending inside the current
|
|
9
|
+
* tool call. Keryx maps it to the MCP `-32042` URL elicitation required error.
|
|
10
|
+
*/
|
|
11
|
+
export class McpUrlElicitationRequiredError extends TypedError {
|
|
12
|
+
/** URL elicitations the client must complete before retrying. */
|
|
13
|
+
readonly elicitations: ElicitRequestURLParams[];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create a URL elicitation required error.
|
|
17
|
+
*
|
|
18
|
+
* @param elicitations - One or more URL-mode elicitation descriptions. Each must
|
|
19
|
+
* include a stable `elicitationId`, absolute `url`, and user-facing `message`.
|
|
20
|
+
* @param message - Error message shown to the MCP client.
|
|
21
|
+
*/
|
|
22
|
+
constructor(
|
|
23
|
+
elicitations: ElicitRequestURLParams[],
|
|
24
|
+
message = "This request requires additional information.",
|
|
25
|
+
) {
|
|
26
|
+
super({
|
|
27
|
+
message,
|
|
28
|
+
type: ErrorType.CONNECTION_MCP_ELICITATION,
|
|
29
|
+
});
|
|
30
|
+
this.elicitations = elicitations;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/classes/TypedError.ts
CHANGED
|
@@ -33,6 +33,7 @@ export enum ErrorType {
|
|
|
33
33
|
|
|
34
34
|
CONNECTION_ACTION_TIMEOUT = "CONNECTION_ACTION_TIMEOUT",
|
|
35
35
|
CONNECTION_RATE_LIMITED = "CONNECTION_RATE_LIMITED",
|
|
36
|
+
CONNECTION_MCP_ELICITATION = "CONNECTION_MCP_ELICITATION",
|
|
36
37
|
|
|
37
38
|
CONNECTION_TASK_DEFINITION = "CONNECTION_TASK_DEFINITION",
|
|
38
39
|
}
|
|
@@ -64,6 +65,7 @@ export const ErrorStatusCodes: Record<ErrorType, number> = {
|
|
|
64
65
|
[ErrorType.CONNECTION_CHANNEL_VALIDATION]: 400,
|
|
65
66
|
[ErrorType.CONNECTION_ACTION_TIMEOUT]: 408,
|
|
66
67
|
[ErrorType.CONNECTION_RATE_LIMITED]: 429,
|
|
68
|
+
[ErrorType.CONNECTION_MCP_ELICITATION]: 406,
|
|
67
69
|
|
|
68
70
|
[ErrorType.CONNECTION_TASK_DEFINITION]: 500,
|
|
69
71
|
};
|
package/index.ts
CHANGED
|
@@ -35,6 +35,7 @@ export type {
|
|
|
35
35
|
} from "./classes/Connection";
|
|
36
36
|
export { CONNECTION_TYPE, Connection } from "./classes/Connection";
|
|
37
37
|
export { LogLevel } from "./classes/Logger";
|
|
38
|
+
export { McpUrlElicitationRequiredError } from "./classes/McpUrlElicitationRequiredError";
|
|
38
39
|
export type { KeryxPlugin, PluginGenerator } from "./classes/Plugin";
|
|
39
40
|
export { SSEResponse, StreamingResponse } from "./classes/StreamingResponse";
|
|
40
41
|
export { ErrorStatusCodes, ErrorType, TypedError } from "./classes/TypedError";
|
|
@@ -69,6 +70,11 @@ export { buildProgram } from "./util/cli";
|
|
|
69
70
|
export { deepMerge, deepMergeDefaults, loadFromEnvIfSet } from "./util/config";
|
|
70
71
|
export { getValidTypes } from "./util/generate";
|
|
71
72
|
export { globLoader, globModuleExports } from "./util/glob";
|
|
73
|
+
export type {
|
|
74
|
+
McpElicitationAction,
|
|
75
|
+
McpFormElicitationResult,
|
|
76
|
+
McpUrlElicitationResult,
|
|
77
|
+
} from "./util/mcpElicitation";
|
|
72
78
|
export { type PaginatedResult, paginate } from "./util/pagination";
|
|
73
79
|
export { safeCompare } from "./util/safeCompare";
|
|
74
80
|
export type { JSONSchema } from "./util/swaggerSchemaGenerator";
|
package/package.json
CHANGED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ClientCapabilities,
|
|
3
|
+
ElicitRequestFormParams,
|
|
4
|
+
ElicitRequestURLParams,
|
|
5
|
+
ElicitResult,
|
|
6
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
7
|
+
import { ElicitRequestFormParamsSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
8
|
+
import { randomUUID } from "crypto";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { ErrorType, TypedError } from "../classes/TypedError";
|
|
11
|
+
|
|
12
|
+
/** Result action returned by an MCP client for an elicitation request. */
|
|
13
|
+
export type McpElicitationAction = ElicitResult["action"];
|
|
14
|
+
|
|
15
|
+
/** Result of an MCP form elicitation, with accepted content inferred from its Zod schema. */
|
|
16
|
+
export type McpFormElicitationResult<TSchema extends z.ZodType> =
|
|
17
|
+
| { action: "accept"; content: z.infer<TSchema> }
|
|
18
|
+
| { action: "decline" | "cancel"; content?: undefined };
|
|
19
|
+
|
|
20
|
+
/** Result of an MCP URL elicitation. Acceptance means consent, not completion. */
|
|
21
|
+
export type McpUrlElicitationResult = {
|
|
22
|
+
action: McpElicitationAction;
|
|
23
|
+
elicitationId: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** @internal Request-scoped MCP functions attached by the MCP transport. */
|
|
27
|
+
export type McpElicitationContext = {
|
|
28
|
+
clientCapabilities?: ClientCapabilities;
|
|
29
|
+
requestSignal: AbortSignal;
|
|
30
|
+
elicitInput: (
|
|
31
|
+
params: ElicitRequestFormParams | ElicitRequestURLParams,
|
|
32
|
+
signal: AbortSignal,
|
|
33
|
+
) => Promise<ElicitResult>;
|
|
34
|
+
completeElicitation: (elicitationId: string) => Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
type McpElicitationState = {
|
|
38
|
+
connectionType: string;
|
|
39
|
+
context?: McpElicitationContext;
|
|
40
|
+
actionAbortSignal?: AbortSignal;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Ask an MCP client to collect and validate structured input.
|
|
45
|
+
*
|
|
46
|
+
* @param state - Current connection type, request context, and action abort signal.
|
|
47
|
+
* @param options - Human-readable prompt and flat Zod schema supported by MCP.
|
|
48
|
+
* @returns The client's decision and validated content when accepted.
|
|
49
|
+
* @throws {TypedError} When MCP is unavailable, the schema is incompatible, or
|
|
50
|
+
* accepted content fails validation.
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
export async function elicitMcpForm<TSchema extends z.ZodType>(
|
|
54
|
+
state: McpElicitationState,
|
|
55
|
+
options: {
|
|
56
|
+
message: string;
|
|
57
|
+
schema: TSchema;
|
|
58
|
+
},
|
|
59
|
+
): Promise<McpFormElicitationResult<TSchema>> {
|
|
60
|
+
const context = requireMcpElicitation(state, "form");
|
|
61
|
+
let requestedSchema: ElicitRequestFormParams["requestedSchema"];
|
|
62
|
+
try {
|
|
63
|
+
requestedSchema = ElicitRequestFormParamsSchema.shape.requestedSchema.parse(
|
|
64
|
+
z.toJSONSchema(options.schema, { target: "draft-7", io: "input" }),
|
|
65
|
+
);
|
|
66
|
+
} catch (cause) {
|
|
67
|
+
throw new TypedError({
|
|
68
|
+
message: `Invalid MCP form elicitation schema: ${cause}`,
|
|
69
|
+
type: ErrorType.CONNECTION_MCP_ELICITATION,
|
|
70
|
+
cause,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const result = await context.elicitInput(
|
|
75
|
+
{ mode: "form", message: options.message, requestedSchema },
|
|
76
|
+
mcpElicitationSignal(context, state.actionAbortSignal),
|
|
77
|
+
);
|
|
78
|
+
if (result.action !== "accept") return { action: result.action };
|
|
79
|
+
|
|
80
|
+
const parsed = await options.schema.safeParseAsync(result.content);
|
|
81
|
+
if (!parsed.success) {
|
|
82
|
+
throw new TypedError({
|
|
83
|
+
message: `MCP client returned invalid elicitation content: ${parsed.error.message}`,
|
|
84
|
+
type: ErrorType.CONNECTION_MCP_ELICITATION,
|
|
85
|
+
cause: parsed.error,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return { action: "accept", content: parsed.data };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Ask an MCP client for consent to open an out-of-band URL.
|
|
93
|
+
*
|
|
94
|
+
* @param state - Current connection type, request context, and action abort signal.
|
|
95
|
+
* @param options - Message, absolute URL, and optional stable elicitation id.
|
|
96
|
+
* @returns The client's decision and the elicitation id used for completion.
|
|
97
|
+
* @throws {TypedError} When MCP or URL elicitation is unavailable, or `url` is invalid.
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
export async function elicitMcpUrl(
|
|
101
|
+
state: McpElicitationState,
|
|
102
|
+
options: {
|
|
103
|
+
message: string;
|
|
104
|
+
url: string | URL;
|
|
105
|
+
elicitationId?: string;
|
|
106
|
+
},
|
|
107
|
+
): Promise<McpUrlElicitationResult> {
|
|
108
|
+
const context = requireMcpElicitation(state, "url");
|
|
109
|
+
let url: string;
|
|
110
|
+
try {
|
|
111
|
+
url = new URL(options.url).toString();
|
|
112
|
+
} catch (cause) {
|
|
113
|
+
throw new TypedError({
|
|
114
|
+
message: `Invalid MCP elicitation URL: ${options.url}`,
|
|
115
|
+
type: ErrorType.CONNECTION_MCP_ELICITATION,
|
|
116
|
+
cause,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const elicitationId = options.elicitationId ?? randomUUID();
|
|
121
|
+
const result = await context.elicitInput(
|
|
122
|
+
{
|
|
123
|
+
mode: "url",
|
|
124
|
+
message: options.message,
|
|
125
|
+
url,
|
|
126
|
+
elicitationId,
|
|
127
|
+
},
|
|
128
|
+
mcpElicitationSignal(context, state.actionAbortSignal),
|
|
129
|
+
);
|
|
130
|
+
return { action: result.action, elicitationId };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Notify the initiating MCP client that URL elicitation completed.
|
|
135
|
+
*
|
|
136
|
+
* @param state - Current connection type and request context.
|
|
137
|
+
* @param elicitationId - Stable id supplied to URL elicitation.
|
|
138
|
+
* @returns A promise that resolves after the notification is sent.
|
|
139
|
+
* @throws {TypedError} When called outside an active URL-capable MCP request.
|
|
140
|
+
* @internal
|
|
141
|
+
*/
|
|
142
|
+
export async function completeMcpElicitation(
|
|
143
|
+
state: McpElicitationState,
|
|
144
|
+
elicitationId: string,
|
|
145
|
+
): Promise<void> {
|
|
146
|
+
const context = requireMcpElicitation(state, "url");
|
|
147
|
+
await context.completeElicitation(elicitationId);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function requireMcpElicitation(
|
|
151
|
+
state: McpElicitationState,
|
|
152
|
+
mode: "form" | "url",
|
|
153
|
+
): McpElicitationContext {
|
|
154
|
+
if (state.connectionType !== "mcp") {
|
|
155
|
+
throw new TypedError({
|
|
156
|
+
message: `MCP elicitation is only available on MCP connections (got ${state.connectionType})`,
|
|
157
|
+
type: ErrorType.CONNECTION_MCP_ELICITATION,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const context = state.context;
|
|
162
|
+
if (!context) {
|
|
163
|
+
throw new TypedError({
|
|
164
|
+
message: "MCP elicitation is only available during an MCP request",
|
|
165
|
+
type: ErrorType.CONNECTION_MCP_ELICITATION,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const elicitation = context.clientCapabilities?.elicitation;
|
|
170
|
+
const supportsForm =
|
|
171
|
+
elicitation !== undefined &&
|
|
172
|
+
(elicitation.form !== undefined || Object.keys(elicitation).length === 0);
|
|
173
|
+
const supported =
|
|
174
|
+
mode === "form" ? supportsForm : elicitation?.url !== undefined;
|
|
175
|
+
if (!supported) {
|
|
176
|
+
throw new TypedError({
|
|
177
|
+
message: `MCP client does not support ${mode} elicitation`,
|
|
178
|
+
type: ErrorType.CONNECTION_MCP_ELICITATION,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return context;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function mcpElicitationSignal(
|
|
185
|
+
context: McpElicitationContext,
|
|
186
|
+
actionAbortSignal?: AbortSignal,
|
|
187
|
+
): AbortSignal {
|
|
188
|
+
return actionAbortSignal
|
|
189
|
+
? AbortSignal.any([context.requestSignal, actionAbortSignal])
|
|
190
|
+
: context.requestSignal;
|
|
191
|
+
}
|
package/util/mcpServer.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
type ServerNotification,
|
|
11
11
|
type ServerRequest,
|
|
12
12
|
SUPPORTED_PROTOCOL_VERSIONS,
|
|
13
|
+
UrlElicitationRequiredError,
|
|
13
14
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
14
15
|
import { randomUUID } from "crypto";
|
|
15
16
|
import * as z4mini from "zod/v4-mini";
|
|
@@ -17,6 +18,7 @@ import { api, logger } from "../api";
|
|
|
17
18
|
import type { Action, McpUiConfig } from "../classes/Action";
|
|
18
19
|
import { MCP_APP_MIME_TYPE, MCP_RESPONSE_FORMAT } from "../classes/Action";
|
|
19
20
|
import { CONNECTION_TYPE, Connection } from "../classes/Connection";
|
|
21
|
+
import { McpUrlElicitationRequiredError } from "../classes/McpUrlElicitationRequiredError";
|
|
20
22
|
import { StreamingResponse } from "../classes/StreamingResponse";
|
|
21
23
|
import { ErrorType, TypedError } from "../classes/TypedError";
|
|
22
24
|
import { UIResponse } from "../classes/UIResponse";
|
|
@@ -58,10 +60,14 @@ export type McpAuthInfo = {
|
|
|
58
60
|
* Create an authenticated MCP Connection from the auth info attached to an MCP request.
|
|
59
61
|
* Shared by tool, resource, and prompt handlers to avoid duplicating connection setup.
|
|
60
62
|
*/
|
|
61
|
-
export async function createMcpConnection(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
export async function createMcpConnection(
|
|
64
|
+
extra: {
|
|
65
|
+
authInfo?: McpAuthInfo;
|
|
66
|
+
sessionId?: string;
|
|
67
|
+
signal?: AbortSignal;
|
|
68
|
+
},
|
|
69
|
+
mcpServer?: McpServer,
|
|
70
|
+
): Promise<Connection> {
|
|
65
71
|
const authInfo = extra.authInfo;
|
|
66
72
|
const clientIp = (authInfo?.extra?.ip as string) || "unknown";
|
|
67
73
|
const connection = new Connection(
|
|
@@ -77,6 +83,17 @@ export async function createMcpConnection(extra: {
|
|
|
77
83
|
await connection.updateSession({ userId: authInfo.extra.userId });
|
|
78
84
|
}
|
|
79
85
|
|
|
86
|
+
if (mcpServer && extra.signal) {
|
|
87
|
+
connection.setMcpElicitationContext({
|
|
88
|
+
clientCapabilities: mcpServer.server.getClientCapabilities(),
|
|
89
|
+
requestSignal: extra.signal,
|
|
90
|
+
elicitInput: (params, signal) =>
|
|
91
|
+
mcpServer.server.elicitInput(params, { signal }),
|
|
92
|
+
completeElicitation: (elicitationId) =>
|
|
93
|
+
mcpServer.server.createElicitationCompletionNotifier(elicitationId)(),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
80
97
|
return connection;
|
|
81
98
|
}
|
|
82
99
|
|
|
@@ -613,7 +630,7 @@ function registerTools(mcpServer: McpServer) {
|
|
|
613
630
|
extra: RequestHandlerExtra<ServerRequest, ServerNotification>,
|
|
614
631
|
) => {
|
|
615
632
|
const mcpSessionId = extra.sessionId || "";
|
|
616
|
-
const connection = await createMcpConnection(extra);
|
|
633
|
+
const connection = await createMcpConnection(extra, mcpServer);
|
|
617
634
|
|
|
618
635
|
try {
|
|
619
636
|
const params =
|
|
@@ -629,6 +646,12 @@ function registerTools(mcpServer: McpServer) {
|
|
|
629
646
|
);
|
|
630
647
|
|
|
631
648
|
if (error) {
|
|
649
|
+
if (error instanceof McpUrlElicitationRequiredError) {
|
|
650
|
+
throw new UrlElicitationRequiredError(
|
|
651
|
+
error.elicitations,
|
|
652
|
+
error.message,
|
|
653
|
+
);
|
|
654
|
+
}
|
|
632
655
|
return {
|
|
633
656
|
content: [
|
|
634
657
|
{
|
|
@@ -724,7 +747,7 @@ function registerResources(mcpServer: McpServer) {
|
|
|
724
747
|
extra: any,
|
|
725
748
|
) => {
|
|
726
749
|
const mcpSessionId = extra.sessionId || "";
|
|
727
|
-
const connection = await createMcpConnection(extra);
|
|
750
|
+
const connection = await createMcpConnection(extra, mcpServer);
|
|
728
751
|
|
|
729
752
|
try {
|
|
730
753
|
const params: Record<string, unknown> = { ...variables };
|
|
@@ -853,7 +876,7 @@ function registerPrompts(mcpServer: McpServer) {
|
|
|
853
876
|
},
|
|
854
877
|
async (args: any, extra: any) => {
|
|
855
878
|
const mcpSessionId = extra.sessionId || "";
|
|
856
|
-
const connection = await createMcpConnection(extra);
|
|
879
|
+
const connection = await createMcpConnection(extra, mcpServer);
|
|
857
880
|
|
|
858
881
|
try {
|
|
859
882
|
const params =
|