Skip to content

Conversation

@threepointone
Copy link
Contributor

fixes #718

Because of hibernation, our preloaded this.mcp.jsonSchema can be lost when the DO wakes up again. We should probably move this helper into anotherl ibrary. Until then, a patch is to eagerly load it when an ai chat agent starts up.

We also throw the error only if there's an active mcp connection.

fixes #718

Because of hibernation, our preloaded this.mcp.jsonSchema can be lost when the DO wakes up again. We should probably move this helper into anotherl ibrary. Until then, a patch is to eagerly load it when an ai chat agent starts up.

We also throw the error only if there's an active mcp connection.
@changeset-bot
Copy link

changeset-bot bot commented Dec 30, 2025

🦋 Changeset detected

Latest commit: f2e415f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@cloudflare/ai-chat Patch
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@claude
Copy link

claude bot commented Dec 30, 2025

Claude Code Review

The PR addresses a real hibernation issue where jsonSchema is lost when DOs wake up. The fix has 3 significant issues:

Critical Issues

  1. Race condition in ai-chat (packages/ai-chat/src/index.ts:521-523): The ensureJsonSchema() call is fire-and-forget. If getAITools() is called before the promise resolves, it will still throw. This is especially problematic immediately after hibernation wakeup.

    // Current - race condition:
    this.mcp.ensureJsonSchema().catch((error) => {
      console.error("Error ensuring jsonSchema:", error);
    });

    Fix: Either await it (with blockConcurrencyWhile if needed despite test issues), or call ensureJsonSchema() synchronously before first use in methods that need it.

  2. Silent error swallowing (packages/ai-chat/src/index.ts:521-523): Errors are logged but not handled. If import fails, subsequent getAITools() calls will crash the agent.

  3. Incomplete fix: The condition if (Object.keys(this.mcpConnections).length > 0) in getAITools() only delays the error - it doesn't solve the root cause. If connections exist but jsonSchema wasn't loaded (due to race or import failure), it still throws.

Test Quality

The updated test at line 1637 correctly validates the new behavior (throws when jsonSchema uninitialized), which is good. However, there's no test coverage for the new ai-chat eager loading behavior or the race condition scenario.

Recommendation

Either:

  • Make the eager load synchronous/blocking in the constructor
  • Or ensure getAITools() calls ensureJsonSchema() itself before using jsonSchema (makes it self-healing)

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 30, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@762

commit: f2e415f

@threepointone threepointone marked this pull request as draft January 1, 2026 13:28
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.

"jsonSchema not initialized" intermittent error when calling agent.mcp.getAITools()

1 participant