Spaces:
Running
Running
File size: 27,560 Bytes
8b54db1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | """
MCP Manager for CodeAct Agent.
Manages MCP (Model Context Protocol) tools and servers.
"""
import os
import sys
import types
from pathlib import Path
from typing import Dict, List, Optional, Any
from rich.console import Console
class MCPManager:
"""Manages MCP (Model Context Protocol) tools and servers."""
def __init__(self, console_display=None):
self.mcp_functions = {}
self.console = console_display.console if console_display else Console()
def has_mcp_functions(self) -> bool:
"""Check if MCP functions are available."""
return bool(self.mcp_functions)
def group_tools_by_server(self, mcp_tools: Dict[str, dict]) -> Dict[str, List[Dict]]:
"""Group MCP tools by server name."""
servers = {}
for tool_name, tool_info in mcp_tools.items():
server_name = tool_info.get('server', 'unknown')
if server_name not in servers:
servers[server_name] = []
servers[server_name].append({
'name': tool_name,
'description': tool_info.get('description', 'No description')
})
return servers
def add_mcp(self, config_path: str = "./mcp_config.yaml", tool_registry=None) -> None:
"""Add MCP tools from configuration file."""
try:
import asyncio
import yaml
except ImportError as e:
raise ImportError(f"Required packages not available: {e}. Install with: pip install pyyaml") from e
try:
import nest_asyncio
from mcp import ClientSession
from mcp.client.stdio import StdioServerParameters, stdio_client
from mcp.client.streamable_http import streamablehttp_client
from langchain_mcp_adapters.tools import _list_all_tools
nest_asyncio.apply()
except ImportError as e:
raise ImportError(f"MCP packages not available: {e}. Install with: pip install mcp langchain-mcp-adapters") from e
def discover_mcp_tools_sync(server_params: StdioServerParameters) -> List[dict]:
"""Discover available tools from MCP server synchronously."""
try:
async def _discover_async():
async with stdio_client(server_params) as (reader, writer):
async with ClientSession(reader, writer) as session:
await session.initialize()
tools_result = await session.list_tools()
tools = tools_result.tools if hasattr(tools_result, "tools") else tools_result
print(tools)
discovered_tools = []
for tool in tools:
if hasattr(tool, "name"):
# Ensure description is never empty or None
description = getattr(tool, 'description', None)
if not description or description.strip() == "":
# Generate description from tool name
formatted_name = tool.name.replace('_', ' ').title()
description = f"MCP tool: {formatted_name}"
discovered_tools.append({
"name": tool.name,
"description": description,
"inputSchema": tool.inputSchema,
})
else:
print(f"Warning: Skipping tool with no name attribute: {tool}")
return discovered_tools
return asyncio.run(_discover_async())
except Exception as e:
print(f"Failed to discover tools: {e}")
return []
def discover_remote_mcp_tools_sync(url: str) -> List[dict]:
"""Discover available tools from remote MCP server synchronously."""
try:
async def _discover_remote_async():
async with streamablehttp_client(url) as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
tools = await _list_all_tools(session)
discovered_tools = []
for tool in tools:
if hasattr(tool, "name"):
# Ensure description is never empty or None
description = getattr(tool, 'description', None)
if not description or description.strip() == "":
# Generate description from tool name
formatted_name = tool.name.replace('_', ' ').title()
description = f"MCP tool: {formatted_name}"
discovered_tools.append({
"name": tool.name,
"description": description,
"inputSchema": tool.inputSchema,
})
else:
print(f"Warning: Skipping tool with no name attribute: {tool}")
return discovered_tools
return asyncio.run(_discover_remote_async())
except Exception as e:
print(f"Failed to discover remote tools from {url}: {e}")
return []
def make_mcp_wrapper(cmd: str, args: List[str], tool_name: str, doc: str, env_vars: dict = None):
"""Create a synchronous wrapper for an async MCP tool call."""
def sync_tool_wrapper(**kwargs):
"""Synchronous wrapper for MCP tool execution."""
try:
server_params = StdioServerParameters(command=cmd, args=args, env=env_vars)
async def async_tool_call():
async with stdio_client(server_params) as (reader, writer):
async with ClientSession(reader, writer) as session:
await session.initialize()
result = await session.call_tool(tool_name, kwargs)
content = result.content[0]
if hasattr(content, "model_dump_json"):
return content.model_dump_json()
elif hasattr(content, "json"):
return content.json()
return content.text
try:
loop = asyncio.get_running_loop()
return loop.create_task(async_tool_call())
except RuntimeError:
return asyncio.run(async_tool_call())
except Exception as e:
raise RuntimeError(f"MCP tool execution failed for '{tool_name}': {e}") from e
sync_tool_wrapper.__name__ = tool_name
sync_tool_wrapper.__doc__ = doc
return sync_tool_wrapper
def make_remote_mcp_wrapper(url: str, tool_name: str, doc: str):
"""Create a synchronous wrapper for an async remote MCP tool call."""
def sync_tool_wrapper(**kwargs):
"""Synchronous wrapper for remote MCP tool execution."""
try:
async def async_tool_call():
async with streamablehttp_client(url) as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.call_tool(tool_name, kwargs)
content = result.content[0]
if hasattr(content, "model_dump_json"):
return content.model_dump_json()
elif hasattr(content, "json"):
return content.json()
return content.text
try:
loop = asyncio.get_running_loop()
return loop.create_task(async_tool_call())
except RuntimeError:
return asyncio.run(async_tool_call())
except Exception as e:
raise RuntimeError(f"Remote MCP tool execution failed for '{tool_name}': {e}") from e
sync_tool_wrapper.__name__ = tool_name
sync_tool_wrapper.__doc__ = doc
return sync_tool_wrapper
# Load and validate configuration
try:
config_content = Path(config_path).read_text(encoding="utf-8")
cfg = yaml.safe_load(config_content) or {}
except FileNotFoundError:
raise FileNotFoundError(f"MCP config file not found: {config_path}") from None
except yaml.YAMLError as e:
raise yaml.YAMLError(f"Invalid YAML in MCP config: {e}") from e
mcp_servers = cfg.get("mcp_servers", {})
if not mcp_servers:
print("Warning: No MCP servers found in configuration")
return
# Process each MCP server configuration
for server_name, server_meta in mcp_servers.items():
if not server_meta.get("enabled", True):
continue
# Check if this is a remote server configuration
remote_url = server_meta.get("url")
if remote_url:
# Handle remote MCP server
self._process_remote_server(server_name, server_meta, remote_url, tool_registry, discover_remote_mcp_tools_sync, make_remote_mcp_wrapper)
continue
# Handle local MCP server (existing logic)
# Validate command configuration
cmd_list = server_meta.get("command", [])
if not cmd_list or not isinstance(cmd_list, list):
print(f"Warning: Invalid command configuration for server '{server_name}'")
continue
cmd, *args = cmd_list
# Process environment variables
env_vars = server_meta.get("env", {})
if env_vars:
processed_env = {}
for key, value in env_vars.items():
if isinstance(value, str) and value.startswith("${") and value.endswith("}"):
var_name = value[2:-1]
processed_env[key] = os.getenv(var_name, "")
else:
processed_env[key] = value
env_vars = processed_env
# Create module namespace for this MCP server
mcp_module_name = f"mcp_servers.{server_name}"
if mcp_module_name not in sys.modules:
sys.modules[mcp_module_name] = types.ModuleType(mcp_module_name)
server_module = sys.modules[mcp_module_name]
tools_config = server_meta.get("tools", [])
# Auto-discover tools if not manually configured
if not tools_config:
try:
server_params = StdioServerParameters(command=cmd, args=args, env=env_vars)
tools_config = discover_mcp_tools_sync(server_params)
if tools_config:
print(f"π Discovered {len(tools_config)} tools from {server_name} MCP server")
else:
print(f"Warning: No tools discovered from {server_name} MCP server")
continue
except Exception as e:
print(f"Failed to discover tools for {server_name}: {e}")
continue
# Register each tool
tools_added = 0
for tool_meta in tools_config:
if isinstance(tool_meta, dict) and "biomni_name" in tool_meta:
# Manual tool definition (Biomni-style)
tool_name = tool_meta.get("biomni_name")
description = tool_meta.get("description", f"MCP tool: {tool_name}")
parameters = tool_meta.get("parameters", {})
required_param_names = []
for param_name, param_spec in parameters.items():
if param_spec.get("required", False):
required_param_names.append(param_name)
else:
# Auto-discovered tool
tool_name = tool_meta.get("name")
description = tool_meta.get("description", "")
# Ensure description is never empty
if not description or description.strip() == "":
formatted_name = tool_name.replace('_', ' ').title()
description = f"MCP tool: {formatted_name}"
input_schema = tool_meta.get("inputSchema", {})
parameters = input_schema.get("properties", {})
required_param_names = input_schema.get("required", [])
if not tool_name:
print(f"Warning: Skipping tool with no name in {server_name}")
continue
# Create wrapper function
wrapper_function = make_mcp_wrapper(cmd, args, tool_name, description, env_vars)
# Add to module namespace
setattr(server_module, tool_name, wrapper_function)
# Store in MCP functions registry with parameter information
self.mcp_functions[tool_name] = {
"function": wrapper_function,
"server": server_name,
"module": mcp_module_name,
"description": description,
"required_parameters": [], # Will be populated below
"optional_parameters": [] # Will be populated below
}
# Register with tool registry if available
if tool_registry:
from .tool_registry import ToolRegistry
# Create tool schema with proper parameter information
required_params = []
optional_params = []
for param_name, param_spec in parameters.items():
param_info = {
"name": param_name,
"type": param_spec.get("type", "string"),
"description": param_spec.get("description", f"Parameter {param_name}"),
}
# Extract enum/literal values if present
if "enum" in param_spec:
param_info["enum"] = param_spec["enum"]
# Handle anyOf schemas (common for optional literal types)
if "anyOf" in param_spec:
# Look for enum in anyOf schemas
for schema_option in param_spec["anyOf"]:
if "enum" in schema_option:
param_info["enum"] = schema_option["enum"]
# Update type if specified
if "type" in schema_option:
param_info["type"] = schema_option["type"]
break
# Handle oneOf schemas (alternative union syntax)
if "oneOf" in param_spec:
# Look for enum in oneOf schemas
for schema_option in param_spec["oneOf"]:
if "enum" in schema_option:
param_info["enum"] = schema_option["enum"]
if "type" in schema_option:
param_info["type"] = schema_option["type"]
break
# Determine if parameter is required based on:
# 1. Explicit required list (if provided)
# 2. If no default value is present in the schema
is_required = (param_name in required_param_names) or ("default" not in param_spec)
if is_required:
required_params.append(param_info)
else:
param_info["default"] = param_spec.get("default")
optional_params.append(param_info)
# Create complete tool schema
tool_schema = {
"name": tool_name,
"description": description,
"required_parameters": required_params,
"optional_parameters": optional_params,
"module": mcp_module_name,
}
success = tool_registry.register_tool(tool_schema, mcp_module_name)
if success:
tool_registry._name_to_function[tool_name] = wrapper_function
tools_added += 1
# Update MCP functions registry with parameter information
self.mcp_functions[tool_name]["required_parameters"] = required_params
self.mcp_functions[tool_name]["optional_parameters"] = optional_params
if tools_added > 0:
print(f"β
Added {tools_added} MCP tools from {server_name} server")
print(f"π οΈ Total MCP tools loaded: {len(self.mcp_functions)}")
def _process_remote_server(self, server_name: str, server_meta: dict, remote_url: str, tool_registry, discover_remote_mcp_tools_sync, make_remote_mcp_wrapper):
"""Process a remote MCP server configuration."""
import sys
import types
# Create module namespace for this remote MCP server
mcp_module_name = f"mcp_servers.{server_name}"
if mcp_module_name not in sys.modules:
sys.modules[mcp_module_name] = types.ModuleType(mcp_module_name)
server_module = sys.modules[mcp_module_name]
tools_config = server_meta.get("tools", [])
# Auto-discover tools if not manually configured
if not tools_config:
try:
tools_config = discover_remote_mcp_tools_sync(remote_url)
if tools_config:
print(f"π Discovered {len(tools_config)} tools from {server_name} remote MCP server")
else:
print(f"Warning: No tools discovered from {server_name} remote MCP server")
return
except Exception as e:
print(f"Failed to discover tools for remote {server_name}: {e}")
return
# Register each tool
tools_added = 0
for tool_meta in tools_config:
if isinstance(tool_meta, dict) and "biomni_name" in tool_meta:
# Manual tool definition (Biomni-style)
tool_name = tool_meta.get("biomni_name")
description = tool_meta.get("description", f"Remote MCP tool: {tool_name}")
parameters = tool_meta.get("parameters", {})
required_param_names = []
for param_name, param_spec in parameters.items():
if param_spec.get("required", False):
required_param_names.append(param_name)
else:
# Auto-discovered tool
tool_name = tool_meta.get("name")
description = tool_meta.get("description", "")
# Ensure description is never empty
if not description or description.strip() == "":
formatted_name = tool_name.replace('_', ' ').title()
description = f"Remote MCP tool: {formatted_name}"
input_schema = tool_meta.get("inputSchema", {})
parameters = input_schema.get("properties", {})
required_param_names = input_schema.get("required", [])
if not tool_name:
print(f"Warning: Skipping tool with no name in remote {server_name}")
continue
# Create wrapper function for remote tool
wrapper_function = make_remote_mcp_wrapper(remote_url, tool_name, description)
# Add to module namespace
setattr(server_module, tool_name, wrapper_function)
# Store in MCP functions registry with parameter information
self.mcp_functions[tool_name] = {
"function": wrapper_function,
"server": server_name,
"module": mcp_module_name,
"description": description,
"required_parameters": [], # Will be populated below
"optional_parameters": [], # Will be populated below
"remote_url": remote_url
}
# Register with tool registry if available
if tool_registry:
from .tool_registry import ToolRegistry
# Create tool schema with proper parameter information
required_params = []
optional_params = []
for param_name, param_spec in parameters.items():
param_info = {
"name": param_name,
"type": param_spec.get("type", "string"),
"description": param_spec.get("description", f"Parameter {param_name}"),
}
# Extract enum/literal values if present
if "enum" in param_spec:
param_info["enum"] = param_spec["enum"]
# Handle anyOf schemas (common for optional literal types)
if "anyOf" in param_spec:
# Look for enum in anyOf schemas
for schema_option in param_spec["anyOf"]:
if "enum" in schema_option:
param_info["enum"] = schema_option["enum"]
# Update type if specified
if "type" in schema_option:
param_info["type"] = schema_option["type"]
break
# Handle oneOf schemas (alternative union syntax)
if "oneOf" in param_spec:
# Look for enum in oneOf schemas
for schema_option in param_spec["oneOf"]:
if "enum" in schema_option:
param_info["enum"] = schema_option["enum"]
if "type" in schema_option:
param_info["type"] = schema_option["type"]
break
# Determine if parameter is required based on:
# 1. Explicit required list (if provided)
# 2. If no default value is present in the schema
is_required = (param_name in required_param_names) or ("default" not in param_spec)
if is_required:
required_params.append(param_info)
else:
param_info["default"] = param_spec.get("default")
optional_params.append(param_info)
# Create complete tool schema
tool_schema = {
"name": tool_name,
"description": description,
"required_parameters": required_params,
"optional_parameters": optional_params,
"module": mcp_module_name,
}
success = tool_registry.register_tool(tool_schema, mcp_module_name)
if success:
tool_registry._name_to_function[tool_name] = wrapper_function
tools_added += 1
# Update MCP functions registry with parameter information
self.mcp_functions[tool_name]["required_parameters"] = required_params
self.mcp_functions[tool_name]["optional_parameters"] = optional_params
if tools_added > 0:
print(f"β
Added {tools_added} remote MCP tools from {server_name} server")
def list_mcp_tools(self) -> Dict[str, dict]:
"""List all loaded MCP tools."""
return self.mcp_functions.copy()
def remove_mcp_tool(self, tool_name: str, tool_registry=None) -> bool:
"""Remove an MCP tool by name."""
if not self.has_mcp_functions() or tool_name not in self.mcp_functions:
return False
# Remove from tool registry
if tool_registry:
tool_registry.remove_tool_by_name(tool_name)
# Remove from MCP functions
del self.mcp_functions[tool_name]
return True
def show_mcp_status(self) -> None:
"""Display detailed MCP status information to the user."""
if not self.has_mcp_functions():
self.console.print("π No MCP tools loaded")
return
mcp_tools = self.mcp_functions
if not mcp_tools:
self.console.print("π MCP system initialized but no tools loaded")
return
# Group tools by server
servers = self.group_tools_by_server(mcp_tools)
# Display server information
self.console.print(f"\nπ MCP Status Report:")
self.console.print(f" π Total servers: {len(servers)}")
self.console.print(f" π οΈ Total MCP tools: {len(mcp_tools)}")
for server_name, tools in servers.items():
self.console.print(f"\n π‘ Server: {server_name}")
self.console.print(f" Status: β
Active ({len(tools)} tools)")
for tool in tools:
self.console.print(f" β’ {tool['name']}: {tool['description']}")
def get_mcp_summary(self) -> Dict[str, any]:
"""Get a summary of MCP tools for programmatic access."""
if not self.has_mcp_functions():
return {"total_tools": 0, "servers": {}, "tools": {}}
mcp_tools = self.mcp_functions
# Group tools by server but only get tool names
servers = {}
for tool_name, tool_info in mcp_tools.items():
server_name = tool_info.get('server', 'unknown')
if server_name not in servers:
servers[server_name] = []
servers[server_name].append(tool_name)
return {
"total_tools": len(mcp_tools),
"total_servers": len(servers),
"servers": servers,
"tools": {name: info.get('description', '') for name, info in mcp_tools.items()}
} |