hello ai
# ask-ai
v
hello ai
t
Thanks for reaching out, Ravi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! What can I help you with today?
v
i have already built a agent using livekit now i want to add the MCP in that how can i do that
t
Hey there! If you want to add MCP (Multi-Channel Processing) to your existing LiveKit agent, here’s a friendly step-by-step guide to help you out: 1. Install MCP Dependencies:
pip install livekit-agents[mcp]~=1.0
2. Create an MCP Server Instance:
Copy code
from livekit.agents import mcp
mcp_server = mcp.MCPServerHTTP("<https://your-mcp-server.com>")
3. Integrate MCP with Your Agent: You’ve got two options here! You can either pass the MCP server directly to the
AgentSession
or use the handy
MCPToolsIntegration
helper class. ◦ Option 1: Pass MCP Server to `AgentSession`:
session = AgentSession(vad=silero.VAD.load(), stt=deepgram.STT(), llm=openai.LLM(), tts=openai.TTS(), mcp_servers=[mcp_server])
Option 2: Use `MCPToolsIntegration`:
Copy code
from mcp_client.agent_tools import MCPToolsIntegration
agent = await MCPToolsIntegration.create_agent_with_tools(agent_class=YourAgentClass, mcp_servers=[mcp_server])
1. Handle Tool Registration: Make sure your agent is ready to handle tools from the MCP server by doing this:
tools = await MCPToolsIntegration.prepare_dynamic_tools(mcp_servers=[mcp_server])
If anything feels unclear or you want a hand with any of these steps, just give me a shout—I’m here to help!
v
session = AgentSession( vad=ctx.proc.userdata["vad"], min_interruption_words=0, userdata=UserData(), min_endpointing_delay=0.3, max_tool_steps=10, user_away_timeout=SILENCE_THRESHOLD, mcp_servers=[mcp_server] # Add this line ) will this work ?
t
Absolutely! Adding the
mcp_servers
parameter to the
AgentSession
constructor is the right move to get MCP working smoothly with LiveKit. Your setup should do just what you need. Just make sure your MCP server instance is properly set up and included in the list. Here’s a quick example:
Copy code
from livekit.agents import mcp
import os

agent_session = AgentSession(
    vad=ctx.proc.userdata["vad"],
    min_interruption_words=0,
    userdata=UserData(),
    min_endpointing_delay=0.3,
    max_tool_steps=10,
    user_away_timeout=SILENCE_THRESHOLD,
    mcp_servers=[
        mcp.MCPServerHTTP(url="<https://your-mcp-server.com>")
    ]
)
If you have any questions or want to chat more about this, just let me know—I’m here to help! Sources: Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
v
will this be able to list the tools by itself
t
Absolutely! When you add MCP servers to the
AgentSession
, it’ll automatically load and show the tools from those servers. LiveKit connects to the MCP servers you specify during the session setup, grabs the available tools, and gets them ready for the agent to use. If you want to double-check which tools are loaded, you can easily access them like this:
Copy code
# Here’s how to see the list of available tools
tools = agent.tools
Feel free to reach out if you have any questions or need a hand with anything else!
v
how do i connect the server if my server is stdio instead of https
t
If you want to connect an MCP server using stdio instead of HTTPS, you can use the
MCPServerStdio
class. Here’s a simple example to get you started:
Copy code
from mcp_client import MCPServerStdio

async def main():
    # Set up the MCP server with stdio parameters
    server = MCPServerStdio(
        params={"command": "path_to_your_server_command"}
    )

    # Connect to the server
    await server.connect()

    # Check out the available tools
    tools = await server.list_tools()
    print(f"Available tools: {tools}")

    # Use a tool
    result = await server.call_tool("tool_name", {"arguments": "here"})
    print(f"Tool result: {result}")

if __name__ == "__main__":
    asyncio.run(main())
Just swap out
path_to_your_server_command
with the actual command that starts your server. If you hit any bumps or have questions, feel free to ask—I’m here to help!
v
i was asking how do i connect it in agent , i already have stdio mcp server
t
Hey there! To connect your existing stdio MCP server in an agent, just follow these simple steps: 1. Install MCP Dependencies:
pip install livekit-agents[mcp]
2. Create an MCPServerStdio Instance:
Copy code
from livekit.agents.llm import mcp

mcp_server = mcp.MCPServerStdio(
    command="path/to/your/mcp_server_command",
    args=["--port", "5000"]
)
3. Pass the MCP Server to the Agent: When you create your agent or session, just include the MCP server like this:
Copy code
from livekit.agents import Agent, AgentSession

agent = Agent(
    instructions="Your agent instructions here",
    mcp_servers=[mcp_server]
)

session = AgentSession(
    agent=agent,
    mcp_servers=[mcp_server]
)
Don’t forget to swap out
path/to/your/mcp_server_command
and
args
with the actual command and arguments you use to start your MCP server. If you hit any bumps or have questions, just give me a shout—I’m here to help!
v
tools: [<bound method OutboundAgent.api_call of <__mp_main__.OutboundAgent object at 0x166be8d50>>, <bound method OutboundAgent.call_data of <__mp_main__.OutboundAgent object at 0x166be8d50>>, <bound method OutboundAgent.detect_voicemail of <__mp_main__.OutboundAgent object at 0x166be8d50>>, <bound method OutboundAgent.end_call of <__mp_main__.OutboundAgent object at 0x166be8d50>>, <bound method OutboundAgent.hold_for_duration of <__mp_main__.OutboundAgent object at 0x166be8d50>>, <bound method OutboundAgent.search_engine of <__mp_main__.OutboundAgent object at 0x166be8d50>>, <bound method OutboundAgent.smart_router of <__mp_main__.OutboundAgent object at 0x166be8d50>>, <bound method OutboundAgent.transfer_call of <__mp_main__.OutboundAgent object at 0x166be8d50>>] have my agent discovered the tools from the mcp
t
Hey there! To check if your agent has picked up tools from the MCP server, here’s what you can do: 1. Peek at the Agent Logs: Look out for messages like "Connected to MCP server" and "Received X tools from [server name]." These are good signs your agent is connected. 2. Check Out the Available Tools: Take a look at the
agent.tools
property to see all the tools your agent has, including those from the MCP server. 3. Give Tool Execution a Try: Use commands that should activate MCP tools and watch the logs for messages like "Invoking tool '[tool name]'." 4. Keep an Eye on Execution Logs: This helps confirm the tools are running smoothly and were discovered properly. From the tools you mentioned, it looks like your agent has found the tools successfully! If you want, I’m here to help you test them out or troubleshoot any hiccups. Sources: README.md | livekit-agents/livekit/agents/voice/agent.py