0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【初心者向け】TypeScript製エージェントフレームワーク「Mastra」入門ガイド

Posted at

そもそもMastraってなに?

Mastraは、エージェント(AIで考える小さな作業者)を組み合わせて、自動的にタスクをこなしてくれるフレームワークです。


Mastraでできること

例えば、こんな流れが作れます。

  1. Webスクレイピングツールでデータを集める(Tool)
  2. AIエージェントでデータを要約する(Agent)
  3. プランナーで一連の流れを管理する(Plan)

これを、少ないコードで自然に構築できるのがMastraです!


Mastraのコンポーネント構成

Mastraは、
たった4つのコンポーネントでできています!

コンポーネント 役割
Tool 単純な作業をする API叩く、ファイル読む
Agent 判断・推論する テキスト要約、分類
Plan 作業の流れを組み立てる 「AしてBしてCする」フロー
Memory 状態や履歴を記録する 作業ログ保存(予定機能含む)

このシンプルさが、初心者にもわかりやすく、応用しやすいポイントです。


1. Tool(ツール)

単純な作業を担当するパーツ。

  • Webスクレイピングをする
  • APIを叩いてデータを取ってくる
  • ファイルを読み込む

など、「何かをする」シンプルな処理をToolにまとめます。


✅ たとえば:

class FetchUserData extends Tool {
  async run() {
    return await fetch("https://api.example.com/user");
  }
}

→ データ取得だけに専念する!


2. Agent(エージェント)

考えたり、判断したりするパーツ。

  • テキストを要約する
  • データを分類する
  • 意思決定をする

など、**「知的な作業」**をAgentに任せます。


✅ たとえば:

class SummarizeTextAgent extends Agent {
  async run(text: string) {
    return `要約: ${text.substring(0, 100)}...`;
  }
}

→ 簡単な要約ロジックを持たせたりできる!


3. Plan(プランナー)

ToolとAgentをつないで流れを作るパーツ。

  • 「最初にこれをやって、次にあれをやって」という順番管理をします。
  • 分岐や並列処理もサポート予定!(発展)

✅ たとえば:

const plan = new Plan([
  new FetchUserData(),
  new SummarizeTextAgent()
]);

流れを組み立てて「一気に自動実行」できる!


4. Memory(メモリー)

データや履歴を保存するパーツ。(※将来拡張される予定)

  • 途中結果を覚えさせたり
  • 成功・失敗の履歴を取ったり
  • チームで共有するために使う

現在はシンプルな使い方が中心ですが、
ログ管理・状態保持などに活用できるようになる方向です。


【コンポーネントまとめ図】

[ Tool ] → [ Agent ] → [ Tool ] → [ Agent ]
     ↓           ↓            ↓          ↓
   作業        判断         作業        判断
            ↓
          [ Plan ] ← 流れを管理
  • Tool:動く手
  • Agent:考える頭
  • Plan:流れを設計する設計図

これがMastraの基本設計!


Mastraを使うメリット

  • TypeScriptだけで完結(Node.js上で動く)
  • Tool/Agent/Planをシンプルに設計できる
  • 拡張・カスタマイズが超しやすい
  • 軽量&最小構成から始められる

Mastraの始め方(超かんたんステップ)

1. インストール

まずnpmでインストール!

npm install mastra

またはyarnなら

yarn add mastra

これだけで準備完了!


2. 最初のサンプルコード

超シンプルな例を見てみよう!

import { Tool, Agent, Plan } from 'mastra';

// Toolの定義(単純作業)
class FetchDataTool extends Tool {
  async run() {
    return "データを取得しました!";
  }
}

// Agentの定義(AI的な作業)
class SummarizeAgent extends Agent {
  async run(input: string) {
    return `要約結果: ${input}`;
  }
}

// Planの定義(流れを設計)
const plan = new Plan([
  new FetchDataTool(),
  new SummarizeAgent()
]);

// 実行
(async () => {
  const result = await plan.execute();
  console.log(result); // => 要約結果: データを取得しました!
})();

これだけで
**「データ取得 → 要約」**の流れが自動で動きます!


Mastraのイメージ図

[ FetchDataTool ] → [ SummarizeAgent ]
         ↓
      Planが順番に実行

こんな感じで、小さな部品を順番に組み立てていくスタイルです!


Mastraはどんな人に向いている?

✅ TypeScript/Node.jsでAIや自動化に挑戦したい人
✅ シンプルな自動ワークフローを作りたい人
✅ フロントと連携して使いたい人(Next.js、NestJSとの相性も良い)


【参考】

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?