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.2.0.tar.gz (27.3 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.2.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mcp_haystack-0.2.0.tar.gz
Algorithm Hash digest
SHA256 243bcd97f93002d789843e110c7d3f0ab28dd5fb0a8279134b12e521081ee84e
MD5 ecfb1145595c9785de79357573aac5a4
BLAKE2b-256 82982a796258525f43873b783a5a6ee9bfb3346dee14c58e31e67d3c80f3445a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mcp_haystack-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6940ac7936489ae09cc1cd135b696e71d68d200d62da38cc77d81348440d550c
MD5 567b02f6d6229b05e194c9b2a1f31424
BLAKE2b-256 872ef7b84676461058d3501f2fd27e312934ed8daada6ec0763d502b8f492923

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