Skip to content

Commit ffbc9f5

Browse files
committed
PCX edit
1 parent 2333a18 commit ffbc9f5

File tree

3 files changed

+142
-128
lines changed

3 files changed

+142
-128
lines changed

src/content/docs/agents/guides/connect-mcp-client.mdx

Lines changed: 134 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar:
77
order: 5
88
---
99

10-
import { Render, TypeScriptExample, PackageManagers } from "~/components";
10+
import { Render, TypeScriptExample, PackageManagers, Steps } from "~/components";
1111

1212
Your Agent can connect to external [Model Context Protocol (MCP)](https://modelcontextprotocol.io) servers to access their tools and extend your Agent's capabilities. In this tutorial, you'll create an Agent that connects to an MCP server and uses one of its tools.
1313

@@ -24,179 +24,190 @@ An MCP server to connect to (or use the public example in this tutorial).
2424

2525
## 1. Create a basic Agent
2626

27-
Create a new Agent project using the hello-world template:
27+
<Steps>
2828

29-
<PackageManagers
30-
type="create"
31-
pkg="cloudflare@latest"
32-
args={"my-mcp-client --template=cloudflare/ai/demos/hello-world"}
33-
/>
29+
1. Create a new Agent project using the `hello-world` template:
3430

35-
Move into the project directory:
31+
<PackageManagers
32+
type="create"
33+
pkg="cloudflare@latest"
34+
args={"my-mcp-client --template=cloudflare/ai/demos/hello-world"}
35+
/>
3636

37-
```sh
38-
cd my-mcp-client
39-
```
37+
2. Move into the project directory:
4038

41-
Your Agent is ready! The template includes a minimal Agent in `src/index.ts`:
39+
```sh
40+
cd my-mcp-client
41+
```
4242

43-
<TypeScriptExample>
43+
Your Agent is ready! The template includes a minimal Agent in `src/index.ts`:
4444

45-
```ts title="src/index.ts"
46-
import { Agent, type AgentNamespace, routeAgentRequest } from "agents";
45+
<TypeScriptExample>
4746

48-
type Env = {
49-
HelloAgent: AgentNamespace<HelloAgent>;
50-
};
47+
```ts title="src/index.ts"
48+
import { Agent, type AgentNamespace, routeAgentRequest } from "agents";
5149

52-
export class HelloAgent extends Agent<Env, never> {
53-
async onRequest(request: Request): Promise<Response> {
54-
return new Response("Hello, Agent!", { status: 200 });
55-
}
56-
}
50+
type Env = {
51+
HelloAgent: AgentNamespace<HelloAgent>;
52+
};
5753

58-
export default {
59-
async fetch(request: Request, env: Env) {
60-
return (
61-
(await routeAgentRequest(request, env, { cors: true })) ||
62-
new Response("Not found", { status: 404 })
63-
);
64-
},
65-
} satisfies ExportedHandler<Env>;
66-
```
54+
export class HelloAgent extends Agent<Env, never> {
55+
async onRequest(request: Request): Promise<Response> {
56+
return new Response("Hello, Agent!", { status: 200 });
57+
}
58+
}
6759

68-
</TypeScriptExample>
60+
export default {
61+
async fetch(request: Request, env: Env) {
62+
return (
63+
(await routeAgentRequest(request, env, { cors: true })) ||
64+
new Response("Not found", { status: 404 })
65+
);
66+
},
67+
} satisfies ExportedHandler<Env>;
68+
```
69+
</TypeScriptExample>
70+
</Steps>
6971

7072
## 2. Add MCP connection endpoint
7173

72-
Add an endpoint to connect to MCP servers. Update your Agent class in `src/index.ts`:
74+
<Steps>
75+
76+
1. Add an endpoint to connect to MCP servers. Update your Agent class in `src/index.ts`:
77+
78+
<TypeScriptExample>
7379

74-
<TypeScriptExample>
80+
```ts title="src/index.ts"
81+
export class HelloAgent extends Agent<Env, never> {
82+
async onRequest(request: Request): Promise<Response> {
83+
const url = new URL(request.url);
7584

76-
```ts title="src/index.ts"
77-
export class HelloAgent extends Agent<Env, never> {
78-
async onRequest(request: Request): Promise<Response> {
79-
const url = new URL(request.url);
85+
// Connect to an MCP server
86+
if (url.pathname.endsWith("add-mcp") && request.method === "POST") {
87+
const { serverUrl, name } = (await request.json()) as {
88+
serverUrl: string;
89+
name: string;
90+
};
8091

81-
// Connect to an MCP server
82-
if (url.pathname.endsWith("add-mcp") && request.method === "POST") {
83-
const { serverUrl, name } = (await request.json()) as {
84-
serverUrl: string;
85-
name: string;
86-
};
92+
const { id, authUrl } = await this.addMcpServer(name, serverUrl);
8793

88-
const { id, authUrl } = await this.addMcpServer(name, serverUrl);
94+
if (authUrl) {
95+
// OAuth required - return auth URL
96+
return new Response(
97+
JSON.stringify({ serverId: id, authUrl }),
98+
{ headers: { "Content-Type": "application/json" } },
99+
);
100+
}
89101

90-
if (authUrl) {
91-
// OAuth required - return auth URL
92102
return new Response(
93-
JSON.stringify({ serverId: id, authUrl }),
103+
JSON.stringify({ serverId: id, status: "connected" }),
94104
{ headers: { "Content-Type": "application/json" } },
95105
);
96106
}
97107

98-
return new Response(
99-
JSON.stringify({ serverId: id, status: "connected" }),
100-
{ headers: { "Content-Type": "application/json" } },
101-
);
108+
return new Response("Not found", { status: 404 });
102109
}
103-
104-
return new Response("Not found", { status: 404 });
105110
}
106-
}
107-
```
111+
```
108112

109-
</TypeScriptExample>
113+
</TypeScriptExample>
114+
</Steps>
110115

111116
The `addMcpServer()` method connects to an MCP server. If the server requires OAuth authentication, it returns an `authUrl` that users must visit to complete authorization.
112117

113118
## 3. Test the connection
114119

115-
Start your development server:
120+
<Steps>
121+
122+
1. Start your development server:
116123

117-
```sh
118-
npm start
119-
```
124+
```sh
125+
npm start
126+
```
120127

121-
In a new terminal, connect to an MCP server (using a public example):
128+
2. In a new terminal, connect to an MCP server (using a public example):
122129

123-
```sh
124-
curl -X POST http://localhost:8788/agents/hello-agent/default/add-mcp \
125-
-H "Content-Type: application/json" \
126-
-d '{
127-
"serverUrl": "https://docs.mcp.cloudflare.com/mcp",
128-
"name": "Example Server"
129-
}'
130-
```
130+
```sh
131+
curl -X POST http://localhost:8788/agents/hello-agent/default/add-mcp \
132+
-H "Content-Type: application/json" \
133+
-d '{
134+
"serverUrl": "https://docs.mcp.cloudflare.com/mcp",
135+
"name": "Example Server"
136+
}'
137+
```
131138

132-
You should see a response with the server ID:
139+
You should see a response with the server ID:
140+
141+
```json
142+
{
143+
"serverId": "example-server-id",
144+
"status": "connected"
145+
}
146+
```
133147

134-
```json
135-
{
136-
"serverId": "example-server-id",
137-
"status": "connected"
138-
}
139-
```
148+
</Steps>
140149

141150
## 4. List available tools
142151

143-
Add an endpoint to see which tools are available from connected servers:
152+
<Steps>
144153

145-
<TypeScriptExample>
154+
1. Add an endpoint to see which tools are available from connected servers:
146155

147-
```ts title="src/index.ts"
148-
export class HelloAgent extends Agent<Env, never> {
149-
async onRequest(request: Request): Promise<Response> {
150-
const url = new URL(request.url);
156+
<TypeScriptExample>
151157

152-
// ... previous add-mcp endpoint ...
158+
```ts title="src/index.ts"
159+
export class HelloAgent extends Agent<Env, never> {
160+
async onRequest(request: Request): Promise<Response> {
161+
const url = new URL(request.url);
153162

154-
// List MCP state (servers, tools, etc)
155-
if (url.pathname.endsWith("mcp-state") && request.method === "GET") {
156-
const mcpState = this.getMcpServers();
163+
// ... previous add-mcp endpoint ...
157164

158-
return new Response(JSON.stringify(mcpState, null, 2), {
159-
headers: { "Content-Type": "application/json" },
160-
});
161-
}
165+
// List MCP state (servers, tools, etc)
166+
if (url.pathname.endsWith("mcp-state") && request.method === "GET") {
167+
const mcpState = this.getMcpServers();
162168

163-
return new Response("Not found", { status: 404 });
164-
}
165-
}
166-
```
169+
return new Response(JSON.stringify(mcpState, null, 2), {
170+
headers: { "Content-Type": "application/json" },
171+
});
172+
}
167173

168-
</TypeScriptExample>
174+
return new Response("Not found", { status: 404 });
175+
}
176+
}
177+
```
178+
</TypeScriptExample>
169179

170-
Test it:
180+
2. Test it:
171181

172-
```sh
173-
curl http://localhost:8788/agents/hello-agent/default/mcp-state
174-
```
182+
```sh
183+
curl http://localhost:8788/agents/hello-agent/default/mcp-state
184+
```
175185

176-
You'll see all connected servers, their connection states, and available tools:
186+
You'll see all connected servers, their connection states, and available tools:
177187

178-
```json
179-
{
180-
"servers": {
181-
"example-server-id": {
182-
"name": "Example Server",
183-
"state": "ready",
184-
"server_url": "https://docs.mcp.cloudflare.com/mcp",
185-
...
186-
}
187-
},
188-
"tools": [
189-
{
190-
"name": "add",
191-
"description": "Add two numbers",
192-
"serverId": "example-server-id",
193-
...
194-
}
195-
]
196-
}
197-
```
188+
```json
189+
{
190+
"servers": {
191+
"example-server-id": {
192+
"name": "Example Server",
193+
"state": "ready",
194+
"server_url": "https://docs.mcp.cloudflare.com/mcp",
195+
...
196+
}
197+
},
198+
"tools": [
199+
{
200+
"name": "add",
201+
"description": "Add two numbers",
202+
"serverId": "example-server-id",
203+
...
204+
}
205+
]
206+
}
207+
```
208+
</Steps>
198209

199-
## What you built
210+
## Summary
200211

201212
You created an Agent that can:
202213
- Connect to external MCP servers dynamically

src/content/docs/agents/guides/oauth-mcp-client.mdx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ export class ObservabilityAgent extends Agent<Env, never> {
6161

6262
Your user is automatically redirected to the provider's OAuth page to authorize access.
6363

64-
**Alternative approaches**: Instead of automatic redirect, you can present the `authUrl` to your user as:
64+
### Alternative approaches
65+
66+
Instead of an automatic redirect, you can also present the `authUrl` to your user as a:
67+
6568
- **Popup window**: `window.open(authUrl, '_blank', 'width=600,height=700')` (for dashboard-style apps)
6669
- **Clickable link**: Display as a button or link (for API documentation or multi-step flows)
6770
- **Deep link**: Use custom URL schemes for mobile apps
@@ -217,7 +220,7 @@ export class MyAgent extends Agent<Env, never> {
217220

218221
</TypeScriptExample>
219222

220-
Connection states: `authenticating` (needs OAuth) `connecting` (completing setup) `ready` (available for use)
223+
Connection states: `authenticating` (needs OAuth) > `connecting` (completing setup) > `ready` (available for use)
221224

222225
## Handle authentication failures
223226

src/content/docs/agents/model-context-protocol/mcp-client-api.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Connections persist in the Agent's [SQL storage](/agents/api-reference/store-and
4848

4949
## Agent MCP Client Methods
5050

51-
### addMcpServer()
51+
### `addMcpServer()`
5252

5353
Add a connection to an MCP server and make its tools available to your Agent.
5454

@@ -121,7 +121,7 @@ If the MCP server requires OAuth authentication, `authUrl` will be returned for
121121
- [Transport options](/agents/model-context-protocol/transport)
122122
- [removeMcpServer()](#removemcpserver)
123123

124-
### removeMcpServer()
124+
### `removeMcpServer()`
125125

126126
Disconnect from an MCP server and clean up its resources.
127127

@@ -160,7 +160,7 @@ export class MyAgent extends Agent<Env, never> {
160160

161161
Disconnects from the MCP server, removes all related resources, and deletes the server record from storage.
162162

163-
### getMcpServers()
163+
### `getMcpServers()`
164164

165165
Get the current state of all MCP server connections.
166166

0 commit comments

Comments
 (0)