अंधेरी desk पर laptop जिसकी screen पर editor में code खुला है, वैसा TypeScript project जिसमें Mastra agent और उसके tools रहते हैं
Photo by Mohammad Rahmani on Unsplash · source · credits

Mastra Tutorial: अपना Tool इस्तेमाल करने वाला TypeScript AI Agent बनाएँ

Also available in English: Read this article in English

सिर्फ़ बात करने वाला chatbot बनाना आसान है। जो agent आपके लिए जानकारी खोजे, उसे tools चाहिए। Mastra एक open-source TypeScript framework है (core Apache-2.0 license में) जो यही करता है: आप schema के साथ tool बताते हैं, उसे agent को देते हैं, और model तय करता है कि tool कब call करना है। यहाँ हम एक छोटा agent बनाएँगे जो किसी public GitHub repository के बारे में जवाब देता है।

Project शुरू करें

npm create mastra@latest

यह project बनाता है। Agents src/mastra/agents में, tools src/mastra/tools में रहते हैं, और सब कुछ src/mastra/index.ts में register होता है।

Tool लिखें

Mastra के चरण: scaffold, createTool, Agent, register, Studio में test
Mastra docs (Get started, Agents, Studio) से। यह illustration है, screenshot नहीं।
// src/mastra/tools/repo-tool.ts
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'

export const repoTool = createTool({
  id: 'get-repo',
  description: 'Get stars, open issues and description of a public GitHub repo',
  inputSchema: z.object({ owner: z.string(), repo: z.string() }),
  execute: async ({ owner, repo }) => {
    const r = await fetch(`https://api.github.com/repos/${owner}/${repo}`)
    const d = await r.json()
    return { stars: d.stargazers_count, openIssues: d.open_issues_count, about: d.description }
  },
})

Tools createTool() से ही बनाएँ; Mastra docs चेतावनी देते हैं कि plain objects चुपचाप fail हो जाते हैं। Model description पढ़कर तय करता है कि tool कब call करना है।

Agent को tool दें

// src/mastra/agents/repo-agent.ts
import { Agent } from '@mastra/core/agent'
import { repoTool } from '../tools/repo-tool.ts'

export const repoAgent = new Agent({
  id: 'repo-agent',
  name: 'Repo Agent',
  instructions: 'Answer questions about GitHub repos. Always use repoTool for numbers.',
  model: 'openai/gpt-6.1-sol',
  tools: { repoTool },
})

Model एक provider/model string है, और Mastra उससे जुड़ी key आपके environment से पढ़ता है, जैसे OPENAI_API_KEY। index.ts में new Mastra({ agents: { repoAgent } }) से agent register करें।

Studio में test करें

npm run dev चलाएँ और localhost:4111 खोलें। Studio में आप agent से chat कर सकते हैं, हर tool call और उसका output देख सकते हैं, और tool को अकेले भी चला सकते हैं। REST API localhost:4111/swagger-ui पर मिलता है।

Mastra agent calls: generate, stream, memory और Studio
Mastra docs (Agents overview, Memory, Studio) से। यह illustration है, screenshot नहीं।

Code में mastra.getAgentById('repo-agent') से agent लें, फिर पूरे जवाब (tool calls और token usage के साथ) के लिए .generate() या chat UI के लिए .stream() call करें।

Follow-up सवालों के लिए memory जोड़ें

@mastra/memory और @mastra/libsql जैसा storage provider install करें, उसे Mastra instance पर storage के रूप में set करें, agent में memory: new Memory() जोड़ें, और हर call में वही resource (user) और thread (बातचीत) IDs भेजें। अब "और open issues कितने हैं?" repo का नाम दोहराए बिना काम करता है।

बड़ी तस्वीर के लिए AI agent कैसे बनाएँ पढ़ें, सबसे अच्छे AI agent builders और बिना code वाला रास्ता CrewAI JSON crews देखें, या Mastra टूल पेज पर जाएँ।

स्रोत

FAQ

क्या Mastra free है?

Core framework Apache-2.0 में open source है। Tokens का पैसा आप अपने model provider को देते हैं।

क्या हर model provider का अलग SDK चाहिए?

नहीं। provider/model string लिखें और उस provider की API key set करें।

मेरा tool कभी call क्यों नहीं होता?

उसे createTool() से बनाएँ, agent के tools में जोड़ें और साफ़ description लिखें।

UI बनाए बिना test कहाँ करूँ?

npm run dev के बाद localhost:4111 पर Mastra Studio में।

About the author

Suraj Kumar builds and edits AIUseFlow, a directory of AI tools for vibe coders, agent builders and creators. Guides like this one walk through a practical workflow with cited sources — not a news wire rewrite. How we rank tools.

Get the weekly stack

Rank movers and new AI tools for builders, once a week. Free, no spam, unsubscribe anytime.