여러 MCP 서버 연결하기

여러 MCP 서버 연결하기 (Connecting to Multiple MCP Servers)

crewai-tools의 MCPServerAdapter를 사용하면 여러 MCP 서버에 동시에 연결할 수 있어요. 에이전트가 서로 다른 서비스나 환경에 분산된 툴에 접근해야 할 때 유용해요. 어댑터는 지정된 모든 서버의 툴을 집계해서 CrewAI 에이전트가 사용할 수 있게 해 줘요.

출처: 문서

본문

Overview (개요)

Configuration (구성)

여러 서버에 연결하려면 MCPServerAdapter에 서버 파라미터 딕셔너리의 리스트를 제공하면 돼요. 리스트의 각 딕셔너리는 하나의 MCP 서버에 대한 파라미터를 정의해요.

리스트의 각 서버가 지원하는 전송 유형은 stdio, sse, streamable-http가 있어요.

from crewai import Agent, Task, Crew, Process
from crewai_tools import MCPServerAdapter
from mcp import StdioServerParameters # Needed for Stdio example

# Define parameters for multiple MCP servers
server_params_list = [
    # Streamable HTTP Server
    {
        "url": "http://localhost:8001/mcp", 
        "transport": "streamable-http"
    },
    # SSE Server
    {
        "url": "http://localhost:8000/sse",
        "transport": "sse"
    },
    # StdIO Server
    StdioServerParameters(
        command="python3",
        args=["servers/your_stdio_server.py"],
        env={"UV_PYTHON": "3.12", **os.environ},
    )
]

try:
    with MCPServerAdapter(server_params_list) as aggregated_tools:
        print(f"Available aggregated tools: {[tool.name for tool in aggregated_tools]}")

        multi_server_agent = Agent(
            role="Versatile Assistant",
            goal="Utilize tools from local Stdio, remote SSE, and remote HTTP MCP servers.",
            backstory="An AI agent capable of leveraging a diverse set of tools from multiple sources.",
            tools=aggregated_tools, # All tools are available here
            verbose=True,
        )

        ... # Your other agent, tasks, and crew code here

except Exception as e:
    print(f"Error connecting to or using multiple MCP servers (Managed): {e}")
    print("Ensure all MCP servers are running and accessible with correct configurations.")

Connection Management (연결 관리)

컨텍스트 매니저(with 문)를 사용하면 MCPServerAdapter가 구성된 모든 MCP 서버에 대한 연결의 수명주기(시작과 중지)를 처리해 줘요. 이는 리소스 관리를 단순화하고 컨텍스트를 벗어날 때 모든 연결이 제대로 닫히도록 보장해요.

더 알아보기 (Learn more)