|
| 1 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 14 | + |
| 15 | +import os |
| 16 | +from typing import Any, Dict, List, Optional |
| 17 | + |
| 18 | +from camel.toolkits import BaseToolkit, FunctionTool |
| 19 | + |
| 20 | +from .mcp_toolkit import MCPToolkit |
| 21 | + |
| 22 | + |
| 23 | +class MinimaxMCPToolkit(BaseToolkit): |
| 24 | + r"""MinimaxMCPToolkit provides an interface for interacting with |
| 25 | + MiniMax AI services using the MiniMax MCP server. |
| 26 | +
|
| 27 | + This toolkit enables access to MiniMax's multimedia generation |
| 28 | + capabilities including text-to-audio, voice cloning, video generation, |
| 29 | + image generation, music generation, and voice design. |
| 30 | +
|
| 31 | + This toolkit can be used as an async context manager for automatic |
| 32 | + connection management: |
| 33 | +
|
| 34 | + # Using explicit API key |
| 35 | + async with MinimaxMCPToolkit(api_key="your-key") as toolkit: |
| 36 | + tools = toolkit.get_tools() |
| 37 | + # Toolkit is automatically disconnected when exiting |
| 38 | +
|
| 39 | + # Using environment variables (recommended for security) |
| 40 | + # Set MINIMAX_API_KEY=your-key in environment |
| 41 | + async with MinimaxMCPToolkit() as toolkit: |
| 42 | + tools = toolkit.get_tools() |
| 43 | +
|
| 44 | + Environment Variables: |
| 45 | + MINIMAX_API_KEY: MiniMax API key for authentication |
| 46 | + MINIMAX_API_HOST: API host URL (default: https://api.minimax.io) |
| 47 | + MINIMAX_MCP_BASE_PATH: Base path for output files |
| 48 | +
|
| 49 | + Attributes: |
| 50 | + timeout (Optional[float]): Connection timeout in seconds. |
| 51 | + (default: :obj:`None`) |
| 52 | + """ |
| 53 | + |
| 54 | + def __init__( |
| 55 | + self, |
| 56 | + api_key: Optional[str] = None, |
| 57 | + api_host: str = "https://api.minimax.io", |
| 58 | + base_path: Optional[str] = None, |
| 59 | + timeout: Optional[float] = None, |
| 60 | + ) -> None: |
| 61 | + r"""Initializes the MinimaxMCPToolkit. |
| 62 | +
|
| 63 | + Args: |
| 64 | + api_key (Optional[str]): MiniMax API key for authentication. |
| 65 | + If None, will attempt to read from MINIMAX_API_KEY |
| 66 | + environment variable. (default: :obj:`None`) |
| 67 | + api_host (str): MiniMax API host URL. Can be either |
| 68 | + "https://api.minimax.io" (global) or |
| 69 | + "https://api.minimaxi.com" (mainland China). |
| 70 | + Can also be read from MINIMAX_API_HOST environment variable. |
| 71 | + (default: :obj:`"https://api.minimax.io"`) |
| 72 | + base_path (Optional[str]): Base path for output files. |
| 73 | + If None, uses current working directory. Can also be read |
| 74 | + from MINIMAX_MCP_BASE_PATH environment variable. |
| 75 | + (default: :obj:`None`) |
| 76 | + timeout (Optional[float]): Connection timeout in seconds. |
| 77 | + (default: :obj:`None`) |
| 78 | + """ |
| 79 | + super().__init__(timeout=timeout) |
| 80 | + |
| 81 | + # Read API key from parameter or environment variable |
| 82 | + if api_key is None: |
| 83 | + api_key = os.getenv("MINIMAX_API_KEY") |
| 84 | + |
| 85 | + if not api_key: |
| 86 | + raise ValueError( |
| 87 | + "api_key must be provided either as a parameter or through " |
| 88 | + "the MINIMAX_API_KEY environment variable" |
| 89 | + ) |
| 90 | + |
| 91 | + # Read API host from environment variable if not overridden |
| 92 | + env_api_host = os.getenv("MINIMAX_API_HOST") |
| 93 | + if env_api_host: |
| 94 | + api_host = env_api_host |
| 95 | + |
| 96 | + # Read base path from environment variable if not provided |
| 97 | + if base_path is None: |
| 98 | + base_path = os.getenv("MINIMAX_MCP_BASE_PATH") |
| 99 | + |
| 100 | + # Set up environment variables for the MCP server |
| 101 | + env = { |
| 102 | + "MINIMAX_API_KEY": api_key, |
| 103 | + "MINIMAX_API_HOST": api_host, |
| 104 | + } |
| 105 | + |
| 106 | + if base_path: |
| 107 | + env["MINIMAX_MCP_BASE_PATH"] = base_path |
| 108 | + |
| 109 | + self._mcp_toolkit = MCPToolkit( |
| 110 | + config_dict={ |
| 111 | + "mcpServers": { |
| 112 | + "minimax": { |
| 113 | + "command": "uvx", |
| 114 | + "args": ["minimax-mcp", "-y"], |
| 115 | + "env": env, |
| 116 | + } |
| 117 | + } |
| 118 | + }, |
| 119 | + timeout=timeout, |
| 120 | + ) |
| 121 | + |
| 122 | + async def connect(self): |
| 123 | + r"""Explicitly connect to the MiniMax MCP server.""" |
| 124 | + await self._mcp_toolkit.connect() |
| 125 | + |
| 126 | + async def disconnect(self): |
| 127 | + r"""Explicitly disconnect from the MiniMax MCP server.""" |
| 128 | + await self._mcp_toolkit.disconnect() |
| 129 | + |
| 130 | + @property |
| 131 | + def is_connected(self) -> bool: |
| 132 | + r"""Check if the toolkit is connected to the MCP server. |
| 133 | +
|
| 134 | + Returns: |
| 135 | + bool: True if connected, False otherwise. |
| 136 | + """ |
| 137 | + return self._mcp_toolkit.is_connected |
| 138 | + |
| 139 | + async def __aenter__(self) -> "MinimaxMCPToolkit": |
| 140 | + r"""Async context manager entry point. |
| 141 | +
|
| 142 | + Returns: |
| 143 | + MinimaxMCPToolkit: The connected toolkit instance. |
| 144 | +
|
| 145 | + Example: |
| 146 | + async with MinimaxMCPToolkit(api_key="your-key") as toolkit: |
| 147 | + tools = toolkit.get_tools() |
| 148 | + """ |
| 149 | + await self.connect() |
| 150 | + return self |
| 151 | + |
| 152 | + async def __aexit__(self, _exc_type, _exc_val, _exc_tb) -> None: |
| 153 | + r"""Async context manager exit point. |
| 154 | +
|
| 155 | + Automatically disconnects from the MiniMax MCP server. |
| 156 | + """ |
| 157 | + await self.disconnect() |
| 158 | + |
| 159 | + def get_tools(self) -> List[FunctionTool]: |
| 160 | + r"""Returns a list of tools provided by the MiniMax MCP server. |
| 161 | +
|
| 162 | + This includes tools for: |
| 163 | + - Text-to-audio conversion |
| 164 | + - Voice cloning |
| 165 | + - Video generation |
| 166 | + - Image generation |
| 167 | + - Music generation |
| 168 | + - Voice design |
| 169 | +
|
| 170 | + Returns: |
| 171 | + List[FunctionTool]: List of available MiniMax AI tools. |
| 172 | + """ |
| 173 | + return self._mcp_toolkit.get_tools() |
| 174 | + |
| 175 | + def get_text_tools(self) -> str: |
| 176 | + r"""Returns a string containing the descriptions of the tools. |
| 177 | +
|
| 178 | + Returns: |
| 179 | + str: A string containing the descriptions of all MiniMax tools. |
| 180 | + """ |
| 181 | + return self._mcp_toolkit.get_text_tools() |
| 182 | + |
| 183 | + async def call_tool( |
| 184 | + self, tool_name: str, tool_args: Dict[str, Any] |
| 185 | + ) -> Any: |
| 186 | + r"""Call a MiniMax tool by name. |
| 187 | +
|
| 188 | + Args: |
| 189 | + tool_name (str): Name of the tool to call. |
| 190 | + tool_args (Dict[str, Any]): Arguments to pass to the tool. |
| 191 | +
|
| 192 | + Returns: |
| 193 | + Any: The result of the tool call. |
| 194 | + """ |
| 195 | + return await self._mcp_toolkit.call_tool(tool_name, tool_args) |
0 commit comments