All Agent Engineering

Call Cloudflare Agent methods from clients and narrow exposure

Check @callable() declarations, argument validation, error returns and streaming calls to limit which methods clients can reach.

AINavWritten / Updated
On this page

At a glance

An Agent method marked with @callable() can be called by a connected client as if it were a local async function, and the call travels over the client's WebSocket connection. The convenience has a cost in exposure: any connected client can call a marked method. This acceptance sheet confirms which methods are marked, how their arguments are validated, and what the client sees when a call fails.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • Cloudflare Agents SDK Call Cloudflare Agent methods from clients and narrow exposure

Execution locations and responsibilities

  • Cloudflare Agent server and connected client Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. Inventory the decorated methods → 2. Validate each argument inside the method → 3. Check how the client calls → 4. Inspect error messages and client handling → 5. Check the ending of streaming methods → 6. Confirm decorator configuration → 7. Separate client calls from internal calls. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Inventory the decorated methods2. Validate each argument inside the method3. Check how the client calls4. Inspect error messages and client handling5. Check the ending of streaming methods6. Confirm decorator configuration7. Separate client calls from internal calls

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • Only methods decorated with @callable() can be called by clients; undecorated methods cannot. Each marked method is an external entry point. this.getCallableMethods() lists them, and @callable({ description }) attaches a description.

When not to use

  • The supplied material states only that a throwing method rejects the client's promise with the error message; timeouts and transport details belong to the Callables section and are not inferred here.
  • The decorator decides only whether a client can call a method; it provides no identity or authorization decision, so who may call still needs separate design.
  • @callable() is for clients outside the Worker; calls from a Worker or another Agent should use Durable Object RPC.

Prerequisites and input

  • Access to the Agent class so you can read every public method and its decorator declaration.
  • Access to the front-end code that calls methods through useAgent or AgentClient.

Engineering checks

Exposure declaration — Only methods decorated with @callable() can be called by clients; undecorated methods cannot. Each marked method is an external entry point. this.getCallableMethods() lists them, and @callable({ description }) attaches a description.

Argument trust — Any connected client can call a marked method, so treat all arguments as untrusted input and validate them inside the method. The official example checks that the argument to increment is an integer and throws otherwise.

Call path and error return — Clients can call through the typed stub on agent.stub, or through the untyped agent.call form. When a method throws, the client's promise rejects with the error message. Streaming methods use streaming: true and return data through send, end and error on StreamingResponse.

1. Inventory the decorated methods

Where: the Agent class and the output of this.getCallableMethods(). Input: every public method in the class. Action: list which ones carry @callable() and judge whether each is really a user action such as adding an item, approving a step or stopping a run. Completion check: every exposed method has a stated reason. Stop condition: if a method meant only for the server is decorated, remove the decorator first.

2. Validate each argument inside the method

Where: the start of every decorated method. Input: the arguments a client sends. Action: check type, range and structure for each one, throw on a mismatch, and only then change state. Completion check: every argument has a matching validation. Stop condition: a method that changes state from unvalidated arguments fails acceptance.

3. Check how the client calls

Where: the front end that uses useAgent. Input: the Agent class as the type parameter, plus the agent and name options. Action: confirm typed calls go through agent.stub, untyped calls use agent.call with a method name and an argument array, and the names match the server. Completion check: each client call maps to a decorated method. Stop condition: if the client calls an undecorated method, fix the client rather than decorating the method.

4. Inspect error messages and client handling

Where: the throw statements in methods and the await sites on the client. Input: various invalid arguments. Action: confirm the client receives a promise rejected with the error message, so the text must explain the cause without revealing internals, and the client needs a branch for rejection. Completion check: each invalid input has an expected client behavior. Stop condition: rewrite any message that exposes internal structure before moving on.

5. Check the ending of streaming methods

Where: methods with streaming: true and the client's stream callbacks. Input: the StreamingResponse the method receives and the client's onChunk, onDone and onError. Action: confirm normal completion calls end, failure calls error, and each of the three client callbacks has handling. Completion check: both the success and failure paths reach an end. Stop condition: a branch that neither ends nor reports an error fails the check.

6. Confirm decorator configuration

Where: build configuration. Input: vite.config.ts and tsconfig.json. Action: if the dev server reports a SyntaxError, first add the agents/vite plugin for decorator transformation; then either extend agents/tsconfig or set target to ES2021 yourself. Neither tsconfig option replaces adding the plugin; also confirm experimentalDecorators is not set to true, because it silently breaks @callable() at runtime. Completion check: the configuration matches those items. Stop condition: if that option is enabled, turn it off first.

7. Separate client calls from internal calls

Where: Worker or other Agent code that calls the Agent. Input: those internal callers. Action: confirm they get a stub with getAgentByName() and use Durable Object RPC on public methods, which needs no decorator, so no @callable() is added just for internal use. Completion check: no internal caller depends on a decorator. Stop condition: withdraw any decoration added only for an internal caller.

Task example

This is a synthetic manual walkthrough; observations are expectations, not actual verification. Imagine a counter Agent with increment and reset. In the inventory you would expect only increment to carry @callable(). Inside it you would expect an integer check that throws on other input, and the front end would receive a promise rejected with the error message and show an invalid-argument notice. reset is undecorated, so you would expect clients cannot call it. If a separate Worker needs to reset the count, it should use getAgentByName() and Durable Object RPC rather than decorating reset. All of these conclusions still need checking against real code and a real run.

Completion checks

  • Every method carrying @callable() maps to a clear user action, and each of its arguments is validated inside the method.
  • Client calls through agent.stub and agent.call all point to decorated methods, and invalid arguments reach the client as rejected promises with a handling branch.
  • Streaming methods call end on success and error on failure, and experimentalDecorators is not set to true.

Verification scope

This article is based on reading the supplied official documentation and relies only on its description of @callable(), typed stubs, streaming responses and decorator setup. Installation, login, API execution and output were not tested, and the observations in the example are not verified.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Cloudflare Agents · Cloudflare Agents documentation: cf-callable

Found an error? Tell us →