How to build custom MCP server or API for any Bubble app

Hey everyone!

Super excited to release a big new Buildprint feature today. I’m posting it here as this should be interesting to lots of people who’ve never used Buildprint before!

In case you haven’t tried it, Buildprint is the best way to build on Bubble with AI. In our testing, it is twice as fast as Bubble’s MCP, is half the cost for agents, and has significantly more powerful capabilities.

In addition, Buildprint supports:

  • Connecting your Claude Code / Codex / Cursor subscription
  • Triage, which automatically detects and fixes customer support requests
  • Radar, which reviews your merges (like CodeRabbit or Greptile for Bubble)

How to build an MCP server for Bubble

Using Buildprint, just use /mcp in your project, and Buildprint will do it for you, end to end, in one prompt.

However, for those of you that want to do it manually (as I know this is a popular ask), I wanted to share how.

Deciding auth (Token vs OAuth)

First, you need to decide which authentication methods you want to support. If exposing a public API, then API tokens are useful as they’re easy for people to integrate with. However, it is less secure as these tokens can leak. In addition, if a user wants to connect their MCP, they’ll generally have to go into the advanced setup settings of Claude/ChatGPT etc.

Instead, we tend to recommend OAuth. This means that when adding https://mcp.yourapp.com/mcp to their Claude/ChatGPT account, it will say ‘Login with [your app]’ and redirect the user to log in. This is also useful because it means that the Bubble workflows that run from the MCP server will run as that user by default, meaning privacy rules are enforced and Current User can be referenced.

Exposing an MCP/API server

The reason creating a public MCP or API in Bubble is tricky is that Bubble only supports workflow execution through the workflow API (/api/1.1/wf/[your workflow]). This gives you limited flexibility, and it’s entirely impossible to support hosting an MCP server, which must expose endpoints in a specific way (though, I believe Bubble is working on making this possible).

For this, we need some custom code to sit in front of our Bubble backend. For this guide, I’ll use Cloudflare Workers as an example, but it could be any custom service.

Note that if you use Buildprint, /mcp handles all of this for you using Buildprint Extensions, which is included in your plan.

Our custom code service will be the one that exposes the MCP server and API endpoints. This is the URL that you’ll give customers connecting to your app. When they send a request, the custom code service will receive it, and forward it to the relevant endpoint in our Bubble backend workflow. It will then receive the response from Bubble, and forward it from the user. It sits in between the user and the Bubble app - a ‘proxy’.

I won’t go into the technical details of creating an MCP server service but any decent AI agent will help you with this. The main hassle is creating your Cloudflare/AWS/Railway/whatever account and linking domains etc (Buildprint handles this for you out of the box).

Maintainability

Now, what we want to avoid is introducing duplication in our app. i.e, if you already have a frontend workflow which, for example, deletes an invoice, then you need the MCP/API to use the same logic. You don’t want two copies of the same action, because then if you update one and forget to update the other, you’ll have bugs.

For this reason, we recommend using a backend custom event for each MCP-exposed functionality. For instance, the ‘delete invoice’ custom event would take an invoice ID, and delete the invoice (and any other associated data like line items, according to your app logic). Our public Bubble backend workflow called by our worker would trigger this custom event. Similarly, any frontend that does the same would trigger this custom event. They use the same logic in both places, because it is the same functionality.

Implementing API tokens securely

OAuth is actually the easiest way to set this all up with less footguns. But if you want users to be able to connect to your Bubble app with an API key, then there’s a blueprint we recommend to avoid accidentally creating an insecure implementation. If you’re using OAuth/not giving users API tokens then you can skip this.

I’m not gonna go through every step (I’m assuming a decent understanding of Bubble) but at a high level:

First decide which type you need:

  • Personal keys act as the user who created them.
  • Team keys act as a team/organisation/account, not a person. Use these for shared integrations that should keep working when someone leaves (B2B SaaS is common use case here)

For both

  • Store only a hash of the key, not the entire key. We don’t want to store the text of a generated key and be like Do a search for API Tokens where key = X. If someone saw your database (e.g due to a privacy rule or logic misconfiguration) they would be able to use the keys. Instead, we ‘hash’ them. This is basically a one way text conversion - we can go from key → hash but not hash → key. So, if someone gives us a key, we can hash it, and then search our database for the key with the matching hash, without ever needing to store the original key.
  • do this using:formatted as → HMAC SHA-256 inside one backend custom event (e.g. API key - hash). That way the HMAC secret never reaches the browser.
  • give keys a prefix e.g. yourapp_ followed by a long random string. Your MCP proxy will be able to use the prefix to tell keys from OAuth tokens (if you’re using both auth methods), and it makes leaked keys easy to spot + revoke
  • Create and revoke keys in backend custom events with the permission checks inside them.

Personal keys

Let’s suppose we want tools to run as the user, so privacy rules and Current User still work. The trick is magic login links:

  1. The proxy sends the key to an admin-only backend workflow, mcp-login.
  2. mcp-login hashes the key and finds a matching API Key that isn’t revoked or expired.
  3. It runs Send magic login link for the key’s owner with ‘Just create the link’ ticked, and returns the link.
  4. The proxy opens the link and uses the session cookie that Bubble sets to call your tools.

mcp-login must be admin-only, and it must return nothing for a bad key. To avoid having to do the magic login link every call you can in theory cache it for a few minutes (but a revoked key would keep working until the cache runs out unless you hook up that cache revocation logic separately).

Team keys

The proxy calls each tool with your Bubble admin token and passes the key as an api_key parameter. Each tool first calls a custom event (e.g. Fetch Organisation and Verify Key). This checks the key and returns the organisation. If the key is invalid, then tool would just stop with an error.

This is the risky one. The admin token skips privacy rules and there is no Current User, so you have to do the privacy rules’ job yourself.

Alternatively you can go the route of creating a ‘dummy’ user for each API token that is basically a special kind of bot user inside each team. This also has other benefits like being able to attribute actions to a specific API key. You’d just exclude it from user tables etc.

TLDR

Just use /mcp in Buildprint and have fun

Happy to answer questions below or hear about other people’s approaches!

I have a question:

How does an AI agent running a 20-step loop survive Bubble’s bot limits and session expirations when your manual proxy is scraping magic login link cookies, and won’t running multiple workflows per tool call incinerate a client’s monthly Workload Unit bill?

False premise, it’s not. It uses Bubble’s public API + functionality for authentication. Most people don’t realise you can have “Login with [your app]” in Bubble.

Not really? One backend workflow, and whatever logic you have configured. If using API key auth then some calls would get an extra workflow call.

It’s not using much more workload than what a user would use themselves in the application (possibly less if you account for the fact that an agent doesn’t need dozens of searches performed in frontend to do everything!)

If it’s a false premise, why does your guide literally tell people: ‘The proxy opens the link and uses the session cookie that Bubble sets to call your tools?’

You make it sound like some hacky, fragile system… when it’s not… a magic login link is just a good way to get an auth token for a user in Bubble…

‘bot limits and session expirations’ are irrelevant… it’s the public workflow API…

A magic link creates a temporary browser session, not an API Bearer token.

The problem is when you’re building with AI, you don’t even understand the whole meaning of what you’re doing.

if only you knew how silly this looks

if you read the guide, we recommend OAuth, and also provide a framework for securely implementing API token authentication in Bubble (in the sense that an end user can create an API token they use with their services to talk to your app).

this isn’t a new concept George Collier on X: "You can implement 'login as user' functionality in any @bubble app, which is really useful for customer support. Here's how to do it securely 🧵" / X

Now, if you’d like to concretely flag something you think is insecure and explain to the forum step by step why, be my guest

else lighten up a little!

Well, you just admitted I was right.

Using a magic login link for a human support rep to ‘log in as a user’ in a browser is standard practice.

Using that same magic-link cookie scraping as an automated M2M authentication pipeline for autonomous AI agent tool loops is completely wild.

Those are two completely different architectural problems.

Ironically, that meme is the exact diagram of using Cloudflare Workers to scrape magic login link cookies for an API agent. :sweat_smile:

Best of luck!

How are you storing the HMAC secret?

Inside a backend custom event - you can treat a backend custom event with no input parameters and a single static return param as a kind of environment variable.

i.e ‘Trigger Get HMAC Secret’ gives you secret to return and use as Result of step X in subsequent steps. Unless you trigger it from the frontend and return it to be used in frontend stuff,
it’ll stay secure.

Other approach would be a private data type but that’s a bit ugly and messy for such a simple thing.

Bubble really ought to have a built in Vault concept for credentials instead of checking stuff as private in API connector.

(edit: like in the guide, you can also just have a single backend custom event ‘Hash API Key’ which has the secret inside it AND hashes a text, it’s a bit cleaner)

I use that technique to create workflow “variables” - i.e. values that can be accessed from anywhere in the workflow.

The problem with putting the secret anywhere in an expression is that it winds up in application exports, which is far from ideal.

Exactly. And I know I’m not the only one to have requested it. And once added to the secrets store / vault, it shouldn’t even be viewable any more. It should only be able to be changed / rotated.

The secret shouldn’t be stored in the DB in plain text, nor (in my view) in the application “schema” that gets exported. Some devs use the private keys feature of a dedicated Bubble plugin, but even then, anyone with access to the app’s backend can see those in the plugin tab.

I find it quite odd that Bubble provides an operator to generate cryptographically secure hashes (HMACs) but no means for securely storing the secret itself. That’s a shame because HMACs are ideal for user tokens, signed URLs, and such; but without a secure secrets store, it doesn’t follow industry best practices. :confused:

Yep agreed on all fronts! But it’s the best we’ve got for now!

This is great George.. you should be getting a speeding ticket for the product velocity on buildprint!

buildprint go brrrr

If I read the concern properly, you’re curious how single use magic link will be useful for long running sessions, implying there is likely a need to create more than one magic login link to complete the task?

In terms of WUs for multiple workflows per tool call, that would I suppose require the entire setup to pass required data along from tool to tool, otherwise if each tool needs to search for data again, it will add up.

I’m overall curious what the main differences for most likely uses cases for this method versus an LLM doc outlining the API calls and their purposes so agents can just read the doc and know what backend workflows to run for the task. In my mind most likely use cases are not to have a bubble app be an MCP server but instead enable agents to run workflows on a user behalf, like update invoice or make booking.

it’s for SaaS tools that have their customers asking ‘why don’t you have an MCP server!’, and it also lets them get listed in Claude/ChatGPT plugin/MCP directories etc which helps with discoverability.

An MCP is just an API, with a specific structure. The guide above helps you do that too!

That is a good thing to have. Besides that, is it the same to have a LLM file as instructions on where to find which api for the bubble app an LLM can use, or does creating the MCP server give functionality like tools or scripts that you can’t get with just an LLM file directing Ali call locations.