Skip to content

Commit b9bc94d

Browse files
authored
MCP Agent docs (#21115)
1 parent f480724 commit b9bc94d

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
pcx_content_type: concept
3+
title: McpAgent — API Reference
4+
sidebar:
5+
order: 6
6+
---
7+
8+
import { Render, TypeScriptExample } from "~/components";
9+
10+
When you build MCP Servers on Cloudflare, you extend the [`McpAgent` class](https://github.com/cloudflare/agents/blob/5881c5d23a7f4580600029f69307cfc94743e6b8/packages/agents/src/mcp.ts), from the Agents SDK, like this:
11+
12+
<TypeScriptExample>
13+
14+
```ts title="src/index.ts"
15+
import { McpAgent } from "agents/mcp";
16+
import { DurableMCP } from "@cloudflare/model-context-protocol";
17+
18+
export class MyMCP extends McpAgent {
19+
server = new McpServer({ name: "Demo", version: "1.0.0" });
20+
21+
async init() {
22+
this.server.tool(
23+
"add",
24+
{ a: z.number(), b: z.number() },
25+
async ({ a, b }) => ({
26+
content: [{ type: "text", text: String(a + b) }],
27+
}),
28+
);
29+
}
30+
}
31+
```
32+
33+
</TypeScriptExample>
34+
35+
This means that each instance of your MCP server has its own durable state, backed by a [Durable Object](/durable-objects/), with its own [SQL database](/agents/api-reference/store-and-sync-state).
36+
37+
Your MCP server doesn't necessarily have to be an Agent. You can build MCP servers that are stateless, and just add [tools](/agents/model-context-protocol/tools) to your MCP server using the `@modelcontextprotocol/typescript-sdk` package.
38+
39+
But if you want your MCP server to:
40+
41+
- remember previous tool calls, and responses it provided
42+
- provide a game to the MCP client, remembering the state of the game board, previous moves, and the score
43+
- cache the state of a previous external API call, so that subsequent tool calls can reuse it
44+
- do anything that an Agent can do, but allow MCP clients to communicate with it
45+
46+
You can use the following APIs in order to do so.
47+
48+
### State synchronization APIs
49+
50+
The `McpAgent` class makes the following subset of methods from the [Agents SDK](/agents/api-reference/agents-api/) available:
51+
52+
- [`state`](/agents/api-reference/store-and-sync-state/)
53+
- [`initialState`](/agents/api-reference/store-and-sync-state/#set-the-initial-state-for-an-agent)
54+
- [`setState`](/agents/api-reference/store-and-sync-state/)
55+
- [`onStateUpdate`](/agents/api-reference/store-and-sync-state/#synchronizing-state)
56+
- [`sql`](/agents/api-reference/agents-api/#sql-api)
57+
58+
:::note[State resets after the session ends]
59+
Currently, each client session is backed by an instance of the `McpAgent` class. This is handled automatically for you, as shown in the [getting started guide](/agents/guides/remote-mcp-server). This means that when the same client reconnects, they will start a new session, and the state will be reset.
60+
:::
61+
62+
For example, the following code implements an MCP server that remembers a counter value, and updates the counter when the `add` tool is called:
63+
64+
<TypeScriptExample>
65+
66+
```ts title="src/index.ts"
67+
import { McpAgent } from "agents/mcp";
68+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
69+
import { z } from "zod";
70+
71+
type State = { counter: number };
72+
73+
export class MyMCP extends McpAgent<Env, State, {}> {
74+
server = new McpServer({
75+
name: "Demo",
76+
version: "1.0.0",
77+
});
78+
79+
initialState: State = {
80+
counter: 1,
81+
};
82+
83+
async init() {
84+
this.server.resource(`counter`, `mcp://resource/counter`, (uri) => {
85+
return {
86+
contents: [{ uri: uri.href, text: String(this.state.counter) }],
87+
};
88+
});
89+
90+
this.server.tool(
91+
"add",
92+
"Add to the counter, stored in the MCP",
93+
{ a: z.number() },
94+
async ({ a }) => {
95+
this.setState({ ...this.state, counter: this.state.counter + a });
96+
97+
return {
98+
content: [
99+
{
100+
type: "text",
101+
text: String(`Added ${a}, total is now ${this.state.counter}`),
102+
},
103+
],
104+
};
105+
},
106+
);
107+
}
108+
109+
onStateUpdate(state: State) {
110+
console.log({ stateUpdate: state });
111+
}
112+
}
113+
```
114+
115+
</TypeScriptExample>
116+
117+
### Not yet supported APIs
118+
119+
The following APIs from the Agents SDK are not yet available on `McpAgent`:
120+
121+
- [WebSocket APIs](/agents/api-reference/websockets/) (`onMessage`, `onError`, `onClose`, `onConnect`)
122+
- [Scheduling APIs](/agents/api-reference/schedule-tasks/) `this.schedule`

0 commit comments

Comments
 (0)