Skip to main content

What You’ll Learn

  • Defining a createTool with @mastra/core/tools that executes Python in a Declaw sandbox
  • Validating tool inputs with Zod schemas
  • Creating a Mastra Agent with the tool and an OpenAI model
  • Demo mode that exercises the sandbox tool directly without needing an OpenAI key

Prerequisites

  • Declaw instance running and DECLAW_API_KEY / DECLAW_DOMAIN set
  • OPENAI_API_KEY (optional — the example runs in demo mode without it)

Code Walkthrough

1. Define the Declaw sandbox tool

createTool accepts an id, description, a Zod inputSchema, and an async execute function. The context parameter contains the validated input:
Zod validates the input before execute is called, so context.code is always a string. The tool returns a structured object — Mastra serializes it as JSON for the LLM to read.

2. Create a Mastra Agent

3. Run the agent

4. Demo mode (no API key needed)

Run the sandbox tool directly without creating a Mastra Agent:

Expected Output

In demo mode: