(Python) When init AgentSession, it is allowed to ...
# ask-ai
b
(Python) When init AgentSession, it is allowed to inject a list of mcp servers. However, after AgentSession is initialized, can the mcp server list be updated? Simple assign the AgentSession.mcp_servers var, or need to call update func similar to AgentSession.update_tools() ?
t
Thanks for reaching out, Jura. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! I’d love to help clarify this for you. The MCP server list in an
AgentSession
is usually set up during initialization, and there isn’t a documented method like
update_mcp_servers()
to change that list on the fly. Since the
mcp_servers
property is read-only, you can’t assign to it directly. That said, you can definitely update tools from MCP servers dynamically using the
update_tools()
method. A great way to do this is by preparing tools from new MCP servers with helper methods like
MCPToolsIntegration.prepare_dynamic_tools()
, and then adding them through
update_tools()
. If you have any more questions or a particular scenario you’re working with, please don’t hesitate to ask—I’m here to help! Sources: README.md | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | Tool definition and use | LiveKit Docs | agent.py
b
so i need to extract the whole mcp server functions and add them one by one?
t
I totally understand that adding MCP server functions one by one can feel a bit overwhelming, but don’t worry—you actually have two main ways to make this easier: 1. Automatic Integration (Recommended): You can simply pass the MCP servers directly to the
AgentSession
constructor using the
mcp_servers
parameter. This way, the tools will be automatically loaded and managed for you within the session—making things much smoother! 2. Manual Tool Extraction and Registration: If you prefer having more control, you can use the
MCPToolsIntegration.prepare_dynamic_tools()
method to extract tools from MCP servers, and then register them with your agent using
update_tools()
. This method also includes solid error handling and lets you tweak the tools before adding them. If you'd like, I’d be happy to walk you through the detailed steps for either option—just let me know how I can help! Sources: Tool definition and use | LiveKit Docs | complex-agents/shopify-voice-shopper/shopify.py | README.md
b
where can i find this func
MCPToolsIntegration.prepare_dynamic_tools()
t
Hi there! Just to clarify, the
MCPToolsIntegration.prepare_dynamic_tools()
function comes from an example repository that shows how MCP integration works with LiveKit agents. It’s not actually part of the main LiveKit Python SDK. Here’s where you can find it: - Repository: https://github.com/livekit-examples/basic-mcp - File:
mcp_client/agent_tools.py
This function is designed to fetch tools from multiple MCP servers and get them ready for use with LiveKit agents. If you’d like, I’m here to help you integrate this into your project—just let me know! Sources: mcp_client/agent_tools.py | livekit-agents/livekit/agents/llm/mcp.py | Tool definition and use | LiveKit Docs