Skip to main content

Haystack integration for Model Context Protocol (MCP)

Project description

MCP Haystack Integration

This integration adds support for the Model Context Protocol (MCP) to Haystack. MCP is an open protocol that standardizes how applications provide context to LLMs, similar to how USB-C provides a standardized way to connect devices.

Installation

pip install mcp-haystack

Usage

from haystack_integrations.tools.mcp import MCPTool, SSEServerInfo

# Create an MCP tool that connects to an HTTP server
server_info = SSEServerInfo(base_url="http://localhost:8000")
tool = MCPTool(name="my_tool", server_info=server_info)

# Use the tool
result = tool.invoke(param1="value1", param2="value2")

Examples

Check out the examples directory to see practical demonstrations of how to integrate the MCPTool into Haystack's tooling architecture. These examples will help you get started quickly with your own agentic applications.

What is uvx?

In some examples below, we use the StdioServerInfo class which relies on uvx behind the scenes. uvx is a convenient command from the uv package that runs Python tools in temporary, isolated environments. You only need to install uvx once, and it will automatically fetch any required packages on first use without needing manual installation.

Example 1: MCP Server with SSE Transport

This example demonstrates how to create a simple calculator server using MCP and connect to it using the MCPTool with SSE transport.

Step 1: Run the MCP Server

First, run the server that exposes calculator functionality (addition and subtraction) via MCP:

python examples/mcp_sse_server.py

This creates a FastMCP server with two tools:

  • add(a, b): Adds two numbers
  • subtract(a, b): Subtracts two numbers

The server runs on http://localhost:8000 by default.

Step 2: Connect with the MCP Client

In a separate terminal, run the client that connects to the calculator server:

python examples/mcp_sse_client.py

The client creates MCPTool instances that connect to the server, inspect the tool specifications, and invoke the calculator functions remotely.

Example 2: MCP with StdIO Transport

This example shows how to use MCPTool with stdio transport to execute a local program directly:

python examples/mcp_stdio_client.py

The example creates an MCPTool that uses stdio transport with StdioServerInfo, which automatically uses uvx behind the scenes to run the mcp-server-time tool without requiring manual installation. It queries the current time in different timezones (New York and Los Angeles) by invoking the tool with different parameters.

This demonstrates how MCPTool can work with local programs without running a separate server process, using standard input/output for communication.

Example 3: MCPTool in a Haystack Pipeline

This example showcases how to integrate MCPTool into a Haystack pipeline along with an LLM:

python examples/time_pipeline.py

This example creates a pipeline that:

  1. Takes a user query about the current time in a city
  2. Uses an LLM (GPT-4o-mini) to interpret the query and decide which tool to use
  3. Invokes the time tool with the appropriate parameters (using uvx behind the scenes)
  4. Sends the tool's response back to the LLM to generate a final answer

This demonstrates how MCPTool can be seamlessly integrated into Haystack's agentic architecture, allowing LLMs to use external tools via the Model Context Protocol.

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_haystack-0.3.1.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_haystack-0.3.1-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file mcp_haystack-0.3.1.tar.gz.

File metadata

  • Download URL: mcp_haystack-0.3.1.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for mcp_haystack-0.3.1.tar.gz
Algorithm Hash digest
SHA256 80fec04df3e10d648a7b2923ba51ac2355d852040a25ceadeb96959318fdaccf
MD5 243d03fca7ca278632537d0822d9b354
BLAKE2b-256 2b8e256839b4090382b4220a9b6ce8f007ea53d9da27bb7154523a4c78bf2101

See more details on using hashes here.

File details

Details for the file mcp_haystack-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: mcp_haystack-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for mcp_haystack-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 67d00efe9cb34b6f4be5e84acf460651508ce83a3e06d6efaf252f7f494f2efe
MD5 55b345b1b4d04dbeefe58a83e6102b60
BLAKE2b-256 d5e457a1db696d88453a48b3c7665b64cfcdfe02c797c0918c26831d8286049f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page