mcp-haystack 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ volumes/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ .python-version
87
+
88
+ # pipenv
89
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
90
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
91
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
92
+ # install all needed dependencies.
93
+ #Pipfile.lock
94
+
95
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
96
+ __pypackages__/
97
+
98
+ # Celery stuff
99
+ celerybeat-schedule
100
+ celerybeat.pid
101
+
102
+ # SageMath parsed files
103
+ *.sage.py
104
+
105
+ # Environments
106
+ .env
107
+ .venv
108
+ env/
109
+ venv/
110
+ ENV/
111
+ env.bak/
112
+ venv.bak/
113
+
114
+ # Spyder project settings
115
+ .spyderproject
116
+ .spyproject
117
+
118
+ # Rope project settings
119
+ .ropeproject
120
+
121
+ # mkdocs documentation
122
+ /site
123
+
124
+ # mypy
125
+ .mypy_cache/
126
+ .dmypy.json
127
+ dmypy.json
128
+
129
+ # Pyre type checker
130
+ .pyre/
131
+
132
+ # IDEs
133
+ .vscode
134
+
135
+ # Docs generation artifacts
136
+ _readme_*.md
137
+ .idea
138
+
139
+ # macOS
140
+ .DS_Store
141
+
142
+ # http cache (requests-cache)
143
+ **/http_cache.sqlite
144
+
145
+ # ruff
146
+ .ruff_cache
@@ -0,0 +1,73 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
10
+
11
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
12
+
13
+ "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
14
+
15
+ "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
16
+
17
+ "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
18
+
19
+ "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
20
+
21
+ "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
22
+
23
+ "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
24
+
25
+ "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
26
+
27
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
28
+
29
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
30
+
31
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
32
+
33
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
34
+
35
+ (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
36
+
37
+ (b) You must cause any modified files to carry prominent notices stating that You changed the files; and
38
+
39
+ (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
40
+
41
+ (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
42
+
43
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
44
+
45
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
46
+
47
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
48
+
49
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
50
+
51
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
52
+
53
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
54
+
55
+ END OF TERMS AND CONDITIONS
56
+
57
+ APPENDIX: How to apply the Apache License to your work.
58
+
59
+ To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
60
+
61
+ Copyright [yyyy] [name of copyright owner]
62
+
63
+ Licensed under the Apache License, Version 2.0 (the "License");
64
+ you may not use this file except in compliance with the License.
65
+ You may obtain a copy of the License at
66
+
67
+ http://www.apache.org/licenses/LICENSE-2.0
68
+
69
+ Unless required by applicable law or agreed to in writing, software
70
+ distributed under the License is distributed on an "AS IS" BASIS,
71
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72
+ See the License for the specific language governing permissions and
73
+ limitations under the License.
@@ -0,0 +1,114 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcp-haystack
3
+ Version: 0.0.1
4
+ Summary: Haystack integration for Model Context Protocol (MCP)
5
+ Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mcp#readme
6
+ Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
7
+ Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mcp
8
+ Author-email: deepset GmbH <info@deepset.ai>
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE.txt
11
+ Keywords: Haystack,MCP,Model Context Protocol
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: Implementation :: CPython
20
+ Requires-Python: >=3.10
21
+ Requires-Dist: haystack-ai>=2.9.0
22
+ Requires-Dist: mcp
23
+ Description-Content-Type: text/markdown
24
+
25
+ # MCP Haystack Integration
26
+
27
+ This integration adds support for the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) 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.
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ pip install mcp-haystack
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ```python
38
+ from haystack_integrations.components.tools.mcp import MCPTool, SSEServerInfo
39
+
40
+ # Create an MCP tool that connects to an HTTP server
41
+ server_info = SSEServerInfo(base_url="http://localhost:8000")
42
+ tool = MCPTool(name="my_tool", server_info=server_info)
43
+
44
+ # Use the tool
45
+ result = tool.invoke(param1="value1", param2="value2")
46
+ ```
47
+
48
+ # Examples
49
+
50
+ 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.
51
+
52
+ ## What is uvx?
53
+
54
+ 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.
55
+
56
+ ## Example 1: MCP Server with SSE Transport
57
+
58
+ This example demonstrates how to create a simple calculator server using MCP and connect to it using the MCPTool with SSE transport.
59
+
60
+ ### Step 1: Run the MCP Server
61
+
62
+ First, run the server that exposes calculator functionality (addition and subtraction) via MCP:
63
+
64
+ ```bash
65
+ python examples/mcp_sse_server.py
66
+ ```
67
+
68
+ This creates a FastMCP server with two tools:
69
+ - `add(a, b)`: Adds two numbers
70
+ - `subtract(a, b)`: Subtracts two numbers
71
+
72
+ The server runs on http://localhost:8000 by default.
73
+
74
+ ### Step 2: Connect with the MCP Client
75
+
76
+ In a separate terminal, run the client that connects to the calculator server:
77
+
78
+ ```bash
79
+ python examples/mcp_sse_client.py
80
+ ```
81
+
82
+ The client creates MCPTool instances that connect to the server, inspect the tool specifications, and invoke the calculator functions remotely.
83
+
84
+ ## Example 2: MCP with StdIO Transport
85
+
86
+ This example shows how to use MCPTool with stdio transport to execute a local program directly:
87
+
88
+ ```bash
89
+ python examples/mcp_stdio_client.py
90
+ ```
91
+
92
+ 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.
93
+
94
+ This demonstrates how MCPTool can work with local programs without running a separate server process, using standard input/output for communication.
95
+
96
+ ## Example 3: MCPTool in a Haystack Pipeline
97
+
98
+ This example showcases how to integrate MCPTool into a Haystack pipeline along with an LLM:
99
+
100
+ ```bash
101
+ python examples/time_pipeline.py
102
+ ```
103
+
104
+ This example creates a pipeline that:
105
+ 1. Takes a user query about the current time in a city
106
+ 2. Uses an LLM (GPT-4o-mini) to interpret the query and decide which tool to use
107
+ 3. Invokes the time tool with the appropriate parameters (using `uvx` behind the scenes)
108
+ 4. Sends the tool's response back to the LLM to generate a final answer
109
+
110
+ This demonstrates how MCPTool can be seamlessly integrated into Haystack's agentic architecture, allowing LLMs to use external tools via the Model Context Protocol.
111
+
112
+ ## License
113
+
114
+ Apache 2.0
@@ -0,0 +1,90 @@
1
+ # MCP Haystack Integration
2
+
3
+ This integration adds support for the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) 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.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install mcp-haystack
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```python
14
+ from haystack_integrations.components.tools.mcp import MCPTool, SSEServerInfo
15
+
16
+ # Create an MCP tool that connects to an HTTP server
17
+ server_info = SSEServerInfo(base_url="http://localhost:8000")
18
+ tool = MCPTool(name="my_tool", server_info=server_info)
19
+
20
+ # Use the tool
21
+ result = tool.invoke(param1="value1", param2="value2")
22
+ ```
23
+
24
+ # Examples
25
+
26
+ 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.
27
+
28
+ ## What is uvx?
29
+
30
+ 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.
31
+
32
+ ## Example 1: MCP Server with SSE Transport
33
+
34
+ This example demonstrates how to create a simple calculator server using MCP and connect to it using the MCPTool with SSE transport.
35
+
36
+ ### Step 1: Run the MCP Server
37
+
38
+ First, run the server that exposes calculator functionality (addition and subtraction) via MCP:
39
+
40
+ ```bash
41
+ python examples/mcp_sse_server.py
42
+ ```
43
+
44
+ This creates a FastMCP server with two tools:
45
+ - `add(a, b)`: Adds two numbers
46
+ - `subtract(a, b)`: Subtracts two numbers
47
+
48
+ The server runs on http://localhost:8000 by default.
49
+
50
+ ### Step 2: Connect with the MCP Client
51
+
52
+ In a separate terminal, run the client that connects to the calculator server:
53
+
54
+ ```bash
55
+ python examples/mcp_sse_client.py
56
+ ```
57
+
58
+ The client creates MCPTool instances that connect to the server, inspect the tool specifications, and invoke the calculator functions remotely.
59
+
60
+ ## Example 2: MCP with StdIO Transport
61
+
62
+ This example shows how to use MCPTool with stdio transport to execute a local program directly:
63
+
64
+ ```bash
65
+ python examples/mcp_stdio_client.py
66
+ ```
67
+
68
+ 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.
69
+
70
+ This demonstrates how MCPTool can work with local programs without running a separate server process, using standard input/output for communication.
71
+
72
+ ## Example 3: MCPTool in a Haystack Pipeline
73
+
74
+ This example showcases how to integrate MCPTool into a Haystack pipeline along with an LLM:
75
+
76
+ ```bash
77
+ python examples/time_pipeline.py
78
+ ```
79
+
80
+ This example creates a pipeline that:
81
+ 1. Takes a user query about the current time in a city
82
+ 2. Uses an LLM (GPT-4o-mini) to interpret the query and decide which tool to use
83
+ 3. Invokes the time tool with the appropriate parameters (using `uvx` behind the scenes)
84
+ 4. Sends the tool's response back to the LLM to generate a final answer
85
+
86
+ This demonstrates how MCPTool can be seamlessly integrated into Haystack's agentic architecture, allowing LLMs to use external tools via the Model Context Protocol.
87
+
88
+ ## License
89
+
90
+ Apache 2.0
@@ -0,0 +1,41 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from haystack_integrations.tools.mcp import MCPTool, SSEServerInfo
6
+
7
+ # run this client after running the server mcp_sse_server.py
8
+ # it shows how easy it is to use the MCPTool with SSE transport
9
+
10
+
11
+ def main():
12
+ """Example of synchronous usage of MCPTool with SSE transport."""
13
+
14
+ server_info = SSEServerInfo(
15
+ base_url="http://localhost:8000",
16
+ )
17
+
18
+ tool = MCPTool(name="add", server_info=server_info)
19
+
20
+ tool_subtract = MCPTool(name="subtract", server_info=server_info)
21
+
22
+ try:
23
+ print(f"Tool spec: {tool.tool_spec}")
24
+
25
+ result = tool.invoke(a=7, b=3)
26
+ print(f"7 + 3 = {result}")
27
+
28
+ result = tool_subtract.invoke(a=5, b=3)
29
+ print(f"5 - 3 = {result}")
30
+
31
+ result = tool.invoke(a=10, b=20)
32
+ print(f"10 + 20 = {result}")
33
+
34
+ except Exception as e:
35
+ print(f"Error in sync example: {e}")
36
+ finally:
37
+ pass
38
+
39
+
40
+ if __name__ == "__main__":
41
+ main()
@@ -0,0 +1,29 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from mcp.server.fastmcp import FastMCP
6
+
7
+ # run this server first before running the client mcp_sse_client.py
8
+ # it shows how easy it is to create a MCP server in just a few lines of code
9
+ # then we'll use the MCPTool to invoke the server
10
+
11
+
12
+ mcp = FastMCP("MCP Calculator")
13
+
14
+
15
+ @mcp.tool()
16
+ def add(a: int, b: int) -> int:
17
+ """Add two numbers"""
18
+ return a + b
19
+
20
+
21
+ @mcp.tool()
22
+ def subtract(a: int, b: int) -> int:
23
+ """Subtract two numbers"""
24
+ return a - b
25
+
26
+
27
+ if __name__ == "__main__":
28
+ # Initialize and run the server
29
+ mcp.run(transport="sse")
@@ -0,0 +1,35 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from haystack_integrations.tools.mcp import MCPTool, StdioServerInfo
6
+
7
+ # For stdio MCPTool we don't need to run a server, we can just use the MCPTool directly
8
+ # Here we use the mcp-server-time server
9
+ # See https://github.com/modelcontextprotocol/servers/tree/main/src/time for more details
10
+ # prior to running this script, pip install mcp-server-time
11
+
12
+
13
+ def main():
14
+ """Example of using the MCPTool implementation with stdio transport."""
15
+
16
+ try:
17
+
18
+ stdio_tool = MCPTool(
19
+ name="get_current_time",
20
+ server_info=StdioServerInfo(command="uvx", args=["mcp-server-time", "--local-timezone=Europe/Berlin"]),
21
+ )
22
+
23
+ print(f"Tool spec: {stdio_tool.tool_spec}")
24
+
25
+ result = stdio_tool.invoke(timezone="America/New_York")
26
+ print(f"Current time in New York: {result}")
27
+
28
+ result = stdio_tool.invoke(timezone="America/Los_Angeles")
29
+ print(f"Current time in Los Angeles: {result}")
30
+ except Exception as e:
31
+ print(f"Error in stdio example: {e}")
32
+
33
+
34
+ if __name__ == "__main__":
35
+ main()
@@ -0,0 +1,51 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ # Full example of a pipeline that uses MCPTool to get the current time
6
+ # and then uses the time to answer a user question.
7
+ # Here we use the mcp-server-time mcp package
8
+ # See https://github.com/modelcontextprotocol/servers/tree/main/src/time for more details
9
+ # prior to running this script, pip install mcp-server-time
10
+
11
+ from haystack import Pipeline
12
+ from haystack.components.converters import OutputAdapter
13
+ from haystack.components.generators.chat import OpenAIChatGenerator
14
+ from haystack.components.tools import ToolInvoker
15
+ from haystack.dataclasses import ChatMessage
16
+
17
+ from haystack_integrations.tools.mcp.mcp_tool import MCPTool, StdioServerInfo
18
+
19
+
20
+ def main():
21
+ time_tool = MCPTool(
22
+ name="get_current_time",
23
+ server_info=StdioServerInfo(command="uvx", args=["mcp-server-time", "--local-timezone=Europe/Berlin"]),
24
+ )
25
+ pipeline = Pipeline()
26
+ pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini", tools=[time_tool]))
27
+ pipeline.add_component("tool_invoker", ToolInvoker(tools=[time_tool]))
28
+ pipeline.add_component(
29
+ "adapter",
30
+ OutputAdapter(
31
+ template="{{ initial_msg + initial_tool_messages + tool_messages }}",
32
+ output_type=list[ChatMessage],
33
+ unsafe=True,
34
+ ),
35
+ )
36
+ pipeline.add_component("response_llm", OpenAIChatGenerator(model="gpt-4o-mini"))
37
+ pipeline.connect("llm.replies", "tool_invoker.messages")
38
+ pipeline.connect("llm.replies", "adapter.initial_tool_messages")
39
+ pipeline.connect("tool_invoker.tool_messages", "adapter.tool_messages")
40
+ pipeline.connect("adapter.output", "response_llm.messages")
41
+
42
+ user_input = "What is the time in New York? Be brief." # can be any city
43
+ user_input_msg = ChatMessage.from_user(text=user_input)
44
+
45
+ result = pipeline.run({"llm": {"messages": [user_input_msg]}, "adapter": {"initial_msg": [user_input_msg]}})
46
+
47
+ print(result["response_llm"]["replies"][0].text)
48
+
49
+
50
+ if __name__ == "__main__":
51
+ main()
@@ -0,0 +1,29 @@
1
+ loaders:
2
+ - type: haystack_pydoc_tools.loaders.CustomPythonLoader
3
+ search_path: [../src]
4
+ modules: [
5
+ "haystack_integrations.tools.mcp.mcp_tool",
6
+ ]
7
+ ignore_when_discovered: ["__init__"]
8
+ processors:
9
+ - type: filter
10
+ expression:
11
+ documented_only: true
12
+ do_not_filter_modules: false
13
+ skip_empty_modules: true
14
+ - type: smart
15
+ - type: crossref
16
+ renderer:
17
+ type: haystack_pydoc_tools.renderers.ReadmeIntegrationRenderer
18
+ excerpt: MCP integration for Haystack
19
+ category_slug: integrations-api
20
+ title: mcp
21
+ slug: integrations-mcp
22
+ order: 145
23
+ markdown:
24
+ descriptive_class_title: false
25
+ classdef_code_block: false
26
+ descriptive_module_title: true
27
+ add_method_class_prefix: true
28
+ add_member_class_prefix: false
29
+ filename: _readme_mcp.md