Open History to see the last commit
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 }
19}
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 }
35}
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 }
46}
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>
55}
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(' ')
71}
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 }
92}