mastra 0.10.3 → 0.10.4-alpha.1

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.
@@ -1,7 +1,7 @@
1
1
  import * as p from '@clack/prompts';
2
2
  import color2 from 'picocolors';
3
- import child_process from 'node:child_process';
4
- import util from 'node:util';
3
+ import child_process from 'child_process';
4
+ import util from 'util';
5
5
  import * as fs3 from 'fs';
6
6
  import fs3__default, { existsSync } from 'fs';
7
7
  import fs4 from 'fs/promises';
@@ -146,7 +146,7 @@ function getPackageManagerInstallCommand(pm) {
146
146
  return "install";
147
147
  }
148
148
  }
149
- var args = ["-y", "@mastra/mcp-docs-server@latest"];
149
+ var args = ["-y", "@mastra/mcp-docs-server"];
150
150
  var createMcpConfig = (editor) => {
151
151
  if (editor === "vscode") {
152
152
  return {
@@ -165,10 +165,7 @@ var createMcpConfig = (editor) => {
165
165
  }
166
166
  return {
167
167
  mcpServers: {
168
- mastra: process.platform === `win32` ? {
169
- command: "cmd",
170
- args: ["/c", "npx", ...args]
171
- } : {
168
+ mastra: {
172
169
  command: "npx",
173
170
  args
174
171
  }
@@ -1 +1 @@
1
- export { create } from '../../chunk-LDPP24SN.js';
1
+ export { create } from '../../chunk-MKPHGZFU.js';
package/dist/index.js CHANGED
@@ -1,30 +1,35 @@
1
1
  #! /usr/bin/env node
2
2
  import { PosthogAnalytics } from './chunk-7OXWUU2Q.js';
3
3
  export { PosthogAnalytics } from './chunk-7OXWUU2Q.js';
4
- import { DepsService, create, checkPkgJson, checkAndInstallCoreDeps, interactivePrompt, init, logger, FileService } from './chunk-LDPP24SN.js';
5
- export { create } from './chunk-LDPP24SN.js';
4
+ import { DepsService, create, checkPkgJson, checkAndInstallCoreDeps, interactivePrompt, init, logger, FileService } from './chunk-MKPHGZFU.js';
5
+ export { create } from './chunk-MKPHGZFU.js';
6
6
  import { Command } from 'commander';
7
7
  import { config } from 'dotenv';
8
- import { join as join$1, dirname } from 'node:path';
8
+ import { join, dirname } from 'path';
9
9
  import { getServerOptions, FileService as FileService$2, getWatcherInputOptions, writeTelemetryConfig, createWatcher } from '@mastra/deployer/build';
10
10
  import { Bundler } from '@mastra/deployer/bundler';
11
11
  import { getDeployer, FileService as FileService$1 } from '@mastra/deployer';
12
- import { join } from 'path';
13
12
  import { isWebContainer } from '@webcontainer/env';
14
13
  import { execa } from 'execa';
15
- import { writeFile } from 'node:fs/promises';
16
- import { fileURLToPath } from 'node:url';
14
+ import getPort from 'get-port';
15
+ import { writeFile } from 'fs/promises';
16
+ import { fileURLToPath } from 'url';
17
17
  import * as fsExtra from 'fs-extra';
18
18
  import fs, { readFileSync } from 'fs';
19
19
  import stripJsonComments from 'strip-json-comments';
20
20
  import { spawn } from 'child_process';
21
21
 
22
22
  var BuildBundler = class extends Bundler {
23
- constructor() {
23
+ customEnvFile;
24
+ constructor(customEnvFile) {
24
25
  super("Build");
26
+ this.customEnvFile = customEnvFile;
25
27
  }
26
28
  getEnvFiles() {
27
29
  const possibleFiles = [".env.production", ".env.local", ".env"];
30
+ if (this.customEnvFile) {
31
+ possibleFiles.unshift(this.customEnvFile);
32
+ }
28
33
  try {
29
34
  const fileService = new FileService$2();
30
35
  const envFile = fileService.getFirstExistingFile(possibleFiles);
@@ -99,18 +104,23 @@ var BuildBundler = class extends Bundler {
99
104
  await super.lint(entryFile, outputDirectory, toolsPaths);
100
105
  }
101
106
  };
102
- async function build({ dir: dir2, tools, root }) {
107
+ async function build({
108
+ dir: dir2,
109
+ tools,
110
+ root,
111
+ env
112
+ }) {
103
113
  const rootDir = root || process.cwd();
104
- const mastraDir = dir2 ? dir2.startsWith("/") ? dir2 : join$1(rootDir, dir2) : join$1(rootDir, "src", "mastra");
105
- const outputDirectory = join$1(rootDir, ".mastra");
106
- const defaultToolsPath = join$1(mastraDir, "tools/**/*");
114
+ const mastraDir = dir2 ? dir2.startsWith("/") ? dir2 : join(rootDir, dir2) : join(rootDir, "src", "mastra");
115
+ const outputDirectory = join(rootDir, ".mastra");
116
+ const defaultToolsPath = join(mastraDir, "tools/**/*");
107
117
  const discoveredTools = [defaultToolsPath, ...tools ?? []];
108
118
  try {
109
119
  const fs2 = new FileService();
110
- const mastraEntryFile = fs2.getFirstExistingFile([join$1(mastraDir, "index.ts"), join$1(mastraDir, "index.js")]);
120
+ const mastraEntryFile = fs2.getFirstExistingFile([join(mastraDir, "index.ts"), join(mastraDir, "index.js")]);
111
121
  const platformDeployer = await getDeployer(mastraEntryFile, outputDirectory);
112
122
  if (!platformDeployer) {
113
- const deployer = new BuildBundler();
123
+ const deployer = new BuildBundler(env);
114
124
  await deployer.prepare(outputDirectory);
115
125
  await deployer.bundle(mastraEntryFile, outputDirectory, discoveredTools);
116
126
  logger.info(`Build successful, you can now deploy the .mastra/output directory to your target platform.`);
@@ -162,11 +172,16 @@ async function deploy({ dir: dir2 }) {
162
172
  }
163
173
  }
164
174
  var DevBundler = class extends Bundler {
165
- constructor() {
175
+ customEnvFile;
176
+ constructor(customEnvFile) {
166
177
  super("Dev");
178
+ this.customEnvFile = customEnvFile;
167
179
  }
168
180
  getEnvFiles() {
169
181
  const possibleFiles = [".env.development", ".env.local", ".env"];
182
+ if (this.customEnvFile) {
183
+ possibleFiles.unshift(this.customEnvFile);
184
+ }
170
185
  try {
171
186
  const fileService = new FileService$1();
172
187
  const envFile = fileService.getFirstExistingFile(possibleFiles);
@@ -175,14 +190,12 @@ var DevBundler = class extends Bundler {
175
190
  }
176
191
  return Promise.resolve([]);
177
192
  }
178
- async writePackageJson() {
179
- }
180
193
  async prepare(outputDirectory) {
181
194
  await super.prepare(outputDirectory);
182
195
  const __filename = fileURLToPath(import.meta.url);
183
196
  const __dirname = dirname(__filename);
184
- const playgroundServePath = join$1(outputDirectory, this.outputDir, "playground");
185
- await fsExtra.copy(join$1(dirname(__dirname), "src/playground/dist"), playgroundServePath, {
197
+ const playgroundServePath = join(outputDirectory, this.outputDir, "playground");
198
+ await fsExtra.copy(join(dirname(__dirname), "src/playground/dist"), playgroundServePath, {
186
199
  overwrite: true
187
200
  });
188
201
  }
@@ -194,9 +207,13 @@ var DevBundler = class extends Bundler {
194
207
  "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || "development")
195
208
  });
196
209
  const toolsInputOptions = await this.getToolsInputOptions(toolsPaths);
197
- await writeTelemetryConfig(entryFile, join$1(outputDirectory, this.outputDir));
198
- await this.writeInstrumentationFile(join$1(outputDirectory, this.outputDir));
199
- const outputDir = join$1(outputDirectory, this.outputDir);
210
+ const outputDir = join(outputDirectory, this.outputDir);
211
+ await writeTelemetryConfig(entryFile, outputDir);
212
+ await this.writeInstrumentationFile(outputDir);
213
+ await this.writePackageJson(outputDir, /* @__PURE__ */ new Map(), {});
214
+ this.logger.info("Installing dependencies");
215
+ await this.installDependencies(outputDirectory);
216
+ this.logger.info("Done installing dependencies");
200
217
  const copyPublic = this.copyPublic.bind(this);
201
218
  const watcher = await createWatcher(
202
219
  {
@@ -216,7 +233,7 @@ var DevBundler = class extends Bundler {
216
233
  {
217
234
  name: "public-dir-watcher",
218
235
  buildStart() {
219
- this.addWatchFile(join$1(dirname(entryFile), "public"));
236
+ this.addWatchFile(join(dirname(entryFile), "public"));
220
237
  },
221
238
  buildEnd() {
222
239
  return copyPublic(dirname(entryFile), outputDirectory);
@@ -226,12 +243,12 @@ var DevBundler = class extends Bundler {
226
243
  name: "tools-watcher",
227
244
  async buildEnd() {
228
245
  const toolsInputPaths = Array.from(Object.keys(toolsInputOptions || {})).filter((key) => key.startsWith("tools/")).map((key) => `./${key}.mjs`);
229
- await writeFile(join$1(outputDir, "tools.mjs"), `export const tools = ${JSON.stringify(toolsInputPaths)};`);
246
+ await writeFile(join(outputDir, "tools.mjs"), `export const tools = ${JSON.stringify(toolsInputPaths)};`);
230
247
  }
231
248
  }
232
249
  ],
233
250
  input: {
234
- index: join$1(__dirname, "templates", "dev.entry.js"),
251
+ index: join(__dirname, "templates", "dev.entry.js"),
235
252
  ...toolsInputOptions
236
253
  }
237
254
  },
@@ -265,9 +282,9 @@ var DevBundler = class extends Bundler {
265
282
  // src/commands/dev/dev.ts
266
283
  var currentServerProcess;
267
284
  var isRestarting = false;
268
- var errorRestartCount = 0;
269
285
  var ON_ERROR_MAX_RESTARTS = 3;
270
- var startServer = async (dotMastraPath, port, env) => {
286
+ var startServer = async (dotMastraPath, port, env, errorRestartCount = 0) => {
287
+ let serverIsReady = false;
271
288
  try {
272
289
  logger.info("[Mastra Dev] - Starting server...");
273
290
  const commands = [];
@@ -282,22 +299,12 @@ var startServer = async (dotMastraPath, port, env) => {
282
299
  NODE_ENV: "production",
283
300
  ...Object.fromEntries(env),
284
301
  MASTRA_DEV: "true",
285
- PORT: port.toString() || process.env.PORT || "4111",
302
+ PORT: port.toString(),
286
303
  MASTRA_DEFAULT_STORAGE_URL: `file:${join(dotMastraPath, "..", "mastra.db")}`
287
304
  },
288
- stdio: "inherit",
305
+ stdio: ["inherit", "inherit", "inherit", "ipc"],
289
306
  reject: false
290
307
  });
291
- currentServerProcess.on("close", (code) => {
292
- if (!code) {
293
- logger.info("Server exited, restarting...");
294
- setTimeout(() => {
295
- if (!isRestarting) {
296
- startServer(dotMastraPath, port, env);
297
- }
298
- }, 1e3);
299
- }
300
- });
301
308
  if (currentServerProcess?.exitCode && currentServerProcess?.exitCode !== 0) {
302
309
  if (!currentServerProcess) {
303
310
  throw new Error(`Server failed to start`);
@@ -306,35 +313,37 @@ var startServer = async (dotMastraPath, port, env) => {
306
313
  `Server failed to start with error: ${currentServerProcess.stderr || currentServerProcess.stdout}`
307
314
  );
308
315
  }
309
- await new Promise((resolve) => setTimeout(resolve, 1e3));
310
- try {
311
- await fetch(`http://localhost:${port}/__refresh`, {
312
- method: "POST",
313
- headers: {
314
- "Content-Type": "application/json"
315
- }
316
- });
317
- } catch {
318
- await new Promise((resolve) => setTimeout(resolve, 1500));
319
- try {
320
- await fetch(`http://localhost:${port}/__refresh`, {
321
- method: "POST",
322
- headers: {
323
- "Content-Type": "application/json"
316
+ currentServerProcess.on("message", async (message) => {
317
+ if (message?.type === "server-ready") {
318
+ serverIsReady = true;
319
+ try {
320
+ await fetch(`http://localhost:${port}/__refresh`, {
321
+ method: "POST",
322
+ headers: {
323
+ "Content-Type": "application/json"
324
+ }
325
+ });
326
+ } catch {
327
+ await new Promise((resolve) => setTimeout(resolve, 1500));
328
+ try {
329
+ await fetch(`http://localhost:${port}/__refresh`, {
330
+ method: "POST",
331
+ headers: {
332
+ "Content-Type": "application/json"
333
+ }
334
+ });
335
+ } catch {
324
336
  }
325
- });
326
- } catch {
337
+ }
327
338
  }
328
- }
329
- if (currentServerProcess.exitCode !== null) {
330
- throw new Error(
331
- `Server failed to start with error: ${currentServerProcess.stderr || currentServerProcess.stdout}`
332
- );
333
- }
339
+ });
334
340
  } catch (err) {
335
341
  const execaError = err;
336
342
  if (execaError.stderr) logger.error("Server error output:", { stderr: execaError.stderr });
337
343
  if (execaError.stdout) logger.debug("Server output:", { stdout: execaError.stdout });
344
+ if (!serverIsReady) {
345
+ throw err;
346
+ }
338
347
  setTimeout(() => {
339
348
  if (!isRestarting) {
340
349
  errorRestartCount++;
@@ -345,7 +354,7 @@ var startServer = async (dotMastraPath, port, env) => {
345
354
  logger.error(
346
355
  `Attempting to restart server after error... (Attempt ${errorRestartCount}/${ON_ERROR_MAX_RESTARTS})`
347
356
  );
348
- startServer(dotMastraPath, port, env);
357
+ startServer(dotMastraPath, port, env, errorRestartCount);
349
358
  }
350
359
  }, 1e3);
351
360
  }
@@ -370,9 +379,9 @@ async function dev({
370
379
  port,
371
380
  dir: dir2,
372
381
  root,
373
- tools
382
+ tools,
383
+ env
374
384
  }) {
375
- errorRestartCount = 0;
376
385
  const rootDir = root || process.cwd();
377
386
  const mastraDir = dir2 ? dir2.startsWith("/") ? dir2 : join(process.cwd(), dir2) : join(process.cwd(), "src", "mastra");
378
387
  const dotMastraPath = join(rootDir, ".mastra");
@@ -380,17 +389,25 @@ async function dev({
380
389
  const discoveredTools = [defaultToolsPath, ...tools || []];
381
390
  const fileService = new FileService$1();
382
391
  const entryFile = fileService.getFirstExistingFile([join(mastraDir, "index.ts"), join(mastraDir, "index.js")]);
383
- const bundler = new DevBundler();
392
+ const bundler = new DevBundler(env);
384
393
  await bundler.prepare(dotMastraPath);
385
394
  const watcher = await bundler.watch(entryFile, dotMastraPath, discoveredTools);
386
- const env = await bundler.loadEnvVars();
395
+ const loadedEnv = await bundler.loadEnvVars();
387
396
  const serverOptions = await getServerOptions(entryFile, join(dotMastraPath, "output"));
388
- const startPort = port ?? serverOptions?.port ?? 4111;
389
- await startServer(join(dotMastraPath, "output"), startPort, env);
397
+ let portToUse = port ?? serverOptions?.port ?? process.env.PORT;
398
+ if (!portToUse || isNaN(Number(portToUse))) {
399
+ const portList = Array.from({ length: 21 }, (_, i) => 4111 + i);
400
+ portToUse = String(
401
+ await getPort({
402
+ port: portList
403
+ })
404
+ );
405
+ }
406
+ await startServer(join(dotMastraPath, "output"), Number(portToUse), loadedEnv);
390
407
  watcher.on("event", (event) => {
391
408
  if (event.code === "BUNDLE_END") {
392
409
  logger.info("[Mastra Dev] - Bundling finished, restarting server...");
393
- rebundleAndRestart(dotMastraPath, startPort, bundler);
410
+ rebundleAndRestart(dotMastraPath, Number(portToUse), bundler);
394
411
  }
395
412
  });
396
413
  process.on("SIGINT", () => {
@@ -720,7 +737,7 @@ program.command("lint").description("Lint your Mastra project").option("-d, --di
720
737
  origin
721
738
  });
722
739
  });
723
- program.command("dev").description("Start mastra server").option("-d, --dir <dir>", "Path to your mastra folder").option("-r, --root <root>", "Path to your root folder").option("-t, --tools <toolsDirs>", "Comma-separated list of paths to tool files to include").option("-p, --port <port>", "deprecated: Port number for the development server (defaults to 4111)").action((args) => {
740
+ program.command("dev").description("Start mastra server").option("-d, --dir <dir>", "Path to your mastra folder").option("-r, --root <root>", "Path to your root folder").option("-t, --tools <toolsDirs>", "Comma-separated list of paths to tool files to include").option("-p, --port <port>", "deprecated: Port number for the development server (defaults to 4111)").option("-e, --env <env>", "Custom env file to include in the dev server").action((args) => {
724
741
  analytics.trackCommand({
725
742
  command: "dev",
726
743
  origin
@@ -732,12 +749,13 @@ program.command("dev").description("Start mastra server").option("-d, --dir <dir
732
749
  port: args?.port ? parseInt(args.port) : null,
733
750
  dir: args?.dir,
734
751
  root: args?.root,
735
- tools: args?.tools ? args.tools.split(",") : []
752
+ tools: args?.tools ? args.tools.split(",") : [],
753
+ env: args?.env
736
754
  }).catch((err) => {
737
755
  logger.error(err.message);
738
756
  });
739
757
  });
740
- program.command("build").description("Build your Mastra project").option("-d, --dir <path>", "Path to your Mastra Folder").option("-r, --root <path>", "Path to your root folder").option("-t, --tools <toolsDirs>", "Comma-separated list of paths to tool files to include").action(async (args) => {
758
+ program.command("build").description("Build your Mastra project").option("-d, --dir <path>", "Path to your Mastra Folder").option("-r, --root <path>", "Path to your root folder").option("-t, --tools <toolsDirs>", "Comma-separated list of paths to tool files to include").option("-e, --env <env>", "Custom env file to include in the build").action(async (args) => {
741
759
  await analytics.trackCommandExecution({
742
760
  command: "mastra build",
743
761
  args,
@@ -745,7 +763,8 @@ program.command("build").description("Build your Mastra project").option("-d, --
745
763
  await build({
746
764
  dir: args?.dir,
747
765
  root: args?.root,
748
- tools: args?.tools ? args.tools.split(",") : []
766
+ tools: args?.tools ? args.tools.split(",") : [],
767
+ env: args?.env
749
768
  });
750
769
  },
751
770
  origin
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mastra",
3
- "version": "0.10.3",
3
+ "version": "0.10.4-alpha.1",
4
4
  "license": "Elastic-2.0",
5
5
  "description": "cli for mastra",
6
6
  "type": "module",
@@ -39,9 +39,10 @@
39
39
  "@opentelemetry/instrumentation": "^0.57.2",
40
40
  "@webcontainer/env": "^1.1.1",
41
41
  "commander": "^12.1.0",
42
- "dotenv": "^16.4.7",
42
+ "dotenv": "^16.5.0",
43
43
  "execa": "^9.5.2",
44
44
  "fs-extra": "^11.3.0",
45
+ "get-port": "^7.1.0",
45
46
  "json-schema-to-zod": "^2.6.0",
46
47
  "picocolors": "^1.1.1",
47
48
  "posthog-node": "4.16.0",
@@ -54,32 +55,32 @@
54
55
  "swr": "^2.3.3",
55
56
  "tcp-port-used": "^1.0.2",
56
57
  "yocto-spinner": "^0.1.2",
57
- "zod": "^3.24.3",
58
+ "zod": "^3.25.56",
58
59
  "zod-to-json-schema": "^3.24.5",
59
- "@mastra/deployer": "^0.10.3",
60
- "@mastra/mcp": "^0.10.2",
61
- "@mastra/loggers": "^0.10.1"
60
+ "@mastra/deployer": "^0.10.4-alpha.1",
61
+ "@mastra/loggers": "^0.10.2-alpha.0",
62
+ "@mastra/mcp": "^0.10.3-alpha.0"
62
63
  },
63
64
  "devDependencies": {
64
- "@microsoft/api-extractor": "^7.52.5",
65
+ "@microsoft/api-extractor": "^7.52.8",
65
66
  "@types/fs-extra": "^11.0.4",
66
- "@types/node": "^20.17.27",
67
+ "@types/node": "^20.17.57",
67
68
  "@types/prompt": "^1.1.9",
68
69
  "@types/shell-quote": "^1.7.5",
69
70
  "@types/tcp-port-used": "^1.0.4",
70
71
  "cpy-cli": "^5.0.0",
71
- "eslint": "^9.23.0",
72
+ "eslint": "^9.28.0",
72
73
  "memfs": "^4.17.0",
73
74
  "npm-run-all2": "^7.0.2",
74
- "rollup": "^4.40.1",
75
- "tsup": "^8.4.0",
75
+ "rollup": "^4.41.1",
76
+ "tsup": "^8.5.0",
76
77
  "type-fest": "^4.37.0",
77
78
  "typescript": "^5.8.2",
78
- "vitest": "^3.1.2",
79
+ "vitest": "^3.2.2",
79
80
  "@internal/lint": "0.0.10",
80
- "@mastra/client-js": "0.10.2",
81
- "@mastra/playground-ui": "5.1.3",
82
- "@mastra/core": "0.10.3"
81
+ "@mastra/client-js": "0.10.3-alpha.1",
82
+ "@mastra/core": "0.10.4-alpha.1",
83
+ "@mastra/playground-ui": "5.1.4-alpha.1"
83
84
  },
84
85
  "peerDependencies": {
85
86
  "@mastra/core": "^0.10.2-alpha.0"