big-controller-60015
10/13/2025, 12:45 PMtall-airline-16756
10/13/2025, 12:46 PMtall-airline-16756
10/13/2025, 12:47 PMbig-controller-60015
10/13/2025, 12:47 PMtall-airline-16756
10/13/2025, 12:48 PMimport asyncio
from livekit import api
async def list_rooms_by_agent(agent_name: str):
"""
List all rooms that have a specific agent dispatched to them.
Args:
agent_name: The name of the agent to filter by
Returns:
List of room names where the agent is dispatched
"""
# Initialize the LiveKit API client
lkapi = api.LiveKitAPI()
try:
# Step 1: Get all active rooms
rooms_response = await lkapi.room.list_rooms(api.ListRoomsRequest())
matching_rooms = []
# Step 2: Check each room for the target agent
for room in rooms_response.rooms:
dispatches = await lkapi.agent_dispatch.list_dispatch(room.name)
# Step 3: Filter dispatches by agent name
for dispatch in dispatches:
if dispatch.agent_name == agent_name:
matching_rooms.append({
'room_name': room.name,
'dispatch_id': dispatch.dispatch_id,
'metadata': dispatch.metadata
})
break # Found a match, so no need to check other dispatches in this room
return matching_rooms
finally:
await lkapi.aclose()
# Example usage
async def main():
agent_name = "test-agent"
rooms = await list_rooms_by_agent(agent_name)
print(f"Rooms with agent '{agent_name}':")
for room in rooms:
print(f" - {room['room_name']} (dispatch: {room['dispatch_id']})")
if __name__ == "__main__":
asyncio.run(main())
Here's a quick summary of what this code does:
• It retrieves all active rooms by calling lkapi.room.list_rooms().
• Then, it checks each room for any agent dispatches using lkapi.agent_dispatch.list_dispatch(room_name).
• Finally, it filters to find the rooms where the agent's name matches the one you specify.
If you'd like, I’m here to help you tweak this code further—whether that's adding features like partial name matching or improving performance. Just let me know!
Sources: mcp/server.py | livekit-api/livekit/api/agent_dispatch_service.py