In the documentation, we see that initial metadata...
# ask-ai
m
In the documentation, we see that initial metadata can be set through the access token. Could you please describe what claims we can change or update in order to achieve this initial metadata setting? "Initial values can be set in the participant’s access token, ensuring the value is immediately available when the participant connects."
t
Thanks for reaching out, Stanislav. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To help you set initial metadata for a participant in LiveKit using an access token, here’s a friendly guide on the claims you can use: • metadata: This lets you add a custom string that will be attached to the participant right when they join the room. The great part is, this metadata is shared and synchronized with everyone in the room. • attributes: Think of this as a handy key-value store of strings, perfect for managing more detailed participant state. You can set these claims directly in the access token before issuing it. How you do this depends on the SDK you’re working with, so here’s a quick rundown: • Node.js SDK: Use
set metadata(md: string)
or
set attributes(attrs: Record<string, string>)
. • Python SDK: Use
with_metadata(metadata: str)
or
with_attributes(attributes: dict[str, str])
. • Go SDK: Use
SetMetadata(md string)
or
SetAttributes(attrs map[string]string)
. • Rust SDK: Use
with_metadata(metadata: &str)
or
with_attributes()
. • Ruby SDK: Use
metadata=
or
attributes=
. • Kotlin SDK: Use the
metadata
or
attributes
properties. Once set, these initial values will be ready and waiting as soon as your participant connects to the room, as long as the token includes the necessary grants. If you have any questions or need a hand with this, feel free to ask—I’m here to help! Sources: auth/accesstoken.go | Authentication | LiveKit Docs | packages/livekit-server-sdk/src/grants.ts | examples/agent_dispatch.py | livekit-protocol/src/livekit.rs
m
How to dynamically update mcp servers in the session?
t
Thank you for reaching out! Could you please help me understand a bit more about what you mean by dynamically updating MCP servers in the session? Are you talking about changing the server configuration while a session is active, or is it something different? Any extra details you can share will really help me support you better.
m
Based on change in user attributes, I want to add or remove MCP server to the session to control what tools are available
t
I'd love to help you dynamically update MCP servers in a session based on user attributes. Here's a straightforward approach you can follow: 1. Set the MCP server URL as a participant attribute: You can do this when generating the token, through dispatch rules if you're using SIP integrations, or by calling
updateParticipant
from a backend service once the user has joined. This helps keep things flexible! 2. Access participant attributes in the session: After connecting to the room, you can retrieve these attributes from the participant object. Here's a simple example in Python:
Copy code
async def entrypoint(ctx: JobContext):
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
    participant = await ctx.wait_for_participant()

    # Read custom attribute
    mcp_url = participant.attributes.get("mcp_server_url")
    if mcp_url:
        mcp_servers.append(mcp.MCPServerHTTP(url=mcp_url))
1. Update the MCP server list: With the attribute value you retrieved, you can dynamically configure the
mcp_servers
list and pass it along to the
AgentSession
. This keeps your session up-to-date with the right servers. If you find that attributes aren't accessible at runtime, a handy alternative is to embed the MCP server URL in the
metadata
field or pass it through environment variables during deployment. This way, you still maintain flexibility without any hiccups. Feel free to reach out if you'd like me to walk you through any of these steps in more detail—I'm here to help! Sources: examples/voice_agents/mcp/mcp-agent.py | Tool definition and use | LiveKit Docs | Using MCP with LiveKit Agents | agent-console/app/_components/participant-action-panel.tsx | SIP overview | LiveKit Docs
m
is there a way to hook into the tool call before it is sent to mcp?
t
Thank you for reaching out! While LiveKit and MCP don’t have a built-in hook or interceptor specifically for catching tool calls before they reach the MCP server, there’s a great way to handle this by overriding the
llm_node
method in a custom
Agent
class. This approach is supported and reliable within LiveKit Agents, letting you inspect and respond to tool call initiations just before they happen. Here’s a friendly example to guide you through overriding `llm_node`:
Copy code
async def llm_node(self, chat_ctx, tools, model_settings):
    tool_call_detected = False
    async for chunk in super().llm_node(chat_ctx, tools, model_settings):
        if isinstance(chunk, ChatChunk) and chunk.delta and chunk.delta.tool_calls and not tool_call_detected:
            tool_call_detected = True
            self.activity.agent.say("Sure, I'll check that for you.")
        yield chunk
This method helps you to: • Spot when the LLM decides to make a tool call. • Carry out side effects like logging, updating states, or giving feedback. • Continue with the tool call smoothly by yielding the chunk. If anything feels unclear or you’d like some extra help implementing this, please don’t hesitate to ask—I’m here to support you every step of the way! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | mcp_client/server.py | Tool definition and use | LiveKit Docs
m
how to get audio microphone level to visualize user voice as they speak in flutter? is it possible to subscribe or get it in the stream? I want to make my own SoundWaveformWidget
t
I'm happy to help you visualize microphone audio levels in Flutter by creating a custom
SoundWaveformWidget
using the LiveKit Flutter SDK's handy tools. Here's a simple way to get started: 1. Install Required Packages: First, add these to your `pubspec.yaml`:
Copy code
dependencies:
  livekit_client: ^1.4.0
  livekit_components: ^0.5.0
1. Configure Permissions: Make sure your app has microphone access enabled in your configuration files (like
Info.plist
for iOS and
AndroidManifest.xml
for Android). 2. Use the
SoundWaveformWidget
or `AudioVisualizerWidget`:
These widgets will take care of processing the PCM audio buffers and display a lively waveform or bar visualizer. Here's an example:
Copy code
SoundWaveformWidget(
  audioTrack: localAudioTrack, // e.g., room.localParticipant.audioTrack
  options: AudioVisualizerWidgetOptions(
    barCount: 7,
    width: 12,
    minHeight: 12,
    maxHeight: 100,
    color: Colors.blue,
    spacing: 5,
  ),
)
If you prefer, you can also try
AudioVisualizerWidget
which integrates nicely with LiveKit's provider pattern:
Copy code
AudioVisualizerWidget(
  options: AudioVisualizerWidgetOptions(
    barCount: 7,
    color: Colors.green,
    width: 10,
    minHeight: 10,
    maxHeight: 80,
  ),
  backgroundColor: Colors.black12,
)
Both of these widgets handle the audio visualization seamlessly by processing the audio track's data for you. If you have any questions or want to explore more customization options, please feel free to ask. I'm here to help you every step of the way! Sources: Web and mobile frontends | LiveKit Docs | lib/src/ui/widgets/track/audio_visualizer_widget.dart | Flutter quickstart | LiveKit docs | shared_swift/Visualizer.swift | Docs/audio.md