Did anyone integrate mcp_servers to livekit agent,...
# agents
e
Did anyone integrate mcp_servers to livekit agent, i've mcp server url from composio but as soon as i add it to AgentSession config under mcp_servers, the agent won't work and close and were not able to generate_reply
f
check logs
we are using our own mcp server and it's working
e
have you did anything extra except passing your mcp url to mcp_servers?
l
We also have an MCP short tutorial at https://docs.livekit.io/recipes/http_mcp_client/. Sample: https://github.com/livekit-examples/python-agents-examples/blob/main/docs/examples/http_mcp_client/http_mcp_client.py. You should also be able to switch the MCP server with our docs one at https://docs.livekit.io/mcp then ask the agent questions about LiveKit
e
Thanks @lively-winter-88668, i am following the same toturial, as soon as i add my mcp server url, it starts throwing this error. Note: I tested my mcp server url with online agents like elevenlabs and it worked.
Copy code
11:35:34.789 ERROR  asyncio            Task exception was never retrieved
                                           future: <Task finished name='Task-22'
                                           coro=<RoomIO._on_user_text_input.<locals>._read_text() done, defined at
                                           /home/karandeep/Development/agent-starter-python/.venv/lib/python3.10/site-packages/l…
                                           exception=RuntimeError('AgentSession is closing, cannot use generate_reply()')>
                                           Traceback (most recent call last):
                                             File
                                           "/home/karandeep/Development/agent-starter-python/.venv/lib/python3.10/site-packages/…
                                           line 408, in _read_text
                                               text_input_result = text_input_cb(
                                             File
                                           "/home/karandeep/Development/agent-starter-python/.venv/lib/python3.10/site-packages/…
                                           line 54, in _default_text_input_cb
                                               sess.generate_reply(user_input=ev.text)
                                             File
                                           "/home/karandeep/Development/agent-starter-python/.venv/lib/python3.10/site-packages/…
                                           line 941, in generate_reply
                                               raise RuntimeError("AgentSession is closing, cannot use generate_reply()")
                                           RuntimeError: AgentSession is closing, cannot use generate_reply()
                                  {"room": "voice_assistant_room_1526", "pid": 510405, "job_id": "AJ_mpGGNgbXdLzQ", "room_id":
"RM_bqKpR7ukAufx"}
Traceback (most recent call last):
  File
"/home/karandeep/Development/agent-starter-python/.venv/lib/python3.10/site-packages/livekit/agents/voice/room_io/room_io.py", line
408, in _read_text
    text_input_result = text_input_cb(
  File "/home/karandeep/Development/agent-starter-python/.venv/lib/python3.10/site-packages/livekit/agents/voice/room_io/types.py",
line 54, in _default_text_input_cb
    sess.generate_reply(user_input=ev.text)
  File "/home/karandeep/Development/agent-starter-python/.venv/lib/python3.10/site-packages/livekit/agents/voice/agent_session.py",
line 941, in generate_reply
    raise RuntimeError("AgentSession is closing, cannot use generate_reply()")
RuntimeError: AgentSession is closing, cannot use generate_reply()
l
Your errors are complaining about
generate_reply()
. I'm not 100% convinced this is just the MCP server. Are you able to share your MCP server?
e
here's my AgentSession
Copy code
session = AgentSession(
        # Speech-to-text (STT) is your agent's ears, turning the user's speech into text that the LLM can understand
        # See all available models at <https://docs.livekit.io/agents/models/stt/>
        stt=deepgram.STTv2(
            model="flux-general-en",
            eager_eot_threshold=0.4,
        ),
        # A Large Language Model (LLM) is your agent's brain, processing user input and generating a response
        # See all available models at <https://docs.livekit.io/agents/models/llm/>
        llm=openai.LLM(model="gpt-4o-mini"),
        # Text-to-speech (TTS) is your agent's voice, turning the LLM's text into speech that the user can hear
        # See all available models as well as voice selections at <https://docs.livekit.io/agents/models/tts/>
        tts=elevenlabs.TTS(
            model="eleven_turbo_v2_5",
            voice_id="Xb7hH8MSUJpSbSDYk0k2",
        ),
        # VAD and turn detection are used to determine when the user is speaking and when the agent should respond
        # See more at <https://docs.livekit.io/agents/build/turns>
        turn_detection=MultilingualModel(),
        vad=ctx.proc.userdata["vad"],
        # allow the LLM to generate a response while waiting for the end of turn
        # See more at <https://docs.livekit.io/agents/build/audio/#preemptive-generation>
        preemptive_generation=True,
        mcp_servers=[
            mcp.MCPServerHTTP(url="<https://backend.composio.dev/v3/mcp/939df709-071c-484a-8cec-15d2cd1febe7?user_id=68f2376bbc13f312fec2bd96>")
        ]
    )
l
If I
curl
https://backend.composio.dev/v3/mcp/939df709-071c-484a-8cec-15d2cd1febe7?user_id=68f2376bbc13f312fec2bd96 I get redirected to https://backend.composio.dev/v3/mcp/939df709-071c-484a-8cec-15d2cd1febe7/mcp?user_id=68f2376bbc13f312fec2bd96 (note the /mcp before the
?
). I suspect the LiveKit MCP server is not correctly handling the redirect. Can you try with the modified MCP server above? When I test with this updated URL, it seems to load the tools successfully (without the extra /mcp it was not loading the tools)
e
Thanks for checking, I'm trying...
Many thanks @lively-winter-88668, it worked! Do livekit push events when calling tool
l
@echoing-lamp-18687 That's great, I'll report the redirect issue internally to try and get that fixed in the future
Do livekit push events when calling tool
We have https://docs.livekit.io/agents/build/events/#function_tools_executed that fires after all the tools are complete