Chat agent does not work after deployment
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.
can you make a repro?
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 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.
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.
@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.