集成:模型上下文协议 - MCP
Haystack 与 MCP 的工具集成
目录
概述
MCP Haystack 集成支持 Haystack 的模型上下文协议(MCP)。MCP 是一项开放协议,它标准化应用程序如何为 LLM 提供上下文,类似于 USB-C 为连接设备提供标准化方式。
此集成使您能够通过模型上下文协议轻松地将外部工具和服务连接到 Haystack 管道,从而实现更强大、更灵活的代理应用程序。
安装
pip install mcp-haystack
使用
from haystack_integrations.tools.mcp import MCPTool, SSEServerInfo
# Create an MCP tool that connects to an HTTP server
server_info = SSEServerInfo(base_url="https://:8000")
tool = MCPTool(name="my_tool", server_info=server_info)
# Use the tool
result = tool.invoke(param1="value1", param2="value2")
示例
查看 examples 目录,了解将 MCPTool 集成到 Haystack 的工具架构中的实际演示。这些示例将帮助您快速开始构建自己的代理应用程序。
什么是 uvx?
在下面的某些示例中,我们使用 StdioServerInfo 类,该类在后台依赖于 uvx。uvx 是 uv 包中的一个便捷命令,可在临时、隔离的环境中运行 Python 工具。您只需安装一次 uvx,它将在首次使用时自动获取任何必需的包,而无需手动安装。
示例 1:带 SSE 传输的 MCP 服务器
此示例演示了如何创建简单的计算器服务器(使用 MCP)并通过 MCPTool 和 SSE 传输连接到它。
步骤 1:运行 MCP 服务器
首先,运行 服务器,该服务器通过 MCP 公开计算器功能(加法和减法)。
python examples/mcp_sse_server.py
这会创建一个具有两个工具的 FastMCP 服务器:
add(a, b):将两个数字相加subtract(a, b):将两个数字相减
服务器默认在 https://:8000 上运行。
步骤 2:使用 MCP 客户端连接
在单独的终端中,运行 客户端,连接到计算器服务器。
python examples/mcp_sse_client.py
客户端创建 MCPTool 实例,连接到服务器,检查工具规范,并远程调用计算器函数。
示例 2:带 StdIO 传输的 MCP
此 示例 展示了如何使用带 stdio 传输的 MCPTool 来直接执行本地程序。
python examples/mcp_stdio_client.py
该示例创建了一个 MCPTool,它使用带 StdioServerInfo 的 stdio 传输,后者在后台自动使用 uvx 来运行 mcp-server-time 工具,而无需手动安装。它通过调用具有不同参数的工具来查询不同时区(纽约和洛杉矶)的当前时间。
这演示了 MCPTool 如何与本地程序配合使用,而无需运行单独的服务器进程,而是使用标准输入/输出来进行通信。
示例 3:Haystack 管道中的 MCPTool
此 示例 展示了如何将 MCPTool 与 LLM 一起集成到 Haystack 管道中。
from haystack import Pipeline
from haystack.components.converters import OutputAdapter
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.tools import ToolInvoker
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("llm", OpenAIChatGenerator(model="gpt-4o-mini", tools=[time_tool]))
pipeline.add_component("tool_invoker", ToolInvoker(tools=[time_tool]))
pipeline.add_component(
"adapter",
OutputAdapter(
template="{{ initial_msg + initial_tool_messages + tool_messages }}",
output_type=list[ChatMessage],
unsafe=True,
),
)
pipeline.add_component("response_llm", OpenAIChatGenerator(model="gpt-4o-mini"))
pipeline.connect("llm.replies", "tool_invoker.messages")
pipeline.connect("llm.replies", "adapter.initial_tool_messages")
pipeline.connect("tool_invoker.tool_messages", "adapter.tool_messages")
pipeline.connect("adapter.output", "response_llm.messages")
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({"llm": {"messages": [user_input_msg]}, "adapter": {"initial_msg": [user_input_msg]}})
print(result["response_llm"]["replies"][0].text)
当您运行
python examples/time_pipeline.py
输出将类似于:##纽约的当前时间是下午 1:57。
总之,此示例创建了一个管道,该管道:
- 接收用户关于某个城市当前时间的查询
- 使用 LLM(GPT-4o-mini)来解释查询并决定使用哪个工具
- 调用具有适当参数的 MCP 时间工具(在后台使用 uvx)
- 将工具的响应发送回 LLM 以生成最终答案
这演示了 MCPTool 如何无缝集成到 Haystack 的代理架构中,使 LLM 能够通过模型上下文协议使用外部工具。
许可证
mcp-haystack 在 Apache-2.0 许可条款下分发。
