-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy patherrors.ts
More file actions
219 lines (201 loc) · 5.68 KB
/
errors.ts
File metadata and controls
219 lines (201 loc) · 5.68 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
/**
* 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;
readonly cause?: Error;
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, server: options?.server },
recoveryHint: "Check that the MCP server is connected and the resource URI is valid.",
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, server: options?.server, 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: { session: options?.session },
recoveryHint: "Check browser console for detailed errors. The iframe may have failed to load.",
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: { session: options?.session },
recoveryHint: "The session may have expired or been closed. Try opening the UI again.",
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: { port: options?.port },
recoveryHint: "Check if the port is available. Another process may be using it.",
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, tool: options?.tool },
recoveryHint: "Check that the MCP server is running and responsive.",
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 },
recoveryHint: error.recoveryHint,
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,
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;
}