Mastra:TypeScript AI应用框架入门教程

项目:mastra-ai/mastra · ⭐ 5.5k+ · Apache 2.0

项目简介

Mastra是用TypeScript编写的AI应用开发框架,提供工作流引擎、Agent编排、RAG知识库、第三方集成和评估工具。支持本地开发及Serverless部署,"一套代码到处运行"。

安装

# 创建项目
npm create mastra@latest my-ai-app
cd my-ai-app
npm install

# 启动开发服务器
npm run dev

创建第一个Agent

import { Mastra } from '@mastra/core';
import { openai } from '@ai-sdk/openai';

const agent = new Mastra.Agent({
  name: 'assistant',
  model: openai('gpt-4o'),
  instructions: '你是一个有用的AI助手',
  tools: {
    search: webSearchTool,
    code: codeExecutorTool,
  },
});

// 运行
const result = await agent.generate('帮我搜索AI最新动态');
console.log(result.text);

工作流编排

const workflow = new Mastra.Workflow({
  name: 'content-pipeline',
  steps: [
    { id: 'research', agent: researchAgent },
    { id: 'write', agent: writerAgent },
    { id: 'review', agent: reviewerAgent },
  ],
});

const result = await workflow.run({ topic: 'AI Agent趋势' });

Mastra的优势:TypeScript原生类型安全、可视化工作流编辑器、内置评估工具链、Vercel/Cloudflare Workers一键部署。

📚 常见问题

Mastra是什么?

项目:mastra-ai/mastra · ⭐ 5.5k+ · Apache 2.0

如何上手Mastra?

LangGraph多Agent编排教程