= MCP server :nav-title: MCP :description: Every Ulabase service is also an MCP server: AI agents such as Claude can read and change its data, with the same permissions as your app. :keywords: MCP server, Model Context Protocol, AI agent backend, connect Claude to database, agent tools, AI app backend :group: AI :order: 30 MCP, the **Model Context Protocol**, is the standard way AI agents such as Claude, ChatGPT or Cursor connect to data and tools outside them. Every Ulabase service is an MCP server: an agent connects to it, and reads or changes your data on its own. Use it to work with your data by asking questions in plain words, such as "How many orders are still open?" or "Which products are low in stock?". The agent finds the right collection or aggregation, calls it, and answers. What you get: * **No code to write.** You do not build an API for agents. You choose what to publish, and describe each item in one sentence so the agent knows what it is for. * **The same permissions as your app.** The agent signs in as a user of the service, and can do exactly what that user can do over REST. * **You decide what it sees.** An agent finds only the collections, aggregations, change streams and GraphQL apps you publish. image::/assets/docs-images/mcp.png["The MCP page: the endpoint and the steps to connect a client, above the published resources grouped by type"] == Your first agent, in ten minutes Three steps take you from a service to Claude answering a question about your data, with no code. . **Publish a collection.** Under **Published resources**, **Add collection**, pick one, and write what it holds: a sentence an agent reads to decide whether this is the data it wants. For `orders`: "Customer orders, one document per order, with status and total." You can skip this: the next step publishes a test collection. . **Demo setup.** Under **Tools → Demo setup**, click **Do all**. It creates the user `agent`, a permission for it on `/mcp` and on the demo collection `/catalog`, fills `/catalog` with the sample products of the ecommerce starter when it is empty, publishes it with a JSON schema and the aggregation `low-stock`, and issues an API key, shown once. To let `agent` read `orders` too, add a permission for the role `agent` on `/orders` on the xref:managing-permissions.adoc[Permissions] page. **Catalogue check** now has an identity `agent` with the key: click **Read the catalogues**, and the collections are there. . **Connect Claude.** In Claude, **Settings → Connectors → Add custom connector**, paste the endpoint from **Connect an agent**, and when Claude asks you to sign in, sign in as `agent` with the password shown in Demo setup. In Cursor, VS Code or Claude Code, use the snippet under **Connect an agent** instead: the key is already in it. Then ask: *"How many orders are still open?"* The agent reads the catalogue, finds `orders`, reads it with the permissions of `agent`, and answers. From here it can do exactly what that user, with that role, can do over REST. == Connect an agent The first panel of the page gives you the four facts every client needs: [cols="1,3"] |=== | | Value | URL | `https://c0ffee.ulabase.app/mcp` | Transport | Streamable HTTP | Authorization | `Bearer ` | Protocol | MCP 2025-06-18 |=== The panel also has ready-made steps for the three common clients: * **Claude**: Settings → Connectors, in the desktop app or on claude.ai. Add → Add custom connector, and paste the endpoint. Claude then opens your service's login page. Sign in as a **user of the service**, with that user's username and password; your Ulabase account is for the console, and the service does not know it. Next the page asks which role the agent gets and for how long, within what you allowed that user on the API Keys page. Claude then holds an **API key** for that role, the same kind a user issues by hand, and you or the user can revoke it from API Keys. * **Cursor**: add it from Cursor's own settings and paste the endpoint. In the `Authorization` header put `Bearer` and an API key (`ulak_…`) a user of the service issued for the agent. The panel shows the `.cursor/mcp.json` it expects, with the key from Demo setup already in it. * **VS Code**: run "MCP: Add Server" from the Command Palette and paste the endpoint, with the same `Authorization` header. The panel shows the `.vscode/mcp.json`. For anything else, such as Zed, Windsurf, Claude Code or an agent you wrote yourself, the four facts above are all the configuration you need. === Give the agent its own credential An agent reads with the permissions of the user it authenticates as. Before it can connect, you need two things: . **An API key.** A user of your service issues one for the agent, with a role you allowed for that user on the API Keys page. See xref:api-keys.adoc[API keys] for how to turn keys on and issue one. Give the agent a user of its own: an agent that authenticates as the administrator reads everything the administrator can. . **Permissions for that role**, written on the Permissions page like any other. One to reach the endpoint, with predicate `path-prefix('/mcp')`, and one for each collection or resource the agent may read, as for a REST `GET` on it: the same `readFilter` and `projectResponse` apply. A resource that no permission of the role could allow is not in its catalogue, published or not. A client that signs the user in through the browser, like Claude, gets a key by signing in. The sign-in page asks the user for consent: role, duration and name, within the `apiKeys` block of the user's permission. Claude then gets a `ulak_` key with that role from `/token`. So the two points above apply to Claude as to Cursor: the user needs a permission on `/keys` naming the roles a key may carry, and the chosen role needs its permissions. Without a permission on `/keys`, the sign-in falls back to the user's own token, with every role the user holds; <> says how to forbid that. == Published resources Under the connect panel, **Published resources** lists the collections, aggregations, change streams and GraphQL apps an agent can find, and read or write within your permissions. It starts empty. To publish something, click the **Add** button for its type at the bottom of the panel (**Add collection**, **Add aggregation**, **Add change stream**, **Add GraphQL app**), and a form opens in its place: . **Which one**: pick from what is not published yet, of that type. . **What it is for**: the description the agent reads to decide whether this is the resource it wants. . **Publish**. The description matters most. An agent picks a resource by reading it, so write what the resource holds or computes; its name is already shown: [cols="1,3"] |=== | Type | What to write | Collection | What it holds, so an agent knows when to read it. | Aggregation | What it computes, and what its parameters mean. | Change stream | What changes it reports. | GraphQL app | What this API is for. |=== The panel groups what is published by type, with a count for each, and a search box over names and descriptions. To edit a description, click it and change it; **Save** appears on that row. Beyond twenty-five resources the list is paged, and the counts beside each type stay the totals. To unpublish, remove the entry. The agent stops seeing it on its next catalogue read. The page reads every collection of the service to find what is published, because the `mcp` block lives there. Past a thousand collections it stops and says so, and the rest is not listed. Publishing and permissions are two separate switches. A resource is in the catalogue only when it is published and a permission of the role could allow it. === Who the catalogue is composed for [[who-the-catalogue-is-composed-for]] A catalogue is built per caller. RESTHeart reads the permissions of the role the agent authenticated as, and announces a resource as soon as one of them could allow a read of it. "Could allow" is meant exactly. A permission that depends on something the call carries, such as a query parameter or the request body, cannot be decided while the listing is built, because there is no call yet. That part is left open and the resource is announced; the call itself is authorized as usual and may answer `403`. A condition on *who* is asking is known, so it decides. A permission such as `path-prefix('/orders') and equals(@subscription.plan, 'gold')` announces `/orders` only to customers on the gold plan, which no list of roles could express, since every customer holds the same role. The same rule decides a resource's actions. A resource is described with the actions the caller's permissions could allow: a role with only `GET` on a collection is not offered its writes. An action the permissions *might* allow is offered with a note: for a write whose rule reads a query parameter, the catalogue says that the outcome depends on what the call carries. A collection also publishes the operations on itself: reading and changing its properties, dropping it, its indexes, and the bulk writes. Each says what it acts on (`delete` removes one document, `drop` removes the collection with everything in it). Each needs **two** things: a permission matching the request, and a switch in that permission's `mongo` block (`allowManagementRequests`, `allowBulkPatch`, `allowBulkDelete`). Those switches are off unless written, so by default nobody is offered these operations. **Your permissions are the only thing enforced**, and for almost every policy they are all the catalogue needs: Ulabase reads them and works out the listing. One case needs your help, and only for the accuracy of the listing, because the permission is enforced anyway: a condition that uses a predicate or variable added by custom code that declares nothing about itself, such as `is-premium()` or `@billing.tier`. The catalogue cannot read it, so it announces the resource to everyone the rest of the rule admits. The same holds when a custom authorizer decides access, since there is no predicate to read. The two keys below let you say what those cases mean. [IMPORTANT] ==== The catalogue is **discovery**. It decides what an agent is told about; granting and blocking belong to the permissions: * a resource in the catalogue can still answer `403`: the call is authorized when it is made, exactly as the equivalent REST call would be; * a resource **absent** from the catalogue can still be called. An agent that knows the URI reaches it, and the permissions decide, as they would over REST. So keeping a resource out of a catalogue protects its **name and description**, which often say what the data is. Only permissions protect the data itself. ==== Two keys of a resource's `mcp` metadata override all of this. Both decide what is *announced*; the permissions still decide what may be read: [cols="1,3"] |=== | Key | What it does | `hide_from_roles` | A list of roles the resource is not announced to, whatever the permissions say. Use it for a resource the permissions allow but whose name should stay private. | `show_if` | A predicate replacing the reading of the permissions. It may only read the caller and the resource, never the call; one that reads the call is refused and the resource is not announced. |=== `hide_from_roles` is checked first and wins over `show_if`. In the console both are under **Catalogue visibility**, below each published resource, as **Hidden from** and **Shown only if**. WARNING: `hide_from_roles` keeps a name out of a listing and leaves the read allowed. If the role must not read the data, write that on the xref:managing-permissions.adoc[Permissions] page: the permission is the one that is enforced. === Parameters of a parametric aggregation An aggregation whose pipeline uses `$var` takes parameters. The page lists them under the entry, one row each, read from the pipeline: add a variable to the stages and its row appears. Give each one a type and a description. Without a description, the agent knows only that the parameter exists, and guesses the rest. A value MongoDB stores in its own type takes that type: `date`, `objectId`, `long` or `decimal`. The agent is told to send `{"$date": }` for a date, and the pipeline receives a date. As a `string`, a parameter compared with a date field matches nothing. A parametric aggregation reaches the agent as a **resource template**, a different kind of entry from a plain resource. Many clients, Claude Desktop among them, list only plain resources, so the aggregation may not appear there. It is still available: the agent finds it with `list_apis` and runs it with `call_api`, and the server says so in the instructions it sends when a client connects. == Tools At the bottom, three folded panels: click a title to open it. [[demo-setup]] === Demo setup Creates a test user, its permission, a test collection and an API key, so you can connect an agent right away. In real use you define roles for agents, let your users issue API keys with those roles, write the permissions and publish resources. This panel makes a test version of each, and a checkbox shows which ones already exist: [cols="1,2,1"] |=== | Item | What it is | Button | Test user `agent` | A user of the service with the role `agent`. The password is generated and shown once; the service never returns it, so after a page reload the console no longer knows it. | **Create**, or **Reset password** when the user exists | Test permission `agentTest` | The role `agent` may use `/mcp` and read `/catalog`, its aggregations included. | **Create**, or **Rewrite** | Demo collection `/catalog` | The collection the ecommerce starter sells from. Created if missing; when empty, filled with the starter's hundred sample products, from `catalog.seed.json` in its repository. It gets the JSON schema `product` (`name`, `description`, `category`, `unit_amount` in cents, `purchasable`, `in_stock`, `variants`) and the aggregation `low-stock`, which lists the products with `in_stock` at or under a `threshold` (5 when omitted), on the product or on one of its variants. Both are published, so an agent can query the catalog and ask what is running low. | **Publish** | API key for `agent` | Turns API keys on if needed, lets the role `agent` issue keys, revokes the keys `agent` already holds, and issues one as `agent`. It needs the password: when the console does not know it, it resets it first. The key is shown once, and goes into the snippets under Connect an agent. | **Issue**, or **Reissue** |=== **Do all** runs the unchecked items in order. It writes ordinary users, permissions, schemas and keys under those names, which you can change on the xref:managing-users.adoc[Users], xref:managing-permissions.adoc[Permissions], xref:schemas.adoc[Schemas] and xref:api-keys.adoc[API Keys] pages. A permission applies within 20 seconds. For a quick try, the root user created with the service works too, and sees everything. === Catalogue check [[catalogue-check]] Each identity gets its own catalogue, and this panel compares them. Add up to eight identities, such as an API key for each role or a user on each plan, give each a label, type its key or password, and click **Read the catalogues**. *Resources.* One row per resource, one column per identity: _visible_ or _hidden_. Click a cell to record what that identity should see; a cell that disagrees with its expectation is marked, and the number of failing expectations is at the top. Look twice at a row all _visible_ for a resource that should be reserved, and at a row all _hidden_ for one you published on purpose. *Actions.* Click a resource to see its actions for every identity, with the actions on its elements kept apart from those on the resource itself: `delete` removes a document, `drop` removes the collection. ✓ means offered, ~ offered but decided by the call's arguments (the note says which), ✗ refused by that identity's permissions, and a dash means the resource is hidden from that identity. A row of ✗ on `drop` is normal: the data management operations need `allowManagementRequests` in the permission. *Reading it.* The catalogue errs in one direction only: it never hides what a permission could allow, and when it cannot tell, it lists the resource. So: * a resource *missing* for an identity is missing because that identity's permissions do not allow it, or because of its `show_if`: look there; * a resource listed *where it should not be* is the usual surprise. Keep it out with **Hidden from** or **Shown only if** under the resource's **Catalogue visibility** (a failing expectation links straight to it). Neither stops a read: only a permission does; * an action shown ✗ is almost always a switch the permission does not grant: `allowManagementRequests` for the operations on the collection itself, `allowBulkPatch` and `allowBulkDelete` for the bulk writes. The matrix names the one each action needs. The identities and the expectations are saved with the service; the keys and passwords are not, so type them again when you come back. **Demo setup** adds the identity `agent` with the credential it has just made. Test with the credential the agent will use. A user's password shows what the _user_ sees; a key carries only the roles it was issued with, so its agent may see less. Three things the matrices do not show: * What an agent then _reads_ is narrowed again by the permission's `readFilter` and `projectResponse`, which apply to MCP reads as they do to REST ones. * A resource listed as _visible_ can still answer `403`. The catalogue announces what a permission _could_ allow; each call is authorized when it is made. See <>. * A GraphQL app's schema is already per caller: a field its `@visible` directive hides from a role is absent for that identity. === The OAuth login page [[the-oauth-login-page]] Clients like Claude sign in with OAuth: when a user adds your service as a connector, Claude opens a login page where they enter their credentials for your service. Clients that use an API key, like Cursor and VS Code, never show it. If you switched on **Google** in the OAuth section of the xref:signup-mgmt.adoc#oauth[Sign-up and login] page, the default login page offers it too: a **Continue with Google** button above the username field. A user who has never signed in to your service gets an account on the spot, like any new user, and the agent then works with that user's roles. There is nothing else to configure: it uses the same client ID and secret as your app's own sign-in. The default page is `https://c0ffee.ulabase.app/oauth/login`, ours with your app's name, color and logo from xref:branding.adoc[Branding]. Under **OAuth login page** you can set your own, so the user sees your application's login. It receives the OAuth parameters as a query string and must POST them, with the user's credentials, to `https://c0ffee.ulabase.app/authorize`. The URL must be absolute, `https://` or `http://`. The same panel lists the **allowed redirect URIs**: the `redirect_uri` values `/authorize` accepts, with `*` as a wildcard. It starts with the default list, which covers Claude and the desktop clients. Your list replaces the default, so if you edit it, keep the `claude.ai` entry or Claude can no longer connect. ==== What the agent gets [[what-the-agent-gets]] The panel's first setting says what the sign-in hands the agent: [cols="1,3"] |=== | API key when possible | The default. After the credentials, the page offers the user the roles their permission on `/keys` allows a key to carry, a duration up to the maximum, and a name. `/token` then issues a `ulak_` key with that role, listed on the API Keys page with the client that asked for it (`claude.ai`) and revocable like any other. When the user's permission allows no key, or API keys are off, the agent gets the user's own token, with every role the user holds; the page says so before continuing. | API key only | As above, but a user whose permission allows no key cannot connect an agent: the page tells them to ask you. Choose this when no agent should ever hold a user's full permissions. | The user's own sign-in | The agent gets the user's own token, renewable without end, with nothing to revoke. Choose it only if your service needs this. |=== A key obtained this way is not renewed. When the time the user chose runs out, Claude asks the user to sign in again. Revoking the key from API Keys disconnects the agent within 20 seconds. A custom login page follows the same contract as the default one, in two steps. First it verifies the credentials with `GET /authorize/offer` (Basic authentication, the same query string as `/authorize`) and renders the choices the answer lists: `204` means nothing to choose, `403` a refusal to show. Then it POSTs the form to `/authorize` with each choice as a `choice.` query parameter (`choice.role`, `choice.days`, `choice.name`). A page that sends no choice gets the defaults: the first role the user may give, the default duration, and a name with the client and the date. == Do it from the command line You can keep what you publish on MCP in git and apply it with xref:cli.adoc[`ulabase setup`]. The step below publishes the `catalog` collection and its `low-stock` aggregation. Each has its own `mcp` block: the collection in its metadata, the aggregation in its entry of `aggrs`: [source,typescript] ---- import { defineSetup, step } from '@ulabase/cli'; // what the agent reads about the collection const mcp = { enabled: true, description: 'The products of the shop, with price and units in stock.' }; // what the agent reads about the aggregation, with the parameter it must pass const lowStock = { enabled: true, description: 'Products with fewer units in stock than the given threshold.', params: { threshold: { type: 'integer', description: 'Units below which a product counts as low.' } }, }; export default defineSetup('MCP', [ step('catalog published on MCP', { async check({ service }) { const meta = await (await service.fetch('/catalog/_meta')).json(); // the aggregation's mcp block is in its entry of the aggrs list const aggr = (meta.aggrs ?? []).find((a: any) => a.uri === 'low-stock'); return meta.mcp?.description === mcp.description && aggr?.mcp?.description === lowStock.description; }, async apply({ service }) { const meta = await (await service.fetch('/catalog/_meta')).json(); // write the whole aggrs list back, adding the mcp block to the low-stock aggregation only const aggrs = (meta.aggrs ?? []).map((a: any) => (a.uri === 'low-stock' ? { ...a, mcp: lowStock } : a)); await service.fetch('/catalog', { method: 'PATCH', body: JSON.stringify({ mcp, aggrs }) }); }, }), ]); ---- To unpublish, set `enabled: false`: the description stays. A change stream works the same way, with its block in its entry of `streams`. `hide_from_roles` and `show_if` go in the same block as `description`. The agent also needs a user, an API key and permissions. Each has its own step, on the xref:managing-users.adoc[Users], xref:api-keys.adoc[API keys] and xref:managing-permissions.adoc[Permissions] pages. == Related pages * https://github.com/ulabase/market-ai-game[The market game^]: a full tutorial with three AI agents trading through a service's MCP server, with one collection, six rules and no code. * xref:api-keys.adoc[API keys]: the credential an agent uses, and how a user issues one. * xref:managing-permissions.adoc[Permissions]: what an agent may read, including `readFilter` and `projectResponse`. * xref:schemas.adoc[Schemas]: what the demo collection's schema is made of. * xref:aggregations.adoc[Aggregations]: where `$var` parameters come from. * xref:graphql.adoc[GraphQL applications]: the `@visible` directive.