|
| 1 | +import Anthropic from "@anthropic-ai/sdk"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Configuration for a single tool in the MCP toolset. |
| 5 | + */ |
| 6 | +export interface MCPToolConfig { |
| 7 | + /** |
| 8 | + * Whether this tool is enabled. |
| 9 | + * @default true |
| 10 | + */ |
| 11 | + enabled?: boolean; |
| 12 | + /** |
| 13 | + * If true, tool description is not sent to the model initially. |
| 14 | + * Used with Tool Search Tool for on-demand loading. |
| 15 | + * @default false |
| 16 | + */ |
| 17 | + deferLoading?: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Per-tool configuration overrides. |
| 22 | + * Keys are tool names, values are configuration objects. |
| 23 | + */ |
| 24 | +export type MCPToolConfigs = Record<string, MCPToolConfig>; |
| 25 | + |
| 26 | +/** |
| 27 | + * Options for creating an MCP toolset. |
| 28 | + */ |
| 29 | +export interface MCPToolsetOptions { |
| 30 | + /** |
| 31 | + * Must match a server name defined in the `mcp_servers` array in call options. |
| 32 | + */ |
| 33 | + serverName: string; |
| 34 | + /** |
| 35 | + * Default configuration applied to all tools in this toolset. |
| 36 | + * Individual tool configs will override these defaults. |
| 37 | + */ |
| 38 | + defaultConfig?: MCPToolConfig; |
| 39 | + /** |
| 40 | + * Per-tool configuration overrides. |
| 41 | + * Keys are tool names, values are configuration objects. |
| 42 | + */ |
| 43 | + configs?: MCPToolConfigs; |
| 44 | + /** |
| 45 | + * Create a cache control breakpoint at this content block. |
| 46 | + */ |
| 47 | + cacheControl?: Anthropic.Beta.BetaCacheControlEphemeral; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * MCP toolset definition type. |
| 52 | + */ |
| 53 | +export interface MCPToolset { |
| 54 | + type: "mcp_toolset"; |
| 55 | + mcp_server_name: string; |
| 56 | + default_config?: { |
| 57 | + enabled?: boolean; |
| 58 | + defer_loading?: boolean; |
| 59 | + }; |
| 60 | + configs?: Record< |
| 61 | + string, |
| 62 | + { |
| 63 | + enabled?: boolean; |
| 64 | + defer_loading?: boolean; |
| 65 | + } |
| 66 | + >; |
| 67 | + cache_control?: Anthropic.Beta.BetaCacheControlEphemeral; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Creates an MCP toolset that connects to a remote MCP server to access its tools. |
| 72 | + * This enables Claude to use tools from MCP servers without implementing a separate MCP client. |
| 73 | + * |
| 74 | + * @note This tool requires the beta header `mcp-client-2025-11-20` in API requests. |
| 75 | + * The header is automatically added when using this tool. |
| 76 | + * |
| 77 | + * @see {@link https://docs.anthropic.com/en/docs/agents-and-tools/mcp-connector | Anthropic MCP Connector Documentation} |
| 78 | + * @param options - Configuration options for the MCP toolset |
| 79 | + * @returns An MCP toolset definition to be passed to the Anthropic API tools array |
| 80 | + * |
| 81 | + * @example |
| 82 | + * ```typescript |
| 83 | + * import { ChatAnthropic, tools } from "@langchain/anthropic"; |
| 84 | + * |
| 85 | + * const model = new ChatAnthropic({ |
| 86 | + * model: "claude-sonnet-4-5-20250929", |
| 87 | + * }); |
| 88 | + * |
| 89 | + * // Basic usage - enable all tools from an MCP server |
| 90 | + * const response = await model.invoke("What tools do you have available?", { |
| 91 | + * mcp_servers: [{ |
| 92 | + * type: "url", |
| 93 | + * url: "https://example-server.modelcontextprotocol.io/sse", |
| 94 | + * name: "example-mcp", |
| 95 | + * authorization_token: "YOUR_TOKEN", |
| 96 | + * }], |
| 97 | + * tools: [ |
| 98 | + * tools.mcpToolset_20251120({ serverName: "example-mcp" }), |
| 99 | + * ], |
| 100 | + * }); |
| 101 | + * |
| 102 | + * // Allowlist pattern - enable only specific tools |
| 103 | + * const responseAllowlist = await model.invoke("Search for events", { |
| 104 | + * mcp_servers: [{ |
| 105 | + * type: "url", |
| 106 | + * url: "https://calendar.example.com/sse", |
| 107 | + * name: "google-calendar-mcp", |
| 108 | + * authorization_token: "YOUR_TOKEN", |
| 109 | + * }], |
| 110 | + * tools: [ |
| 111 | + * tools.mcpToolset_20251120({ |
| 112 | + * serverName: "google-calendar-mcp", |
| 113 | + * defaultConfig: { enabled: false }, |
| 114 | + * configs: { |
| 115 | + * search_events: { enabled: true }, |
| 116 | + * create_event: { enabled: true }, |
| 117 | + * }, |
| 118 | + * }), |
| 119 | + * ], |
| 120 | + * }); |
| 121 | + * |
| 122 | + * // Denylist pattern - disable specific tools |
| 123 | + * const responseDenylist = await model.invoke("List my events", { |
| 124 | + * mcp_servers: [{ |
| 125 | + * type: "url", |
| 126 | + * url: "https://calendar.example.com/sse", |
| 127 | + * name: "google-calendar-mcp", |
| 128 | + * authorization_token: "YOUR_TOKEN", |
| 129 | + * }], |
| 130 | + * tools: [ |
| 131 | + * tools.mcpToolset_20251120({ |
| 132 | + * serverName: "google-calendar-mcp", |
| 133 | + * configs: { |
| 134 | + * delete_all_events: { enabled: false }, |
| 135 | + * share_calendar_publicly: { enabled: false }, |
| 136 | + * }, |
| 137 | + * }), |
| 138 | + * ], |
| 139 | + * }); |
| 140 | + * |
| 141 | + * // With deferred loading for use with Tool Search Tool |
| 142 | + * const responseDeferred = await model.invoke("Search for tools", { |
| 143 | + * mcp_servers: [{ |
| 144 | + * type: "url", |
| 145 | + * url: "https://example.com/sse", |
| 146 | + * name: "example-mcp", |
| 147 | + * }], |
| 148 | + * tools: [ |
| 149 | + * tools.toolSearchRegex_20251119(), |
| 150 | + * tools.mcpToolset_20251120({ |
| 151 | + * serverName: "example-mcp", |
| 152 | + * defaultConfig: { deferLoading: true }, |
| 153 | + * }), |
| 154 | + * ], |
| 155 | + * }); |
| 156 | + * ``` |
| 157 | + */ |
| 158 | +export function mcpToolset_20251120(options: MCPToolsetOptions): MCPToolset { |
| 159 | + const defaultConfig = |
| 160 | + options.defaultConfig?.enabled !== undefined || |
| 161 | + options.defaultConfig?.deferLoading !== undefined |
| 162 | + ? { |
| 163 | + enabled: options.defaultConfig?.enabled, |
| 164 | + defer_loading: options.defaultConfig?.deferLoading, |
| 165 | + } |
| 166 | + : undefined; |
| 167 | + |
| 168 | + const configs = options.configs |
| 169 | + ? Object.fromEntries( |
| 170 | + Object.entries(options.configs).map(([toolName, config]) => [ |
| 171 | + toolName, |
| 172 | + { |
| 173 | + enabled: config.enabled, |
| 174 | + defer_loading: config.deferLoading, |
| 175 | + }, |
| 176 | + ]) |
| 177 | + ) |
| 178 | + : undefined; |
| 179 | + |
| 180 | + return { |
| 181 | + type: "mcp_toolset", |
| 182 | + mcp_server_name: options.serverName, |
| 183 | + default_config: defaultConfig, |
| 184 | + configs, |
| 185 | + cache_control: options.cacheControl, |
| 186 | + }; |
| 187 | +} |
0 commit comments