2026-02-19 20:11:23 +00:00
|
|
|
# Callable Methods
|
|
|
|
|
|
|
|
|
|
Fetch `docs/callable-methods.md` from `https://github.com/cloudflare/agents/tree/main/docs` for complete documentation.
|
|
|
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
|
|
|
|
|
`@callable()` exposes agent methods to clients via WebSocket RPC.
|
|
|
|
|
|
|
|
|
|
```typescript
|
2026-02-19 20:12:16 +00:00
|
|
|
import { Agent, callable } from "agents"
|
2026-02-19 20:11:23 +00:00
|
|
|
|
|
|
|
|
export class MyAgent extends Agent<Env, State> {
|
|
|
|
|
@callable()
|
|
|
|
|
async greet(name: string): Promise<string> {
|
2026-02-19 20:12:16 +00:00
|
|
|
return `Hello, ${name}!`
|
2026-02-19 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@callable()
|
|
|
|
|
async processData(data: unknown): Promise<Result> {
|
|
|
|
|
// Long-running work
|
2026-02-19 20:12:16 +00:00
|
|
|
return result
|
2026-02-19 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Client Usage
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Basic call
|
2026-02-19 20:12:16 +00:00
|
|
|
const greeting = await agent.call("greet", ["World"])
|
2026-02-19 20:11:23 +00:00
|
|
|
|
|
|
|
|
// With timeout
|
|
|
|
|
const result = await agent.call("processData", [data], {
|
2026-02-19 20:12:16 +00:00
|
|
|
timeout: 5000, // 5 second timeout
|
|
|
|
|
})
|
2026-02-19 20:11:23 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Streaming Responses
|
|
|
|
|
|
|
|
|
|
```typescript
|
2026-02-19 20:12:16 +00:00
|
|
|
import { Agent, callable, StreamingResponse } from "agents"
|
2026-02-19 20:11:23 +00:00
|
|
|
|
|
|
|
|
export class MyAgent extends Agent<Env, State> {
|
|
|
|
|
@callable({ streaming: true })
|
|
|
|
|
async streamResults(stream: StreamingResponse, query: string) {
|
|
|
|
|
for await (const item of fetchResults(query)) {
|
2026-02-19 20:12:16 +00:00
|
|
|
stream.send(JSON.stringify(item))
|
2026-02-19 20:11:23 +00:00
|
|
|
}
|
2026-02-19 20:12:16 +00:00
|
|
|
stream.close()
|
2026-02-19 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@callable({ streaming: true })
|
|
|
|
|
async streamWithError(stream: StreamingResponse) {
|
|
|
|
|
try {
|
|
|
|
|
// ... work
|
|
|
|
|
} catch (error) {
|
2026-02-19 20:12:16 +00:00
|
|
|
stream.error(error.message) // Signal error to client
|
|
|
|
|
return
|
2026-02-19 20:11:23 +00:00
|
|
|
}
|
2026-02-19 20:12:16 +00:00
|
|
|
stream.close()
|
2026-02-19 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Client with streaming:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
await agent.call("streamResults", ["search term"], {
|
|
|
|
|
stream: {
|
|
|
|
|
onChunk: (data) => console.log("Chunk:", data),
|
|
|
|
|
onDone: () => console.log("Complete"),
|
2026-02-19 20:12:16 +00:00
|
|
|
onError: (error) => console.error("Error:", error),
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-02-19 20:11:23 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Introspection
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Get list of callable methods on an agent
|
2026-02-19 20:12:16 +00:00
|
|
|
const methods = await agent.call("getCallableMethods", [])
|
2026-02-19 20:11:23 +00:00
|
|
|
// Returns: ["greet", "processData", "streamResults", ...]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## When to Use
|
|
|
|
|
|
2026-02-19 20:12:16 +00:00
|
|
|
| Scenario | Use |
|
|
|
|
|
| ------------------------------------ | --------------------------- |
|
|
|
|
|
| Browser/mobile calling agent | `@callable()` |
|
|
|
|
|
| External service calling agent | `@callable()` |
|
|
|
|
|
| Worker calling agent (same codebase) | DO RPC directly |
|
|
|
|
|
| Agent calling another agent | `getAgentByName()` + DO RPC |
|