Skip to content

MCP tools intermittently unavailable in onChatMessage #928

@Maxservais

Description

@Maxservais

What I'm trying to do

Connect a Notion MCP server from an AIChatAgent and use the MCP tools in onChatMessage.

What happens

MCP tools are sometimes available and sometimes not. When they're missing, getAITools() returns an empty set with this warning:

[getAITools] WARNING: Reading tools from connection aik32tRf in state "connecting". Tools may not be loaded yet.

The connection finishes restoring after the response is already sent:

Chat message request
[getAITools] WARNING: Reading tools from connection aik32tRf in state "connecting". Tools may not be loaded yet.
Available tools: [ 'getWeatherInformation', 'getLocalTime', 'scheduleTask' ]
Connected successfully using streamable-http transport for https://mcp.notion.com/mcp
Discovery completed for https://mcp.notion.com/mcp
Chat message response

It seems like a race condition — sometimes the MCP connection is ready by the time onChatMessage runs, sometimes it isn't.

My code

async onChatMessage(
  onFinish: StreamTextOnFinishCallback<ToolSet>,
  options?: { abortSignal?: AbortSignal }
) {
  const mcpState = this.getMcpServers();
  const hasNotion = Object.values(mcpState.servers).some(
    (s) => s.name === "notion"
  );

  if (!hasNotion) {
    await this.addMcpServer("notion", "https://mcp.notion.com/mcp", {
      callbackHost: this._callbackHost
    });
  }

  await this.mcp.ensureJsonSchema();
  const mcpTools = this.mcp.getAITools(); // ← sometimes empty
  // ...
}

Versions

  • agents@0.5.0
  • @cloudflare/ai-chat@0.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions