Build and deploy Remote Model Context Protocol (MCP) servers to Cloudflare

Brendan Irvine-Broque
14 min readintermediate
--
View Original

Overview

The article discusses how to build and deploy Remote Model Context Protocol (MCP) servers on Cloudflare, highlighting the new capabilities that allow these servers to be accessible over the internet. It outlines key components such as the workers-oauth-provider for authorization, the McpAgent for remote transport, and tools for connecting existing MCP clients to remote servers.

What You'll Learn

1

How to deploy a Remote Model Context Protocol server to Cloudflare

2

Why using OAuth for authentication and authorization is essential for MCP servers

3

How to use McpAgent for remote transport support in MCP applications

4

When to implement stateful MCP servers using Durable Objects

Prerequisites & Requirements

  • Understanding of OAuth and authorization flows
  • Familiarity with Cloudflare Workers and the Agents SDK

Key Questions Answered

What are the new features added to Cloudflare for building remote MCP servers?
Cloudflare has introduced several features for building remote MCP servers, including the workers-oauth-provider for easy authorization, the McpAgent class for handling remote transport, and the mcp-remote adapter to connect existing MCP clients to remote servers. These features simplify the deployment and integration of MCP servers on the internet.
How does the workers-oauth-provider facilitate authentication for MCP servers?
The workers-oauth-provider acts as an OAuth 2.1 Provider library for Cloudflare Workers, allowing MCP servers to handle user authentication seamlessly. It manages user login and token issuance without requiring developers to handle tokens directly, ensuring a secure and compliant authentication process.
What is the role of McpAgent in remote MCP server communication?
McpAgent is a class within the Agents SDK that facilitates remote transport for MCP servers. It utilizes Durable Objects to maintain persistent connections, allowing MCP clients to send server-sent events (SSE) to the server, enabling real-time communication without additional coding for transport or serialization.
Why do MCP servers issue their own tokens instead of using upstream provider tokens?
MCP servers issue their own tokens to enhance security and control over access. By storing encrypted access tokens and issuing separate tokens to clients, MCP servers can enforce granular permissions and limit the scope of access, reducing the risk of token compromise affecting broader services.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Backend
Cloudflare Workers
Used to deploy and manage MCP servers with built-in OAuth capabilities.
Protocol
Mcp (model Context Protocol)
Enables communication between AI agents and external services.
Authentication
Oauth
Standard protocol for secure authorization in MCP servers.
Backend
Durable Objects
Provides stateful capabilities for MCP servers.

Key Actionable Insights

1
Utilize the workers-oauth-provider to streamline authentication for your MCP servers.
This library simplifies the process of implementing OAuth, allowing you to focus on building features rather than managing authentication complexities.
2
Leverage McpAgent for efficient remote transport in your MCP applications.
Using McpAgent allows you to handle real-time communication seamlessly, which is crucial for applications requiring immediate responses and interactions.
3
Consider building stateful MCP servers using Durable Objects for enhanced functionality.
Stateful servers can manage user sessions and data persistence, opening up possibilities for more complex applications like interactive tools and workflows.

Common Pitfalls

1
Neglecting to implement proper authentication can lead to security vulnerabilities.
Without a robust authentication mechanism, unauthorized users might gain access to sensitive operations within your MCP server, potentially leading to data breaches or misuse of the service.
2
Failing to manage state effectively in MCP servers can result in loss of user context.
If your MCP server does not maintain user session states, it may lead to inconsistent behavior and a poor user experience, as users might have to re-authenticate or lose their progress.

Related Concepts

Oauth 2.1
Durable Objects
Remote Procedure Calls (rpc)
Server-sent Events (sse)