agents icon indicating copy to clipboard operation
agents copied to clipboard

Chat agent does not work after deployment

Open unique1o1 opened this issue 9 months ago • 2 comments

During local testing, the chat agent functions correctly. However, after deployment, only the first few messages are successfully transmitted over the WebSocket before the connection hangs(no new messages get through). No logs (console.log() i have added) are shown on the cloudflare log dashboard, making debugging difficult, similar to the issue reported in cloudflare/workers-sdk#8724.

The codebase is a modified version of the cloudflare/agents-starter template, with mcpclient integrated for tools listing and AI gateway integrated.

unique1o1 avatar Apr 14 '25 15:04 unique1o1

can you make a repro?

threepointone avatar Apr 24 '25 08:04 threepointone

Repo

unique1o1 avatar Apr 24 '25 10:04 unique1o1

We are experiencing the exact same behavior. @unique1o1 have you had any updates or gotten any insights?

We have tested rigorously. All other parts of the DO seem to work. It will send messages, run alarms, and methods can be called.

I am always certain what is happening is the onMessage method that gets defined is not being called.

This leads me to think it is something weird with how the Agent class, specifically the way it override the onMessage method

My next step is to fork the repo and see if I can reproduce.

Again, this bug is super annoying to try and fix because it is not an issue locally.

It only happens when the DO is deployed.

ryleyrandall22 avatar Jul 05 '25 15:07 ryleyrandall22

@ryleyrandall22 For me, the issue was the MCP connection (SSE), which was somehow blocking every request. I switched to streamable HTTP, and it started working. Don't know why it worked locally since wrangler does use the same workerd runtime.

unique1o1 avatar Jul 05 '25 15:07 unique1o1

Hey @unique1o1! I tried doing a very simple repro, by starting a new project with the agents-starter template and making just this change:

diff --git a/src/server.ts b/src/server.ts
index 3cd4cea..d7b6f6c 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -35,9 +35,9 @@ export class Chat extends AIChatAgent<Env> {
     onFinish: StreamTextOnFinishCallback<ToolSet>,
     _options?: { abortSignal?: AbortSignal }
   ) {
-    // const mcpConnection = await this.mcp.connect(
-    //   "https://path-to-mcp-server/sse"
-    // );
+    const mcpConnection = await this.mcp.connect(
+      "https://docs.mcp.cloudflare.com/sse"
+    );
 
     // Collect all tools, including MCP tools
     const allTools = {
@@ -72,7 +72,7 @@ If the user asks to schedule a task, use the schedule tool to schedule the task.
             onFinish(
               args as Parameters<StreamTextOnFinishCallback<ToolSet>>[0]
             );
-            // await this.mcp.closeConnection(mcpConnection.id);
+            await this.mcp.closeConnection(mcpConnection.id);
           },
           onError: (error) => {
             console.error("Error while streaming:", error);

And the MCP servers connect perfectly, both in local and on prod. Same result with the yrifi-mcp-server MCP server configured on your repo too. Can you verify if this works for you? The agents SDK already ships with an MCP client that takes care of every lifecycle combination and gives you a super simple API to work with, and is recommended over handling it yourself with experimental_createMCPClient() from the AI SDK.

ghostwriternr avatar Jul 15 '25 13:07 ghostwriternr

@ryleyrandall22 if you have a repro we can look at, would be happy to debug together! I'll close this issue for the time being. But if you're able to repro this even with the latest version of the SDK, please do re-open this issue.

ghostwriternr avatar Jul 15 '25 13:07 ghostwriternr