showcase/shell-docs/src/content/docs/integrations/mastra/frontend-tools.mdx
<IframeSwitcher id="frontend-actions-example" exampleUrl="https://feature-viewer.copilotkit.ai/mastra/feature/agentic_chat?sidebar=false&chatDefaultOpen=false" codeUrl="https://feature-viewer.copilotkit.ai/mastra/feature/agentic_chat?view=code&sidebar=false&codeLayout=tabs" exampleLabel="Demo" codeLabel="Code" height="700px" />
Frontend tools enable you to define client-side functions that your Mastra agent can invoke, with execution happening entirely in the user's browser. When your agent calls a frontend tool, the logic runs on the client side, giving you direct access to the frontend environment.
This can be utilized to let your agent control the UI, generative UI, or for Human-in-the-loop interactions.
In this guide, we cover the use of frontend tools driving and interacting with the UI.
Use frontend tools when you need your agent to interact with client-side primitives such as:
<Step>
### Create a frontend tool
First, you'll need to create a frontend tool using the [useFrontendTool](/reference/v1/hooks/useFrontendTool) hook. Here's a simple one to get you started
that says hello to the user.
```tsx title="page.tsx"
export function Page() {
// ...
// [!code highlight:12]
useFrontendTool({
name: "sayHello",
description: "Say hello to the user",
parameters: z.object({
name: z.string().describe("The name of the user to say hello to"),
}),
handler: async ({ name }) => {
alert(`Hello, ${name}!`);
return `Said hello to ${name}!`;
},
});
// ...
}
```
</Step>
<Step>
### Accessing Frontend Tools
Since Mastra includes native support for the AG-UI protocol, frontend tools are automatically available to the agent as
tools.
</Step>
<Step>
### Give it a try!
You've now given your agent the ability to directly call any frontend tools you've defined. These tools will be available to the agent where they can be used as needed.
<video src="https://cdn.copilotkit.ai/docs/copilotkit/images/frontend-actions-demo.mp4" className="rounded-lg shadow-xl" loop playsInline controls autoPlay muted />
</Step>