Skip to content

Conversation

@mcandeia
Copy link
Contributor

@mcandeia mcandeia commented Dec 31, 2025

Summary by cubic

Add null-safe checks around env.MESH_REQUEST_CONTEXT.state.DATABASE to prevent crashes when the database binding is missing. Calls now fail gracefully or return empty results where appropriate.

  • Bug Fixes
    • Switched database calls to use optional chaining (?.) and guards before DATABASES_RUN_SQL.
    • Added safe early returns or clear errors in assistants, workflows, prompts, executions, and events when DATABASE is not configured.

Written for commit a258e2d. Summary will update on new commits.

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
@mcandeia mcandeia merged commit 1fc0d1c into main Dec 31, 2025
1 of 4 checks passed
@github-actions
Copy link

🚀 Preview Deployments Ready!

Your changes have been deployed to preview environments:

📦 mcp-studio

🔗 View Preview

These previews will be automatically updated with new commits to this PR.


Deployed from commit: c9a286b

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

3 issues found across 7 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="mcp-studio/server/db/postgres.ts">

<violation number="1" location="mcp-studio/server/db/postgres.ts:22">
P1: Incomplete optional chaining will still throw TypeError. If `MESH_REQUEST_CONTEXT` or `state` is nullish, `?.DATABASE` evaluates to `undefined`, and calling `.DATABASES_RUN_SQL()` on `undefined` throws. Consider adding `?.` before `DATABASES_RUN_SQL` and handling the undefined response, or throwing an explicit error early if the context is required.</violation>
</file>

<file name="mcp-studio/server/engine/events.ts">

<violation number="1" location="mcp-studio/server/engine/events.ts:19">
P1: Incomplete optional chaining will crash if `state` is nullish. When `state?.DATABASE` returns `undefined`, calling `.DATABASES_RUN_SQL()` throws a TypeError. Either add `?.` before `DATABASES_RUN_SQL` and handle the undefined result, or throw an explicit error early if the database connection is unavailable.</violation>
</file>

<file name="mcp-studio/server/tools/workflow.ts">

<violation number="1" location="mcp-studio/server/tools/workflow.ts:117">
P1: Incomplete optional chaining creates a false sense of safety. If `MESH_REQUEST_CONTEXT` or `state` is null/undefined, the chain returns `undefined`, and calling `.DATABASES_RUN_SQL()` on it will throw a `TypeError`. Either complete the chain (`?.DATABASE?.DATABASES_RUN_SQL?.`) or add an early guard clause to throw a meaningful error.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

): Promise<T[]> {
const response =
await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({
await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
Copy link

@cubic-dev-ai cubic-dev-ai bot Dec 31, 2025

Choose a reason for hiding this comment

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

P1: Incomplete optional chaining will still throw TypeError. If MESH_REQUEST_CONTEXT or state is nullish, ?.DATABASE evaluates to undefined, and calling .DATABASES_RUN_SQL() on undefined throws. Consider adding ?. before DATABASES_RUN_SQL and handling the undefined response, or throwing an explicit error early if the context is required.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At mcp-studio/server/db/postgres.ts, line 22:

<comment>Incomplete optional chaining will still throw TypeError. If `MESH_REQUEST_CONTEXT` or `state` is nullish, `?.DATABASE` evaluates to `undefined`, and calling `.DATABASES_RUN_SQL()` on `undefined` throws. Consider adding `?.` before `DATABASES_RUN_SQL` and handling the undefined response, or throwing an explicit error early if the context is required.</comment>

<file context>
@@ -19,7 +19,7 @@ export async function runSQL&lt;T = unknown&gt;(
 ): Promise&lt;T[]&gt; {
   const response =
-    await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({
+    await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
       sql,
       params,
</file context>
Fix with Cubic

const now = Date.now();
const result =
await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({
await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
Copy link

@cubic-dev-ai cubic-dev-ai bot Dec 31, 2025

Choose a reason for hiding this comment

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

P1: Incomplete optional chaining will crash if state is nullish. When state?.DATABASE returns undefined, calling .DATABASES_RUN_SQL() throws a TypeError. Either add ?. before DATABASES_RUN_SQL and handle the undefined result, or throw an explicit error early if the database connection is unavailable.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At mcp-studio/server/engine/events.ts, line 19:

<comment>Incomplete optional chaining will crash if `state` is nullish. When `state?.DATABASE` returns `undefined`, calling `.DATABASES_RUN_SQL()` throws a TypeError. Either add `?.` before `DATABASES_RUN_SQL` and handle the undefined result, or throw an explicit error early if the database connection is unavailable.</comment>

<file context>
@@ -16,7 +16,7 @@ export async function getPendingEvents(
   const now = Date.now();
   const result =
-    await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({
+    await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
       sql: `SELECT * FROM workflow_event WHERE execution_id = ? AND consumed_at IS NULL
           AND (visible_at IS NULL OR visible_at &lt;= ?) ${type ? &quot;AND type = ?&quot; : &quot;&quot;}
</file context>
Fix with Cubic

${whereClause}
${orderByClause}
LIMIT ? OFFSET ?
`;
Copy link

@cubic-dev-ai cubic-dev-ai bot Dec 31, 2025

Choose a reason for hiding this comment

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

P1: Incomplete optional chaining creates a false sense of safety. If MESH_REQUEST_CONTEXT or state is null/undefined, the chain returns undefined, and calling .DATABASES_RUN_SQL() on it will throw a TypeError. Either complete the chain (?.DATABASE?.DATABASES_RUN_SQL?.) or add an early guard clause to throw a meaningful error.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At mcp-studio/server/tools/workflow.ts, line 117:

<comment>Incomplete optional chaining creates a false sense of safety. If `MESH_REQUEST_CONTEXT` or `state` is null/undefined, the chain returns `undefined`, and calling `.DATABASES_RUN_SQL()` on it will throw a `TypeError`. Either complete the chain (`?.DATABASE?.DATABASES_RUN_SQL?.`) or add an early guard clause to throw a meaningful error.</comment>

<file context>
@@ -114,14 +114,14 @@ export const createListTool = (env: Env) =&gt;
 
       const itemsResult: any =
-        await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({
+        await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
           sql,
           params: [...params, limit, offset],
</file context>
Fix with Cubic

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.

2 participants