Describe the bug
MCPTool is not running on python notebook. It works fine in a python script.
To Reproduce
Run the code on the integration page on a colab notebook or locally with a Python notebook
MCP integration page
Error Message
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:473, in MCPTool.__init__(self, name, server_info, description, connection_timeout, invocation_timeout)
[472](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:472) # Connect and get available tools with timeout
--> [473](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:473) tools = self._run_sync(client.connect(), timeout=connection_timeout)
[475](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:475) # Handle no tools case
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:656, in MCPTool._run_sync(self, coro, timeout)
[654](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:654) # Run the coroutine in the loop but don't close it
[655](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:655) # AsyncExitStack depends on this loop staying open
--> [656](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:656) return loop.run_until_complete(coro)
[658](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:658) except asyncio.TimeoutError:
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py:629, in BaseEventLoop.run_until_complete(self, future)
[628](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py:628) self._check_closed()
--> [629](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py:629) self._check_running()
[631](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py:631) new_task = not futures.isfuture(future)
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py:588, in BaseEventLoop._check_running(self)
[587](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py:587) if self.is_running():
--> [588](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py:588) raise RuntimeError('This event loop is already running')
[589](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py:589) if events._get_running_loop() is not None:
RuntimeError: This event loop is already running
The above exception was the direct cause of the following exception:
MCPConnectionError Traceback (most recent call last)
Cell In[4], [line 9](vscode-notebook-cell:?execution_count=4&line=9)
[5](vscode-notebook-cell:?execution_count=4&line=5) from haystack.dataclasses import ChatMessage
[7](vscode-notebook-cell:?execution_count=4&line=7) from haystack_integrations.tools.mcp import MCPTool, StdioServerInfo
----> [9](vscode-notebook-cell:?execution_count=4&line=9) time_tool = MCPTool(
[10](vscode-notebook-cell:?execution_count=4&line=10) name="create_issue",
[11](vscode-notebook-cell:?execution_count=4&line=11) server_info=StdioServerInfo(command="npx", args=["-y", "@modelcontextprotocol/server-github"], env={
[12](vscode-notebook-cell:?execution_count=4&line=12) "GITHUB_PERSONAL_ACCESS_TOKEN": "*********"
[13](vscode-notebook-cell:?execution_count=4&line=13) }),
[14](vscode-notebook-cell:?execution_count=4&line=14) description="Use this tool to create issues on the given github repository"
[15](vscode-notebook-cell:?execution_count=4&line=15) )
[16](vscode-notebook-cell:?execution_count=4&line=16) print("MCP connected")
[17](vscode-notebook-cell:?execution_count=4&line=17) pipeline = Pipeline()
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:511, in MCPTool.__init__(self, name, server_info, description, connection_timeout, invocation_timeout)
[508](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:508) logger.warning(f"Error during cleanup after initialization failure: {cleanup_error}")
[510](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:510) message = f"Failed to initialize MCPTool '{name}': {e}"
--> [511](https://file+.vscode-resource.vscode-cdn.net/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/haystack_integrations/tools/mcp/mcp_tool.py:511) raise MCPConnectionError(message=message, server_info=server_info, operation="initialize") from e
MCPConnectionError: Failed to initialize MCPTool 'create_issue': This event loop is already running
Describe your environment (please complete the following information):
- OS: macOS 14.7.4 & Google Colab
- Haystack version: 2.11
- Integration version: mcp-haystack 0.0.1
Describe the bug
MCPToolis not running on python notebook. It works fine in a python script.To Reproduce
Run the code on the integration page on a colab notebook or locally with a Python notebook
MCP integration page
Error Message
Describe your environment (please complete the following information):