@mastra/server 2.0.2 → 2.0.3-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,159 +1,74 @@
1
- # @mastra/deployer
1
+ # @mastra/server
2
2
 
3
- Core deployment infrastructure for Mastra applications, handling build, packaging, and deployment processes.
3
+ Typed HTTP handlers and utilities for exposing a `Mastra` instance over HTTP.
4
+ This package powers `mastra dev` and can be added to your own server to provide
5
+ REST and streaming endpoints for agents, workflows, telemetry and more.
4
6
 
5
7
  ## Installation
6
8
 
7
9
  ```bash
8
- npm install @mastra/deployer
10
+ npm install @mastra/server
9
11
  ```
10
12
 
11
- ## Overview
12
-
13
- The `@mastra/deployer` package provides the foundational deployment infrastructure for Mastra applications. It handles:
14
-
15
- - Project building and bundling
16
- - Dependency management
17
- - Environment configuration
18
- - Development and production deployments
19
-
20
13
  ## Usage
21
14
 
22
- ```typescript
23
- import { Deployer } from '@mastra/deployer';
15
+ The handlers are framework agnostic functions which accept a `Mastra` instance
16
+ and a request context. They are typically mounted under a URL prefix within your
17
+ web framework of choice:
24
18
 
25
- // Create a deployer instance
26
- const deployer = new Deployer({
27
- dir: '/path/to/project',
28
- type: 'Deploy', // or 'Dev' for development mode
19
+ ```typescript
20
+ import { Hono } from 'hono';
21
+ import { handlers } from '@mastra/server';
22
+ import { mastra } from './mastra-instance';
23
+
24
+ const app = new Hono();
25
+
26
+ app.get('/mastra/agents', ctx =>
27
+ handlers.agents.getAgentsHandler({ mastra, runtimeContext: ctx }),
28
+ );
29
+ app.post('/mastra/agents/:id/generate', async ctx => {
30
+ const body = await ctx.req.json();
31
+ return handlers.agents.generateHandler({
32
+ mastra,
33
+ runtimeContext: ctx,
34
+ agentId: ctx.req.param('id'),
35
+ body,
36
+ });
29
37
  });
30
38
 
31
- // Install dependencies
32
- await deployer.install();
33
-
34
- // Write package.json
35
- await deployer.writePackageJson();
36
-
37
- // Get Mastra instance
38
- const { mastra } = await deployer.getMastra();
39
+ // Mount additional handlers as required
39
40
  ```
40
41
 
41
- ## Configuration
42
-
43
- ### Required Parameters
44
-
45
- - `dir`: Project directory path
46
- - `type`: Deployment type ('Deploy' or 'Dev')
42
+ Running `mastra dev` starts a local development UI at
43
+ `http://localhost:3000` using these handlers.
47
44
 
48
- ## Features
45
+ ## Available Handler Groups
49
46
 
50
- ### Project Structure Management
47
+ - **Agents** - list defined agents, retrieve metadata, and run `generate`
48
+ or `stream`.
49
+ - **Workflows** - start and inspect workflow runs.
50
+ - **Tools** - discover tools available to the `Mastra` instance.
51
+ - **Memory** - interact with configured memory stores.
52
+ - **Logs** - query runtime logs when a supporting logger transport is used.
53
+ - **Telemetry** - expose metrics produced by the telemetry subsystem.
54
+ - **Networks** - interact with agent networks.
55
+ - **Vector / Voice** - endpoints related to vector stores and voice synthesis.
51
56
 
52
- - Creates and manages `.mastra` directory
53
- - Handles package.json generation and updates
54
- - Manages project dependencies
57
+ Handlers return JSON serialisable data and throw an `HTTPException` (subclass of
58
+ `Error`) when a failure should result in a non-2xx HTTP status.
55
59
 
56
- ### Dependency Management
60
+ ## OpenAPI Spec Generation
57
61
 
58
- - Automatic dependency installation
59
- - Workspace dependency resolution
60
- - Version management for @mastra packages
62
+ The local OpenAPI specification used by the CLI playground and similar tools can
63
+ be refreshed by running:
61
64
 
62
- ### Environment Handling
63
-
64
- - Support for multiple environment files:
65
- - `.env`
66
- - `.env.development`
67
- - `.env.local`
68
- - Environment variable validation and processing
69
-
70
- ### Build Process
71
-
72
- - Project bundling
73
- - Asset management
74
- - Source code transformation
75
-
76
- ### Development Support
77
-
78
- - Development server configuration
79
- - Hot reloading capabilities
80
- - Debug logging
81
-
82
- ## Project Structure
83
-
84
- The deployer creates and manages the following structure:
85
-
86
- ```
87
- your-project/
88
- ├── .mastra/
89
- │ ├── package.json
90
- │ ├── mastra.mjs
91
- │ └── index.mjs
92
- ├── .env
93
- ├── .env.development
94
- ├── .env.local
95
- └── package.json
96
- ```
97
-
98
- ## Package.json Management
99
-
100
- The deployer automatically manages dependencies in the `.mastra/package.json`:
101
-
102
- ```json
103
- {
104
- "name": "server",
105
- "version": "1.0.0",
106
- "type": "module",
107
- "dependencies": {
108
- "@mastra/loggers": "latest",
109
- "hono": "4.6.17",
110
- "@hono/node-server": "^1.13.7",
111
- "superjson": "^2.2.2",
112
- "zod-to-json-schema": "^3.24.1"
113
- }
114
- }
65
+ ```bash
66
+ pnpm run pull:openapispec
115
67
  ```
116
68
 
117
- ## Methods
118
-
119
- ### `install()`
120
-
121
- Installs and updates project dependencies.
122
-
123
- ### `writePackageJson()`
124
-
125
- Generates or updates the package.json in the .mastra directory.
126
-
127
- ### `getMastra()`
128
-
129
- Returns the Mastra instance for the project.
130
-
131
- ### `getMastraPath()`
132
-
133
- Returns the path to the .mastra directory.
134
-
135
- ## Error Handling
136
-
137
- The deployer includes comprehensive error handling for:
138
-
139
- - Dependency installation failures
140
- - File system operations
141
- - Environment configuration issues
142
- - Build process errors
143
-
144
- ## Logging
145
-
146
- Built-in logging support through @mastra/core:
147
-
148
- - Debug information
149
- - Installation progress
150
- - Build status
151
- - Error reporting
69
+ within the `@mastra/server` directory.
152
70
 
153
- ## Related Packages
71
+ ## License
154
72
 
155
- - `@mastra/core`: Core Mastra functionality
156
- - `@mastra/loggers`: Logging infrastructure
157
- - Deployer implementations:
158
- - `@mastra/deployer-cloudflare`
159
- - Other platform-specific deployers
73
+ Released under the Elastic License 2.0. The full license text is available in
74
+ this repository.
@@ -13,7 +13,7 @@ import { NewStep } from '@mastra/core/workflows/vNext';
13
13
  import type { NewWorkflow } from '@mastra/core/workflows/vNext';
14
14
  import type { QueryResult } from '@mastra/core/vector';
15
15
  import { ReadableStream as ReadableStream_2 } from 'node:stream/web';
16
- import type { RuntimeContext } from '@mastra/core/runtime-context';
16
+ import { RuntimeContext } from '@mastra/core/runtime-context';
17
17
  import type { RuntimeContext as RuntimeContext_2 } from '@mastra/core/di';
18
18
  import { SerializedStepFlowEntry } from '@mastra/core/workflows/vNext';
19
19
  import { Step } from '@mastra/core/workflows';
@@ -114,6 +114,7 @@ export declare function generateHandler({ mastra, runtimeContext, agentId, body,
114
114
  agentId: string;
115
115
  body: GetBody<'generate'> & {
116
116
  resourceid?: string;
117
+ runtimeContext?: Record<string, unknown>;
117
118
  };
118
119
  }): Promise<GenerateTextResult<any, any>>;
119
120
 
@@ -499,6 +500,7 @@ export declare function streamGenerateHandler({ mastra, runtimeContext, agentId,
499
500
  agentId: string;
500
501
  body: GetBody<'stream'> & {
501
502
  resourceid?: string;
503
+ runtimeContext?: string;
502
504
  };
503
505
  }): Promise<Response | undefined>;
504
506
 
@@ -13,7 +13,7 @@ import { NewStep } from '@mastra/core/workflows/vNext';
13
13
  import type { NewWorkflow } from '@mastra/core/workflows/vNext';
14
14
  import type { QueryResult } from '@mastra/core/vector';
15
15
  import { ReadableStream as ReadableStream_2 } from 'node:stream/web';
16
- import type { RuntimeContext } from '@mastra/core/runtime-context';
16
+ import { RuntimeContext } from '@mastra/core/runtime-context';
17
17
  import type { RuntimeContext as RuntimeContext_2 } from '@mastra/core/di';
18
18
  import { SerializedStepFlowEntry } from '@mastra/core/workflows/vNext';
19
19
  import { Step } from '@mastra/core/workflows';
@@ -114,6 +114,7 @@ export declare function generateHandler({ mastra, runtimeContext, agentId, body,
114
114
  agentId: string;
115
115
  body: GetBody<'generate'> & {
116
116
  resourceid?: string;
117
+ runtimeContext?: Record<string, unknown>;
117
118
  };
118
119
  }): Promise<GenerateTextResult<any, any>>;
119
120
 
@@ -499,6 +500,7 @@ export declare function streamGenerateHandler({ mastra, runtimeContext, agentId,
499
500
  agentId: string;
500
501
  body: GetBody<'stream'> & {
501
502
  resourceid?: string;
503
+ runtimeContext?: string;
502
504
  };
503
505
  }): Promise<Response | undefined>;
504
506
 
@@ -4,6 +4,7 @@ var chunk5SWCVTNL_cjs = require('./chunk-5SWCVTNL.cjs');
4
4
  var chunkQN4KF3BH_cjs = require('./chunk-QN4KF3BH.cjs');
5
5
  var chunkZLBRQFDD_cjs = require('./chunk-ZLBRQFDD.cjs');
6
6
  var chunkFV45V6WC_cjs = require('./chunk-FV45V6WC.cjs');
7
+ var runtimeContext = require('@mastra/core/runtime-context');
7
8
 
8
9
  // src/server/handlers/agents.ts
9
10
  var agents_exports = {};
@@ -124,7 +125,7 @@ async function getLiveEvalsByAgentIdHandler({
124
125
  }
125
126
  async function generateHandler({
126
127
  mastra,
127
- runtimeContext,
128
+ runtimeContext: runtimeContext$1,
128
129
  agentId,
129
130
  body
130
131
  }) {
@@ -133,14 +134,18 @@ async function generateHandler({
133
134
  if (!agent) {
134
135
  throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Agent not found" });
135
136
  }
136
- const { messages, resourceId, resourceid, ...rest } = body;
137
+ const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
137
138
  const finalResourceId = resourceId ?? resourceid;
139
+ const finalRuntimeContext = new runtimeContext.RuntimeContext([
140
+ ...Array.from(runtimeContext$1.entries()),
141
+ ...Array.from(Object.entries(agentRuntimeContext ?? {}))
142
+ ]);
138
143
  chunkQN4KF3BH_cjs.validateBody({ messages });
139
144
  const result = await agent.generate(messages, {
140
145
  ...rest,
141
146
  // @ts-expect-error TODO fix types
142
147
  resourceId: finalResourceId,
143
- runtimeContext
148
+ runtimeContext: finalRuntimeContext
144
149
  });
145
150
  return result;
146
151
  } catch (error) {
@@ -149,7 +154,7 @@ async function generateHandler({
149
154
  }
150
155
  async function streamGenerateHandler({
151
156
  mastra,
152
- runtimeContext,
157
+ runtimeContext: runtimeContext$1,
153
158
  agentId,
154
159
  body
155
160
  }) {
@@ -158,14 +163,18 @@ async function streamGenerateHandler({
158
163
  if (!agent) {
159
164
  throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Agent not found" });
160
165
  }
161
- const { messages, resourceId, resourceid, ...rest } = body;
166
+ const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
162
167
  const finalResourceId = resourceId ?? resourceid;
168
+ const finalRuntimeContext = new runtimeContext.RuntimeContext([
169
+ ...Array.from(runtimeContext$1.entries()),
170
+ ...Array.from(Object.entries(agentRuntimeContext ?? {}))
171
+ ]);
163
172
  chunkQN4KF3BH_cjs.validateBody({ messages });
164
173
  const streamResult = await agent.stream(messages, {
165
174
  ...rest,
166
175
  // @ts-expect-error TODO fix types
167
176
  resourceId: finalResourceId,
168
- runtimeContext
177
+ runtimeContext: finalRuntimeContext
169
178
  });
170
179
  const streamResponse = rest.output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
171
180
  sendUsage: true,
@@ -2,6 +2,7 @@ import { stringify, esm_default } from './chunk-OMN3UI6X.js';
2
2
  import { validateBody } from './chunk-L7XE5QTW.js';
3
3
  import { handleError } from './chunk-3AHQ5RGN.js';
4
4
  import { __export, HTTPException } from './chunk-TRDNDNGQ.js';
5
+ import { RuntimeContext } from '@mastra/core/runtime-context';
5
6
 
6
7
  // src/server/handlers/agents.ts
7
8
  var agents_exports = {};
@@ -131,14 +132,18 @@ async function generateHandler({
131
132
  if (!agent) {
132
133
  throw new HTTPException(404, { message: "Agent not found" });
133
134
  }
134
- const { messages, resourceId, resourceid, ...rest } = body;
135
+ const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
135
136
  const finalResourceId = resourceId ?? resourceid;
137
+ const finalRuntimeContext = new RuntimeContext([
138
+ ...Array.from(runtimeContext.entries()),
139
+ ...Array.from(Object.entries(agentRuntimeContext ?? {}))
140
+ ]);
136
141
  validateBody({ messages });
137
142
  const result = await agent.generate(messages, {
138
143
  ...rest,
139
144
  // @ts-expect-error TODO fix types
140
145
  resourceId: finalResourceId,
141
- runtimeContext
146
+ runtimeContext: finalRuntimeContext
142
147
  });
143
148
  return result;
144
149
  } catch (error) {
@@ -156,14 +161,18 @@ async function streamGenerateHandler({
156
161
  if (!agent) {
157
162
  throw new HTTPException(404, { message: "Agent not found" });
158
163
  }
159
- const { messages, resourceId, resourceid, ...rest } = body;
164
+ const { messages, resourceId, resourceid, runtimeContext: agentRuntimeContext, ...rest } = body;
160
165
  const finalResourceId = resourceId ?? resourceid;
166
+ const finalRuntimeContext = new RuntimeContext([
167
+ ...Array.from(runtimeContext.entries()),
168
+ ...Array.from(Object.entries(agentRuntimeContext ?? {}))
169
+ ]);
161
170
  validateBody({ messages });
162
171
  const streamResult = await agent.stream(messages, {
163
172
  ...rest,
164
173
  // @ts-expect-error TODO fix types
165
174
  resourceId: finalResourceId,
166
- runtimeContext
175
+ runtimeContext: finalRuntimeContext
167
176
  });
168
177
  const streamResponse = rest.output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
169
178
  sendUsage: true,
@@ -1,30 +1,30 @@
1
1
  'use strict';
2
2
 
3
- var chunk4BIX6GMY_cjs = require('../../chunk-4BIX6GMY.cjs');
3
+ var chunk6Q7UXAYJ_cjs = require('../../chunk-6Q7UXAYJ.cjs');
4
4
 
5
5
 
6
6
 
7
7
  Object.defineProperty(exports, "generateHandler", {
8
8
  enumerable: true,
9
- get: function () { return chunk4BIX6GMY_cjs.generateHandler; }
9
+ get: function () { return chunk6Q7UXAYJ_cjs.generateHandler; }
10
10
  });
11
11
  Object.defineProperty(exports, "getAgentByIdHandler", {
12
12
  enumerable: true,
13
- get: function () { return chunk4BIX6GMY_cjs.getAgentByIdHandler; }
13
+ get: function () { return chunk6Q7UXAYJ_cjs.getAgentByIdHandler; }
14
14
  });
15
15
  Object.defineProperty(exports, "getAgentsHandler", {
16
16
  enumerable: true,
17
- get: function () { return chunk4BIX6GMY_cjs.getAgentsHandler; }
17
+ get: function () { return chunk6Q7UXAYJ_cjs.getAgentsHandler; }
18
18
  });
19
19
  Object.defineProperty(exports, "getEvalsByAgentIdHandler", {
20
20
  enumerable: true,
21
- get: function () { return chunk4BIX6GMY_cjs.getEvalsByAgentIdHandler; }
21
+ get: function () { return chunk6Q7UXAYJ_cjs.getEvalsByAgentIdHandler; }
22
22
  });
23
23
  Object.defineProperty(exports, "getLiveEvalsByAgentIdHandler", {
24
24
  enumerable: true,
25
- get: function () { return chunk4BIX6GMY_cjs.getLiveEvalsByAgentIdHandler; }
25
+ get: function () { return chunk6Q7UXAYJ_cjs.getLiveEvalsByAgentIdHandler; }
26
26
  });
27
27
  Object.defineProperty(exports, "streamGenerateHandler", {
28
28
  enumerable: true,
29
- get: function () { return chunk4BIX6GMY_cjs.streamGenerateHandler; }
29
+ get: function () { return chunk6Q7UXAYJ_cjs.streamGenerateHandler; }
30
30
  });
@@ -1 +1 @@
1
- export { generateHandler, getAgentByIdHandler, getAgentsHandler, getEvalsByAgentIdHandler, getLiveEvalsByAgentIdHandler, streamGenerateHandler } from '../../chunk-Y3SV5XK4.js';
1
+ export { generateHandler, getAgentByIdHandler, getAgentsHandler, getEvalsByAgentIdHandler, getLiveEvalsByAgentIdHandler, streamGenerateHandler } from '../../chunk-GVBJ5I2S.js';
@@ -5,7 +5,7 @@ var chunkM2RXDCPV_cjs = require('../chunk-M2RXDCPV.cjs');
5
5
  var chunkM56ECCHK_cjs = require('../chunk-M56ECCHK.cjs');
6
6
  var chunk55HTWX4C_cjs = require('../chunk-55HTWX4C.cjs');
7
7
  var chunkCHFORQ7J_cjs = require('../chunk-CHFORQ7J.cjs');
8
- var chunk4BIX6GMY_cjs = require('../chunk-4BIX6GMY.cjs');
8
+ var chunk6Q7UXAYJ_cjs = require('../chunk-6Q7UXAYJ.cjs');
9
9
  var chunkSKBVVI24_cjs = require('../chunk-SKBVVI24.cjs');
10
10
  var chunk2FJURXCL_cjs = require('../chunk-2FJURXCL.cjs');
11
11
  var chunk5YGDYMRB_cjs = require('../chunk-5YGDYMRB.cjs');
@@ -35,7 +35,7 @@ Object.defineProperty(exports, "workflows", {
35
35
  });
36
36
  Object.defineProperty(exports, "agents", {
37
37
  enumerable: true,
38
- get: function () { return chunk4BIX6GMY_cjs.agents_exports; }
38
+ get: function () { return chunk6Q7UXAYJ_cjs.agents_exports; }
39
39
  });
40
40
  Object.defineProperty(exports, "logs", {
41
41
  enumerable: true,
@@ -3,7 +3,7 @@ export { vNextWorkflows_exports as vNextWorkflows } from '../chunk-OWNA6I2H.js';
3
3
  export { vector_exports as vector } from '../chunk-4JINXASC.js';
4
4
  export { voice_exports as voice } from '../chunk-Q6SHQECN.js';
5
5
  export { workflows_exports as workflows } from '../chunk-3XTEV33Q.js';
6
- export { agents_exports as agents } from '../chunk-Y3SV5XK4.js';
6
+ export { agents_exports as agents } from '../chunk-GVBJ5I2S.js';
7
7
  export { logs_exports as logs } from '../chunk-3EJZQ6TQ.js';
8
8
  export { memory_exports as memory } from '../chunk-RBQASTUP.js';
9
9
  export { network_exports as network } from '../chunk-QJ3AHN64.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/server",
3
- "version": "2.0.2",
3
+ "version": "2.0.3-alpha.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "files": [
@@ -46,7 +46,7 @@
46
46
  "license": "Elastic-2.0",
47
47
  "dependencies": {},
48
48
  "peerDependencies": {
49
- "@mastra/core": "^0.9.2"
49
+ "@mastra/core": "^0.9.3-alpha.0"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@ai-sdk/openai": "^1.3.2",
@@ -59,7 +59,7 @@
59
59
  "vitest": "^2.1.9",
60
60
  "zod-to-json-schema": "^3.24.5",
61
61
  "@internal/lint": "0.0.3",
62
- "@mastra/core": "0.9.2"
62
+ "@mastra/core": "0.9.3-alpha.0"
63
63
  },
64
64
  "scripts": {
65
65
  "build": "tsup src/index.ts src/server/handlers.ts src/server/handlers/*.ts !src/server/handlers/*.test.ts --format esm,cjs --clean --experimental-dts --treeshake=smallest --splitting",