|
| 1 | +--- |
| 2 | +title: Build MCP servers with the Agents SDK |
| 3 | +description: The Agents SDK now includes built-in support for building remote MCP servers directly as part of your Agent. |
| 4 | + - agents |
| 5 | + - workers |
| 6 | +date: 2025-04-07T13:00:00Z |
| 7 | +--- |
| 8 | + |
| 9 | +import { Render, PackageManagers, TypeScriptExample } from "~/components"; |
| 10 | + |
| 11 | +The Agents SDK now includes built-in support for building remote MCP (Model Context Protocol) servers directly as part of your Agent. This allows you to easily create and manage MCP servers, without the need for additional infrastructure or configuration. |
| 12 | + |
| 13 | +The SDK includes a new `MCPAgent` class that extends the `Agent` class and allows you to expose resources and tools over the MCP protocol, as well as authorization and authentication to enable remote MCP servers. |
| 14 | + |
| 15 | +<TypeScriptExample> |
| 16 | + |
| 17 | +```ts |
| 18 | +export class MyMCP extends McpAgent<Env> { |
| 19 | + server = new McpServer({ |
| 20 | + name: "Demo", |
| 21 | + version: "1.0.0", |
| 22 | + }); |
| 23 | + |
| 24 | + async init() { |
| 25 | + this.server.resource(`counter`, `mcp://resource/counter`, (uri) => { |
| 26 | + // ... |
| 27 | + }); |
| 28 | + |
| 29 | + this.server.tool( |
| 30 | + "add", |
| 31 | + "Add two numbers together", |
| 32 | + { a: z.number(), b: z.number() }, |
| 33 | + async ({ a, b }) => { |
| 34 | + // ... |
| 35 | + }, |
| 36 | + ); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +</TypeScriptExample> |
| 42 | + |
| 43 | +See [the example](https://github.com/cloudflare/agents/tree/main/examples/mcp) for the full code and as the basis for building your own MCP servers, and the [client example](https://github.com/cloudflare/agents/tree/main/examples/mcp-client) for how to build an Agent that acts as an MCP client. |
| 44 | + |
| 45 | +To learn more, review the [announcement blog](https://blog.cloudflare.com/building-ai-agents-with-mcp-authn-authz-and-durable-objects) as part of Developer Week 2025. |
| 46 | + |
| 47 | +### Agents SDK updates |
| 48 | + |
| 49 | +We've made a number of improvements to the [Agents SDK](/agents/), including: |
| 50 | + |
| 51 | +- Support for building MCP servers with the new `MCPAgent` class. |
| 52 | +- The ability to export the current agent, request and WebSocket connection context using `import { context } from "agents"`, allowing you to minimize or avoid direct dependency injection when calling tools. |
| 53 | +- Fixed a bug that prevented query parameters from being sent to the Agent server from the `useAgent` React hook. |
| 54 | +- Automatically converting the `agent` name in `useAgent` or `useAgentChat` to kebab-case to ensure it matches the naming convention expected by [`routeAgentRequest`](/agents/api-reference/calling-agents/#calling-your-agent). |
| 55 | + |
| 56 | +To install or update the Agents SDK, run `npm i agents@latest` in an existing project, or explore the `agents-starter` project: |
| 57 | + |
| 58 | +```sh |
| 59 | +npm create cloudflare@latest -- --template cloudflare/agents-starter |
| 60 | +``` |
| 61 | + |
| 62 | +See the full release notes and changelog [on the Agents SDK repository](https://github.com/cloudflare/agents/blob/main/packages/agents/CHANGELOG.md) and |
0 commit comments