> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Define an MCP server

`mcp(spec)` from `@bijectionhq/library-mcp` returns a subtree. Place it in
your app tree: its path names the server, and its route is `POST /<path>`,
with `.` written as `/`. A server placed at `mcp` answers at `/mcp`, and one
placed at `support.mcp` answers at `/support/mcp`. Set `path` to choose
another route.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, mutation, query, role, t, table } from "@bijectionhq/bijection";
import { mcp, tool } from "@bijectionhq/library-mcp";

export const notes = table(
  { owner: t.principal, title: t.string, body: t.string },
  { index: [["owner"]] },
);

export const mine = query(
  { allow: [role.signedIn], args: {}, returns: t.array(t.fact(notes)) },
  (ctx) => ctx.list(notes.where({ owner: ctx.frame.as.id }).first(50)),
);

export const archive = mutation(
  { allow: [role.signedIn], args: { id: t.key(notes) }, refuses: { NotYours: null } },
  async (ctx, { id }) => {
    const note = await ctx.get(notes(id));
    if (note === null || note.owner !== ctx.frame.as.id) ctx.refuse("NotYours", "Only the owner archives a note.");
    await ctx.delete(notes(id));
    return null;
  },
);

const server = mcp({
  server: { name: "notes", version: "1.0.0" },
  tools: {
    mine: tool(mine, { description: "List my notes" }),
    archive: tool(archive, { description: "Archive one of my notes" }),
  },
  oauth: { scopes: ["notes:read"] },
});

export default app({ notes: { notes, mine, archive }, mcp: server });
```

The subtree holds the route reaction `mcp.handle` and the internal query
`mcp.follow`. The reaction holds no roles of its own: each request runs as
the principal the server's verifier admitted, so a tool reaches only what
that principal may reach. [Authenticate MCP requests](https://docs.bijection.com/mcp/authentication.md)
and [Expose tools](https://docs.bijection.com/mcp/native-tools.md) describe the caller and the tool
contract.

`under` sets the label the route commits under, and the public label is the
default. A mutation tool must commit under exactly that label. A query tool
must answer under it or under a label below it. `bijection check` reports a
mutation tool at another label (`ToolUnder`) or a query tool above the
server's label (`ToolAbove`), and the host refuses such a call.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label } from "@bijectionhq/bijection";

export const vault = label();
const vaulted = mcp({
  under: vault,
  server: { name: "vault", version: "1.0.0" },
  tools: { mine: tool(mine, { description: "List my notes" }) },
});

export default app({ access: { vault }, notes: { notes, mine, archive }, vault: { mcp: vaulted } });
```

Here the public `mine` query is a tool of the vaulted server because the
public label is below `vault`. Only callers whose roles clear `vault` can
use that server.

## Declare prompts and resources

`prompts` holds named prompts with string arguments. A prompt's `messages`
are literal text or content, or a read-only handler `(ctx, args)` that
returns them. `resources` holds named text or binary data, keyed by URI,
or a `read(ctx, uri)` handler. `resourceTemplates` holds parameterized URIs
keyed by RFC 6570 expression. Give a template a `match` callback for
expressions beyond `{name}` and `{+path}`. `pageSize` (1 to 100, default 32)
sets how many entries a list method returns. A `complete` source on a prompt
or template argument offers suggestions.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
mcp({
  server: { name: "notes", version: "1.0.0" },
  tools: { mine: tool(mine, { description: "List my notes" }) },
  prompts: {
    summarize: {
      description: "Summarize my notes",
      messages: [{ role: "user", text: "Summarize the notes `mine` returns." }],
    },
  },
  resources: {
    "notes://guide": { name: "guide", mimeType: "text/markdown", text: "# Writing notes\n…" },
  },
});
```

A handler reads with the ordinary `ctx` reads, in the caller's frame. A
`read` handler that returns `null` answers that the URI is absent. The
`ui://` and `skill://` schemes are reserved for the server's views and
skills.

## Mirror routing arguments in headers

`tool(fn, { headers: { tenant: "Tenant" } })` marks a primitive argument as
an HTTP routing field. Dotted paths select nested properties. Clients mirror
the value in `Mcp-Param-Tenant`, and the server checks the header against
the arguments. Use this only for values that may be visible to HTTP
infrastructure.

## Ask for more input

`round: { handle, key }` asks for more input before a tool, prompt or
resource runs. `handle(ctx, request)` is read-only. It returns an
`inputRequests` map, a `state` value, or both, or nothing to proceed. The
library checks client capabilities before returning form elicitation, URL
elicitation, sampling or roots requests.

A `state` value needs `key`, a MAC key, and an admitted principal. The
library signs the state and binds it to the principal, method, arguments
and an expiry five minutes after `ctx.frame.now.entry`. Keep sensitive state in
your tables and put only its key in the signed payload. The handler owns how
it reads the answers and any single-use rule.

## Publish skills

A server can publish authored [Agent Skills](https://agentskills.io/) with
the MCP Skills extension. Declare them beside `tools`:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { skill } from "@bijectionhq/library-mcp";

mcp({
  server: { name: "notes", version: "1.0.0" },
  tools: {
    mine: tool(mine, { description: "List my notes" }),
    archive: tool(archive, { description: "Archive one of my notes" }),
  },
  skills: { "triage-notes": skill("./skills/triage-notes") },
});
```

```md theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
<!-- skills/triage-notes/SKILL.md -->
---
name: triage-notes
description: Find stale notes and archive the ones the user confirms.
---
Find candidates with [mine](tool:mine), then call
[archive](tool:archive) for each note the user confirms. Arguments and
refusal codes are in [the tool reference](references/tools.md).
```

The `skills` key, the directory name and the frontmatter `name` must agree.
The path is relative to the directory that holds `app.ts`.
Authored files are served byte for byte at `skill://<name>/<path>`. A
`tool:` link, or a `references/tools.md#<tool>` link, declares that the
skill uses a tool. At deploy, the library generates `references/tools.md`
with a section for each linked tool: its description, kind, visibility,
argument and result schemas, and the codes it refuses with. Deploy fails if
a skill links a tool the server does not offer, authors
`references/tools.md`, nests another `SKILL.md`, or declares
`allowed-tools` in its frontmatter.

`skills/list` and `skills/get` return each skill's verbatim frontmatter and
the list of its files with SHA-256 digests. A skill is never `"dynamic"`. A
host's approval binds to that file list, so a deploy that changes neither
the skill nor a tool it links keeps the approval. `bijection.lock` records
every file digest, so a changed instruction is a reviewed diff. The digests
are not a trust anchor; the committed lock file and npm integrity are.

## Trace requests

`trace(ctx, { method, log })` emits diagnostic messages for requests that
set `params._meta["io.modelcontextprotocol/logLevel"]`. `log(level, data,
logger?)` takes a syslog severity. Messages are released after the
transaction settles and arrive before the final JSON-RPC response on the
request's event stream.

## Describe the declared contract

From the app project, name the server by its tree path. The CLI reads its
server card and tool schemas from the analyzed app.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection mcp describe mcp --url https://notes.example.com/mcp
```

The optional URL adds the `streamable-http` remote to the JSON card. The
card lists each view with its URI, digest, CSP and permissions, and each
skill with its files. `bijection check` records the same facts in
`bijection.lock`. Use a live `server/discover` to see the deployed endpoint
as the caller sees it. See [Connect an MCP client](https://docs.bijection.com/mcp/clients.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.