agents 0.0.0-9ae8ee3 → 0.0.0-9beccdd

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +31 -6
  2. package/dist/ai-chat-agent.d.ts +9 -8
  3. package/dist/ai-chat-agent.js +142 -59
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/ai-chat-v5-migration.d.ts +152 -0
  6. package/dist/ai-chat-v5-migration.js +19 -0
  7. package/dist/ai-chat-v5-migration.js.map +1 -0
  8. package/dist/ai-react.d.ts +59 -70
  9. package/dist/ai-react.js +144 -37
  10. package/dist/ai-react.js.map +1 -1
  11. package/dist/ai-types.d.ts +36 -19
  12. package/dist/ai-types.js +6 -0
  13. package/dist/chunk-AVYJQSLW.js +17 -0
  14. package/dist/chunk-AVYJQSLW.js.map +1 -0
  15. package/dist/{chunk-PVQZBKN7.js → chunk-LL2AFX7V.js} +5 -2
  16. package/dist/chunk-LL2AFX7V.js.map +1 -0
  17. package/dist/{chunk-3IQQY2UH.js → chunk-PNF6ZMUA.js} +114 -88
  18. package/dist/chunk-PNF6ZMUA.js.map +1 -0
  19. package/dist/{chunk-KUH345EY.js → chunk-QEVM4BVL.js} +5 -5
  20. package/dist/chunk-QEVM4BVL.js.map +1 -0
  21. package/dist/chunk-UJVEAURM.js +150 -0
  22. package/dist/chunk-UJVEAURM.js.map +1 -0
  23. package/dist/{chunk-UNG3FXYX.js → chunk-VYENMKFS.js} +110 -23
  24. package/dist/chunk-VYENMKFS.js.map +1 -0
  25. package/dist/client-CcIORE73.d.ts +4607 -0
  26. package/dist/client.js +2 -1
  27. package/dist/index.d.ts +546 -32
  28. package/dist/index.js +7 -4
  29. package/dist/mcp/client.d.ts +9 -1052
  30. package/dist/mcp/client.js +1 -1
  31. package/dist/mcp/do-oauth-client-provider.d.ts +1 -0
  32. package/dist/mcp/do-oauth-client-provider.js +1 -1
  33. package/dist/mcp/index.d.ts +33 -28
  34. package/dist/mcp/index.js +172 -14
  35. package/dist/mcp/index.js.map +1 -1
  36. package/dist/observability/index.d.ts +46 -14
  37. package/dist/observability/index.js +5 -4
  38. package/dist/react.d.ts +4 -2
  39. package/dist/react.js +7 -5
  40. package/dist/react.js.map +1 -1
  41. package/dist/schedule.d.ts +83 -9
  42. package/dist/schedule.js +15 -2
  43. package/dist/schedule.js.map +1 -1
  44. package/package.json +15 -7
  45. package/src/index.ts +140 -97
  46. package/dist/chunk-3IQQY2UH.js.map +0 -1
  47. package/dist/chunk-KUH345EY.js.map +0 -1
  48. package/dist/chunk-PVQZBKN7.js.map +0 -1
  49. package/dist/chunk-UNG3FXYX.js.map +0 -1
  50. package/dist/index-CLW1aEBr.d.ts +0 -615
@@ -1,8 +1,21 @@
1
1
  import { z } from "zod";
2
2
 
3
- type Schedule = z.infer<typeof unstable_scheduleSchema>;
3
+ /**
4
+ * Get the schedule prompt for a given event
5
+ * @param event - The event to get the schedule prompt for
6
+ * @returns The schedule prompt
7
+ */
8
+ declare function getSchedulePrompt(event: { date: Date }): string;
9
+ /**
10
+ * @deprecated this has been renamed to getSchedulePrompt, and unstable_getSchedulePrompt will be removed in the next major version
11
+ * @param event - The event to get the schedule prompt for
12
+ * @returns The schedule prompt
13
+ */
4
14
  declare function unstable_getSchedulePrompt(event: { date: Date }): string;
5
- declare const unstable_scheduleSchema: z.ZodObject<
15
+ /**
16
+ * The schema for the schedule prompt
17
+ */
18
+ declare const scheduleSchema: z.ZodObject<
6
19
  {
7
20
  description: z.ZodString;
8
21
  when: z.ZodObject<
@@ -16,15 +29,15 @@ declare const unstable_scheduleSchema: z.ZodObject<
16
29
  z.ZodTypeAny,
17
30
  {
18
31
  type: "scheduled" | "delayed" | "cron" | "no-schedule";
19
- delayInSeconds?: number | undefined;
20
- cron?: string | undefined;
21
32
  date?: Date | undefined;
33
+ cron?: string | undefined;
34
+ delayInSeconds?: number | undefined;
22
35
  },
23
36
  {
24
37
  type: "scheduled" | "delayed" | "cron" | "no-schedule";
25
- delayInSeconds?: number | undefined;
26
- cron?: string | undefined;
27
38
  date?: Date | undefined;
39
+ cron?: string | undefined;
40
+ delayInSeconds?: number | undefined;
28
41
  }
29
42
  >;
30
43
  },
@@ -34,20 +47,81 @@ declare const unstable_scheduleSchema: z.ZodObject<
34
47
  description: string;
35
48
  when: {
36
49
  type: "scheduled" | "delayed" | "cron" | "no-schedule";
37
- delayInSeconds?: number | undefined;
38
- cron?: string | undefined;
39
50
  date?: Date | undefined;
51
+ cron?: string | undefined;
52
+ delayInSeconds?: number | undefined;
40
53
  };
41
54
  },
42
55
  {
43
56
  description: string;
44
57
  when: {
45
58
  type: "scheduled" | "delayed" | "cron" | "no-schedule";
59
+ date?: Date | undefined;
60
+ cron?: string | undefined;
46
61
  delayInSeconds?: number | undefined;
62
+ };
63
+ }
64
+ >;
65
+ /**
66
+ * The type for the schedule prompt
67
+ */
68
+ type Schedule = z.infer<typeof scheduleSchema>;
69
+ /**
70
+ * @deprecated this has been renamed to scheduleSchema, and unstable_scheduleSchema will be removed in the next major version
71
+ * @returns The schedule schema
72
+ */
73
+ declare const unstable_scheduleSchema: z.ZodObject<
74
+ {
75
+ description: z.ZodString;
76
+ when: z.ZodObject<
77
+ {
78
+ cron: z.ZodOptional<z.ZodString>;
79
+ date: z.ZodOptional<z.ZodDate>;
80
+ delayInSeconds: z.ZodOptional<z.ZodNumber>;
81
+ type: z.ZodEnum<["scheduled", "delayed", "cron", "no-schedule"]>;
82
+ },
83
+ "strip",
84
+ z.ZodTypeAny,
85
+ {
86
+ type: "scheduled" | "delayed" | "cron" | "no-schedule";
87
+ date?: Date | undefined;
88
+ cron?: string | undefined;
89
+ delayInSeconds?: number | undefined;
90
+ },
91
+ {
92
+ type: "scheduled" | "delayed" | "cron" | "no-schedule";
93
+ date?: Date | undefined;
94
+ cron?: string | undefined;
95
+ delayInSeconds?: number | undefined;
96
+ }
97
+ >;
98
+ },
99
+ "strip",
100
+ z.ZodTypeAny,
101
+ {
102
+ description: string;
103
+ when: {
104
+ type: "scheduled" | "delayed" | "cron" | "no-schedule";
105
+ date?: Date | undefined;
47
106
  cron?: string | undefined;
107
+ delayInSeconds?: number | undefined;
108
+ };
109
+ },
110
+ {
111
+ description: string;
112
+ when: {
113
+ type: "scheduled" | "delayed" | "cron" | "no-schedule";
48
114
  date?: Date | undefined;
115
+ cron?: string | undefined;
116
+ delayInSeconds?: number | undefined;
49
117
  };
50
118
  }
51
119
  >;
52
120
 
53
- export { type Schedule, unstable_getSchedulePrompt, unstable_scheduleSchema };
121
+ export {
122
+ type Schedule,
123
+ getSchedulePrompt,
124
+ scheduleSchema,
125
+ unstable_getSchedulePrompt,
126
+ unstable_scheduleSchema
127
+ };
package/dist/schedule.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/schedule.ts
2
2
  import { z } from "zod";
3
- function unstable_getSchedulePrompt(event) {
3
+ function getSchedulePrompt(event) {
4
4
  return `
5
5
  [Schedule Parser Component]
6
6
 
@@ -49,7 +49,17 @@ Example outputs:
49
49
  [End Schedule Parser Component]
50
50
  `;
51
51
  }
52
- var unstable_scheduleSchema = z.object({
52
+ var didWarnAboutUnstableGetSchedulePrompt = false;
53
+ function unstable_getSchedulePrompt(event) {
54
+ if (!didWarnAboutUnstableGetSchedulePrompt) {
55
+ didWarnAboutUnstableGetSchedulePrompt = true;
56
+ console.warn(
57
+ "unstable_getSchedulePrompt is deprecated, use getSchedulePrompt instead. unstable_getSchedulePrompt will be removed in the next major version."
58
+ );
59
+ }
60
+ return getSchedulePrompt(event);
61
+ }
62
+ var scheduleSchema = z.object({
53
63
  description: z.string().describe("A description of the task"),
54
64
  when: z.object({
55
65
  cron: z.string().optional().describe(
@@ -64,7 +74,10 @@ var unstable_scheduleSchema = z.object({
64
74
  type: z.enum(["scheduled", "delayed", "cron", "no-schedule"]).describe("The type of scheduling details")
65
75
  })
66
76
  });
77
+ var unstable_scheduleSchema = scheduleSchema;
67
78
  export {
79
+ getSchedulePrompt,
80
+ scheduleSchema,
68
81
  unstable_getSchedulePrompt,
69
82
  unstable_scheduleSchema
70
83
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/schedule.ts"],"sourcesContent":["import { z } from \"zod\";\n\nexport type Schedule = z.infer<typeof unstable_scheduleSchema>;\n\nexport function unstable_getSchedulePrompt(event: { date: Date }) {\n return `\n[Schedule Parser Component]\n\nCurrent time: ${event.date.toUTCString()}\n\nThis component parses natural language scheduling requests into a structured format. It extracts:\n1. A clean task description (without timing information)\n2. Scheduling details in one of these formats:\n - scheduled: Specific date/time events\n - delayed: Relative time delays (in seconds)\n - cron: Recurring patterns\n - no-schedule: Tasks without timing\n\nRules:\n- Task descriptions should be clean and focused on the action\n- Use numbers (0-6) for days in cron patterns (0=Sunday)\n- For recurring tasks, use standard cron syntax\n- For relative times, convert to seconds\n- For specific dates, use the current time as reference\n\nExample outputs:\n{\n \"description\": \"meeting with team\",\n \"when\": {\n \"type\": \"scheduled\",\n \"date\": \"tomorrow at 14:00\"\n }\n}\n\n{\n \"description\": \"backup database\",\n \"when\": {\n \"type\": \"cron\",\n \"cron\": \"0 0 * * *\"\n }\n}\n\n{\n \"description\": \"send report\",\n \"when\": {\n \"type\": \"delayed\",\n \"delayInSeconds\": 1800\n }\n}\n\n[End Schedule Parser Component]\n`;\n}\n\nexport const unstable_scheduleSchema = z.object({\n description: z.string().describe(\"A description of the task\"),\n when: z.object({\n cron: z\n .string()\n .optional()\n .describe(\n \"execute task on a recurring interval specified as cron syntax (only use if the type is cron)\"\n ),\n date: z.coerce\n .date()\n .optional()\n .describe(\n \"execute task at the specified date and time (only use if the type is scheduled)\"\n ),\n delayInSeconds: z\n .number()\n .optional()\n .describe(\n \"execute task after a delay in seconds (only use if the type is delayed)\"\n ),\n type: z\n .enum([\"scheduled\", \"delayed\", \"cron\", \"no-schedule\"])\n .describe(\"The type of scheduling details\")\n })\n});\n"],"mappings":";AAAA,SAAS,SAAS;AAIX,SAAS,2BAA2B,OAAuB;AAChE,SAAO;AAAA;AAAA;AAAA,gBAGO,MAAM,KAAK,YAAY,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA4CxC;AAEO,IAAM,0BAA0B,EAAE,OAAO;AAAA,EAC9C,aAAa,EAAE,OAAO,EAAE,SAAS,2BAA2B;AAAA,EAC5D,MAAM,EAAE,OAAO;AAAA,IACb,MAAM,EACH,OAAO,EACP,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,MAAM,EAAE,OACL,KAAK,EACL,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,gBAAgB,EACb,OAAO,EACP,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,MAAM,EACH,KAAK,CAAC,aAAa,WAAW,QAAQ,aAAa,CAAC,EACpD,SAAS,gCAAgC;AAAA,EAC9C,CAAC;AACH,CAAC;","names":[]}
1
+ {"version":3,"sources":["../src/schedule.ts"],"sourcesContent":["import { z } from \"zod\";\n\n/**\n * Get the schedule prompt for a given event\n * @param event - The event to get the schedule prompt for\n * @returns The schedule prompt\n */\nexport function getSchedulePrompt(event: { date: Date }) {\n return `\n[Schedule Parser Component]\n\nCurrent time: ${event.date.toUTCString()}\n\nThis component parses natural language scheduling requests into a structured format. It extracts:\n1. A clean task description (without timing information)\n2. Scheduling details in one of these formats:\n - scheduled: Specific date/time events\n - delayed: Relative time delays (in seconds)\n - cron: Recurring patterns\n - no-schedule: Tasks without timing\n\nRules:\n- Task descriptions should be clean and focused on the action\n- Use numbers (0-6) for days in cron patterns (0=Sunday)\n- For recurring tasks, use standard cron syntax\n- For relative times, convert to seconds\n- For specific dates, use the current time as reference\n\nExample outputs:\n{\n \"description\": \"meeting with team\",\n \"when\": {\n \"type\": \"scheduled\",\n \"date\": \"tomorrow at 14:00\"\n }\n}\n\n{\n \"description\": \"backup database\",\n \"when\": {\n \"type\": \"cron\",\n \"cron\": \"0 0 * * *\"\n }\n}\n\n{\n \"description\": \"send report\",\n \"when\": {\n \"type\": \"delayed\",\n \"delayInSeconds\": 1800\n }\n}\n\n[End Schedule Parser Component]\n`;\n}\n\nlet didWarnAboutUnstableGetSchedulePrompt = false;\n\n/**\n * @deprecated this has been renamed to getSchedulePrompt, and unstable_getSchedulePrompt will be removed in the next major version\n * @param event - The event to get the schedule prompt for\n * @returns The schedule prompt\n */\nexport function unstable_getSchedulePrompt(event: { date: Date }) {\n if (!didWarnAboutUnstableGetSchedulePrompt) {\n didWarnAboutUnstableGetSchedulePrompt = true;\n console.warn(\n \"unstable_getSchedulePrompt is deprecated, use getSchedulePrompt instead. unstable_getSchedulePrompt will be removed in the next major version.\"\n );\n }\n return getSchedulePrompt(event);\n}\n\n/**\n * The schema for the schedule prompt\n */\nexport const scheduleSchema = z.object({\n description: z.string().describe(\"A description of the task\"),\n when: z.object({\n cron: z\n .string()\n .optional()\n .describe(\n \"execute task on a recurring interval specified as cron syntax (only use if the type is cron)\"\n ),\n date: z.coerce\n .date()\n .optional()\n .describe(\n \"execute task at the specified date and time (only use if the type is scheduled)\"\n ),\n delayInSeconds: z\n .number()\n .optional()\n .describe(\n \"execute task after a delay in seconds (only use if the type is delayed)\"\n ),\n type: z\n .enum([\"scheduled\", \"delayed\", \"cron\", \"no-schedule\"])\n .describe(\"The type of scheduling details\")\n })\n});\n\n/**\n * The type for the schedule prompt\n */\nexport type Schedule = z.infer<typeof scheduleSchema>;\n\n/**\n * @deprecated this has been renamed to scheduleSchema, and unstable_scheduleSchema will be removed in the next major version\n * @returns The schedule schema\n */\nexport const unstable_scheduleSchema = scheduleSchema;\n"],"mappings":";AAAA,SAAS,SAAS;AAOX,SAAS,kBAAkB,OAAuB;AACvD,SAAO;AAAA;AAAA;AAAA,gBAGO,MAAM,KAAK,YAAY,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA4CxC;AAEA,IAAI,wCAAwC;AAOrC,SAAS,2BAA2B,OAAuB;AAChE,MAAI,CAAC,uCAAuC;AAC1C,4CAAwC;AACxC,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACA,SAAO,kBAAkB,KAAK;AAChC;AAKO,IAAM,iBAAiB,EAAE,OAAO;AAAA,EACrC,aAAa,EAAE,OAAO,EAAE,SAAS,2BAA2B;AAAA,EAC5D,MAAM,EAAE,OAAO;AAAA,IACb,MAAM,EACH,OAAO,EACP,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,MAAM,EAAE,OACL,KAAK,EACL,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,gBAAgB,EACb,OAAO,EACP,SAAS,EACT;AAAA,MACC;AAAA,IACF;AAAA,IACF,MAAM,EACH,KAAK,CAAC,aAAa,WAAW,QAAQ,aAAa,CAAC,EACpD,SAAS,gCAAgC;AAAA,EAC9C,CAAC;AACH,CAAC;AAWM,IAAM,0BAA0B;","names":[]}
package/package.json CHANGED
@@ -4,19 +4,22 @@
4
4
  "url": "https://github.com/cloudflare/agents/issues"
5
5
  },
6
6
  "dependencies": {
7
- "@modelcontextprotocol/sdk": "^1.13.3",
8
- "ai": "^4.3.16",
7
+ "@modelcontextprotocol/sdk": "^1.17.5",
8
+ "ai": "5.0.39",
9
9
  "cron-schedule": "^5.0.4",
10
10
  "mimetext": "^3.0.27",
11
11
  "nanoid": "^5.1.5",
12
- "partyserver": "^0.0.72",
13
- "partysocket": "1.1.4",
14
- "zod": "^3.25.67"
12
+ "partyserver": "^0.0.73",
13
+ "partysocket": "1.1.5",
14
+ "zod": "^3.25.76"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
15
18
  },
16
19
  "description": "A home for your AI agents",
17
20
  "devDependencies": {
18
21
  "react": "*",
19
- "vitest-browser-react": "^1.0.0"
22
+ "vitest-browser-react": "^1.0.1"
20
23
  },
21
24
  "exports": {
22
25
  ".": {
@@ -29,6 +32,11 @@
29
32
  "import": "./dist/ai-chat-agent.js",
30
33
  "require": "./dist/ai-chat-agent.js"
31
34
  },
35
+ "./ai-chat-v5-migration": {
36
+ "types": "./dist/ai-chat-v5-migration.d.ts",
37
+ "import": "./dist/ai-chat-v5-migration.js",
38
+ "require": "./dist/ai-chat-v5-migration.js"
39
+ },
32
40
  "./ai-react": {
33
41
  "types": "./dist/ai-react.d.ts",
34
42
  "import": "./dist/ai-react.js",
@@ -102,5 +110,5 @@
102
110
  },
103
111
  "type": "module",
104
112
  "types": "dist/index.d.ts",
105
- "version": "0.0.0-9ae8ee3"
113
+ "version": "0.0.0-9beccdd"
106
114
  }
package/src/index.ts CHANGED
@@ -26,6 +26,7 @@ import { MCPClientManager } from "./mcp/client";
26
26
  // import type { MCPClientConnection } from "./mcp/client-connection";
27
27
  import { DurableObjectOAuthClientProvider } from "./mcp/do-oauth-client-provider";
28
28
  import { genericObservability, type Observability } from "./observability";
29
+ import { MessageType } from "./ai-types";
29
30
 
30
31
  export type { Connection, ConnectionContext, WSMessage } from "partyserver";
31
32
 
@@ -43,7 +44,7 @@ export type RPCRequest = {
43
44
  * State update message from client
44
45
  */
45
46
  export type StateUpdateMessage = {
46
- type: "cf_agent_state";
47
+ type: MessageType.CF_AGENT_STATE;
47
48
  state: unknown;
48
49
  };
49
50
 
@@ -51,7 +52,7 @@ export type StateUpdateMessage = {
51
52
  * RPC response message to client
52
53
  */
53
54
  export type RPCResponse = {
54
- type: "rpc";
55
+ type: MessageType.RPC;
55
56
  id: string;
56
57
  } & (
57
58
  | {
@@ -78,7 +79,7 @@ function isRPCRequest(msg: unknown): msg is RPCRequest {
78
79
  typeof msg === "object" &&
79
80
  msg !== null &&
80
81
  "type" in msg &&
81
- msg.type === "rpc" &&
82
+ msg.type === MessageType.RPC &&
82
83
  "id" in msg &&
83
84
  typeof msg.id === "string" &&
84
85
  "method" in msg &&
@@ -96,7 +97,7 @@ function isStateUpdateMessage(msg: unknown): msg is StateUpdateMessage {
96
97
  typeof msg === "object" &&
97
98
  msg !== null &&
98
99
  "type" in msg &&
99
- msg.type === "cf_agent_state" &&
100
+ msg.type === MessageType.CF_AGENT_STATE &&
100
101
  "state" in msg
101
102
  );
102
103
  }
@@ -117,7 +118,7 @@ const callableMetadata = new Map<Function, CallableMetadata>();
117
118
  * Decorator that marks a method as callable by clients
118
119
  * @param metadata Optional metadata about the callable method
119
120
  */
120
- export function unstable_callable(metadata: CallableMetadata = {}) {
121
+ export function callable(metadata: CallableMetadata = {}) {
121
122
  return function callableDecorator<This, Args extends unknown[], Return>(
122
123
  target: (this: This, ...args: Args) => Return,
123
124
  // biome-ignore lint/correctness/noUnusedFunctionParameters: later
@@ -131,6 +132,23 @@ export function unstable_callable(metadata: CallableMetadata = {}) {
131
132
  };
132
133
  }
133
134
 
135
+ let didWarnAboutUnstableCallable = false;
136
+
137
+ /**
138
+ * Decorator that marks a method as callable by clients
139
+ * @deprecated this has been renamed to callable, and unstable_callable will be removed in the next major version
140
+ * @param metadata Optional metadata about the callable method
141
+ */
142
+ export const unstable_callable = (metadata: CallableMetadata = {}) => {
143
+ if (!didWarnAboutUnstableCallable) {
144
+ didWarnAboutUnstableCallable = true;
145
+ console.warn(
146
+ "unstable_callable is deprecated, use callable instead. unstable_callable will be removed in the next major version."
147
+ );
148
+ }
149
+ callable(metadata);
150
+ };
151
+
134
152
  export type QueueItem<T = string> = {
135
153
  id: string;
136
154
  payload: T;
@@ -183,7 +201,7 @@ function getNextCronTime(cron: string) {
183
201
  * MCP Server state update message from server -> Client
184
202
  */
185
203
  export type MCPServerMessage = {
186
- type: "cf_agent_mcp_servers";
204
+ type: MessageType.CF_AGENT_MCP_SERVERS;
187
205
  mcp: MCPServersState;
188
206
  };
189
207
 
@@ -272,7 +290,13 @@ function withAgentContext<T extends (...args: any[]) => any>(
272
290
  method: T
273
291
  ): (this: Agent<unknown, unknown>, ...args: Parameters<T>) => ReturnType<T> {
274
292
  return function (...args: Parameters<T>): ReturnType<T> {
275
- const { connection, request, email } = getCurrentAgent();
293
+ const { connection, request, email, agent } = getCurrentAgent();
294
+
295
+ if (agent === this) {
296
+ // already wrapped, so we can just call the method
297
+ return method.apply(this, args);
298
+ }
299
+ // not wrapped, so we need to wrap it
276
300
  return agentContext.run({ agent: this, connection, request, email }, () => {
277
301
  return method.apply(this, args);
278
302
  });
@@ -447,7 +471,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
447
471
  this.broadcast(
448
472
  JSON.stringify({
449
473
  mcp: this.getMcpServers(),
450
- type: "cf_agent_mcp_servers"
474
+ type: MessageType.CF_AGENT_MCP_SERVERS
451
475
  })
452
476
  );
453
477
 
@@ -516,10 +540,8 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
516
540
  displayMessage: `RPC call to ${method}`,
517
541
  id: nanoid(),
518
542
  payload: {
519
- args,
520
543
  method,
521
- streaming: metadata?.streaming,
522
- success: true
544
+ streaming: metadata?.streaming
523
545
  },
524
546
  timestamp: Date.now(),
525
547
  type: "rpc"
@@ -532,7 +554,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
532
554
  id,
533
555
  result,
534
556
  success: true,
535
- type: "rpc"
557
+ type: MessageType.RPC
536
558
  };
537
559
  connection.send(JSON.stringify(response));
538
560
  } catch (e) {
@@ -542,7 +564,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
542
564
  e instanceof Error ? e.message : "Unknown error occurred",
543
565
  id: parsed.id,
544
566
  success: false,
545
- type: "rpc"
567
+ type: MessageType.RPC
546
568
  };
547
569
  connection.send(JSON.stringify(response));
548
570
  console.error("RPC error:", e);
@@ -561,38 +583,36 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
561
583
  // must fix this
562
584
  return agentContext.run(
563
585
  { agent: this, connection, request: ctx.request, email: undefined },
564
- async () => {
565
- setTimeout(() => {
566
- if (this.state) {
567
- connection.send(
568
- JSON.stringify({
569
- state: this.state,
570
- type: "cf_agent_state"
571
- })
572
- );
573
- }
574
-
586
+ () => {
587
+ if (this.state) {
575
588
  connection.send(
576
589
  JSON.stringify({
577
- mcp: this.getMcpServers(),
578
- type: "cf_agent_mcp_servers"
590
+ state: this.state,
591
+ type: MessageType.CF_AGENT_STATE
579
592
  })
580
593
  );
594
+ }
581
595
 
582
- this.observability?.emit(
583
- {
584
- displayMessage: "Connection established",
585
- id: nanoid(),
586
- payload: {
587
- connectionId: connection.id
588
- },
589
- timestamp: Date.now(),
590
- type: "connect"
596
+ connection.send(
597
+ JSON.stringify({
598
+ mcp: this.getMcpServers(),
599
+ type: MessageType.CF_AGENT_MCP_SERVERS
600
+ })
601
+ );
602
+
603
+ this.observability?.emit(
604
+ {
605
+ displayMessage: "Connection established",
606
+ id: nanoid(),
607
+ payload: {
608
+ connectionId: connection.id
591
609
  },
592
- this.ctx
593
- );
594
- return this._tryCatch(() => _onConnect(connection, ctx));
595
- }, 20);
610
+ timestamp: Date.now(),
611
+ type: "connect"
612
+ },
613
+ this.ctx
614
+ );
615
+ return this._tryCatch(() => _onConnect(connection, ctx));
596
616
  }
597
617
  );
598
618
  };
@@ -607,15 +627,22 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
607
627
  email: undefined
608
628
  },
609
629
  async () => {
610
- const servers = this.sql<MCPServerRow>`
630
+ await this._tryCatch(() => {
631
+ const servers = this.sql<MCPServerRow>`
611
632
  SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
612
633
  `;
613
634
 
614
- // from DO storage, reconnect to all servers not currently in the oauth flow using our saved auth information
615
- if (servers && Array.isArray(servers) && servers.length > 0) {
616
- Promise.allSettled(
617
- servers.map((server) => {
618
- return this._connectToMcpServerInternal(
635
+ this.broadcast(
636
+ JSON.stringify({
637
+ mcp: this.getMcpServers(),
638
+ type: MessageType.CF_AGENT_MCP_SERVERS
639
+ })
640
+ );
641
+
642
+ // from DO storage, reconnect to all servers not currently in the oauth flow using our saved auth information
643
+ if (servers && Array.isArray(servers) && servers.length > 0) {
644
+ servers.forEach((server) => {
645
+ this._connectToMcpServerInternal(
619
646
  server.name,
620
647
  server.server_url,
621
648
  server.callback_url,
@@ -626,18 +653,33 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
626
653
  id: server.id,
627
654
  oauthClientId: server.client_id ?? undefined
628
655
  }
629
- );
630
- })
631
- ).then((_results) => {
632
- this.broadcast(
633
- JSON.stringify({
634
- mcp: this.getMcpServers(),
635
- type: "cf_agent_mcp_servers"
636
- })
637
- );
638
- });
639
- }
640
- await this._tryCatch(() => _onStart());
656
+ )
657
+ .then(() => {
658
+ // Broadcast updated MCP servers state after each server connects
659
+ this.broadcast(
660
+ JSON.stringify({
661
+ mcp: this.getMcpServers(),
662
+ type: MessageType.CF_AGENT_MCP_SERVERS
663
+ })
664
+ );
665
+ })
666
+ .catch((error) => {
667
+ console.error(
668
+ `Error connecting to MCP server: ${server.name} (${server.server_url})`,
669
+ error
670
+ );
671
+ // Still broadcast even if connection fails, so clients know about the failure
672
+ this.broadcast(
673
+ JSON.stringify({
674
+ mcp: this.getMcpServers(),
675
+ type: MessageType.CF_AGENT_MCP_SERVERS
676
+ })
677
+ );
678
+ });
679
+ });
680
+ }
681
+ return _onStart();
682
+ });
641
683
  }
642
684
  );
643
685
  };
@@ -647,7 +689,6 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
647
689
  state: State,
648
690
  source: Connection | "server" = "server"
649
691
  ) {
650
- const previousState = this._state;
651
692
  this._state = state;
652
693
  this.sql`
653
694
  INSERT OR REPLACE INTO cf_agents_state (id, state)
@@ -660,7 +701,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
660
701
  this.broadcast(
661
702
  JSON.stringify({
662
703
  state: state,
663
- type: "cf_agent_state"
704
+ type: MessageType.CF_AGENT_STATE
664
705
  }),
665
706
  source !== "server" ? [source.id] : []
666
707
  );
@@ -673,10 +714,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
673
714
  {
674
715
  displayMessage: "State updated",
675
716
  id: nanoid(),
676
- payload: {
677
- previousState,
678
- state
679
- },
717
+ payload: {},
680
718
  timestamp: Date.now(),
681
719
  type: "state:update"
682
720
  },
@@ -816,41 +854,37 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
816
854
  while (proto && proto !== Object.prototype && depth < 10) {
817
855
  const methodNames = Object.getOwnPropertyNames(proto);
818
856
  for (const methodName of methodNames) {
819
- // Skip if it's a private method or not a function
857
+ const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
858
+
859
+ // Skip if it's a private method, a base method, a getter, or not a function,
820
860
  if (
821
861
  baseMethods.has(methodName) ||
822
862
  methodName.startsWith("_") ||
823
- typeof this[methodName as keyof this] !== "function"
863
+ !descriptor ||
864
+ !!descriptor.get ||
865
+ typeof descriptor.value !== "function"
824
866
  ) {
825
867
  continue;
826
868
  }
827
- // If the method doesn't exist in base prototypes, it's a custom method
828
- if (!baseMethods.has(methodName)) {
829
- const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
830
- if (descriptor && typeof descriptor.value === "function") {
831
- // Wrap the custom method with context
832
-
833
- const wrappedFunction = withAgentContext(
834
- // biome-ignore lint/suspicious/noExplicitAny: I can't typescript
835
- this[methodName as keyof this] as (...args: any[]) => any
836
- // biome-ignore lint/suspicious/noExplicitAny: I can't typescript
837
- ) as any;
838
-
839
- // if the method is callable, copy the metadata from the original method
840
- if (this._isCallable(methodName)) {
841
- callableMetadata.set(
842
- wrappedFunction,
843
- callableMetadata.get(
844
- this[methodName as keyof this] as Function
845
- )!
846
- );
847
- }
848
869
 
849
- // set the wrapped function on the prototype
850
- this.constructor.prototype[methodName as keyof this] =
851
- wrappedFunction;
852
- }
870
+ // Now, methodName is confirmed to be a custom method/function
871
+ // Wrap the custom method with context
872
+ const wrappedFunction = withAgentContext(
873
+ // biome-ignore lint/suspicious/noExplicitAny: I can't typescript
874
+ this[methodName as keyof this] as (...args: any[]) => any
875
+ // biome-ignore lint/suspicious/noExplicitAny: I can't typescript
876
+ ) as any;
877
+
878
+ // if the method is callable, copy the metadata from the original method
879
+ if (this._isCallable(methodName)) {
880
+ callableMetadata.set(
881
+ wrappedFunction,
882
+ callableMetadata.get(this[methodName as keyof this] as Function)!
883
+ );
853
884
  }
885
+
886
+ // set the wrapped function on the prototype
887
+ this.constructor.prototype[methodName as keyof this] = wrappedFunction;
854
888
  }
855
889
 
856
890
  proto = Object.getPrototypeOf(proto);
@@ -1037,7 +1071,10 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
1037
1071
  {
1038
1072
  displayMessage: `Schedule ${schedule.id} created`,
1039
1073
  id: nanoid(),
1040
- payload: schedule,
1074
+ payload: {
1075
+ callback: callback as string,
1076
+ id: id
1077
+ },
1041
1078
  timestamp: Date.now(),
1042
1079
  type: "schedule:create"
1043
1080
  },
@@ -1207,7 +1244,10 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
1207
1244
  {
1208
1245
  displayMessage: `Schedule ${id} cancelled`,
1209
1246
  id: nanoid(),
1210
- payload: schedule,
1247
+ payload: {
1248
+ callback: schedule.callback,
1249
+ id: schedule.id
1250
+ },
1211
1251
  timestamp: Date.now(),
1212
1252
  type: "schedule:cancel"
1213
1253
  },
@@ -1272,7 +1312,10 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
1272
1312
  {
1273
1313
  displayMessage: `Schedule ${row.id} executed`,
1274
1314
  id: nanoid(),
1275
- payload: row,
1315
+ payload: {
1316
+ callback: row.callback,
1317
+ id: row.id
1318
+ },
1276
1319
  timestamp: Date.now(),
1277
1320
  type: "schedule:execute"
1278
1321
  },
@@ -1392,7 +1435,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
1392
1435
  this.broadcast(
1393
1436
  JSON.stringify({
1394
1437
  mcp: this.getMcpServers(),
1395
- type: "cf_agent_mcp_servers"
1438
+ type: MessageType.CF_AGENT_MCP_SERVERS
1396
1439
  })
1397
1440
  );
1398
1441
 
@@ -1481,7 +1524,7 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
1481
1524
  this.broadcast(
1482
1525
  JSON.stringify({
1483
1526
  mcp: this.getMcpServers(),
1484
- type: "cf_agent_mcp_servers"
1527
+ type: MessageType.CF_AGENT_MCP_SERVERS
1485
1528
  })
1486
1529
  );
1487
1530
  }
@@ -1869,7 +1912,7 @@ export class StreamingResponse {
1869
1912
  id: this._id,
1870
1913
  result: chunk,
1871
1914
  success: true,
1872
- type: "rpc"
1915
+ type: MessageType.RPC
1873
1916
  };
1874
1917
  this._connection.send(JSON.stringify(response));
1875
1918
  }
@@ -1888,7 +1931,7 @@ export class StreamingResponse {
1888
1931
  id: this._id,
1889
1932
  result: finalChunk,
1890
1933
  success: true,
1891
- type: "rpc"
1934
+ type: MessageType.RPC
1892
1935
  };
1893
1936
  this._connection.send(JSON.stringify(response));
1894
1937
  }