MCPTool
MCPTool
MCPTool은 Model Context Protocol(MCP)을 통해 외부 도구와 서비스에 통합할 수 있게 해주는 Tool이에요.
출처: MCPTool
본문
개요
MCPTool은 Model Context Protocol(MCP)을 사용해 Haystack이 외부 도구와 서비스와 통신할 수 있게 해주는 Tool이에요. MCP는 애플리케이션이 LLM에 컨텍스트를 제공하는 방식을 표준화하는 개방형 프로토콜이에요. USB-C가 기기를 연결하는 표준 방식을 제공하는 것과 비슷하죠.
MCPTool은 여러 전송 옵션을 지원해요.
- HTTP 서버 연결용 Streamable HTTP,
- HTTP 서버 연결용 SSE(Server-Sent Events) (deprecated),
- 로컬 프로그램 직접 실행용 StdIO.
MCP 프로토콜과 아키텍처에 대해 더 알아보려면 공식 MCP 웹사이트를 방문하세요.
파라미터
name은 필수이며 도구의 이름을 지정해요.server_info는 필수이며,SSEServerInfo,StreamableHttpServerInfo, 또는StdioServerInfo객체 중 하나여야 해요. 연결 정보를 담고 있어요.description은 선택이며 LLM에게 도구가 무엇을 하는지 컨텍스트를 제공해요.
결과
Tool은 mcp-sdk의 TextContent, ImageContent, EmbeddedResource 타입을 나타내는 JSON 객체 목록으로 결과를 반환해요.
사용법
MCPTool을 쓰려면 MCP-Haystack 통합을 설치해요.
pip install mcp-haystack
Streamable HTTP 전송으로
streamable-http 전송으로 외부 HTTP 서버에 연결하는 MCPTool을 만들 수 있어요.
from haystack_integrations.tools.mcp import MCPTool, StreamableHttpServerInfo
# Create an MCP tool that connects to an HTTP server
server_info = StreamableHttpServerInfo(url="http://localhost:8000/mcp")
tool = MCPTool(name="my_tool", server_info=server_info)
# Use the tool
result = tool.invoke(param1="value1", param2="value2")
SSE 전송으로 (deprecated)
경고: SSE 전송은 MCP 사양에서 Streamable HTTP로 대체되어 deprecate됐어요. 새 통합에는 Streamable HTTP를 사용하세요. 기존 SSE 전용 서버에 연결한다면
SSEServerInfo는 계속 동작하지만, 서버가 지원할 때StreamableHttpServerInfo로 마이그레이션하는 걸 고려하세요.
SSE 전송으로 외부 HTTP 서버에 연결하는 MCPTool을 만들 수 있어요.
from haystack_integrations.tools.mcp import MCPTool, SSEServerInfo
# Create an MCP tool that connects to an HTTP server
server_info = SSEServerInfo(url="http://localhost:8000/sse")
tool = MCPTool(name="my_tool", server_info=server_info)
# Use the tool
result = tool.invoke(param1="value1", param2="value2")
StdIO 전송으로
로컬 프로그램을 직접 실행하고 stdio 전송으로 연결하는 MCPTool을 만들 수도 있어요.
from haystack_integrations.tools.mcp import MCPTool, StdioServerInfo
# Create an MCP tool that uses stdio transport
server_info = StdioServerInfo(
command="uvx",
args=["mcp-server-time", "--local-timezone=Europe/Berlin"],
)
tool = MCPTool(name="get_current_time", server_info=server_info)
# Get the current time in New York
result = tool.invoke(timezone="America/New_York")
파이프라인 안에서
Agent 컴포넌트를 통해 MCPTool을 파이프라인에 통합할 수 있어요.
from haystack import Pipeline
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack_integrations.tools.mcp import MCPTool, StdioServerInfo
time_tool = MCPTool(
name="get_current_time",
server_info=StdioServerInfo(
command="uvx",
args=["mcp-server-time", "--local-timezone=Europe/Berlin"],
),
)
pipeline = Pipeline()
pipeline.add_component(
"agent",
Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
tools=[time_tool],
),
)
user_input = "What is the time in New York? Be brief." # can be any city
user_input_msg = ChatMessage.from_user(text=user_input)
result = pipeline.run({"agent": {"messages": [user_input_msg]}})
print(result["agent"]["last_message"].text)
# The current time in New York is 1:57 PM.
Agent 컴포넌트와 함께
MCPTool을 Agent 컴포넌트와 함께 쓸 수 있어요. Agent 컴포넌트는 선택한 ChatGenerator와 내장 도구 실행을 결합해서, 도구 호출을 실행하고 도구 결과를 처리해요.
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.components.agents import Agent
from haystack_integrations.tools.mcp import MCPTool, StdioServerInfo
time_tool = MCPTool(
name="get_current_time",
server_info=StdioServerInfo(
command="uvx",
args=["mcp-server-time", "--local-timezone=Europe/Berlin"],
),
)
# Agent Setup
agent = Agent(
chat_generator=OpenAIChatGenerator(),
tools=[time_tool],
exit_conditions=["text"],
)
# Run the Agent
response = agent.run(
messages=[ChatMessage.from_user("What is the time in New York? Be brief.")],
)
# Output
print(response["messages"][-1].text)