<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mastra AI</title>
    <description>The latest articles on Forem by Mastra AI (@mastra_ai).</description>
    <link>https://forem.com/mastra_ai</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F10639%2Fbb6e9abf-900b-4e45-94c0-eb8061e27b50.jpg</url>
      <title>Forem: Mastra AI</title>
      <link>https://forem.com/mastra_ai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mastra_ai"/>
    <language>en</language>
    <item>
      <title>Build your first agent in 5 minutes with Mastra</title>
      <dc:creator>Shane Thomas</dc:creator>
      <pubDate>Thu, 10 Apr 2025 15:55:00 +0000</pubDate>
      <link>https://forem.com/mastra_ai/build-your-first-agent-in-5-minutes-with-mastra-2ah3</link>
      <guid>https://forem.com/mastra_ai/build-your-first-agent-in-5-minutes-with-mastra-2ah3</guid>
      <description>&lt;p&gt;Everyone seems to be talking about AI agents this year. They can automate everyday tasks, including the ones you probably do yourself (like putting together weekly changelogs). Agents can be customized and are positioned to become the new building blocks of software.&lt;/p&gt;

&lt;p&gt;In this guide, you'll learn how to build your first AI agent in just 5 minutes.&lt;/p&gt;

&lt;p&gt;This tutorial is totally beginner-friendly.&lt;/p&gt;

&lt;p&gt;All you need is a command line, your favorite IDE (we recommend Cursor or Windsurf), and a few minutes to spare.&lt;/p&gt;

&lt;p&gt;Let's dig in...&lt;/p&gt;




&lt;h2&gt;
  
  
  What is covered?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is an agent?&lt;/li&gt;
&lt;li&gt;What is Mastra?&lt;/li&gt;
&lt;li&gt;Building your first agent&lt;/li&gt;
&lt;li&gt;Bonus: Adding memory&lt;/li&gt;
&lt;li&gt;Additional resources&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Agents
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is an agent?
&lt;/h3&gt;

&lt;p&gt;Here's a simple definition of an agent: &lt;em&gt;an agent is any software with non-deterministic code.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That is, any software that can make autonomous decisions based on inputs and environment rather than following fixed, predictable instructions every time.&lt;/p&gt;

&lt;p&gt;Agents are AI systems that can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Perceive their environment through various inputs&lt;/li&gt;
&lt;li&gt;Make decisions based on those inputs&lt;/li&gt;
&lt;li&gt;Take actions to accomplish specific goals&lt;/li&gt;
&lt;li&gt;Learn and adapt their behavior over time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike traditional software that follows explicit rules, agents use AI models (like large language models) to understand context, reason through problems, and determine the best course of action. They can handle ambiguity and adapt to new situations without being explicitly programmed for every scenario.&lt;/p&gt;

&lt;p&gt;The best agents use several important features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Memory: They remember past interactions and learn from them&lt;/li&gt;
&lt;li&gt;Planning: They can break down complex tasks into smaller steps&lt;/li&gt;
&lt;li&gt;Tool use: They can leverage external tools and APIs to expand their capabilities&lt;/li&gt;
&lt;li&gt;Feedback loops: They can evaluate their own performance and adjust accordingly&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The agentic spectrum
&lt;/h3&gt;

&lt;p&gt;When discussing AI agents, it's helpful to understand that they exist on a &lt;em&gt;spectrum&lt;/em&gt; of autonomy (aka agency).&lt;/p&gt;

&lt;p&gt;This spectrum helps us categorize different types of agents based on how much independent decision-making they can perform:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Less agentic systems&lt;/strong&gt; are mostly deterministic workflows with occasional LLM calls. Think trip planners that follow templates but use AI for personalization. These are predictable but less flexible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;More agentic systems&lt;/strong&gt; allow LLMs to build and execute their own plans using external tools. AI customer service assistants with access to knowledge bases exemplify this middle ground—handling complex tasks within defined boundaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Most agentic systems&lt;/strong&gt; operate autonomously with minimal human input. Self-driving cars represent this category, making continuous independent decisions in unpredictable environments. These require exceptional reliability and safety measures.&lt;/p&gt;

&lt;p&gt;As you build your first agent, it's important to consider where on this spectrum your application should fall.&lt;/p&gt;

&lt;p&gt;For beginners, starting with less agentic systems provides a gentler learning curve, so that's where we'll start today.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Mastra?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/mastra-ai/mastra" rel="noopener noreferrer"&gt;Mastra&lt;/a&gt; is an open-source AI Agent Framework for Typescript.&lt;/p&gt;

&lt;p&gt;It includes all the basic primitives for AI engineering right-out-the-box, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Agents with tools, memory, and tracing&lt;/li&gt;
&lt;li&gt;State-machine based workflows with human-in-the loop (suspend/resume) and a simple API (&lt;code&gt;.step(), .then(), .after()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Evals (non-deterministic tests) for tracking and measuring AI output&lt;/li&gt;
&lt;li&gt;Storage for RAG pipelines&lt;/li&gt;
&lt;li&gt;Local development playground&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;... and more.&lt;/p&gt;

&lt;p&gt;We'll be installing and setting up your first Mastra project in today's tutorial.&lt;/p&gt;

&lt;p&gt;So let's get started!&lt;/p&gt;




&lt;h2&gt;
  
  
  Building your first agent
&lt;/h2&gt;

&lt;p&gt;Building your first agent is as easy as installing your first Mastra instance and getting it running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Mastra
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fat4iuwgbh17k21ykt58b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fat4iuwgbh17k21ykt58b.gif" alt="install" width="720" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;npm create mastra@latest&lt;/code&gt; in your command line to get started. Follow the on-sceen prompts to configure your setup. You’ll want to give your project a name and select where to install. &lt;/p&gt;

&lt;p&gt;For this tutorial, please be sure to:&lt;/p&gt;

&lt;p&gt;✅  Opt-in to installing both Agents and Workflows&lt;br&gt;
✅ Say &lt;code&gt;yes&lt;/code&gt; to installing tools&lt;br&gt;
✅ Select &lt;code&gt;OpenAI&lt;/code&gt; for your model&lt;br&gt;
✅ Say &lt;code&gt;yes&lt;/code&gt; to adding an example&lt;/p&gt;

&lt;p&gt;Also be sure to plug in your OpenAI API key. (You'll want to create an &lt;a href="https://www.notion.so/kepler-inc/platform.openai.com" rel="noopener noreferrer"&gt;OpenAI Platform&lt;/a&gt; account and purchase credits &lt;a href="https://platform.openai.com/settings/organization/api-keys" rel="noopener noreferrer"&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Finally, open your project in your favorite AI IDE, whether its Cursor or Windsurf!&lt;/p&gt;
&lt;h3&gt;
  
  
  Exploring the weather agent
&lt;/h3&gt;

&lt;p&gt;Now that you’ve installed Mastra, let’s explore the built-in example. After you become familiar with how it all works, you will be able to easily make edits and build your own agents and workflows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Pull up your project ( &lt;code&gt;cd {your-project-name}&lt;/code&gt; ).&lt;/strong&gt; &lt;br&gt;
Once you’re in your project, type &lt;code&gt;npm run dev&lt;/code&gt; to get the project running locally on your browser. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Open the Playground URL (&lt;a href="http://localhost:4111/" rel="noopener noreferrer"&gt;http://localhost:4111/&lt;/a&gt;).&lt;/strong&gt; &lt;br&gt;
You should see something like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fogoy8ahzxu0uukdznjjs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fogoy8ahzxu0uukdznjjs.png" alt="playgroundUI" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Explore the Playground!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Try talking to the weather agent. Notice how it shows exactly what tools the agent uses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F27l8t5rkffwgstb2tvvs.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F27l8t5rkffwgstb2tvvs.gif" alt="weatherAgent" width="836" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Explore traces: look at all the different events that were triggered when you prompted your agent. This is helpful for understanding what caused your agent to make the decisions it did.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2w1jm79x1h96v6smyrfl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2w1jm79x1h96v6smyrfl.gif" alt="weatherTraces" width="760" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See what tools are available to your agent(s). This is a good place to test tools in isolation, knowing that when you hand them to your agent, they’re already tested and ready to go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi79afog2itibuw75aexh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi79afog2itibuw75aexh.gif" alt="weatherTools" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Access all your workflows in one place. This is a great place to understand what process your agents follow and make adjustments if needed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Funwnp0dalrd3lq6fy8h3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Funwnp0dalrd3lq6fy8h3.gif" alt="weatherWorkflows" width="836" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Explore the code for the example in the &lt;code&gt;src / mastra&lt;/code&gt; folder.&lt;/strong&gt; &lt;br&gt;
You’ll want to look into all of the subfolders  (agents, tools, and workflows) to see how each of the components were built.&lt;/p&gt;

&lt;p&gt;Notice how the agent has a relevant name (&lt;code&gt;weatherAgent&lt;/code&gt; in this case) and a long, detailed system prompt. The prompt lays out what the agent is, what guidelines to follow for responding, and what tools it is supposed to use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6o5j67ynuhkmp6se9hgy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6o5j67ynuhkmp6se9hgy.png" alt="weatherCode" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;weatherTool&lt;/code&gt; gets the weather for a given city and is able to map the data it fetches into the correct descriptions. Tool descriptions are very important. You should make them so clear that a random person is able to look at the description and understand exactly what the tool is.   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1a6h5f5cb4t2ntxoswa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp1a6h5f5cb4t2ntxoswa.png" alt="weatherToolCursor" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our workflow has two steps. It’s triggered by receiving the name of a city and first fetches the weather in that city. Then, it suggests activities to do in that city based on the weather.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6sk10fv5nn1irn67fwf9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6sk10fv5nn1irn67fwf9.png" alt="workflowsCursor" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you understand the basics of a simple example, you can start making your own.&lt;/p&gt;
&lt;h3&gt;
  
  
  Adding more tools to our agent
&lt;/h3&gt;

&lt;p&gt;Let’s get our agent to not only help us with the weather but also with processing simple financial transactions. &lt;/p&gt;

&lt;p&gt;For the purposes of this example, we will use &lt;a href="https://docs.google.com/spreadsheets/d/1Cwpt02G1fGOtSQARSbcmZe3Kl0FivvuUbKtg0UsKcnk/edit?usp=sharing" rel="noopener noreferrer"&gt;mock data&lt;/a&gt;. You can imagine that this data represents a credit card statement or bank account transactions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2gv35bp4pasp9070pgrn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2gv35bp4pasp9070pgrn.png" alt="mockData" width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Create &lt;code&gt;getTransactions&lt;/code&gt; tool
&lt;/h4&gt;

&lt;p&gt;Let’s first create a new tool, &lt;code&gt;getTransactions&lt;/code&gt; , that returns a string of transactions from our CSV. &lt;/p&gt;

&lt;p&gt;The easiest approach is to prompt your IDE. In this case, we tell it: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I need to create a new tool in this file that is called &lt;code&gt;getTransactions&lt;/code&gt;. It needs to fetch data from this URL (&lt;a href="https://docs.google.com/spreadsheets/d/1Cwpt02G1fGOtSQARSbcmZe3Kl0FivvuUbKtg0UsKcnk/edit?gid=0#gid=0" rel="noopener noreferrer"&gt;https://docs.google.com/spreadsheets/d/1Cwpt02G1fGOtSQARSbcmZe3Kl0FivvuUbKtg0UsKcnk/edit?gid=0#gid=0&lt;/a&gt;) and return a string containing the the CSV data. Use the existing weather tool as an example.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxpnyn2e3b8eahuj5s0zk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxpnyn2e3b8eahuj5s0zk.gif" alt="getTransactions" width="672" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If that doesn’t work, you can manually add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getTransactionsTool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createTool&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get-transactions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Get transaction data from Google Sheets&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;inputSchema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({}),&lt;/span&gt;
  &lt;span class="na"&gt;outputSchema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;csvData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getTransactions&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getTransactions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://docs.google.com/spreadsheets/d/e/2PACX-1vTQWaCzJAFsF4owWRHQRLo4G0-ERv31c74OOZFnqLiTLaP7NweoiX7IXvzQud2H6bdUPnIqZEA485Ux/pubhtml?gid=0&amp;amp;single=true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;csvData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: We find that just making this change alone is sufficient 50% of the time for getting your weather agent to process transaction data. (You can always test it by asking questions like “how much did I spend on Amazon this month?”) But don’t worry if you’re in the unlucky half. The next few steps will help out…&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Change your system prompt
&lt;/h4&gt;

&lt;p&gt;Right now, the agent is still defined only as a weather agent. Of course, we want it to do more and understand financial transactions. So we need to change our system prompt.&lt;/p&gt;

&lt;p&gt;We’ve found that it’s deceptively challenging to write good, detailed and highly-specific system prompts. That’s why Mastra dev has a built-in prompt enhancer. &lt;/p&gt;

&lt;p&gt;We suggest telling it something like: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Change this so the assistant is a personal assistant that can help with the weather and personal finance transactions. &lt;br&gt;
Always convert celsius to fahrenheit&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwkljidcxvmp4p7v2uaxz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwkljidcxvmp4p7v2uaxz.gif" alt="promptEnhancer" width="832" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save the results and try it out. For this example, it should do a pretty good job. Test it out with questions both about the weather and your transactions: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpj4fdd4pgapa2qwf9ry6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpj4fdd4pgapa2qwf9ry6.png" alt="final" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you’re satisfied, you can copy and paste the new prompt into your actual code. We suggest renaming your agent to &lt;code&gt;assistantAgent&lt;/code&gt; as well. &lt;/p&gt;

&lt;p&gt;For reference, this is what we ended up copying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;assistantAgent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Agent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Personal Assistant Agent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;instructions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;` ROLE DEFINITION
- You are a personal assistant specializing in both weather information and personal financial transactions.
- Your key responsibilities include providing accurate weather updates and assisting with financial queries and transactions.
- Primary stakeholders are individual users seeking personal assistance.
CORE CAPABILITIES
- Provide weather details for specific locations, including temperature, humidity, wind conditions, and precipitation.
- Assist with personal financial transactions, such as checking account balances, recent transactions, and basic financial advice.
- Use the weatherTool to fetch current weather data and financialTool for financial information.
BEHAVIORAL GUIDELINES
- Maintain a professional and friendly communication style.
- Always ask for a location if none is provided for weather queries.
- Translate non-English location names to English.
- Convert temperature from Celsius to Fahrenheit.
- Keep responses concise but informative.
- Ensure user privacy and data security in financial transactions.
CONSTRAINTS &amp;amp; BOUNDARIES
- Do not provide financial investment advice or handle large financial transactions.
- Avoid discussing topics outside of weather and personal finance.
- Adhere to security protocols to protect user data.
SUCCESS CRITERIA
- Deliver accurate and timely weather information and financial data.
- Achieve high user satisfaction through clear and helpful responses.
- Maintain user trust by ensuring data privacy and security.
`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gpt-4o&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;tools&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;weatherTool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getTransactionsTool&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Bonus: adding memory
&lt;/h3&gt;

&lt;p&gt;Right now our agent does not store memory and remember previous conversations. &lt;/p&gt;

&lt;p&gt;This can easily be solved, however, with the addition of &lt;a href="https://mastra.ai/docs/memory/overview" rel="noopener noreferrer"&gt;Mastra memory&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install memory with &lt;code&gt;npm install @mastra/memory&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;import { Memory } from '@mastra/memory';&lt;/code&gt; to the top of your agents&amp;gt;index.ts file&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;memory: new Memory(),&lt;/code&gt; as a property of your agent, directly below the &lt;code&gt;tools: { weatherTool, getTransactionsTool },&lt;/code&gt; line of code &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Tip: with the help of &lt;a href="https://mastra.ai/blog/introducing-mastra-mcp" rel="noopener noreferrer"&gt;Mastra’s MCP documentation server&lt;/a&gt;, you can simply chat with your IDE to add additional Mastra features. &lt;a href="https://mastra.ai/docs/getting-started/mcp-docs-server" rel="noopener noreferrer"&gt;Find Mastra MCP documentation server installation instructions here&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🎉 Congrats! You’ve built your first agent
&lt;/h2&gt;

&lt;p&gt;If you’ve followed the above steps, you’ll have finished building your first AI agent. If you experience any troubles, you can always ask questions on the &lt;a href="https://discord.gg/BTYqqHKUrf" rel="noopener noreferrer"&gt;Mastra Discord&lt;/a&gt;. Plus, all the code for this example can be found &lt;a href="https://github.com/mastra-ai/mar27-workshop/blob/main/src/mastra/tools/index.ts" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;💫 &lt;a href="https://github.com/mastra-ai/mastra" rel="noopener noreferrer"&gt;If this example was helpful, consider starring us on GitHub&lt;/a&gt;. 💫&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional resources
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://mastra.ai/" rel="noopener noreferrer"&gt;Mastra website&lt;/a&gt;&lt;br&gt;
&lt;a href="https://x.com/mastra_ai" rel="noopener noreferrer"&gt;Mastra twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://discord.gg/BTYqqHKUrf" rel="noopener noreferrer"&gt;Mastra Discord&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s next
&lt;/h3&gt;

&lt;p&gt;Want to level-up your agent so it can send emails, track your GitHub, post on social media, and chat directly with you over telegram or Slack?&lt;/p&gt;

&lt;p&gt;Stay tuned because a crash-course in MCP is in your future… 🔮&lt;/p&gt;

</description>
      <category>ai</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
