You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/agents/examples/build-mcp-server.mdx
+37-36Lines changed: 37 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,44 +13,11 @@ import { Aside } from '@astrojs/starlight/components';
13
13
14
14
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) is an open standard that allows AI agents and assistants (like [Claude Desktop](https://claude.ai/download) or [Cursor](https://www.cursor.com/)) to interact with services directly. If you want users to access your service through an AI assistant, you can spin up an MCP server for your application.
15
15
16
-
### Why use Cloudflare Workers for MCP?
16
+
### Building an MCP Server on Workers
17
17
18
18
With Cloudflare Workers and the [workers-mcp](https://github.com/cloudflare/workers-mcp/) package, you can turn any API or service into an MCP server with minimal setup. Just define your API methods as TypeScript functions, and workers-mcp takes care of tool discovery, protocol handling, and request routing. Once deployed, MCP clients like Claude can connect and interact with your service automatically.
19
19
20
-
#### Example: Exposing a Weather API as an MCP server
21
-
22
-
Here’s a Cloudflare Worker that fetches weather data from an external API and exposes it as an MCP tool that Claude can call directly:
Below we will run you through an example of building an MCP server that fetches weather data from an external API and exposes it as an MCP tool that Claude can call directly:
54
21
55
22
**How it works:**
56
23
***TypeScript methods as MCP tools:** Each public method in your class is exposed as an MCP tool that agents can call. In this example, getWeather is the tool that fetches data from an external weather API.
@@ -138,11 +105,45 @@ This converts your Cloudflare Worker into an MCP server, enabling interactions w
138
105
139
106
**Note:** Every public method that is annotated with JSDoc becomes an MCP tool that is discoverable by AI assistants.
140
107
108
+
### Add data fetching to the MCP
109
+
110
+
When building an MCP, in many cases, you will need to connect to a resource or an API to take an action. To do this you can use the `fetch` method to make direct API calls, such as in the example below for grabbing the current wearther:
0 commit comments