Skip to content

Commit 7d240f5

Browse files
committed
agents
1 parent 5e39a90 commit 7d240f5

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

public/_redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
/agents/examples/schedule-tasks/ /agents/api-reference/schedule-tasks/ 301
106106
/agents/examples/using-ai-models/ /agents/api-reference/using-ai-models/ 301
107107
/agents/examples/websockets/ /agents/api-reference/websockets/ 301
108+
/agents/examples/sdk/ /agents/api-reference/creating-agents/ 301
108109

109110
# ai
110111
/ai/ /use-cases/ai/ 301

src/content/docs/agents/api-reference/calling-agents.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,57 @@ export class MyAgent extends Agent<Env> {
100100
```
101101
</TypeScriptExample>
102102

103+
### Authenticating Agents
104+
105+
When building and deploying Agents using the Agents SDK, you will often want to authenticate clients before passing requests to an Agent in order to restrict who the Agent will call, authorize specific users for specific Agents, and/or to limit who can access administrative or debug APIs exposed by an Agent.
106+
107+
As best practices:
108+
109+
* Handle authentication in your Workers code, before you invoke your Agent.
110+
* Use the built-in hooks when using the `routeAgentRequest` helper - `on
111+
* Use your preferred router (such as Hono) and authentication middleware or provider to apply custom authentication schemes before calling an Agent.
112+
113+
The `routeAgentRequest` helper documented earlier in this guide exposes two useful hooks (`onBeforeConnect`, `onBeforeRequest`) that allow you to apply custom logic before creating or retrieving an Agent:
114+
115+
<TypeScriptExample>
116+
117+
```ts
118+
import { Agent, AgentNamespace, getAgentByName, routeAgentRequest } from 'agents-sdk';
119+
120+
interface Env {
121+
MyAgent: AgentNamespace<MyAgent>;
122+
}
123+
124+
export default {
125+
async fetch(request, env, ctx): Promise<Response> {
126+
// Use the onBeforeConnect and onBeforeRequest hooks to authenticate clients
127+
// or run logic before handling a
128+
return (
129+
(await routeAgentRequest(request, env, {
130+
// Run logic before a WebSocket client connects
131+
onBeforeConnect: (request) => {
132+
// Your code/auth code here
133+
// You can return a Response here - e.g. a HTTP 403 Not Authorized -
134+
// which will stop further request processing and will NOT invoke the
135+
// Agent.
136+
// return Response.json({"error": "not authorized"}, { status: 403 })
137+
},
138+
// Run logic before a HTTP client clients
139+
onBeforeRequest: (request) => {
140+
// Your code/auth code here
141+
// Returning nothing will result in the call to the Agent continuing
142+
},
143+
// Prepend a prefix for how your Agents are named here
144+
prefix: 'name-prefix-here',
145+
})) || Response.json({ msg: 'no agent here' }, { status: 404 })
146+
);
147+
148+
},
149+
} satisfies ExportedHandler<Env>;
150+
```
151+
</TypeScriptExample>
152+
153+
If you are using `getAgentByName` or the underlying Durable Objects routing API, you should authenticate incoming requests or WebSocket connections before calling `getAgentByName`.
103154

104155
### Next steps
105156

src/content/docs/agents/api-reference/sdk.mdx renamed to src/content/docs/agents/api-reference/creating-agents.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: The Agent API
2+
title: Creating Agents
33
pcx_content_type: concept
44
sidebar:
55
order: 1

0 commit comments

Comments
 (0)