Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

errors.ts

180 lines5.1 KB

1export class MCPConnectionError extends Error {
2 constructor(
3 message: string,
4 public override readonly cause?: unknown,
5 ) {
6 super(message)
7 this.name = 'MCPConnectionError'
8 }
9}
10​
11export class DuplicateToolNameError extends Error {
12 constructor(public readonly toolName: string) {
13 super(
14 `Duplicate MCP tool name "${toolName}". Set a unique \`prefix\` on one of the ` +
15 `MCP clients (createMCPClient({ transport, prefix: '...' })) to disambiguate.`,
16 )
17 this.name = 'DuplicateToolNameError'
18 }
20​
21/**
22 * Thrown when a task-required tool is explicitly bound via `mcp.tools([...])`,
23 * called via `callTool()`, or named in a `toolFilter` list, but the server
24 * does not declare the tasks capability for tools/call, so the call could
25 * never execute. (Auto-discovery without a `toolFilter` list skips such tools.)
26 */
27export class MCPTaskRequiredToolError extends Error {
28 constructor(public readonly toolName: string) {
29 super(
30 `MCP tool "${toolName}" requires task-based execution, but the server ` +
31 `does not declare the tasks capability for tools/call`,
32 )
33 this.name = 'MCPTaskRequiredToolError'
34 }
36​
37export class MCPToolNotFoundError extends Error {
38 constructor(public readonly toolName: string) {
39 super(
40 `toolDefinition name "${toolName}" was passed to mcp.tools([...]) but the MCP ` +
41 `server exposes no tool with that name, or the client's \`toolFilter\` hides it. ` +
42 `Check the name or run mcp.tools() to list.`,
43 )
44 this.name = 'MCPToolNotFoundError'
45 }
47​
48type ToolFilterDetails = {
49 /** Listed, but the server has no tool with this name. */
50 missing: Array<string>
51 /** Listed more than once. */
52 repeated: Array<string>
53 /** The server's tool names. */
54 available: Array<string>
56​
57function toolFilterMessage(details: ToolFilterDetails) {
58 const names = (list: Array<string>) =>
59 list.map((name) => `"${name}"`).join(', ')
60 const parts = ['The MCP client `toolFilter` list does not match the server.']
61 if (details.missing.length > 0) {
62 parts.push(`The server has no tool named ${names(details.missing)}.`)
63 }
64 if (details.repeated.length > 0) {
65 parts.push(`The list repeats ${names(details.repeated)}.`)
66 }
67 const available =
68 details.available.length > 0 ? names(details.available) : '(none)'
69 parts.push(`Available tools: ${available}.`)
70 return parts.join(' ')
72​
73/**
74 * Thrown by `tools()` when a `toolFilter` list names a tool that the server
75 * does not have, or names a tool more than once.
76 */
77export class MCPToolFilterError extends Error {
78 /** Listed, but the server has no tool with this name. */
79 readonly missing: Array<string>
80 /** Listed more than once. */
81 readonly repeated: Array<string>
82 /** The server's tool names. */
83 readonly available: Array<string>
84​
85 constructor(details: ToolFilterDetails) {
86 super(toolFilterMessage(details))
87 this.name = 'MCPToolFilterError'
88 this.missing = details.missing
89 this.repeated = details.repeated
90 this.available = details.available
91 }
93​
94/**
95 * The server did not advertise MCP Events, or it answered method not found.
96 *
97 * @experimental
98 */
99export class MCPEventsUnsupportedError extends Error {
100 constructor() {
101 super('The MCP server does not advertise the Events extension')
102 this.name = 'MCPEventsUnsupportedError'
103 }
105​
106/**
107 * Invalid MCP Events client options or subscribe arguments.
108 *
109 * @experimental
110 */
111export class MCPEventConfigError extends Error {
112 constructor(public readonly reason: string) {
113 super(reason)
114 this.name = 'MCPEventConfigError'
115 }
117​
118/**
119 * The event name is missing from `events/list`, or it has no webhook delivery.
120 *
121 * @experimental
122 */
123export class MCPUnknownEventError extends Error {
124 constructor(
125 public readonly eventName: string,
126 public readonly reason: 'not-found' | 'webhook-unsupported',
127 ) {
128 super(
129 reason === 'not-found'
130 ? `Unknown MCP event "${eventName}"`
131 : `MCP event "${eventName}" does not support webhook delivery`,
132 )
133 this.name = 'MCPUnknownEventError'
134 }
136​
137/**
138 * Kind of a JSON-RPC error from an `events/*` request.
139 *
140 * @experimental
141 */
142export type MCPEventRequestKind =
143 | 'invalid-params'
144 | 'not-found'
145 | 'forbidden'
146 | 'resource-exhausted'
147 | 'unsupported'
148 | 'callback-endpoint'
149 | 'unknown'
150​
151/**
152 * The server returned a JSON-RPC error for an `events/*` request.
153 *
154 * @experimental
155 */
156export class MCPEventRequestError extends Error {
157 constructor(
158 message: string,
159 public readonly code: number,
160 public readonly kind: MCPEventRequestKind,
161 public readonly reason?: string,
162 public readonly data?: unknown,
163 public override readonly cause?: unknown,
164 ) {
165 super(message, cause === undefined ? undefined : { cause })
166 this.name = 'MCPEventRequestError'
167 }
169​
170/**
171 * `refresh` or `unsubscribe` was given a key or id that is not in the store.
172 *
173 * @experimental
174 */
175export class MCPEventSubscriptionNotFoundError extends Error {
176 constructor(public readonly ref: { key?: string; id?: string }) {
177 super('MCP event subscription was not found')
178 this.name = 'MCPEventSubscriptionNotFoundError'
179 }