Cloudflare Code Mode MCP: Browser Automation Guide
In this article
This guide shows you how to use Cloudflare's Code Mode MCP (Model Context Protocol) server to automate browser tasks—like filling forms, clicking buttons, and extracting data—using natural language instructions. You'll need a Cloudflare account, Node.js installed, and a basic understanding of command-line tools. No prior MCP experience is required.
What You'll Need
- A Cloudflare account (free tier works)
- Node.js 18 or later installed on your machine
- An MCP-compatible client (e.g., Claude Desktop, Cursor, or any MCP client)
- Cloudflare CLI (
npm install -g @cloudflare/cli)
Step-by-Step
- Install the Cloudflare CLI – Open your terminal and run
npm install -g @cloudflare/cli. Verify withcloudflare --version. - Authenticate with Cloudflare – Run
cloudflare loginand follow the browser prompt to log in. This links your local environment to your account. - Enable Code Mode MCP – In your project directory, run
cloudflare mcp add code-mode. This registers the MCP server with your client. If you're using a custom client, note the server endpoint printed. - Configure your MCP client – In your client's MCP settings (e.g.,
claude_desktop_config.json), add the server with the commandcloudflare mcp run code-mode. Restart the client. - Test the connection – Send a simple prompt like "Open example.com and tell me the title." The MCP server should respond with the page title.
- Perform basic automation – Use natural language commands: "Go to google.com, search for 'weather', and return the first result." The MCP tool will execute the browser actions.
- Handle forms and clicks – For form filling, say: "Navigate to the login page, fill the username field with 'testuser', password with 'pass123', and click the submit button." The MCP server uses selectors based on element names or IDs.
- Extract data – Ask to "Scrape all product names and prices from this page" and the MCP will return structured data (JSON).
- Use multi-step workflows – Combine actions: "Go to site, log in, then navigate to dashboard and download the CSV." The MCP maintains a session.
- Stop the session – When done, send "close browser" or let the session time out. You can also manually kill the MCP process.
Common Mistakes
- Not authenticating first – The MCP server requires a valid Cloudflare session. If you skip
cloudflare login, you'll get authentication errors. - Using vague selectors – Saying "click the button" may fail if there are multiple buttons. Be specific: "click the 'Submit' button with class 'btn-primary'."
- Ignoring page load times – The MCP waits for page load, but if a page uses heavy JavaScript, you may need to add "wait for 2 seconds" in your prompt.
- Assuming it works with all sites – Some sites block automated browsers. You may need to add headers or use a different user agent.
Tips
- Use descriptive language: "click the red button" works if the button has a red class or text.
- Break complex tasks into smaller steps to debug easier.
- Check the MCP logs (usually in your client's console) for detailed errors.
- For dynamic content, add explicit waits: "wait for 3 seconds" after navigation.
- Use the MCP's built-in screenshot tool to verify state: "take a screenshot" and review it.
Troubleshooting
- Connection refused – Ensure the MCP server is running. Restart your client and try again.
- Authentication expired – Run
cloudflare loginagain to refresh your token. - Element not found – The page structure may have changed. Try using a more specific selector or add a wait.
- Browser doesn't open – Some MCP clients run headless by default. Set
headless: falsein your client config if you need to see the browser. - Timeout errors – Increase the timeout in your client settings, or add explicit waits in your prompt.
- Site blocks automation – Try adding a user agent string via the MCP tool: "set user agent to Chrome on Windows" before navigating.
Frequently Asked Questions
What is Code Mode MCP?
It's a server that lets you control a browser through natural language, using Cloudflare's infrastructure to handle the browser session.
Do I need to write code?
No. You use plain English prompts. The MCP translates them into browser actions.
Can I use it with any MCP client?
Yes, as long as the client supports MCP protocol. Popular clients include Claude Desktop and Cursor.
Is it free?
Cloudflare offers a free tier for MCP usage, but there may be rate limits. Check your dashboard for current limits.
Can I automate login-protected sites?
Yes, you can provide credentials in your prompt, but be cautious about storing sensitive data in prompts.
Does it work with iframes or pop-ups?
Yes, but you may need to explicitly mention them: "switch to the iframe" or "handle the pop-up."