-
Notifications
You must be signed in to change notification settings - Fork 10.2k
MCP Agent docs #21115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
MCP Agent docs #21115
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
src/content/docs/agents/model-context-protocol/mcp-agent-api.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| --- | ||
| pcx_content_type: concept | ||
| title: McpAgent — API Reference | ||
| sidebar: | ||
| order: 6 | ||
| --- | ||
|
|
||
| import { Render, TypeScriptExample } from "~/components"; | ||
|
|
||
| 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: | ||
|
|
||
| <TypeScriptExample> | ||
|
|
||
| ```ts title="src/index.ts" | ||
| import { McpAgent } from "agents/mcp"; | ||
| import { DurableMCP } from "@cloudflare/model-context-protocol"; | ||
|
|
||
| export class MyMCP extends McpAgent { | ||
| server = new McpServer({ name: "Demo", version: "1.0.0" }); | ||
|
|
||
| async init() { | ||
| this.server.tool( | ||
| "add", | ||
| { a: z.number(), b: z.number() }, | ||
| async ({ a, b }) => ({ | ||
| content: [{ type: "text", text: String(a + b) }], | ||
| }), | ||
| ); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| 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). | ||
|
|
||
| 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. | ||
|
|
||
| But if you want your MCP server to: | ||
|
|
||
| - remember previous tool calls, and responses it provided | ||
| - provide a game to the MCP client, remembering the state of the game board, previous moves, and the score | ||
| - cache the state of a previous external API call, so that subsequent tool calls can reuse it | ||
| - do anything that an Agent can do, but allow MCP clients to communicate with it | ||
|
|
||
| You can use the following APIs in order to do so. | ||
|
|
||
| ### State synchronization APIs | ||
|
|
||
| The `McpAgent` class makes the following subset of methods from the [Agents SDK](/agents/api-reference/agents-api/) available: | ||
|
|
||
| - [`state`](/agents/api-reference/store-and-sync-state/) | ||
| - [`initialState`](/agents/api-reference/store-and-sync-state/#set-the-initial-state-for-an-agent) | ||
| - [`setState`](/agents/api-reference/store-and-sync-state/) | ||
| - [`onStateUpdate`](/agents/api-reference/store-and-sync-state/#synchronizing-state) | ||
| - [`sql`](/agents/api-reference/agents-api/#sql-api) | ||
|
|
||
| :::note[State resets after the session ends] | ||
| 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. | ||
| ::: | ||
|
|
||
| For example, the following code implements an MCP server that remembers a counter value, and updates the counter when the `add` tool is called: | ||
|
|
||
| <TypeScriptExample> | ||
|
|
||
| ```ts title="src/index.ts" | ||
| import { McpAgent } from "agents/mcp"; | ||
| import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { z } from "zod"; | ||
|
|
||
| type State = { counter: number }; | ||
|
|
||
| export class MyMCP extends McpAgent<Env, State, {}> { | ||
| server = new McpServer({ | ||
| name: "Demo", | ||
| version: "1.0.0", | ||
| }); | ||
|
|
||
| initialState: State = { | ||
| counter: 1, | ||
| }; | ||
|
|
||
| async init() { | ||
| this.server.resource(`counter`, `mcp://resource/counter`, (uri) => { | ||
| return { | ||
| contents: [{ uri: uri.href, text: String(this.state.counter) }], | ||
| }; | ||
| }); | ||
|
|
||
| this.server.tool( | ||
| "add", | ||
| "Add to the counter, stored in the MCP", | ||
| { a: z.number() }, | ||
| async ({ a }) => { | ||
| this.setState({ ...this.state, counter: this.state.counter + a }); | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: "text", | ||
| text: String(`Added ${a}, total is now ${this.state.counter}`), | ||
| }, | ||
| ], | ||
| }; | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| onStateUpdate(state: State) { | ||
| console.log({ stateUpdate: state }); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| ### Not yet supported APIs | ||
|
|
||
| The following APIs from the Agents SDK are not yet available on `McpAgent`: | ||
|
|
||
| - [WebSocket APIs](/agents/api-reference/websockets/) (`onMessage`, `onError`, `onClose`, `onConnect`) | ||
| - [Scheduling APIs](/agents/api-reference/schedule-tasks/) `this.schedule` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is MCP?
Can I get a preamble like https://developers.cloudflare.com/agents/api-reference/websockets/ or https://developers.cloudflare.com/agents/api-reference/schedule-tasks/ have?