Workik.

Mastra Tutorial for Beginners: Build AI Agents in TypeScript

Mastra MCP, Evals, and Deployment

Connect MCP servers to a Mastra agent, score responses with built-in evals, and deploy with mastra build and PostgreSQL. The final lesson.

Connecting an AI agent to external systems requires reliable protocols, quality checks, and a clear path to production. You will connect your support assistant to external tools with the Model Context Protocol, measure output quality with scorers, and deploy your project to production.

Goal: Connect an agent to external tools via MCP, evaluate response safety and relevancy, and deploy the application to a cloud host.

Previous lesson: Mastra RAG: Answering From Your Own Docs


Connecting External Tools with MCP

The Model Context Protocol (MCP) is an open standard that lets applications expose tools and data sources to models in a uniform format. Think of MCP like a standard shipping container: any port that follows the standard can load and unload it, no matter what is packed inside.

Instead of writing custom API wrappers for external platforms, you connect to an MCP server that provides the tools directly. Install the Mastra MCP package:

bash
Copy

You can spawn an MCP server as a local subprocess using npx. Create an agent that uses tools from an external MCP server:

ts
Copy

mcp.listTools() fetches the tool definitions exposed by the server. Your agent inspects these schemas and calls them during execution.

Save this code as src/mastra/agents/research-agent.ts. To see the agent in Mastra Studio, add it to the agents object in src/mastra/index.ts: agents: { supportAgent, researchAgent }. You will see the complete entry point at the end of this lesson.

Common Mistake: Do not call mcp.getTools(). The correct method in current versions is mcp.listTools().


Connecting to Remote MCP Servers

Local subprocesses work well during development, but production environments often require connecting to shared services over HTTP. Mastra supports remote MCP servers using Server-Sent Events (SSE) or standard HTTP endpoints.

Here is how you connect an MCP client to a remote weather service with custom headers:

ts
Copy

Pro Tip: Use listTools() for fixed tool sets loaded at startup. If tools change per request based on customer permissions, use listToolsets() instead.

Quick Challenge: Open Mastra Studio at http://localhost:4111 after registering your MCP agent. Run a query that requires the MCP tool and inspect the trace to see how parameters were passed to the server.


Evaluating Agent Quality with Scorers

Customer support agents must stay relevant and safe. Mastra includes an evaluation framework that scores agent responses in real time using automated evaluation functions called scorers.

Install the evals package:

bash
Copy

Mastra provides prebuilt scorers for common criteria like answer relevancy and toxicity. Add them to the supportAgent you have built throughout this course, so the agent keeps its tools and memory. Here is the complete src/mastra/agents/support-agent.ts:

ts
Copy

The sampling rate controls how often a scorer runs. A rate of 1 evaluates every single response, while 0.5 samples half of them to balance quality assurance with API costs. All evaluation scores appear alongside request traces in Mastra Studio.

Quick Insight: Evaluating an agent with a smaller, faster model like openai/gpt-5-mini keeps evaluation latency low while still catching off-topic or harmful outputs.


Building for Production

When you are ready to ship your customer support application, you compile the TypeScript source into an optimized production bundle.

Run the build command:

bash
Copy

This compiles your agents, tools, and workflows into a standalone Node.js server inside the .mastra/output directory.

To test the production build locally, run:

bash
Copy

This serves your Mastra endpoints and OpenAPI documentation without running the file watchers or development overhead of npm run dev.

Common Mistake: Do not deploy local SQLite database files like file:./mastra.db to serverless platforms. In serverless environments, file systems are ephemeral. Switch to hosted PostgreSQL storage using @mastra/pg for persistent memory across function invocations.


Deploying to Cloud Providers

Mastra provides deployer packages that configure target cloud platforms automatically. For instance, you can bundle your project directly for Vercel.

Install the Vercel deployer:

bash
Copy

Add the deployer to your main Mastra configuration file. Serverless platforms do not keep files between requests, so this final version also replaces the local LibSQL files with PostgreSQL. Install the package with npm install @mastra/pg@latest and set a DATABASE_URL environment variable on your host.

Here is the complete src/mastra/index.ts for the finished project:

ts
Copy

Your knowledge base now lives in PostgreSQL too. Update seed-kb.ts to use PgVector with the same DATABASE_URL, then run the seed script once against the production database.

When you deploy, the deployer sets up the necessary API routes and serverless entry points. Your agents and workflows become accessible through standard HTTP endpoints.

Try This: Run npx mastra build in your terminal and inspect the files generated in .mastra/output. Run npx mastra start to verify that your agents still respond over HTTP before pushing your code to Git.


You Did It: You have built a complete, production-ready AI agent architecture with custom tools, persistent memory, deterministic workflows, RAG search, MCP integration, and automated evals.

This concludes the course series on building customer support agents, giving you the complete foundation to design, test, and ship your own AI applications with Mastra.

You have completed the course

Was this lesson helpful?