Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-schools-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"agents": patch
---

Ensure this.mcp.jsonSchema is initialized on ai chat agent start
25 changes: 24 additions & 1 deletion packages/agents/src/ai-chat-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ type StreamMetadata = {
*/
export class AIChatAgent<
Env extends Cloudflare.Env = Cloudflare.Env,
State = unknown
State = unknown,
Props extends Record<string, unknown> = Record<string, unknown>
> extends Agent<Env, State> {
/**
* Map of message `id`s to `AbortController`s
Expand Down Expand Up @@ -239,6 +240,28 @@ export class AIChatAgent<

this._chatMessageAbortControllers = new Map();

// a fair assumption for the AIChatAgent is that "ai"
// is available, but we still need to ensure .jsonSchema's
// been initialised on this.mcp. Let's do it as soon
// as we start the agent.
const _onStart = this.onStart.bind(this);
this.onStart = async (props?: Props) => {
return agentContext.run(
{
agent: this,
connection: undefined,
request: undefined,
email: undefined
},
async () => {
await this._tryCatchChat(async () => {
await this.mcp.ensureJsonSchema();
return _onStart(props);
});
}
);
};

// Check for any active streams from a previous session
this._restoreActiveStream();
const _onConnect = this.onConnect.bind(this);
Expand Down
4 changes: 3 additions & 1 deletion packages/agents/src/mcp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,9 @@ export class MCPClientManager {
*/
getAITools(): ToolSet {
if (!this.jsonSchema) {
throw new Error("jsonSchema not initialized.");
throw new Error(
"jsonSchema not initialized, please call `await this.mcp.ensureJsonSchema()` before calling getAITools(). This will be fixed in the next major version."
);
}

// Warn if tools are being read from non-ready connections
Expand Down
2 changes: 1 addition & 1 deletion packages/agents/src/tests/mcp/client-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ describe("MCPClientManager OAuth Integration", () => {
});

expect(() => newManager.getAITools()).toThrow(
"jsonSchema not initialized."
"jsonSchema not initialized, please call `await this.mcp.ensureJsonSchema()` before calling getAITools(). This will be fixed in the next major version."
);
});
});
Expand Down
Loading