How Routing Works
When a request comes in,routeAgentRequest() examines the URL and routes it to the appropriate agent instance:
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
BothrouteAgentRequest() 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: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:
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)
ThebasePath option lets clients connect to any URL path:
- 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 usingbasePath, the server must handle routing:
Receiving the Instance Identity (Client-Side)
When usingbasePath, 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
Troubleshooting
”Agent namespace not found”
The error message lists available agents. Check:- Agent class is exported from your entry point
- Class name in code matches
class_nameinwrangler.jsonc - URL uses correct kebab-case name
Request returns 404
- Verify the URL pattern:
/agents/{agent-name}/{instance-name} - Check that
routeAgentRequest()is called before your 404 handler - Ensure the response from
routeAgentRequest()is returned (not just called)
WebSocket won’t connect
- Don’t modify the response from
routeAgentRequest()for WebSocket upgrades - Ensure CORS is enabled if connecting from a different origin
- Check browser dev tools for the actual error
basePath not working
- Ensure your Worker handles the custom path and forwards to the agent
- Use
getAgentByName()+agent.fetch(request)to forward requests - The
agentparameter is still required but ignored whenbasePathis set - 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
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
onStartPromise<DurableObjectStub<T>> - Typed stub for calling agent methods or forwarding requests