//src/mastra/index.ts
export const mastra = new Mastra({
agents: { weatherAgent, productionDbAgent },
workflows: { weatherWorkflow },
logger: new PinoLogger({
name: "Mastra",
level: "info",
}),
storage: new PostgresStore({
id: "mastra-global-storage",
connectionString: process.env.DATABASE_URL,
}),
observability: new Observability({
default: { enabled: true },
}),
server: {
apiRoutes: [
chatRoute({
path: "/chat/production-db-agent",
agent: "production-db-agent",
}),
],
},
});
//mastra-server-config.ts
export async function initializeMastraServer(
app: express.Application,
mastra: Mastra
): Promise<void> {
const mastraServer = new MastraServer({ app, mastra, prefix: "/mastra" });
mastraServer.registerContextMiddleware();
app.use("/mastra", createMastraContextMiddleware);
mastraServer.registerAuthMiddleware();
try {
await mastraServer.registerRoutes();
logger.info("Mastra server initialized with manual middleware");
} catch (error) {
logger.error({ error }, "Error initializing Mastra server");
throw error;
}
}
//index.ts file
initializeMastraServer(app, mastra).catch((error) => {
logger.error({ error }, "Failed to initialize Mastra server");
});
//src/mastra/index.ts
export const mastra = new Mastra({
agents: { weatherAgent, productionDbAgent },
workflows: { weatherWorkflow },
logger: new PinoLogger({
name: "Mastra",
level: "info",
}),
storage: new PostgresStore({
id: "mastra-global-storage",
connectionString: process.env.DATABASE_URL,
}),
observability: new Observability({
default: { enabled: true },
}),
server: {
apiRoutes: [
chatRoute({
path: "/chat/production-db-agent",
agent: "production-db-agent",
}),
],
},
});
//mastra-server-config.ts
export async function initializeMastraServer(
app: express.Application,
mastra: Mastra
): Promise<void> {
const mastraServer = new MastraServer({ app, mastra, prefix: "/mastra" });
mastraServer.registerContextMiddleware();
app.use("/mastra", createMastraContextMiddleware);
mastraServer.registerAuthMiddleware();
try {
await mastraServer.registerRoutes();
logger.info("Mastra server initialized with manual middleware");
} catch (error) {
logger.error({ error }, "Error initializing Mastra server");
throw error;
}
}
//index.ts file
initializeMastraServer(app, mastra).catch((error) => {
logger.error({ error }, "Failed to initialize Mastra server");
});