Repository navigation
Expand file tree
/
Copy patherrors.ts
More file actions
364 lines (331 loc) · 11.6 KB
/
Copy patherrors.ts
File metadata and controls
364 lines (331 loc) · 11.6 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
354
355
356
357
358
359
360
361
362
363
364
/**
* Custom error types for MCP UI operations.
* Provides structured errors with context and recovery hints.
*/
export interface McpUiErrorContext {
server?: string;
tool?: string;
uri?: string;
session?: string;
[key: string]: unknown;
}
/**
* Base error class for MCP UI errors.
*/
export class McpUiError extends Error {
readonly code: string;
readonly context: McpUiErrorContext;
readonly recoveryHint: string | undefined;
readonly cause: Error | undefined;
constructor(
message: string,
options: {
code: string;
context?: McpUiErrorContext;
recoveryHint?: string;
cause?: Error;
}
) {
super(message);
this.name = "McpUiError";
this.code = options.code;
this.context = options.context ?? {};
this.recoveryHint = options.recoveryHint;
this.cause = options.cause;
// Maintain proper stack trace
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
toJSON(): Record<string, unknown> {
return {
name: this.name,
code: this.code,
message: this.message,
context: this.context,
recoveryHint: this.recoveryHint,
stack: this.stack,
};
}
}
/**
* Error fetching a UI resource from the MCP server.
*/
export class ResourceFetchError extends McpUiError {
constructor(
uri: string,
reason: string,
options?: { server?: string; cause?: Error }
) {
super(`Failed to fetch UI resource "${uri}": ${reason}`, {
code: "RESOURCE_FETCH_ERROR",
context: { uri, ...(options?.server !== undefined ? { server: options.server } : {}) },
recoveryHint: "Check that the MCP server is connected and the resource URI is valid.",
...(options?.cause !== undefined ? { cause: options.cause } : {}),
});
this.name = "ResourceFetchError";
}
}
/**
* Error parsing or validating UI resource content.
*/
export class ResourceParseError extends McpUiError {
constructor(
uri: string,
reason: string,
options?: { server?: string; mimeType?: string }
) {
super(`Invalid UI resource "${uri}": ${reason}`, {
code: "RESOURCE_PARSE_ERROR",
context: {
uri,
...(options?.server !== undefined ? { server: options.server } : {}),
...(options?.mimeType !== undefined ? { mimeType: options.mimeType } : {}),
},
recoveryHint: "Ensure the resource returns valid HTML with the correct MIME type.",
});
this.name = "ResourceParseError";
}
}
/**
* Error connecting to the AppBridge.
*/
export class BridgeConnectionError extends McpUiError {
constructor(reason: string, options?: { session?: string; cause?: Error }) {
super(`AppBridge connection failed: ${reason}`, {
code: "BRIDGE_CONNECTION_ERROR",
context: options?.session !== undefined ? { session: options.session } : {},
recoveryHint: "Check browser console for detailed errors. The iframe may have failed to load.",
...(options?.cause !== undefined ? { cause: options.cause } : {}),
});
this.name = "BridgeConnectionError";
}
}
/**
* Error related to user consent for tool calls.
*/
export class ConsentError extends McpUiError {
readonly denied: boolean;
constructor(
server: string,
options: { denied?: boolean; requiresApproval?: boolean }
) {
const message = options.denied
? `Tool calls for "${server}" were denied for this session`
: `Tool call approval required for "${server}"`;
super(message, {
code: options.denied ? "CONSENT_DENIED" : "CONSENT_REQUIRED",
context: { server },
recoveryHint: options.denied
? "The user denied tool access. Start a new session to try again."
: "Prompt the user for consent before calling tools.",
});
this.name = "ConsentError";
this.denied = options.denied ?? false;
}
}
/**
* Error with UI server session management.
*/
export class SessionError extends McpUiError {
constructor(
reason: string,
options?: { session?: string; cause?: Error }
) {
super(`Session error: ${reason}`, {
code: "SESSION_ERROR",
context: options?.session !== undefined ? { session: options.session } : {},
recoveryHint: "The session may have expired or been closed. Try opening the UI again.",
...(options?.cause !== undefined ? { cause: options.cause } : {}),
});
this.name = "SessionError";
}
}
/**
* Error starting or operating the UI server.
*/
export class ServerError extends McpUiError {
constructor(
reason: string,
options?: { port?: number; cause?: Error }
) {
super(`UI server error: ${reason}`, {
code: "SERVER_ERROR",
context: options?.port !== undefined ? { port: options.port } : {},
recoveryHint: "Check if the port is available. Another process may be using it.",
...(options?.cause !== undefined ? { cause: options.cause } : {}),
});
this.name = "ServerError";
}
}
/**
* Error communicating with the MCP server.
*/
export class McpServerError extends McpUiError {
constructor(
server: string,
reason: string,
options?: { tool?: string; cause?: Error }
) {
super(`MCP server "${server}" error: ${reason}`, {
code: "MCP_SERVER_ERROR",
context: { server, ...(options?.tool !== undefined ? { tool: options.tool } : {}) },
recoveryHint: "Check that the MCP server is running and responsive.",
...(options?.cause !== undefined ? { cause: options.cause } : {}),
});
this.name = "McpServerError";
}
}
/**
* Wrap an unknown error into an McpUiError.
*/
export function wrapError(error: unknown, context?: McpUiErrorContext): McpUiError {
if (error instanceof McpUiError) {
// Merge contexts
return new McpUiError(error.message, {
code: error.code,
context: { ...error.context, ...context },
...(error.recoveryHint !== undefined ? { recoveryHint: error.recoveryHint } : {}),
...(error.cause !== undefined ? { cause: error.cause } : {}),
});
}
const cause = error instanceof Error ? error : undefined;
const message = error instanceof Error ? error.message : String(error);
return new McpUiError(message, {
code: "UNKNOWN_ERROR",
...(context !== undefined ? { context } : {}),
...(cause !== undefined ? { cause } : {}),
});
}
/**
* Check if an error is a specific MCP UI error type.
*/
export function isErrorCode(error: unknown, code: string): boolean {
return error instanceof McpUiError && error.code === code;
}
/**
* Stable adapter error code for an SDK-native input_required result that the
* current client cannot fulfil because its embedded-request handler is not
* registered. The SDK keeps the request key/method in `SdkError.data`; this
* adapter detail intentionally copies only bounded, actionable fields.
*/
export const INPUT_REQUIRED_NEEDS_UI = "input_required_needs_ui" as const;
export interface InputRequiredNeedsUiDetails {
error: typeof INPUT_REQUIRED_NEEDS_UI;
server: string;
tool?: string;
resourceUri?: string;
inputKey: string;
inputMethod: string;
message: string;
}
export interface InputRequiredIdentity {
server: string;
tool?: string;
resourceUri?: string;
}
const INPUT_REQUIRED_METHODS = new Set([
"elicitation/create",
"sampling/createMessage",
]);
const MAX_INPUT_REQUIRED_SERVER_LENGTH = 96;
const MAX_INPUT_REQUIRED_TARGET_LENGTH = 160;
const MAX_INPUT_REQUIRED_DETAIL_LENGTH = 128;
function asRecord(value: unknown): Record<string, unknown> | undefined {
return value !== null && typeof value === "object" && !Array.isArray(value)
? value as Record<string, unknown>
: undefined;
}
function boundedText(value: unknown, maxLength: number): string | undefined {
if (typeof value !== "string" || value.length === 0) return undefined;
return value.length <= maxLength ? value : `${value.slice(0, maxLength - 1)}…`;
}
interface EmbeddedInputRequest {
inputKey: string;
inputMethod: string;
server?: string;
tool?: string;
resourceUri?: string;
}
function findEmbeddedInputRequest(error: unknown): EmbeddedInputRequest | undefined {
let current: unknown = error;
const seen = new Set<unknown>();
for (let depth = 0; depth < 8 && current !== undefined && current !== null; depth++) {
if (seen.has(current)) return undefined;
seen.add(current);
const record = asRecord(current);
if (!record) return undefined;
if (record.code === INPUT_REQUIRED_NEEDS_UI) {
const details = asRecord(record.details);
const inputKey = boundedText(details?.inputKey, MAX_INPUT_REQUIRED_DETAIL_LENGTH);
const inputMethod = boundedText(details?.inputMethod, MAX_INPUT_REQUIRED_DETAIL_LENGTH);
if (inputKey && inputMethod && INPUT_REQUIRED_METHODS.has(inputMethod)) {
const server = boundedText(details?.server, MAX_INPUT_REQUIRED_SERVER_LENGTH);
const tool = boundedText(details?.tool, MAX_INPUT_REQUIRED_TARGET_LENGTH);
const resourceUri = boundedText(details?.resourceUri, MAX_INPUT_REQUIRED_TARGET_LENGTH);
return {
inputKey,
inputMethod,
...(server ? { server } : {}),
...(tool ? { tool } : {}),
...(resourceUri ? { resourceUri } : {}),
};
}
}
if (record.code === "CAPABILITY_NOT_SUPPORTED") {
const data = asRecord(record.data);
const inputKey = boundedText(data?.key, MAX_INPUT_REQUIRED_DETAIL_LENGTH);
const inputMethod = boundedText(data?.method, MAX_INPUT_REQUIRED_DETAIL_LENGTH);
if (inputKey && inputMethod && INPUT_REQUIRED_METHODS.has(inputMethod)) {
return { inputKey, inputMethod };
}
}
current = record.cause;
}
return undefined;
}
/**
* Convert a missing embedded-input handler into an adapter-owned result
* detail. Matching requires the SDK's typed code/data shape (or this module's
* typed wrapper), rather than arbitrary error-message text.
*/
export function getInputRequiredNeedsUiDetails(
error: unknown,
identity: InputRequiredIdentity,
): InputRequiredNeedsUiDetails | undefined {
const request = findEmbeddedInputRequest(error);
if (!request) return undefined;
const server = boundedText(request.server ?? identity.server, MAX_INPUT_REQUIRED_SERVER_LENGTH) ?? "unknown";
const tool = boundedText(request.tool ?? identity.tool, MAX_INPUT_REQUIRED_TARGET_LENGTH);
const resourceUri = boundedText(request.resourceUri ?? identity.resourceUri, MAX_INPUT_REQUIRED_TARGET_LENGTH);
const target = resourceUri
? `read resource "${resourceUri}"`
: tool
? `call tool "${tool}"`
: "complete the MCP request";
const guidance = request.inputMethod === "elicitation/create"
? "Run this call in an interactive Pi session with elicitation enabled, then retry."
: "Run this call in an interactive Pi session with the required MCP capability enabled, then retry.";
const message = `MCP server "${server}" requested input to ${target}, but this session has no handler for "${request.inputMethod}" (input "${request.inputKey}"). ${guidance}`;
return {
error: INPUT_REQUIRED_NEEDS_UI,
server,
...(tool ? { tool } : {}),
...(resourceUri ? { resourceUri } : {}),
inputKey: request.inputKey,
inputMethod: request.inputMethod,
message,
};
}
/** Error form used by UiResourceHandler so an outer tool call can preserve the classification. */
export class InputRequiredNeedsUiError extends McpUiError {
constructor(readonly details: InputRequiredNeedsUiDetails, cause?: Error) {
super(details.message, {
code: INPUT_REQUIRED_NEEDS_UI,
context: { ...details },
recoveryHint: "Run this call in an interactive Pi session with the required MCP input handler enabled, then retry.",
...(cause ? { cause } : {}),
});
this.name = "InputRequiredNeedsUiError";
}
}