Tools are the primary way to extend your Magma agent’s capabilities. They allow your agent to interact with external systems, process data, and perform specific tasks.Tools are asynchronous methods that return a string or object response.
import { MagmaAgent } from "@pompeii-labs/magma";export default class MyAgent extends MagmaAgent { // We can define a tool using the @tool decorator // We can define parameters for the tool using the @toolparam decorator @tool({ description: "Get the current weather for a city" }) @toolparam({ key: "city", type: "string", description: "Name of the city", }) async getWeather(call: MagmaToolCall) { const { city } = call.fn_args; // Implementation here return `The weather in ${city} is sunny!`; }}
cache: Try to cache the tool if supported by the provider.
enabled: Dynamically enable or disable the tool based on agent state. Disabled tools are not supplied to the provider. If omitted, the tool will always be enabled.
Tools should return meaningful success/error messages
Tool return values should be concise and informative
Error Handling
While you can use a normal return string for failed tools, it is best practice to throw an error. The error message will be passed to the agent and it will attempt to fix the issue.
Remember that tool responses are used as context for the agent’s next action. Keep responses clear and structured to help the agent make better decisions.
For more examples and advanced usage, check out some of our Templates