Hey <@U08CKJH7C12>, Could you please share an exa...
# agents
e
Hey @refined-appointment-81829, Could you please share an example of MCP implementation, I am unable to connect to n8n SSE triggers
e
I have tried this and I keep getting this error, can you share your thoughts
Copy code
File "/home/avinish/.pyenv/versions/3.12.11/lib/python3.12/site-packages/livekit/agents/voice/generation.py", line 517, in _traceable_fnc_tool
    val = await function_callable()
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/avinish/.pyenv/versions/3.12.11/lib/python3.12/site-packages/livekit/agents/llm/mcp.py", line 100, in _tool_called
    tool_result = await self._client.call_tool(name, raw_arguments)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/avinish/.pyenv/versions/3.12.11/lib/python3.12/site-packages/mcp/client/session.py", line 293, in call_tool
    result = await self.send_request(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/avinish/.pyenv/versions/3.12.11/lib/python3.12/site-packages/mcp/shared/session.py", line 261, in send_request
    await self._write_stream.send(SessionMessage(message=JSONRPCMessage(jsonrpc_request), metadata=metadata))
  File "/home/avinish/.pyenv/versions/3.12.11/lib/python3.12/site-packages/anyio/streams/memory.py", line 243, in send
    self.send_nowait(item)
  File "/home/avinish/.pyenv/versions/3.12.11/lib/python3.12/site-packages/anyio/streams/memory.py", line 212, in send_nowait
    raise ClosedResourceError
anyio.ClosedResourceError {"function": "Create_an_event_in_Google_Calendar", "speech_id": "speech_670b2897b588", "pid": 248170, "job_id": "AJ_AtVvn9vzbZxA"}
I am trying to use the mcp module which leads to this error by connecting to the n8n MCP server like this from livekit.agents import mcp mcp_server = mcp.MCPServerHTTP(url=server_url) session = AgentSession[]( llm=llm_plugin, stt=stt_plugin, tts=tts_plugin, vad=vad_plugin, userdata=spiced_data, mcp_servers=[mcp_server] ) Also, this works like one in 10 times and then keeps failing
r
It seems like you're encountering an issue with the
MCPServerHTTP
connection to your n8n MCP server, specifically related to the
ClosedResourceError
. This error typically indicates that the connection to the MCP server has been closed unexpectedly, which can happen for various reasons, such as network issues, server unavailability, or incorrect configurations. Here are a few steps you can take to troubleshoot and potentially resolve the issue: 1. Check Server Availability: Ensure that your n8n MCP server is running and accessible from the machine where your LiveKit agent is running. You can do this by trying to access the server URL directly in a web browser or using a tool like
curl
. 2. Verify URL and Configuration: Double-check the
server_url
you are using to instantiate
MCPServerHTTP
. Make sure it is correctly formatted and points to the right endpoint of your n8n server. 3. Connection Handling: Since you mentioned that it works intermittently, consider implementing retry logic in your code to handle transient connection issues. You can wrap your connection logic in a try-except block and attempt to reconnect a few times before failing. 4. Logging: Add logging around your MCP server connection and tool calls to capture more details about the state of the connection when the error occurs. This can help you identify patterns or specific conditions that lead to the error. 5. Check MCP Tool Availability: Ensure that the tools you are trying to call on the MCP server are available and correctly configured. You can list the available tools from the MCP server to verify this. Make sure to replace
"http://your-n8n-server-url"
with your actual server URL. If the problem persists, you may want to check the logs of your n8n server for any errors or warnings that could provide more insight into why the connection is being closed. Notes: • The
ClosedResourceError
indicates that the connection to the MCP server was unexpectedly closed, which could be due to various reasons. • Ensure that the MCP server is running and accessible, and verify the configuration of the tools you are trying to use.
e
I have tried adding retries but its always leading to closedresource error, also tried increasing the client session timeout and instantly trigerrring the tools as soon as the session starts but nothing seems to be working
@refined-appointment-81829, This issue has been seen in other packages as well actually https://github.com/langchain-ai/langchain-mcp-adapters/issues/142 https://github.com/open-webui/mcpo/issues/240 Checking these out but is it possible that the current implementation could be buggy?
figured it without the MCP trigger by simply using a webhook for now, Thanks @refined-appointment-81829