{"version":3,"file":"mcp-client-v2.mjs","sources":["webpack://@multimodal/mcp-agent/./src/mcp-client-v2.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { MCPClient as V2Client } from '@agent-infra/mcp-client';\nimport { Tool } from '@modelcontextprotocol/sdk/types.js';\nimport { IMCPClient, MCPClientResult, MCPServerConfig } from './mcp-types';\nimport type { Logger } from '@agent-infra/logger';\n\n/**\n * Implementation of IMCPClient using @agent-infra/mcp-client\n */\nexport class MCPClientV2 implements IMCPClient {\n  private v2Client: V2Client;\n  private serverName: string;\n  private tools: Tool[] = [];\n  private isInitialized = false;\n\n  constructor(\n    serverName: string,\n    config: MCPServerConfig,\n    private logger: Logger,\n  ) {\n    this.serverName = serverName;\n\n    // Create the v2 client with appropriate configuration\n    this.v2Client = new V2Client(\n      [\n        {\n          name: serverName,\n          ...config,\n          status: 'activate',\n        },\n      ],\n      { isDebug: false },\n    );\n  }\n\n  async initialize(): Promise<Tool[]> {\n    if (this.isInitialized) {\n      return this.tools;\n    }\n\n    try {\n      this.logger.info(`Initializing MCP client v2 for ${this.serverName}`);\n      await this.v2Client.init();\n      this.tools = await this.v2Client.listTools(this.serverName as string);\n      this.isInitialized = true;\n      this.logger.success(\n        `MCP client v2 initialized successfully for ${this.serverName}, found ${this.tools.length} tools`,\n      );\n      return this.tools;\n    } catch (error) {\n      this.logger.error(`Error initializing v2 MCP client for ${this.serverName}:`, error);\n      throw error;\n    }\n  }\n\n  async callTool(toolName: string, args: unknown): Promise<MCPClientResult> {\n    if (!this.isInitialized) {\n      this.logger.info(`Client not initialized, initializing before tool call: ${toolName}`);\n      await this.initialize();\n    }\n\n    try {\n      this.logger.info(`Calling tool ${toolName} with client ${this.serverName}`);\n      const result = await this.v2Client.callTool({\n        client: this.serverName as string,\n        name: toolName,\n        args,\n      });\n\n      // Convert the v2 result format to v1 format\n      return { content: result.content };\n    } catch (error) {\n      this.logger.error(`Error calling MCP tool ${toolName}:`, error);\n      return {\n        content: `Error: Failed to execute tool ${toolName}: ${error}`,\n      };\n    }\n  }\n\n  async close(): Promise<void> {\n    if (this.isInitialized) {\n      this.logger.info(`Closing MCP client v2 for ${this.serverName}`);\n      await this.v2Client.cleanup();\n      this.isInitialized = false;\n      this.tools = [];\n      this.logger.success(`MCP client v2 closed successfully for ${this.serverName}`);\n    }\n  }\n\n  getTools(): Tool[] {\n    return [...this.tools];\n  }\n}\n"],"names":["MCPClientV2","error","toolName","args","result","serverName","config","logger","V2Client"],"mappings":";;;;;AAGC;;;;;;;;;;AAUM,MAAMA;IA0BX,MAAM,aAA8B;QAClC,IAAI,IAAI,CAAC,aAAa,EACpB,OAAO,IAAI,CAAC,KAAK;QAGnB,IAAI;YACF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,+BAA+B,EAAE,IAAI,CAAC,UAAU,EAAE;YACpE,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI;YACxB,IAAI,CAAC,KAAK,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU;YAC1D,IAAI,CAAC,aAAa,GAAG;YACrB,IAAI,CAAC,MAAM,CAAC,OAAO,CACjB,CAAC,2CAA2C,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;YAEnG,OAAO,IAAI,CAAC,KAAK;QACnB,EAAE,OAAOC,OAAO;YACd,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,qCAAqC,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,EAAEA;YAC9E,MAAMA;QACR;IACF;IAEA,MAAM,SAASC,QAAgB,EAAEC,IAAa,EAA4B;QACxE,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE;YACvB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,uDAAuD,EAAED,UAAU;YACrF,M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