Skip to main content
This guide explains how requests are routed to agents, how naming works, and patterns for organizing your agents.

How Routing Works

When a request comes in, routeAgentRequest() examines the URL and routes it to the appropriate agent instance:
Example URLs:

Name Resolution

Agent class names are automatically converted to kebab-case for URLs: The router matches both the original name and kebab-case version, so these all work:
  • useAgent({ agent: "Counter" })/agents/counter/...
  • useAgent({ agent: "counter" })/agents/counter/...

Basic Usage

routeAgentRequest()

The main entry point for agent routing. Handles both HTTP requests and WebSocket upgrades:

getAgentByName()

Get a specific agent instance for server-side RPC calls or request forwarding:

Instance Naming Patterns

The instance name (the last part of the URL) determines which agent instance handles the request. Each unique name gets its own isolated agent with its own state.

Per-User Agents

Each user gets their own agent instance:

Shared Rooms

Multiple users share the same agent instance:

Global Singleton

A single instance for the entire application:

Dynamic Naming

Generate instance names based on context:

Routing Options

Both routeAgentRequest() and getAgentByName() accept options for customizing routing behavior.

CORS

For cross-origin requests (common when your frontend is on a different domain):

Location Hints

For latency-sensitive applications, hint where the agent should run:
Available location hints: wnam, enam, sam, weur, eeur, apac, oc, afr, me

Jurisdiction

For data residency requirements:

Props

Since agents are instantiated by the runtime rather than constructed directly, props provides a way to pass initialization arguments:
Props are passed to the agent’s onStart lifecycle method:
For McpAgent, props are automatically stored and accessible via this.props.

Hooks

routeAgentRequest supports hooks for intercepting requests before they reach agents:

Custom URL Routing

For advanced use cases where you need control over the URL structure, you can bypass the default /agents/{agent}/{name} pattern.

Using basePath (Client-Side)

The basePath option lets clients connect to any URL path:
This is useful when:
  • You want clean URLs without the /agents/ prefix
  • The instance name is determined server-side (e.g., from auth/session)
  • You’re integrating with an existing URL structure

Server-Side Instance Selection

When using basePath, the server must handle routing:

Receiving the Instance Identity (Client-Side)

When using basePath, the client doesn’t know which instance it connected to until the server tells it:

Multiple Agents

You can have multiple agent classes in one project. Each gets its own namespace:
wrangler.jsonc
Each agent is accessed via its own path:

Troubleshooting

”Agent namespace not found”

The error message lists available agents. Check:
  1. Agent class is exported from your entry point
  2. Class name in code matches class_name in wrangler.jsonc
  3. URL uses correct kebab-case name

Request returns 404

  1. Verify the URL pattern: /agents/{agent-name}/{instance-name}
  2. Check that routeAgentRequest() is called before your 404 handler
  3. Ensure the response from routeAgentRequest() is returned (not just called)

WebSocket won’t connect

  1. Don’t modify the response from routeAgentRequest() for WebSocket upgrades
  2. Ensure CORS is enabled if connecting from a different origin
  3. Check browser dev tools for the actual error

basePath not working

  1. Ensure your Worker handles the custom path and forwards to the agent
  2. Use getAgentByName() + agent.fetch(request) to forward requests
  3. The agent parameter is still required but ignored when basePath is set
  4. Check that the server-side route matches the client’s basePath

API Reference

routeAgentRequest(request, env, options?)

Routes a request to the appropriate agent.
Request
required
The incoming request
Env
required
Environment with agent bindings
boolean | HeadersInit
Enable CORS headers
Record<string, unknown>
Props passed to whichever agent handles the request
string
Preferred location for agent instances
string
Data jurisdiction for agent instances
Function
Callback before WebSocket connections
Function
Callback before HTTP requests
Returns: Promise<Response | undefined> - Response if matched, undefined if no agent route

getAgentByName(namespace, name, options?)

Get an agent instance by name for server-side RPC or request forwarding.
DurableObjectNamespace<T>
required
Agent binding from env
string
required
Instance name
string
Preferred location
string
Data jurisdiction
Record<string, unknown>
Initialization properties for onStart
Returns: Promise<DurableObjectStub<T>> - Typed stub for calling agent methods or forwarding requests