
38:01
Build a Google Authenticated CloudFlare MCP Worker for ChatGPT - Step By Step
MCP Quest
Overview
This video guides viewers through building a custom MCP server using Cloudflare Workers, integrating Google authentication, and connecting it to a D1 SQL database. The server is then made accessible to ChatGPT, allowing users to query employee data through a natural language interface. The tutorial covers setting up the Cloudflare environment, configuring the worker, managing database connections, and integrating the custom server with ChatGPT's developer mode.
How was this?
Save this permanently with flashcards, quizzes, and AI chat
Chapters
- MCP (Meta-Communication Protocol) is an emerging standard for AI applications, adopted by major players like OpenAI and Anthropic.
- Understanding MCP is crucial for developers as it's becoming a foundational technology for AI integrations, akin to HTTP's role in the early 2000s.
- Prerequisites include knowledge of TypeScript/JavaScript, basic Git operations, AI coding assistants (like Code-Llama or GitHub Copilot), and SQL for database interaction.
This chapter establishes the importance of MCP in the current AI landscape and outlines the necessary technical skills, ensuring learners are prepared for the practical steps ahead.
The speaker likens MCP's importance for AI in 2025 to HTTP's importance for the web in 2000.
- Begin by creating a new Cloudflare MCP worker project using the Wrangler CLI, selecting a template (e.g., from `create-cloudflare`).
- Configure the `wrangler.toml` file, commenting out initial configurations like KV namespaces that will be set up later.
- Deploy the initial worker to Cloudflare using `wrangler deploy` and log in via `wrangler login` to authenticate with your Cloudflare account.
- Bind a Workers KV (Key-Value) namespace to the worker, naming it `okv` to ensure compatibility with the authentication library.
This section lays the groundwork for the custom server by setting up the basic Cloudflare Worker environment and essential bindings, preparing it for further customization.
Using `npm create cloudflare@latest` to scaffold a new worker project.
- Set up Google OAuth 2.0 credentials in the Google Cloud Console, creating an OAuth client ID for a web application.
- Crucially, configure the authorized redirect URIs to include both the local development server (`http://localhost:8788/callback`) and the deployed Cloudflare worker's callback URL (`https://your-worker-url.workers.dev/callback`).
- Store the obtained Google Client ID and Client Secret securely in the Cloudflare Workers KV namespace (`okv`).
- Modify the worker's code (e.g., `googleHandler.ts`) to retrieve these secrets from KV instead of environment variables, using `okv.get()` and handling asynchronous operations with `await`.
This enables secure access to your MCP server, ensuring only authorized users can interact with it, and demonstrates how to manage sensitive credentials using Cloudflare's KV store.
Creating a Google OAuth client ID and then storing the `client_id` and `client_secret` in the `okv` KV namespace.
- Create a D1 database instance within Cloudflare, naming it appropriately (e.g., `SQL employees`).
- Bind this D1 database to your worker, using a specific name like `employees` (case-sensitive) for the binding.
- Update the `wrangler.toml` file to include the D1 database binding, referencing its ID and ensuring `remote_true` is set for local development access.
- Import data into the D1 database using a compatible SQLite SQL file via the `wrangler d1 execute` command.
- Modify the worker's logic to interact with the D1 database, replacing placeholder tools with actual queries (e.g., listing tables, reading schemas, querying employee data).
This step adds persistent data storage and enables the MCP server to perform meaningful data retrieval and manipulation, making it a functional application.
Importing an `employees.sql` file containing 25,000 rows into the D1 database using `wrangler d1 execute --binding=SQL_employees --file=employees.sql`.
- Deploy the fully configured worker to Cloudflare using `wrangler deploy`.
- Test the remote worker's functionality by updating Postman requests to use the worker's public URL instead of `localhost`.
- Troubleshoot potential issues like 'redirect URI mismatch' by ensuring the Google OAuth application's redirect URIs include the deployed worker's callback URL.
- Verify that both authentication and database query functionalities work correctly with the remote deployment.
This ensures the MCP server is accessible globally and confirms that all components, including authentication and database access, function correctly in a live environment.
Replacing the `localhost:8788` URL in Postman with the deployed worker's URL (e.g., `https://your-worker-name.your-account.workers.dev`) to test remote access.
- Enable Developer Mode in ChatGPT settings (under Advanced) to access the 'Create' button for custom GPTs/apps.
- Create a new MCP app in ChatGPT, providing a name (e.g., 'Cloudflare MCP'), description, and the server's URL.
- Authenticate the ChatGPT connection with your Google account, which will then link to your Cloudflare MCP server.
- Once connected, you can invoke your custom MCP server from ChatGPT by typing '@' followed by the app's name, allowing natural language queries to be processed by your worker and D1 database.
This is the final step, enabling users to leverage the power of ChatGPT's natural language understanding to interact with the custom MCP server and its underlying data.
Typing '@Cloudflare MCP quest 005' in ChatGPT to select the custom server and then asking a question like 'How many female employees were born before 1970?'
Key takeaways
- MCP is a critical emerging standard for AI integrations, and learning it provides a significant advantage for developers.
- Cloudflare Workers provide a powerful, serverless platform for building and deploying custom APIs and backend logic.
- Securely managing secrets and credentials is vital; Cloudflare Workers KV is an effective solution for storing sensitive information.
- D1 provides a performant, SQL-compatible database solution integrated directly within the Cloudflare ecosystem.
- OAuth 2.0, specifically Google OAuth, is a standard method for user authentication in web applications.
- Integrating custom MCP servers with platforms like ChatGPT allows for powerful, extended functionality through natural language interfaces.
- The process involves setting up infrastructure (Cloudflare), implementing core logic (authentication, database), and finally connecting to the user-facing application (ChatGPT).
Key terms
MCP (Meta-Communication Protocol)Cloudflare WorkersWrangler CLIWorkers KVD1 DatabaseGoogle OAuth 2.0Client IDClient SecretRedirect URIServerlessSQLite
Test your understanding
- What is the primary purpose of MCP in the context of AI development, and why is it considered important?
- How does Cloudflare Workers KV contribute to the security of your MCP server, especially when handling authentication credentials?
- Explain the role of redirect URIs in the Google OAuth 2.0 authentication flow and why they need to be configured for both local and remote environments.
- What are the key steps involved in setting up and importing data into a Cloudflare D1 database for use with a Worker?
- How can a custom MCP server, built with Cloudflare Workers, be integrated and utilized within ChatGPT?