I have an issue with multi agent architecture and ...
# ask-ai
i
I have an issue with multi agent architecture and mcp. Agent 1 has a specific set of MCP tools to verify the user. Once verified the user is transferred to agent 2 which has access to a different set of MCP tools. I have ChatCTX working and am able to pass the context of the chat and tools to Agent 2. Now when Agent 2 attempts to use MCP tools it doesn’t seem to recognize the member ID from previous context unless the same MCP tools are available for both agents. Is there a way to not expose mcp tools, read the correct context and not hard code.
t
Thanks for reaching out, Spencer. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you out better, could you please clarify a few things? • Are the MCP tools for Agent 1 and Agent 2 completely different, or do they share some overlapping features? • How are you passing the context between the agents? Are you using a particular method or protocol for this? • Can you share a bit more about the "member ID" issue? For instance, is it stored in the context but not accessible to Agent 2, or is it getting overwritten during the transfer? Understanding these details will really help us find a solution that avoids hardcoding and makes sure the context is handled smoothly. Sources: Model Context Protocol Integration | livekit/livekit_composite | DeepWiki | Workflows | LiveKit Docs | complex-agents/personal_shopper/personal_shopper.py
i
• there are no overlapping tools or features other than the variable needed (memberID) which is found during validation. • I am using a data class of “shared_context” and saving the function call output there and passing into the chatctx per agent. • The memberID is found during validation and stored in the output of a tool call. Agent 2 refuses to use the tool output unless it has access to the same tools.
t
Hey there! To safely share the
memberID
between agents without exposing tools, here are some friendly tips you can try out: • Use `context.userdata`: You can store the
memberID
in
context.userdata
while Agent 1 is running its tool. This way, the data sticks around for the whole session and Agent 2 can access it without needing the same tools. • Agent Handoff with Data: When Agent 1 finishes up, just pass the
memberID
along with the next agent instance. This keeps your data safe and sound during the handoff. • Global Tool Definitions: If several agents need to use the same logic, defining the tool globally and adding it to both agents’ toolsets helps avoid repeating yourself and keeps things consistent. • Dynamic Tool Updates: You can also use
agent.update_tools()
to add or remove tools on the fly, making sure each agent only has what it really needs. For what you’re working on, I’d suggest combining
context.userdata
with agent handoffs. Here’s a quick example:
Copy code
@function_tool()
async def authenticate(self, context: RunContext, user_identifier: str):
    member_id = await lookup_member_id(user_identifier)
    context.userdata.member_id = member_id  # Store securely
    return BillingAgent(), f"User {member_id} authenticated. Proceeding to billing."
This way, your tools stay behind the scenes, your data is shared safely, and you’re making the most of LiveKit’s multi-agent workflow features. If you want to chat more or need a hand, just let me know—I’m here to help! Sources: Tool definition and use | LiveKit Docs | Building Multi-Agent Architectures with LiveKit Agents | livekit-agents/livekit/agents/voice/agent_activity.py
i
Since the MCP tools are dynamic I am unable to hard code them. And since every agent won’t need the memberID I am unable to explicitly require that data.
t
To work with dynamic MCP tools and optional shared data like
memberID
, here are some friendly strategies you might find helpful: • Dynamic Context Updates: You can use
context.userdata
to store the
memberID
only when it’s available and relevant. This way, the data stays optional and you don’t have to require it for every agent. • Dynamic Tool Management: Try using
agent.update_tools()
to add or remove tools on the fly, depending on what the agent needs at the moment. It’s a neat way to keep things flexible without hardcoding. • Conditional Logic in Tools: Inside your tools, you can check if
memberID
exists in
context.userdata
. If it does, great—you use it! If not, no worries, the tool can just carry on without it. Here’s a quick example of how you might check and use
memberID
dynamically:
Copy code
@function_tool()
async def process_data(self, context: RunContext):
    member_id = context.userdata.get('member_id', None)
    if member_id:
        # Use memberID if available
        return await perform_action_with_member_id(member_id)
    else:
        # Proceed without memberID
        return await perform_action_without_member_id()
This way, you keep things flexible and avoid hardcoding, all while letting your MCP tools stay dynamic. If anything’s unclear or you want to dive deeper, just let me know—I’m here to help!
i
Ideally I would rely on the member ID stored in tool output inside UserData.shared context. Then the MCP tools use that data inside the context
t
To make sure the
memberID
stored in
UserData.shared_context
is used smoothly by your MCP tools, here’s a friendly way to handle it: • Save the
memberID
in `shared_context`:
When Agent 1 runs, pop the
memberID
into
context.userdata.shared_context
. This way, it’s easy for all agents to access it. • Let MCP tools grab
memberID
from `shared_context`:
Update your MCP tools so they look for the
memberID
in
shared_context
. And if it’s not there? No worries — just handle that case smoothly. Here’s a quick example to show how it works:
Copy code
@function_tool()\nasync def mcp_tool(self, context: RunContext):\n    member_id = context.userdata.shared_context.get('member_id', None)\n    if member_id:\n        # Use memberID from shared_context\n        return await perform_action_with_member_id(member_id)\n    else:\n        # Handle case where memberID is not available\n        return await perform_action_without_member_id()\n
This way, your MCP tools can flexibly use the
memberID
without any hardcoding or tricky dependencies. If you want me to walk you through anything else, just let me know — I’m here to help!