Skip to content

fix(backend): guard startMcp and use direct url in mastra store - #10

Merged
JOY (JOY) merged 1 commit into
mainfrom
dev
Aug 26, 2026
Merged

fix(backend): guard startMcp and use direct url in mastra store#10
JOY (JOY) merged 1 commit into
mainfrom
dev

Conversation

@JOY

@JOY JOY (JOY) commented Aug 26, 2026

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix & Stability

Why was this change needed?

  1. Ensures startMcp does not block app.listen(port) during backend boot if Mastra's internal table migrations encounter connection limits.
  2. Directs Mastra PostgresStore to use DATABASE_DIRECT_URL (direct PostgreSQL connection) instead of pooler session mode.

Checklist:


Note

Medium Risk
Failed MCP init is swallowed so Copilot/MCP may be unavailable without a hard crash; switching Mastra to a direct DB URL changes connection behavior and must match deployment env vars.

Overview
Backend boot no longer fails when Mastra MCP initialization errors (e.g. Postgres migration/connection limits during startup). startMcp is wrapped in try/catch so failures are logged and the API can still reach app.listen.

Mastra PostgresStore now prefers DATABASE_DIRECT_URL over DATABASE_URL, so Mastra uses a direct Postgres connection instead of a pooler session URL when both are set.

Reviewed by Cursor Bugbot for commit d935773. Bugbot is set up for automated code reviews on this repo. Configure here.

…in mastra store

Prevent Mastra DB pool initialization issues from blocking backend server startup, ensuring port 3000 always starts promptly.

Co-authored-by: Cursor <cursoragent@cursor.com>
@cursor

cursor Bot commented Aug 26, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_c55599c8-b5f2-4942-9dbb-0505149e3d46)

@JOY
JOY (JOY) merged commit 3f7eeaf into main Aug 26, 2026
11 checks passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request wraps the Mastra MCP server initialization in a try-catch block to handle startup failures gracefully, and updates the Mastra PostgresStore connection string to fall back to DATABASE_URL if DATABASE_DIRECT_URL is not set. The review feedback recommends adding explicit validation for these environment variables to avoid runtime crashes caused by unsafe non-null assertions.

export const pStore = new PostgresStore({
id: 'postiz-store',
connectionString: process.env.DATABASE_URL!,
connectionString: process.env.DATABASE_DIRECT_URL || process.env.DATABASE_URL!,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using the non-null assertion operator ! on process.env.DATABASE_URL is unsafe because if both DATABASE_DIRECT_URL and DATABASE_URL are missing, it will pass undefined to PostgresStore, leading to a runtime crash with a cryptic error. It is safer to validate that at least one of these environment variables is defined and throw a clear configuration error if they are missing.

  connectionString: (() => {
    const url = process.env.DATABASE_DIRECT_URL || process.env.DATABASE_URL;
    if (!url) {
      throw new Error('Database connection URL is missing. Please set DATABASE_DIRECT_URL or DATABASE_URL.');
    }
    return url;
  })(),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant