Overview
Local tools are functions that run in the browser and can be called by the AI during a conversation. They’re useful for:- Accessing browser APIs (DOM, localStorage, geolocation)
- Making authenticated API calls with user credentials
- Manipulating client-side state
- Performing actions that require user context
How It Works
When you register a local tool:- Tambo converts the tool definition into an LLM function call schema
- The AI can choose to call your tool during a conversation
- Your function executes in the browser and returns results
- The AI receives the result and continues the conversation
import { z } from 'zod';
import { TamboTool } from '@tambo-ai/react';
const getCurrentLocationTool: TamboTool = {
name: 'getCurrentLocation',
description: 'Gets the user\'s current geographic location using the browser Geolocation API. Use this when the user asks about nearby places or wants location-based information.',
tool: async () => {
// Check if geolocation is available
if (!navigator.geolocation) {
throw new Error('Geolocation is not supported by this browser');
}
// Get position
const position = await new Promise<GeolocationPosition>((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
return {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
accuracy: position.coords.accuracy,
};
},
inputSchema: z.object({}), // No input parameters needed
outputSchema: z.object({
latitude: z.number().describe('Latitude coordinate'),
longitude: z.number().describe('Longitude coordinate'),
accuracy: z.number().describe('Accuracy in meters'),
}),
};
export const tools: TamboTool[] = [getCurrentLocationTool];
import { TamboProvider } from '@tambo-ai/react';
import { tools } from '@/lib/tambo-tools';
import { components } from '@/lib/tambo-components';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<TamboProvider
apiKey={process.env.NEXT_PUBLIC_TAMBO_API_KEY!}
userKey="user-123"
components={components}
tools={tools}
>
{children}
</TamboProvider>
</body>
</html>
);
}
Tool with Parameters
Here’s a tool that accepts input parameters:lib/tambo-tools.ts
Type-Safe Tools with defineTool
Use thedefineTool helper for full type inference:
Accessing React State
Tools can access React state and hooks by defining them inside a component:components/chat.tsx
Error Handling
Throw errors in your tool implementation to signal failures:Best Practices
Write clear descriptions
Write clear descriptions
Descriptions help the AI understand when to call your tool. Include:
- What the tool does
- When it should be used
- Any prerequisites or requirements
Use descriptive parameter names
Use descriptive parameter names
Schema field descriptions help the AI provide correct inputs:
Validate input carefully
Validate input carefully
Don’t trust AI-provided parameters. Validate and sanitize:
Handle errors gracefully
Handle errors gracefully
Throw descriptive errors that the AI can relay to the user:
Keep tools focused
Keep tools focused
Each tool should do one thing well. Instead of a single
manageUser tool, create separate createUser, updateUser, and deleteUser tools.Troubleshooting
Tool never gets called
Tool never gets called
- Check that the tool is in the
toolsarray passed toTamboProvider - Verify the description clearly indicates when to use the tool
- Ensure the tool name is unique and descriptive
- Check browser console for registration errors
Tool receives wrong parameters
Tool receives wrong parameters
- Add
.describe()to all schema fields - Make the input schema more specific with validations
- Check if parameter names clearly indicate their purpose
Tool errors not showing to user
Tool errors not showing to user
- Ensure you’re throwing errors (not just returning error objects)
- Check that error messages are descriptive
- Verify the AI is receiving the error (check network tab)
Local Tools vs MCP Tools
| Feature | Local Tools | MCP Tools |
|---|---|---|
| Execution | Browser (client-side) | Server-side |
| Access | Browser APIs, DOM, user state | Databases, filesystems, external APIs |
| Authentication | User’s cookies/tokens | Server credentials |
| Performance | Dependent on user’s device | Consistent server performance |
| Use Cases | UI manipulation, client state, authenticated APIs | Data fetching, file operations, integrations |
Next Steps
Context Helpers
Add dynamic context to AI conversations
MCP Integration
Connect server-side MCP tools
Register Components
Define components the AI can render
Error Handling
Handle tool errors properly