Skip to content

How to update MCP tools after starting an agent session? #3513

@narendra-bluebash

Description

@narendra-bluebash

I’m currently working with MCP servers in my agent setup.
When creating the agent session, I pass all MCP servers like this:

# Create agent session with all components
self.session = AgentSession(
    turn_detection=turn_detection,
    stt=self.stt_service.stt_service,
    vad=silero.VAD.load(),
    llm=self.llm_service.llm_service,
    tts=self.tts_service.tts_service,
    max_tool_steps=2,
    user_away_timeout=config.agent.max_silence_duration,
    mcp_servers=active_mcp_servers
)

Now, I want to assign only the tools that are required for a specific agent, instead of passing all tools from all MCP servers.
For initialization, I’m doing this:

async def initialize_mcp_servers_for_multi_agent(
    self, agent_name: str, mcp_servers: List[mcp.MCPServerHTTP]
) -> None:

    for i, server in enumerate(mcp_servers):
        logger.debug(f"Initializing MCP server {i} ({server})...")
        if not server.initialized:
            try:
                await server.initialize()
            except Exception as e:
                logger.critical(f"[MCP initialize_mcp_servers_for_multi_agent]: {e}")
                return

This works fine if we want all mcp tools but if want only required tools to pass llm then
The problem is: once the agent session has started, I can’t update the MCP tools (e.g., add/remove tools dynamically).

  1. Is there any way to pass new tools after the agent session has already started?

  2. Or alternatively, what’s the best approach to handle await server.initialize()
    I noticed that if an MCP server is not running, await server.initialize() hangs and never exits.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions