Skip to content

Commit 7cfc1db

Browse files
committed
Use ChatAgent.stub API; add idempotent schedule
Import ChatAgent type and instantiate useAgent with ChatAgent generic in app.tsx, then switch from agent.call(...) to the agent.stub methods (e.g. stub.addServer, stub.removeServer) to invoke agent RPCs. In server.ts, add a maxPersistedMessages property (100) to the ChatAgent and make scheduled tasks idempotent by passing { idempotent: true } to schedule. These changes align the UI with the agent stub API and improve scheduling and message persistence behavior.
1 parent f660931 commit 7cfc1db

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/app.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useAgentChat } from "@cloudflare/ai-chat/react";
44
import { isToolUIPart, getToolName } from "ai";
55
import type { UIMessage } from "ai";
66
import type { MCPServersState } from "agents";
7+
import type { ChatAgent } from "./server";
78
import {
89
Button,
910
Badge,
@@ -241,7 +242,7 @@ function Chat() {
241242
const [isAddingServer, setIsAddingServer] = useState(false);
242243
const mcpPanelRef = useRef<HTMLDivElement>(null);
243244

244-
const agent = useAgent({
245+
const agent = useAgent<ChatAgent>({
245246
agent: "ChatAgent",
246247
onOpen: useCallback(() => setConnected(true), []),
247248
onClose: useCallback(() => setConnected(false), []),
@@ -290,11 +291,11 @@ function Chat() {
290291
if (!mcpName.trim() || !mcpUrl.trim()) return;
291292
setIsAddingServer(true);
292293
try {
293-
await agent.call("addServer", [
294+
await agent.stub.addServer(
294295
mcpName.trim(),
295296
mcpUrl.trim(),
296297
window.location.origin
297-
]);
298+
);
298299
setMcpName("");
299300
setMcpUrl("");
300301
} catch (e) {
@@ -306,7 +307,7 @@ function Chat() {
306307

307308
const handleRemoveServer = async (serverId: string) => {
308309
try {
309-
await agent.call("removeServer", [serverId]);
310+
await agent.stub.removeServer(serverId);
310311
} catch (e) {
311312
console.error("Failed to remove MCP server:", e);
312313
}

src/server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function inlineDataUrls(messages: ModelMessage[]): ModelMessage[] {
3535
}
3636

3737
export class ChatAgent extends AIChatAgent<Env> {
38+
maxPersistedMessages = 100;
39+
3840
onStart() {
3941
// Configure OAuth popup behavior for MCP servers that require authentication
4042
this.mcp.configureOAuthCallback({
@@ -161,7 +163,9 @@ If the user asks to schedule a task, use the schedule tool to schedule the task.
161163
: null;
162164
if (!input) return "Invalid schedule type";
163165
try {
164-
this.schedule(input, "executeTask", description);
166+
this.schedule(input, "executeTask", description, {
167+
idempotent: true
168+
});
165169
return `Task scheduled: "${description}" (${when.type}: ${input})`;
166170
} catch (error) {
167171
return `Error scheduling task: ${error}`;

0 commit comments

Comments
 (0)