Mastra Server
Relevant source files
The following files were used as context for generating this wiki page:The Mastra Server provides the HTTP server infrastructure for deploying Mastra applications. It exposes agents, workflows, and other core components through a set of RESTful API endpoints. Built on the Hono web framework, it is designed for performance and can be deployed as a standalone service or integrated into various environments.
The server functionality is primarily encapsulated within the @mastra/server and @mastra/deployer packages. These packages provide the necessary tools to create, configure, and run a production-ready server for any Mastra application. A related but distinct concept is the Model Context Protocol (MCP) Server, which is specialized for exposing tools to AI models.
Sources: packages/server/package.json, packages/core/README.md:52-54, templates/template-docs-chatbot/README.md:5-9
Architecture
The Mastra Server architecture is centered around a Hono instance that is configured with a Mastra instance. The Mastra instance contains the application's agents, workflows, and storage configurations. The server registers a series of predefined handlers to expose these components via API endpoints.
Sources: packages/deployer/src/server/index.ts:13-16, packages/server/src/server/handlers.ts
The following diagram illustrates the high-level architecture of the Mastra Server.
This diagram shows how the createServer function from @mastra/deployer takes a Mastra instance and uses it to configure a Hono web server with API handlers from @mastra/server.
Sources: packages/deployer/src/server/index.ts:13-16, packages/server/src/server/handlers.ts
Server Creation
The primary entry point for creating a server is the createServer function located in the @mastra/deployer package. This function initializes a new Hono application and registers all the necessary API routes and middleware.
The function signature is:
createServer(mastra: Mastra<any>): Hono
It takes a configured Mastra instance and returns a Hono application instance, which can then be run.
Sources: packages/deployer/src/server/index.ts:13-24
API Endpoints and Handlers
The @mastra/server package provides a standard set of HTTP handlers to expose core Mastra functionality. These handlers are responsible for processing incoming requests, interacting with the Mastra instance, and sending back responses.
The main API endpoints are organized by feature:
| Method | Endpoint | Handler Function | Description |
|---|---|---|---|
POST | /api/agents/:agentId/chat | handleAgentChat | Initiates a chat with a specified agent. Supports streaming. |
POST | /api/agents/:agentId/threads | handleCreateThread | Creates a new conversation thread for an agent. |
GET | /api/agents/:agentId/threads | handleListThreads | Lists all conversation threads for an agent. |
GET | /api/agents/:agentId/threads/:threadId | handleGetThread | Retrieves a specific conversation thread. |
POST | /api/workflows/:workflowId/run | handleWorkflowRun | Starts a new run of a specified workflow. |
POST | /api/workflows/:workflowId/stream | handleWorkflowStream | Starts a new run of a workflow and streams the output. |
POST | /api/runs/:runId/resume | handleWorkflowResume | Resumes a suspended workflow run. |
GET | /health | (inline) | A health check endpoint that returns a 200 OK status. |
GET | /openapi.json | (inline) | Serves an OpenAPI specification for the server's API. |
Sources: packages/server/src/server/handlers.ts, packages/agent-builder/src/defaults.ts:870-871
Request Flow Example
The following sequence diagram shows the flow of a request to the /api/agents/:agentId/chat endpoint.
This diagram illustrates a client sending a chat request, the server routing it to the correct handler, the handler interacting with the core Mastra agent, and streaming the response back to the client.
Sources: packages/server/src/server/handlers.ts:25-63
Server Management
The @mastra/agent-builder package includes a set of tools for programmatically managing the Mastra server's lifecycle, which is particularly useful for development and testing environments. These tools allow an agent to start, stop, restart, and check the status of the server.
| Tool | Action | Description |
|---|---|---|
manageServer | start | Starts the Mastra development server on a specified port. |
manageServer | stop | Stops the server running on a specified port by finding and killing the PID. |
manageServer | restart | Stops and then starts the server. |
manageServer | status | Checks if the server is running and healthy via the /health endpoint. |
httpRequest | (any) | Makes HTTP requests to the server to test API endpoints. |
Sources: packages/agent-builder/src/defaults.ts:866-972
Model Context Protocol (MCP) Servers
In addition to the main application server, Mastra supports a specialized type of server known as a Model Context Protocol (MCP) Server. While the main server exposes agents and workflows, an MCP server's primary role is to expose tools over an HTTP/SSE interface.
This separation allows for a modular architecture where tools can be developed, deployed, and scaled independently from the agents that consume them. An agent running on a main Mastra server can connect to one or more MCP servers to dynamically acquire tools.
Sources: templates/template-docs-chatbot/README.md:5-9, packages/mcp-docs-server/README.md:3-8
The diagram below shows the relationship between an Agent Server and an MCP Server.
This diagram illustrates that a client interacts with the main Agent Server. The Agent, in turn, communicates with a separate MCP Server to access and use its available tools.
Sources: templates/template-docs-chatbot/README.md:5-9, 110-114, packages/mcp-docs-server/README.md:3-8, 54-66
The @mastra/mcp-docs-server is a concrete example of an MCP server that provides tools for accessing Mastra's documentation, examples, and changelogs.