@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
85 lines (57 loc) • 4.63 kB
Markdown
---
name: mcp
description: >-
Open the app to AI assistants over MCP: softrMcpMiddleware({ actions }) from
/sdk, registered in src/start.ts ahead of softrAuthMiddleware, turns the
actions you list into tools an assistant can call as the signed-in visitor.
The list is explicit — an action left out is invisible over MCP. The app's own
login must land on toReturnPath, or the approval step dead-ends.
metadata:
type: sub-skill
library: softr-sdk
library_version: 1.4.1
---
**Boundary.** `softrMcpMiddleware` lives on `/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP.
# MCP
An app can answer an AI assistant on its own origin. The assistant signs in as an ordinary app user, and the actions you list become the tools it may call. Not wired by default — a new app serves no MCP endpoint until you add the middleware.
## The URL
The app's own public origin, plus `/_softr/mcp`:
```
https://myapp.mysoftr.app/_softr/mcp
```
Custom domain, default subdomain, a staging host — whatever origin the app is published on, the path is the same. That whole string is the MCP URL. The origin on its own is the app's home page, and an assistant pointed at it gets HTML back.
## Setup
```ts
// src/start.ts
import { createCsrfMiddleware, createStart } from '@tanstack/react-start'
import { softrAuthMiddleware, softrMcpMiddleware } from '@softr/sdk'
import { listCafes, addCafe } from './actions'
export const startInstance = createStart(() => ({
requestMiddleware: [
createCsrfMiddleware({ filter: (ctx) => ctx.handlerType === 'serverFn' }),
softrMcpMiddleware({ actions: [listCafes, addCafe] }),
softrAuthMiddleware(),
],
}))
```
Order is part of the contract. `softrMcpMiddleware` goes **before** `softrAuthMiddleware`, and that is the only order it supports.
## Choosing the actions
The list is the whole tool surface. Write it out:
- An action you leave out is invisible to the assistant, even where the app's pages use it freely.
- An action you list still obeys its own `permissions`. Listing it does not widen who may run it — the assistant runs as whoever signed in, with that user's groups.
- Two actions with the same `id` throw at startup. So does anything that is not a `defineQuery` or `defineMutation` value.
Reads first. Add a write only when the user asked for one, and name it back to them.
## The login round trip
Connecting starts with an approval page the app serves. Most people hit it signed out, and that page cannot sign anyone in. It sends them to the login page named in `softr.json` (`auth.routes.login.path`), with the approval page in the search param named there (`auth.routes.login.redirectParam`).
**The app's login has to send them back.** Every exit that ends a sign-in, the second-factor step included, goes to `toReturnPath(location)` instead of the home page. See [auth/SKILL.md](../auth/SKILL.md). Lose the search param anywhere along the way and the approval dies quietly: they land on the home page, the assistant never gets its answer, nothing reports an error.
`toReturnPath` only ever goes to a path on this app. Never follow the param without it. The whole flow starts with a link someone was sent, so this is the one redirect an attacker can reliably aim.
## Failure modes
**Registering with no actions.** `softrMcpMiddleware()` is valid and serves an endpoint with nothing on it. The assistant connects, lists tools, finds only `whoami`. Pass the actions.
**Registering after `softrAuthMiddleware`.** Not a supported order. Put the MCP middleware first.
**Handing over the app's URL as the MCP URL.** The origin is where the app's pages are. Without `/_softr/mcp` on the end the assistant has nothing to talk to, and the owner is left comparing a URL that looks right against a connection that never works.
**A login that drops the return target.** Sign-in works, approval doesn't. The most common break, and silent — test the connect flow signed out, not only signed in.
**Following the param without `toReturnPath`.** Anything that is not a path on this app is someone else's page, and `/\evil.test` is one of them. A prefix check misses it.
## References
- [record-queries/SKILL.md](../record-queries/SKILL.md): declaring the read actions you expose
- [record-mutations/SKILL.md](../record-mutations/SKILL.md): declaring the write actions you expose
- [auth/SKILL.md](../auth/SKILL.md): the sign-in and second-factor flows the round trip passes through