* Description for the `execute` tool, including the per-session Cloudflare type * declarations and a multipart Worker-upload example.
(props?: AuthProps)
| 241 | * declarations and a multipart Worker-upload example. |
| 242 | */ |
| 243 | function executeToolDescription(props?: AuthProps): string { |
| 244 | const types = cloudflareTypesForAccount(props) |
| 245 | const accountSelection = accountSelectionDescription(props) |
| 246 | |
| 247 | return `Execute JavaScript code against the Cloudflare API. First use the 'search' tool to find the right endpoints, then write code using the cloudflare.request() function. |
| 248 | |
| 249 | Available in your code: |
| 250 | ${types}${accountSelection} |
| 251 | |
| 252 | Your code must be an async arrow function that returns the result. |
| 253 | |
| 254 | Example: Worker with bindings (requires multipart/form-data): |
| 255 | async () => { |
| 256 | const code = \`addEventListener('fetch', e => e.respondWith(MY_KV.get('key').then(v => new Response(v || 'none'))));\`; |
| 257 | const metadata = { body_part: "script", bindings: [{ type: "kv_namespace", name: "MY_KV", namespace_id: "your-kv-id" }] }; |
| 258 | const b = \`--F\${Date.now()}\`; |
| 259 | const body = [\`--\${b}\`, 'Content-Disposition: form-data; name="metadata"', 'Content-Type: application/json', '', JSON.stringify(metadata), \`--\${b}\`, 'Content-Disposition: form-data; name="script"', 'Content-Type: application/javascript', '', code, \`--\${b}--\`].join("\\r\\n"); |
| 260 | return cloudflare.request({ method: "PUT", path: \`/accounts/\${accountId}/workers/scripts/my-worker\`, body, contentType: \`multipart/form-data; boundary=\${b}\`, rawBody: true }); |
| 261 | }` |
| 262 | } |
| 263 | |
| 264 | function accountSelectionDescription(props?: AuthProps): string { |
| 265 | if (!isMultiAccountUser(props)) return '' |
no test coverage detected