|
|
""" |
|
|
MCP client configuration and setup for bird classification agents. |
|
|
""" |
|
|
import os |
|
|
from typing import List, Dict, Any |
|
|
from langchain_mcp_adapters.client import MultiServerMCPClient |
|
|
from .config import AgentConfig |
|
|
|
|
|
class MCPClientManager: |
|
|
"""Manages MCP client connections to various servers""" |
|
|
|
|
|
@staticmethod |
|
|
async def create_classifier_client() -> MultiServerMCPClient: |
|
|
""" |
|
|
Create MCP client for Modal bird classifier only. |
|
|
|
|
|
Returns: |
|
|
MultiServerMCPClient configured for Modal server |
|
|
""" |
|
|
print("[STATUS]: Connecting to Modal MCP server...") |
|
|
|
|
|
client = MultiServerMCPClient({ |
|
|
"bird_classifier": { |
|
|
"transport": "streamable_http", |
|
|
"url": AgentConfig.MODAL_MCP_URL, |
|
|
"headers": { |
|
|
"X-API-Key": AgentConfig.BIRD_CLASSIFIER_API_KEY |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
return client |
|
|
|
|
|
@staticmethod |
|
|
async def create_multi_server_client() -> MultiServerMCPClient: |
|
|
""" |
|
|
Create MCP client for both Modal classifier and Nuthatch species database. |
|
|
|
|
|
Returns: |
|
|
MultiServerMCPClient configured for both servers |
|
|
""" |
|
|
print("[STATUS]: Connecting to Modal and Nuthatch servers...") |
|
|
|
|
|
servers_config = { |
|
|
"bird_classifier": { |
|
|
"transport": "streamable_http", |
|
|
"url": AgentConfig.MODAL_MCP_URL, |
|
|
"headers": { |
|
|
"X-API-Key": AgentConfig.BIRD_CLASSIFIER_API_KEY |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if AgentConfig.NUTHATCH_USE_STDIO: |
|
|
|
|
|
servers_config["nuthatch"] = { |
|
|
"transport": "stdio", |
|
|
"command": "python", |
|
|
"args": ["nuthatch_tools.py"], |
|
|
"env": { |
|
|
|
|
|
|
|
|
"NUTHATCH_API_KEY": AgentConfig.NUTHATCH_API_KEY, |
|
|
"NUTHATCH_BASE_URL": AgentConfig.NUTHATCH_BASE_URL |
|
|
} |
|
|
} |
|
|
else: |
|
|
|
|
|
nuthatch_config = { |
|
|
"transport": "streamable_http", |
|
|
"url": AgentConfig.NUTHATCH_MCP_URL |
|
|
} |
|
|
|
|
|
if AgentConfig.NUTHATCH_MCP_AUTH_KEY: |
|
|
nuthatch_config["headers"] = { |
|
|
"Authorization": f"Bearer {AgentConfig.NUTHATCH_MCP_AUTH_KEY}" |
|
|
} |
|
|
servers_config["nuthatch"] = nuthatch_config |
|
|
|
|
|
client = MultiServerMCPClient(servers_config) |
|
|
return client |
|
|
|
|
|
@staticmethod |
|
|
async def get_tools(client: MultiServerMCPClient) -> List[Any]: |
|
|
""" |
|
|
Get tools from MCP client and print summary. |
|
|
|
|
|
Args: |
|
|
client: MultiServerMCPClient instance |
|
|
|
|
|
Returns: |
|
|
List of tools |
|
|
""" |
|
|
print("[STATUS]: Loading MCP tools...") |
|
|
tools = await client.get_tools() |
|
|
|
|
|
print(f"[LOADED]: {len(tools)} tools available") |
|
|
for tool in tools: |
|
|
print(f" - {tool.name}") |
|
|
|
|
|
return tools |