Overview
This page provides a complete, working example of a basic MCP server that uses ATXP to charge for tool calls and is deployed on Cloudflare Workers. The code for this example implementation can be found at atxp-dev/atxp-cloudflare-mcp-server-example.Project setup
In this tutorial, we’ll build a TypeScript MCP server that exposes tools that can only be called after a successful payment is made using ATXP. We’ll use Cloudflare Workers to build and deploy a scalable MCP server that can be accessed from Goose or other MCP clients. Our MCP server will expose a simple hello_world tool that provides a personalized message, with each call costing 0.01 USDC.Initialize the project
First, we need to create a new Cloudflare Workers project using the MCP template. We’ll use the Cloudflare CLI to initialize the project.Install ATXP dependencies
We need to install the ATXP Cloudflare package to add payment capabilities:Configure the project
The project comes pre-configured with TypeScript and Cloudflare Workers settings. You should verify that yourpackage.json
includes the necessary scripts and dependencies:
package.json
Configure environment variables
Create your local environment configurationSet up how you will receive payments
In order to receive payments, you will need to set up a payment destination. You can do this by creating a free ATXP account OR by specifying a wallet address and network.Create an ATXP account
If you don’t have an ATXP account yet, create one and copy your wallet address. Once you have set up your account, you should add the environment variableFor production deployments, set
ALLOW_INSECURE_HTTP_REQUESTS_DEV_ONLY_PLEASE
to "false"
and use wrangler secret put
for sensitive values like wallet addresses instead of storing them in wrangler.jsonc
.Develop the MCP server
We’re ready to start coding our MCP server now. The template provides a basic structure, but we need to modify it to integrate ATXP payments.Set up the MCP Agent with ATXP
Replace the contents ofsrc/index.ts
with the following code that integrates ATXP payments:
index.ts
npm run dev
. However, we haven’t defined any tools yet, so it won’t be very useful.
Define the MCP tools
Now let’s add a hello_world tool that requires payment. This tool will take an optional name parameter and return a personalized greeting.index.ts
hello_world
tool. Now let’s test the payment integration by running the MCP server locally and then deploying it to Cloudflare.
Testing the MCP server locally
You can test your MCP server locally before deploying it to Cloudflare.Run the MCP server locally
Start the development server using Wrangler:http://localhost:8788/sse
.
Test with MCP Inspector
You can use the MCP Inspector to test your server:- Open the MCP Inspector in your browser at
http://localhost:5173
- Connect to your local server using the URL
http://localhost:8788/sse
- Test the
hello_world
tool to verify it works correctly
Deploying to Cloudflare
Once your MCP server is working locally, you can deploy it to Cloudflare Workers.Deploy the server
Deploy your MCP server using Wrangler:https://atxp-mcp-cloudflare-server.<YOUR_SUBDOMAIN>.workers.dev/sse
.
Configure production environment
For production deployment, follow these steps:- Set environment variables in
wrangler.jsonc
or usewrangler secret put
for sensitive values:
- Update your
wrangler.jsonc
for production settings:- Set
ALLOW_INSECURE_HTTP_REQUESTS_DEV_ONLY_PLEASE
to"false"
- Consider using
wrangler secret put
for sensitive values instead of storing them directly in the config
- Set
Connect to MCP clients
Now that your MCP server is deployed, you can connect it to MCP clients like Goose.Connect to Goose
To connect your deployed MCP server to Goose:- Go to Extensions in the Goose sidebar
- Click Add custom extension
- Provide a name for your extension (e.g., “ATXP Greeting Server”)
- Select Streamable HTTP as the type
- Enter your Cloudflare Worker URL:
https://atxp-mcp-cloudflare-server.<YOUR_SUBDOMAIN>.workers.dev/sse
- Click Add Extension
Test the payment flow
Once connected to Goose, test the payment flow:- Send a message like “Greet me with the name Alice”
- Goose will show that payment is required
- Click the payment URL to complete the payment
- Return to Goose and confirm the tool call was successful
Congratulations! You’ve successfully built and deployed a paid MCP server on Cloudflare Workers using ATXP. Your server can now handle payments and scale automatically with Cloudflare’s infrastructure.
Key differences from Express
This Cloudflare approach offers several advantages over Express:- Serverless scaling: No need to manage server infrastructure
- Global edge deployment: Reduced latency for users worldwide
- Simplified deployment: Single command deployment with Wrangler
- Built-in HTTPS: Secure connections by default
- Environment management: Integrated secrets and environment variables
@atxp/cloudflare
) handles the complexity of integrating payments into your Cloudflare Worker, providing the same requirePayment
functionality as the Express version but optimized for the Workers runtime.