@rsc-kit/mcp
Version:
An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.
174 lines (133 loc) • 5.53 kB
Markdown
# OpenAPI
> A document derived from your route.ts files, and Scalar's page over it.
Every `route.ts` already says what an OpenAPI operation needs: the methods it
exports, and the `params`, `searchParams` and `body` schemas beside them. Turn
the document on, and it is derived from your routes, so it can't go stale:
```ts title="vite.config.ts"
rscKit({
openapi: true,
})
```
The document answers at `/openapi.json`.
## Describing the API
What no route can say — the title, where the API is served, how a caller
authenticates — goes in the plugin option:
```ts title="vite.config.ts"
rscKit({
openapi: {
info: { title: 'Shop API', version: '1.0.0' },
servers: [{ url: 'https://api.shop.example' }],
security: [{ bearerAuth: [] }],
components: { securitySchemes: { bearerAuth: { type: 'http', scheme: 'bearer' } } },
},
})
```
`openapi: true` is the same with defaults. `path` moves the document:
```ts
rscKit({ openapi: { path: '/api/openapi.json' } })
```
It is an api route the build stores, and it runs without middleware: a
document exists to be read.
## What a route contributes
```ts title="src/app/api/orders/[id]/route.ts"
import { z } from 'zod'
import type { RouteContext } from '@rsc-kit/core/route-schema'
export const params = z.object({ id: z.coerce.number().int() })
export const searchParams = z.object({ expand: z.boolean().optional() })
export const body = z.object({ title: z.string().min(1) })
export const openapi = {
tags: ['Orders'],
PATCH: { summary: 'Rename an order' },
responses: { 200: { description: 'The order' } },
}
export async function GET(request: Request, { params }: RouteContext<typeof params>) { … }
export async function PATCH(request: Request, { params, body }: RouteContext<typeof params, never, typeof body>) { … }
```
| from | into the document |
| --- | --- |
| the directory | the path: `/api/orders/[id]` → `/api/orders/{id}` |
| each method export | an operation |
| `params` | the path parameters' types; a segment with no schema is a string |
| `searchParams` | one query parameter per property, required where the schema requires it |
| `body` | the request body of `POST`, `PUT`, `PATCH`, `DELETE`, and a documented `422` |
| a `middleware.ts` above | a `session` security requirement, and `401`/`403` |
| `export const openapi` | anything else an operation may say — `summary`, `description`, `tags`, `responses` — shared, or per method under `GET`/`POST`/… |
A schema contributes by describing itself as JSON Schema (Standard JSON
Schema). Zod 4 and ArkType do. Valibot needs its own converter and
contributes nothing yet.
## Describing a response
A handler returns a `Response`, so nothing else knows the shape. Declare it in
`openapi.responses`:
```ts title="src/app/api/orders/[id]/route.ts"
export const openapi = {
GET: {
summary: 'Read an order',
responses: {
200: {
description: 'The order',
content: {
'application/json': {
schema: {
type: 'object',
properties: { id: { type: 'integer' }, title: { type: 'string' } },
},
},
},
},
404: { description: 'No such order' },
},
},
}
```
## Leaving a route out
```ts title="src/app/api/webhooks/stripe/route.ts"
export const openapi = false
```
`false` leaves the whole route out: the page that renders the document, a
webhook meant for one caller. `{ DELETE: false }` leaves one method out:
```ts
export const openapi = { DELETE: false }
```
`HEAD` and `OPTIONS` are never documented. The engine answers them for every
route, and a file exporting `OPTIONS` for a CORS preflight isn't describing an
operation.
### Documenting only some routes
An app whose routes are mostly webhooks turns the default around:
```ts title="vite.config.ts"
rscKit({ openapi: { include: 'declared' } })
```
Only a route that exports `openapi` is documented, and a callback needs no
line.
## Showing a reference page
Scalar's API Reference renders from a single function, so the page is one
file:
```sh
bun add @scalar/client-side-rendering
```
```ts title="src/app/reference/route.ts"
import { renderApiReference } from '@scalar/client-side-rendering'
export const GET = () =>
new Response(renderApiReference({ config: { url: '/openapi.json' } }), {
headers: { 'Content-Type': 'text/html; charset=utf-8' },
})
export const openapi = false
```
`renderApiReference` takes the rest of Scalar's options too: `pageTitle`,
`cdn` to pin a version, and `nonce` for a strict
[content security policy](/guides/content-security-policy).
The page is stored at build, like any route that reads nothing per request.
Scalar is the app's dependency, at the app's version; the engine ships no UI
and no dependency for it.
Use `@scalar/client-side-rendering`, not `@scalar/nextjs-api-reference`. The
Next.js package is this same call plus a theme, and it names `next` as a peer
dependency, which npm, pnpm and bun install for you, so the app gains a copy
of Next.js it never runs.
## Coming from a hand-written spec
A spec kept in a file has three parts, and each moves:
- **the `paths`**: delete them. They are the routes and their schemas now,
and the same `body` schema validates the request at runtime, which the
hand-written spec never did.
- **`info`, `servers`, `security`, `components.securitySchemes`**: into
`rscKit({ openapi })`.
- **a route's `summary`, `tags` and response shapes**: into its
`export const openapi`.