DEV Community

Khushiii
Khushiii

Posted on

Building AI Agents with Mastra β€” A TypeScript Edition (and Broke It Twice) πŸͺ‘πŸ€–

Hey everyone! Konichiwa, Khushi here (URESHIKO - learning Japanese now btw 🌸)

So this time it's Mastra - a TypeScript-native framework for building AI agents, bundling agents, tools, workflows, and memory into one toolkit instead of making you stitch together five libraries and pray.

I was exploring it and thought: let's actually build something instead of just reading about it. Spent a whole night digging in, and I built something cooool!

My Idea

You all know I love communities, so it's around communities this time.

Chat moves fast. Someone ships something incredible at 9am, and by 9:40 it's buried under meetup planning and "does anyone know a good React state library" chatter.

So what if an agent could scan through that noise and pull out the real wins β€” and even draft a tweet about it?

I called it Community Radar. Small idea, real use case, just vibes and curiosity 🌸

Setup

npm create mastra@latest
Enter fullscreen mode Exit fullscreen mode

The CLI handles everything - a few quick questions (project name, AI provider), and you've got a working project with an example weather agent already built in.

Mastra CLI setup running in terminal

Running the Mastra dev server

Your src/mastra/ folder ends up with:

  • index.ts β€” registers everything with Mastra
  • agents/weather-agent.ts β€” the agent itself
  • tools/weather-tool.ts β€” a tool the agent uses to fetch weather
  • workflows/weather-workflow.ts β€” a workflow that runs the agent
  • scorers/weather-scorer.ts β€” evaluates the agent's output

This isn't just filler - it's a template showing the exact pattern Mastra wants: Agent (the brain) + Tool (the hands) + Workflow (the process). You copy this pattern for your own idea.

I tested the weather agent first - asked it the weather in my hometown, Buxar, Bihar, just to confirm the pipeline worked. It did. Real weather, real response.

BTW, I used Gemini API keys from Google AI Studio β€” free for experiments like this.

Building My Agent

Copied the shape of the weather agent and rewrote it for my own thing:

Community Radar agent code

Registered it in index.ts (you have to manually wire every agent into the main Mastra setup or Studio won't know it exists β€” learned this the hard way when my agent didn't show up πŸ˜‚).

The First Real Test

Wrote up a fake community chat log - real wins hidden in a pile of small talk, exactly like my actual community chat looks:

Sample community chat test input

Agent response with summary and draft tweet

That's the moment this stopped feeling like "following a tutorial" and started feeling like "wait, I built something real" πŸ₯Ή

Adding a Workflow (and Immediately Breaking It)

Wanted the full pattern - Agent and Workflow together β€” not just the easy half.

A workflow is just a recipe with numbered steps. Mine only needed one: take chat text, hand it to the agent, return whatever comes back.

First attempt:

const response = await agent.stream([{ role: 'user', content: chatText }]);

let resultText = '';
for await (const chunk of response.textStream) {
  resultText += chunk;
}

return { result: resultText };
Enter fullscreen mode Exit fullscreen mode

Status: Success βœ…. Felt great for four seconds - until I opened the output:

Workflow showing success status with empty output

Empty result JSON output

Empty string. "Successfully" returned nothing. I cackled a little.

Turns out .stream() and manually collecting chunks doesn't resolve reliably inside a workflow step. Swapped to .generate(), which just hands back the full response:

const response = await agent.generate([{ role: 'user', content: chatText }]);
return { result: response.text };
Enter fullscreen mode Exit fullscreen mode

New error πŸ˜΅β€πŸ’«:

You exceeded your current quota... limit: 0, model: gemini-2.5-pro
Enter fullscreen mode Exit fullscreen mode

I'd left it pointed at gemini-2.5-pro, which doesn't have a real free tier. Swapped to gemini-2.5-flash - the same lighter model that worked fine earlier:

Successful workflow run with full output

Final summary and draft tweet output

Full pipeline, finally working, for real this time.

In Plain Terms, Community Radar Does:

  1. Input β†’ community chat text (pasted manually for v1)
  2. Agent + Workflow β†’ identifies the wins, summarizes them
  3. Output β†’ a draft tweet ready to share

What I Learned

Build first, read docs second. By the end of one night I had something real, and a much clearer picture of what Mastra actually is - not just what the landing page said.

If you're on the fence about trying a new framework: build the small, dumb version of your idea. You'll learn more from one broken workflow than an hour of docs.

Resources


πŸ’Œ Building something too? Tell me what β€” let's build, break things, and figure it out together 🫢

β€” Khushiii

Top comments (0)