package tech.easyflow.ai.easyagents.tool;

import com.easyagents.core.model.chat.tool.BaseTool;
import com.easyagents.core.model.chat.tool.Tool;
import com.easyagents.mcp.client.McpClientManager;
import tech.easyflow.ai.entity.Mcp;
import tech.easyflow.ai.service.McpService;
import tech.easyflow.ai.service.impl.McpServiceImpl;
import tech.easyflow.common.util.SpringContextUtil;
import tech.easyflow.common.util.StringUtil;

import java.math.BigInteger;
import java.util.Map;

/**
 * @deprecated 该类仅用于 Bot 旧版 function/tool 链路的 MCP 工具调用。
 * 后续请迁移到 agent-runtime 的 MCP 工具适配链路。
 */
@Deprecated(since = "0.4", forRemoval = false)
public class McpTool extends BaseTool {
    private BigInteger mcpId;

    @Override
    public Object invoke(Map<String, Object> argsMap) {
        return runMcp(this.mcpId, argsMap);
    }

    public Object runMcp(BigInteger mcpId, Map<String, Object> argsMap) {

        McpService mcpService = SpringContextUtil.getBean(McpService.class);
        Mcp mcp = mcpService.getMapper().selectOneById(mcpId);
        String serverName = McpServiceImpl.getFirstMcpServerName(mcp.getConfigJson());
        if (StringUtil.hasText(serverName)) {
            McpClientManager mcpClientManager = McpClientManager.getInstance();
            Tool mcpTool = mcpClientManager.getMcpTool(serverName, this.name);
            Object result = mcpTool.invoke(argsMap);
            return result;
        }
        return null;
    }


    public void setMcpId(BigInteger mcpId) {
        this.mcpId = mcpId;
    }

    public BigInteger getMcpId() {
        return mcpId;
    }
}
