MCPcopy Create free account
hub / github.com/MiniMax-AI/Mini-Agent / connect

Method connect

mini_agent/tools/mcp_loader.py:171–232  ·  view source on GitHub ↗

Connect to the MCP server with timeout protection.

(self)

Source from the content-addressed store, hash-verified

169 return self.execute_timeout or _default_timeout_config.execute_timeout
170
171 async def connect(self) -> bool:
172 """Connect to the MCP server with timeout protection."""
173 connect_timeout = self._get_connect_timeout()
174
175 try:
176 self.exit_stack = AsyncExitStack()
177
178 # Wrap connection with timeout
179 async with asyncio.timeout(connect_timeout):
180 if self.connection_type == "stdio":
181 read_stream, write_stream = await self._connect_stdio()
182 elif self.connection_type == "sse":
183 read_stream, write_stream = await self._connect_sse()
184 else: # http / streamable_http
185 read_stream, write_stream = await self._connect_streamable_http()
186
187 # Enter client session context
188 session = await self.exit_stack.enter_async_context(ClientSession(read_stream, write_stream))
189 self.session = session
190
191 # Initialize the session
192 await session.initialize()
193
194 # List available tools
195 tools_list = await session.list_tools()
196
197 # Wrap each tool with execute timeout
198 execute_timeout = self._get_execute_timeout()
199 for tool in tools_list.tools:
200 parameters = tool.inputSchema if hasattr(tool, "inputSchema") else {}
201 mcp_tool = MCPTool(
202 name=tool.name,
203 description=tool.description or "",
204 parameters=parameters,
205 session=session,
206 execute_timeout=execute_timeout,
207 )
208 self.tools.append(mcp_tool)
209
210 conn_info = self.url if self.url else self.command
211 print(f"✓ Connected to MCP server '{self.name}' ({self.connection_type}: {conn_info}) - loaded {len(self.tools)} tools")
212 for tool in self.tools:
213 desc = tool.description[:60] if len(tool.description) > 60 else tool.description
214 print(f" - {tool.name}: {desc}...")
215 return True
216
217 except TimeoutError:
218 print(f"✗ Connection to MCP server '{self.name}' timed out after {connect_timeout}s")
219 if self.exit_stack:
220 await self.exit_stack.aclose()
221 self.exit_stack = None
222 return False
223
224 except Exception as e:
225 print(f"✗ Failed to connect to MCP server '{self.name}': {e}")
226 if self.exit_stack:
227 await self.exit_stack.aclose()
228 self.exit_stack = None

Calls 8

_get_connect_timeoutMethod · 0.95
_connect_stdioMethod · 0.95
_connect_sseMethod · 0.95
_get_execute_timeoutMethod · 0.95
MCPToolClass · 0.85
initializeMethod · 0.80
list_toolsMethod · 0.80