UNPKG

@langchain/anthropic

Version:
1 lines 5.8 kB
{"version":3,"file":"mcpToolset.cjs","names":["options: MCPToolsetOptions"],"sources":["../../src/tools/mcpToolset.ts"],"sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport { type ServerTool } from \"@langchain/core/tools\";\n\n/**\n * Configuration for a single tool in the MCP toolset.\n */\nexport interface MCPToolConfig {\n /**\n * Whether this tool is enabled.\n * @default true\n */\n enabled?: boolean;\n /**\n * If true, tool description is not sent to the model initially.\n * Used with Tool Search Tool for on-demand loading.\n * @default false\n */\n deferLoading?: boolean;\n}\n\n/**\n * Per-tool configuration overrides.\n * Keys are tool names, values are configuration objects.\n */\nexport type MCPToolConfigs = Record<string, MCPToolConfig>;\n\n/**\n * Options for creating an MCP toolset.\n */\nexport interface MCPToolsetOptions {\n /**\n * Must match a server name defined in the `mcp_servers` array in call options.\n */\n serverName: string;\n /**\n * Default configuration applied to all tools in this toolset.\n * Individual tool configs will override these defaults.\n */\n defaultConfig?: MCPToolConfig;\n /**\n * Per-tool configuration overrides.\n * Keys are tool names, values are configuration objects.\n */\n configs?: MCPToolConfigs;\n /**\n * Create a cache control breakpoint at this content block.\n */\n cacheControl?: Anthropic.Beta.BetaCacheControlEphemeral;\n}\n\n/**\n * Creates an MCP toolset that connects to a remote MCP server to access its tools.\n * This enables Claude to use tools from MCP servers without implementing a separate MCP client.\n *\n * @note This tool requires the beta header `mcp-client-2025-11-20` in API requests.\n * The header is automatically added when using this tool.\n *\n * @see {@link https://docs.anthropic.com/en/docs/agents-and-tools/mcp-connector | Anthropic MCP Connector Documentation}\n * @param options - Configuration options for the MCP toolset\n * @returns An MCP toolset definition to be passed to the Anthropic API tools array\n *\n * @example\n * ```typescript\n * import { ChatAnthropic, tools } from \"@langchain/anthropic\";\n *\n * const model = new ChatAnthropic({\n * model: \"claude-sonnet-4-5-20250929\",\n * });\n *\n * // Basic usage - enable all tools from an MCP server\n * const response = await model.invoke(\"What tools do you have available?\", {\n * mcp_servers: [{\n * type: \"url\",\n * url: \"https://example-server.modelcontextprotocol.io/sse\",\n * name: \"example-mcp\",\n * authorization_token: \"YOUR_TOKEN\",\n * }],\n * tools: [\n * tools.mcpToolset_20251120({ serverName: \"example-mcp\" }),\n * ],\n * });\n *\n * // Allowlist pattern - enable only specific tools\n * const responseAllowlist = await model.invoke(\"Search for events\", {\n * mcp_servers: [{\n * type: \"url\",\n * url: \"https://calendar.example.com/sse\",\n * name: \"google-calendar-mcp\",\n * authorization_token: \"YOUR_TOKEN\",\n * }],\n * tools: [\n * tools.mcpToolset_20251120({\n * serverName: \"google-calendar-mcp\",\n * defaultConfig: { enabled: false },\n * configs: {\n * search_events: { enabled: true },\n * create_event: { enabled: true },\n * },\n * }),\n * ],\n * });\n *\n * // Denylist pattern - disable specific tools\n * const responseDenylist = await model.invoke(\"List my events\", {\n * mcp_servers: [{\n * type: \"url\",\n * url: \"https://calendar.example.com/sse\",\n * name: \"google-calendar-mcp\",\n * authorization_token: \"YOUR_TOKEN\",\n * }],\n * tools: [\n * tools.mcpToolset_20251120({\n * serverName: \"google-calendar-mcp\",\n * configs: {\n * delete_all_events: { enabled: false },\n * share_calendar_publicly: { enabled: false },\n * },\n * }),\n * ],\n * });\n *\n * // With deferred loading for use with Tool Search Tool\n * const responseDeferred = await model.invoke(\"Search for tools\", {\n * mcp_servers: [{\n * type: \"url\",\n * url: \"https://example.com/sse\",\n * name: \"example-mcp\",\n * }],\n * tools: [\n * tools.toolSearchRegex_20251119(),\n * tools.mcpToolset_20251120({\n * serverName: \"example-mcp\",\n * defaultConfig: { deferLoading: true },\n * }),\n * ],\n * });\n * ```\n */\nexport function mcpToolset_20251120(options: MCPToolsetOptions): ServerTool {\n const defaultConfig =\n options.defaultConfig?.enabled !== undefined ||\n options.defaultConfig?.deferLoading !== undefined\n ? {\n enabled: options.defaultConfig?.enabled,\n defer_loading: options.defaultConfig?.deferLoading,\n }\n : undefined;\n\n const configs = options.configs\n ? Object.fromEntries(\n Object.entries(options.configs).map(([toolName, config]) => [\n toolName,\n {\n enabled: config.enabled,\n defer_loading: config.deferLoading,\n },\n ])\n )\n : undefined;\n\n return {\n type: \"mcp_toolset\",\n mcp_server_name: options.serverName,\n default_config: defaultConfig,\n configs,\n cache_control: options.cacheControl,\n } satisfies Anthropic.Beta.BetaMCPToolset;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0IA,SAAgB,oBAAoBA,SAAwC;CAC1E,MAAM,gBACJ,QAAQ,eAAe,YAAY,UACnC,QAAQ,eAAe,iBAAiB,SACpC;EACE,SAAS,QAAQ,eAAe;EAChC,eAAe,QAAQ,eAAe;CACvC,IACD;CAEN,MAAM,UAAU,QAAQ,UACpB,OAAO,YACL,OAAO,QAAQ,QAAQ,QAAQ,CAAC,IAAI,CAAC,CAAC,UAAU,OAAO,KAAK,CAC1D,UACA;EACE,SAAS,OAAO;EAChB,eAAe,OAAO;CACvB,CACF,EAAC,CACH,GACD;AAEJ,QAAO;EACL,MAAM;EACN,iBAAiB,QAAQ;EACzB,gBAAgB;EAChB;EACA,eAAe,QAAQ;CACxB;AACF"}