I want to do rpc based agent tool triggering.
# ask-ai
c
I want to do rpc based agent tool triggering.
t
Thanks for reaching out, Akshay. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To trigger an RPC-based agent tool in LiveKit, follow these steps: 1. Define the Tool with RPC: Create a tool in your agent to handle RPC calls using the
@function_tool()
decorator. Implement the logic for handling RPC payloads. 2. Register the RPC Method: Register the RPC method in your agent's initialization code to make it available for remote invocation. 3. Invoke the RPC Method from the Frontend: Use the
performRpc
method on the frontend to call the registered RPC method, specifying the destination identity and payload. For a detailed example and code snippets, refer to the RPC + State Agent Example. Let me know if you need further assistance!
c
Example simple from FE i will call the rpc method which will trigger the agent tool
t
Here’s a straightforward example of how your frontend can call an RPC method to trigger an agent tool in LiveKit: 1. Frontend Side (JavaScript): First, register the RPC method and create a handler function to manage the incoming payload.
Copy code
room.localParticipant.registerRpcMethod("client.flashcard", handleFlashcard);

async function handleFlashcard(data) {
    const payload = JSON.parse(data.payload);
    if (payload.action === "show") {
        // Update UI to display the flashcard
        console.log("Flashcard Data:", payload);
    }
}
Agent Side (Python): Then, use
perform_rpc
to send the RPC call over to the frontend.
Copy code
await ctx.room.local_participant.perform_rpc(
    destination_identity="frontend-identity",
    method="client.flashcard",
    payload=json.dumps({
        "action": "show",
        "id": "card123",
        "question": "What is the capital of France?",
        "answer": "Paris"
    })
)
This way, the agent can dynamically update the frontend UI whenever needed. If you’d like me to walk you through it more or share some extra examples, just let me know—I’m here to help! Sources: Remote method calls (RPC) | LiveKit Docs | Function calling with Voice Agents | LiveKit Docs
c
Once rpc called by FE... Rpc method should be able to trigger function tool from Assistant Agent. get_weather
t
Hey there! If you want to trigger a function tool like
get_weather
from an Assistant Agent using an RPC method, here’s a simple way to do it: 1. Define the Function Tool: Inside your agent, just use the
@function_tool()
decorator to set up the function tool.
Copy code
from livekit.agents import function_tool, Agent, RunContext

class MyAgent(Agent):
    @function_tool()
    async def get_weather(self, context: RunContext, location: str) -> dict:
        """Fetch weather information for a given location."""
        return {"weather": "sunny", "temperature_f": 70}
1. Trigger the Function via RPC: Then, from the frontend, you can call the function tool using
perform_rpc
.
Copy code
response = await ctx.room.local_participant.perform_rpc(
    destination_identity="agent-identity",
    method="get_weather",
    payload=json.dumps({
        "location": "New York"
    }),
    response_timeout=5.0
)
This way, your frontend can easily call the
get_weather
function tool via RPC. If anything’s unclear or you want more examples, just let me know—I’m here to help! Sources: RPC | LiveKit Docs | Tool definition and use | LiveKit Docs | complex-agents/shopify-voice-shopper/shopify.py