{"version":3,"file":"mcp-agent.mjs","sources":["webpack://@multimodal/mcp-agent/./src/mcp-agent.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport { Agent, ToolDefinition } from '@multimodal/agent';\nimport { MCPAgentOptions, IMCPClient, MCPServerRegistry } from './mcp-types';\nimport { MCPClient } from './mcp-client';\nimport { MCPClientV2 } from './mcp-client-v2';\nimport { MCPToolAdapter } from './mcp-tool-adapter';\n\nexport class MCPAgent extends Agent {\n  private mcpClients: Map<string, IMCPClient> = new Map();\n  private mcpServerConfig: MCPServerRegistry;\n  private clientVersion: 'v1' | 'v2';\n\n  constructor(options: MCPAgentOptions) {\n    // Create a new agent with the base options\n    super(options);\n\n    this.mcpServerConfig = options.mcpServers;\n    this.clientVersion = options.mcpClientVersion ?? 'v2';\n  }\n\n  /**\n   * Initialize the MCP Agent and connect to MCP servers\n   */\n  async initialize(): Promise<void> {\n    // Initialize MCP clients and register tools\n    for (const [serverName, config] of Object.entries(this.mcpServerConfig)) {\n      try {\n        this.logger.info(`🔌 Connecting to MCP server: ${serverName}`);\n\n        // Create appropriate client based on clientVersion\n        let mcpClient: IMCPClient;\n\n        if (this.clientVersion === 'v2') {\n          mcpClient = new MCPClientV2(serverName, config, this.logger);\n        } else {\n          // Default to v1\n          mcpClient = new MCPClient(serverName, config, this.logger);\n        }\n\n        // Initialize the client and get tools\n        await mcpClient.initialize();\n\n        // Store the client for later use\n        this.mcpClients.set(serverName, mcpClient);\n\n        // Create and register tool adapters\n        const toolAdapter = new MCPToolAdapter(mcpClient, serverName);\n        const tools = toolAdapter.createTools();\n\n        // Register each tool with the agent\n        for (const tool of tools) {\n          this.registerTool(tool as unknown as ToolDefinition);\n        }\n\n        this.logger.success(`✅ Connected to MCP server ${serverName} with ${tools.length} tools`);\n      } catch (error) {\n        this.logger.error(\n          `❌ Failed to connect to MCP server ${serverName}: ${error instanceof Error ? error.message : JSON.stringify(error)}`,\n        );\n        throw new Error(\n          `❌ Failed to connect to MCP server ${serverName}: ${error instanceof Error ? error.message : JSON.stringify(error)}`,\n        );\n      }\n    }\n\n    // Call it to update the initialization state.\n    super.initialize();\n  }\n\n  /**\n   * Clean up resources when done\n   */\n  async cleanup(): Promise<void> {\n    for (const [serverName, client] of this.mcpClients.entries()) {\n      try {\n        await client.close();\n        this.logger.info(`✅ Closed connection to MCP server: ${serverName}`);\n      } catch (error) {\n        this.logger.error(`❌ Error closing MCP client ${serverName}: ${error}`);\n      }\n    }\n    this.mcpClients.clear();\n  }\n}\n"],"names":["MCPAgent","Agent","serverName","config","Object","mcpClient","MCPClientV2","MCPClient","toolAdapter","MCPToolAdapter","tools","tool","error","Error","JSON","client","options","Map"],"mappings":";;;;;;;;AAGC;;;;;;;;;;AAOM,MAAMA,iBAAiBC,uDAAAA,KAAKA;IAgBjC,MAAM,aAA4B;QAEhC,KAAK,MAAM,CAACC,YAAYC,OAAO,IAAIC,OAAO,OAAO,CAAC,IAAI,CAAC,eAAe,EACpE,IAAI;YACF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,6BAA6B,EAAEF,YAAY;YAG7D,IAAIG;YAGFA,YADE,AAAuB,SAAvB,IAAI,CAAC,aAAa,GACR,IAAIC,wDAAAA,WAAWA,CAACJ,YAAYC,QAAQ,IAAI,CAAC,MAAM,IAG/C,IAAII,qDAAAA,SAASA,CAACL,YAAYC,QAAQ,IAAI,CAAC,MAAM;YAI3D,MAAME,UAAU,UAAU;YAG1B,IAAI,CAAC,UAAU,CAAC,GAAG,CAACH,YAAYG;YAGhC,MAAMG,cAAc,IAAIC,2DAAAA,cAAcA,CAACJ,WAAWH;YAClD,MAAMQ,QAAQF,YAAY,WAAW;YAGrC,KAAK,MAAMG,QAAQD,MACjB,IAAI,CAAC,YAAY,CAACC;YAGpB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,0BAA0B,EAAET,WAAW,MAAM,EAAEQ,MAAM,MAA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