# mcpPlugin

[← All plugins](/docs/reference/plugins)

```ts
import { mcpPlugin } from '@routecraft/ai'
```

Starts an MCP server so capabilities exposed with `.from(mcp(...))` are reachable by external MCP clients. Also registers named remote MCP clients (HTTP or stdio subprocess) so capabilities can call external MCP servers by a short server id. Required when any capability uses `mcp()` as a source.

Tools discovered from remote MCP servers (stdio clients and HTTP clients) are collected into an `McpToolRegistry` stored in the context store under `MCP_TOOL_REGISTRY`. Local `mcp()` routes defined in the same context are not auto-populated into this registry; the MCP server reads them directly from the direct-adapter registry when responding to `tools/list`.

```ts
import { mcpPlugin, jwt } from '@routecraft/ai'
import type { CraftConfig } from '@routecraft/routecraft'

export const craftConfig: CraftConfig = {
  servers: {
    default: { port: 3001 },
  },
  plugins: [
    mcpPlugin({
      transport: 'http',
      auth: jwt({
        secret: process.env.JWT_SECRET!,
        issuer: 'https://idp.example.com',
        audience: 'https://mcp.example.com',
      }),
      clients: {
        browser: {
          url: 'http://127.0.0.1:8089/mcp',
          auth: { token: process.env.BROWSER_MCP_TOKEN! },
        },
        search: { url: 'http://127.0.0.1:8090/mcp' },
        filesystem: {
          transport: 'stdio',
          command: 'npx',
          args: ['-y', '@modelcontextprotocol/server-filesystem', '/tmp'],
        },
      },
      maxRestarts: 5,
      restartDelay: 1000,
      restartBackoffMultiplier: 2,
    }),
  ],
}

```

**Options:**

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `name` | `string` | `'routecraft'` | Server name exposed in MCP metadata (`serverInfo.name`) |
| `title` | `string` | -- | Human-readable display title (`serverInfo.title`) |
| `version` | `string` | `'1.0.0'` | Server version |
| `description` | `string` | `'Powered by Routecraft.dev'` | `serverInfo.description`; pass `''` to omit |
| `websiteUrl` | `string` | `'https://routecraft.dev'` | `serverInfo.websiteUrl`; pass `''` to omit |
| `instructions` | `string` | -- | Server-wide usage guidance on the `initialize` result; pass `''` (or omit) to send none |
| `icons` | `McpIcon[]` | Routecraft logo | `serverInfo.icons`, inherited by tools that set none of their own; pass `[]` to omit. See [Server identity and branding](#server-identity-and-branding). |
| `transport` | `'http' \| 'stdio'` | `'stdio'` | Transport protocol for the MCP server |
| `server` | `string` | `'default'` | Named entry under `defineConfig({ servers })` to mount the HTTP transport on (http transport only) |
| `path` | `string` | `'/mcp'` | HTTP path for the MCP endpoint on the shared listener (http transport only) |
| `auth` | `McpHttpAuthOptions \| false` | inherits `servers.<name>.auth` | Auth for the HTTP endpoint (http transport only; see below). `false` removes the wall while keeping the inherited validator reachable. |
| `cors` | `McpCorsOptions \| false` | loopback-only | Response-header policy and additional Origin refusal. `false` delegates CORS handling to the proxy; Host and browser-admission checks remain enforced. |
| `browserOrigins` | `readonly string[]` | `[]` | Exact HTTP(S) origins allowed to send requests, including port. Required for local browser tools too; independent of CORS. Ignored for MCP stdio. |
| `resource` | `McpResourceOptions` (`{ url?: string \| URL; scopesSupported?: string[]; documentationUrl?: string \| URL }`) | -- | RFC 9728 protected-resource metadata (http transport only). `url` is the public URL clients reach, which a proxy hides from the bind address, so outside `NODE_ENV=development` or `test` it is required and must be HTTPS; the server refuses to start with `RC5003` otherwise. `scopesSupported` and `documentationUrl` fill `scopes_supported` and `resource_documentation`. |
| `tools` | `string[] \| (meta) => boolean` | -- | Allowlist of local route tool names to expose, or a filter function. Applies to both `tools/list` and `tools/call`. |
| `clients` | `Record<string, McpClientHttpConfig \| McpClientStdioConfig>` | -- | Named remote MCP servers (see below) |
| `proxy` | `Array<string \| McpProxyToolConfig>` | -- | Tools from registered `clients` to re-expose through this server (see below) |
| `maxRestarts` | `number` | `5` | Max automatic restarts for stdio clients before giving up |
| `restartDelay` | `Duration` | `1000` | Initial delay before the first restart attempt |
| `restartBackoffMultiplier` | `number` | `2` | Multiplier applied to delay on each successive restart |
| `toolRefreshInterval` | `Duration` | `60000` | Polling interval for HTTP client tool lists (0 = no polling) |

**Client names may not be empty, contain `__`, or end in `_`:**

The key you register a client under becomes the server segment of the `mcp__<server>__<tool>` name agents see, and that name is split at the first `__` after the prefix. Three shapes break that split, and `mcpPlugin` rejects all of them with RC5003 at startup:

| Key | Composes | Reads back as | Problem |
|-----|----------|---------------|---------|
| `a__b` | `mcp__a__b__c` | server `a`, tool `b__c` | Wrong tool if a client `a` exposes `b__c`, otherwise unresolved |
| `foo_` | `mcp__foo___bar` | server `foo`, tool `_bar` | **Collides** with key `foo` exposing `_bar` |
| `""` | `mcp____bar` | empty server | Unresolvable |

The collision is the reason this is rejected rather than merely warned about: two different clients can compose the same tool name, the resolved tool map is keyed by that name with later-wins, so one silently replaces the other and the model's call reaches the wrong client.

A single underscore inside the name is fine (`my_company_api`), and because only the server half is constrained, a remote may still expose tools whose own names contain `__` (`mcp__github__issues__create` resolves correctly).

The rule applies whenever the client is registered, including contexts with no agent in them. `mcpPlugin()` validates its options at construction, before it can know whether an agent will later join the same context, so the constraint is namespace-wide rather than conditional on how the client is consumed.

**Logging when `transport` is `'stdio'`:**

The stdio transport uses stdout as the protocol channel. Routecraft's logger defaults to stdout, so logs will corrupt the protocol stream unless you redirect them. When running an MCP server over stdio, always pass one of:

- `--log-file <path>` -- write logs to a file
- `--log-level silent` -- disable logging entirely

On `run`, both belong before the file (`craft run --log-level silent ./mcp-server.ts`). Anything after `run <file>` is pass-through and reaches the route rather than the logger, which leaves the protocol stream exactly as corrupted as passing nothing.

**HTTP server auth (`McpHttpAuthOptions`):**

When `auth` is set and `transport` is `'http'`, every request to `/mcp` must include a valid `Authorization: Bearer <token>` header. The `auth` object requires a `validator` function that receives the raw bearer token and returns an `AuthPrincipal` on success or `null` to reject. The principal is made available on exchange headers so routes can read the caller's identity.

When `auth` is omitted, the named server's validator (if any) is inherited as the wall. `auth: false` removes the wall: the surface never demands credentials, requires no scopes, and issues no challenge, but the inherited validator stays reachable, so a valid token still attaches a principal to tool calls (which is how a tool's `.authorize()` admits on a public mount), an invalid or expired one is treated as absent, and a validator infrastructure failure still maps to 500.

A tool's `.authorize()` refusing the caller answers the call with a fixed reason rather than the error message: insufficient permissions, the missing scopes, an expired credential, or, on a mount with a validator that served the call anonymously, that the tool needs an authenticated caller. Arguments the tool's `.input()` schema refuses come back with each issue's path and message, and a result body that breaks the advertised output schema names the failing fields; every other failure carries the tool name and the error code only. See [Failed calls](/docs/reference/adapters/mcp) for the texts.

| Field | Type | Description |
|-------|------|-------------|
| `validator` | `(token: string) => AuthPrincipal \| null \| Promise<AuthPrincipal \| null>` | Validates the bearer token and returns the caller's identity, or `null` to reject with 401. |

**AuthPrincipal:**

`AuthPrincipal` is a discriminated union on the `kind` field. Every subtype carries `kind`, `scheme`, and `subject`; other fields live on the subtype that gives them meaning. Narrow on `kind` to reach scheme-specific data.

Shared fields on every subtype:

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `kind` | `'jwt' \| 'oauth' \| 'api-key' \| 'basic' \| 'custom'` | Yes | Discriminator for the principal subtype |
| `scheme` | `string` | Yes | HTTP authentication scheme (`'bearer'`, `'basic'`, `'api-key'`) |
| `subject` | `string` | Yes | Stable identity for the caller (JWT `sub`, user ID, key ID) |

Subtypes:

| `kind` | Additional fields |
|--------|-------------------|
| `'jwt'` | `name?`, `email?`, `issuer?`, `audience?`, `scopes?`, `roles?`, `expiresAt?`, `claims` (required) |
| `'oauth'` | `clientId` (required), `name?`, `email?`, `issuer?`, `audience?`, `scopes?`, `roles?`, `expiresAt?`, `claims?` |
| `'api-key'` | `name?`, `expiresAt?` |
| `'basic'` | `name?` |
| `'custom'` | `name?`, `email?`, `roles?`, `scopes?`, `expiresAt?`, `claims?` |

The populated principal rides on the exchange as a single structured header (`routecraft.auth.principal`) and is exposed ergonomically via the `ex.principal` getter; read fields with `ex.principal?.subject`, `ex.principal?.scopes`, `ex.principal?.claims`, etc.

## Server identity and branding

When a client like Claude adds your server, it renders the server's identity from `serverInfo`, returned by `server/discover` for 2026-07-28 clients and by the `initialize` handshake for 2025-era ones. Configure it on the `mcp` key; the option table above has each field's default and how to suppress it:

```ts
// craft.config.ts
import { defineConfig } from '@routecraft/routecraft'
import '@routecraft/ai'

export const craftConfig = defineConfig({
  mcp: {
    name: 'acme-bot',                          // serverInfo.name (machine id)
    title: 'Acme Bot',                         // serverInfo.title (display name)
    version: '2.1.0',                          // serverInfo.version
    description: 'Acme operations over MCP.',  // serverInfo.description
    websiteUrl: 'https://acme.example.com',    // serverInfo.websiteUrl
    instructions: 'Call orders_search before orders_refund.', // server instructions, not serverInfo
    icons: [
      { src: 'https://acme.example.com/icon.svg', mimeType: 'image/svg+xml' },
      { src: 'data:image/png;base64,...', mimeType: 'image/png', sizes: ['48x48'], theme: 'light' },
    ],
  },
})
```

`instructions` is server-wide guidance the client may add to the model's context (advisory per the spec). It complements each tool's own `.description()`, which is the per-tool equivalent.

### Per-tool icons and inheritance

A capability can carry its own icon via the `mcp()` source. The icon shape follows the MCP `Icon` spec (`src`, optional `mimeType`, `sizes` as a string array, and an optional `theme`):

```ts
craft()
  .id('orders_search')
  .description('Search orders')
  .from(mcp({
    annotations: { readOnlyHint: true },
    icons: [{ src: 'https://acme.example.com/search.svg', mimeType: 'image/svg+xml', sizes: ['48x48'] }],
  }))
```

Icons resolve with the same rule at both levels: omit `icons` to inherit (a tool with no icon of its own shows the server's icon, including the Routecraft default), set `icons: [...]` for a custom icon, or set `icons: []` to show none.

## Built-in `jwt()` helper

The `jwt()` helper creates a validator that verifies JWT signatures, checks expiry, and maps standard claims to `AuthPrincipal` fields. Zero dependencies (uses `node:crypto`).

```ts
import { mcpPlugin, jwt } from '@routecraft/ai'
```

**HMAC (HS256 / HS384 / HS512):**

```ts
auth: jwt({
  secret: process.env.JWT_SECRET!,
  issuer: 'https://idp.example.com',
  audience: 'https://mcp.example.com',
})

// Explicit algorithm
auth: jwt({
  algorithm: 'HS384',
  secret: process.env.JWT_SECRET!,
  issuer: 'https://idp.example.com',
  audience: 'https://mcp.example.com',
})
```

**RSA (RS256):**

```ts
import fs from 'node:fs'

auth: jwt({
  algorithm: 'RS256',
  publicKey: fs.readFileSync('./public.pem', 'utf-8'),
  issuer: 'https://idp.example.com',
  audience: 'https://mcp.example.com',
})
```

`issuer` and `audience` are required on every `jwt()` / `jwks()` call: without them the server would accept a token minted by a different IdP, or for a different resource.

**Custom validator:**

```ts
auth: {
  validator: async (token) => {
    const user = await db.verifyApiKey(token)
    if (!user) throw new Error('unknown key')
    return {
      kind: 'api-key',
      scheme: 'api-key',
      subject: user.id,
      name: user.label,
    }
  },
}
```

## OAuth with `oauth()`

The MCP server is an OAuth 2.0 **Resource Server**. `oauth()` verifies bearer tokens, enforces required scopes, and advertises the Authorization Server through RFC 9728 metadata so clients run the authorization flow directly against your IdP. Routecraft mounts no `/authorize`, `/token`, `/register` or `/revoke` endpoints of its own.

**`OAuthFactoryOptions` fields:**

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `verify` | `OAuthValidatorAuthOptions \| OAuthTokenVerifier` | Yes | `jwks(...)`, `jwt(...)`, or a raw `(token) => OAuthPrincipal` for opaque tokens and introspection. Runs on every request |
| `issuer` | `string \| string[]` | Only for a raw `verify` | Authorization Server issuer advertised as `authorization_servers`. Supplied automatically by `jwks()` / `jwt()` |
| `requiredScopes` | `string[]` | No | Every request must carry all of them; a token missing any is refused with `403 insufficient_scope` |
| `clockToleranceSec` | `number` | No | Skew allowed when the server re-checks the verified principal's `expiresAt`. Supplied automatically by `jwks()` / `jwt()`; pass it only when `verify` is a raw function that tolerates skew of its own. Defaults to `0` |

**JWKS-backed verification (recommended):**

```ts
import { mcpPlugin, oauth, jwks } from '@routecraft/ai'

auth: oauth({
  verify: jwks({
    jwksUrl: 'https://idp.example.com/.well-known/jwks.json',
    issuer: 'https://idp.example.com',
    audience: 'https://mcp.example.com',
  }),
  requiredScopes: ['mcp:invoke'],
})
```

`issuer` and `audience` are required on `jwks()` / `jwt()`, so the server cannot silently accept tokens from a different IdP or minted for a different resource. Standard claims (`sub`, `client_id`, `email`, `name`, `iss`, `aud`, `scope`, `roles`, `exp`) map to `OAuthPrincipal` fields automatically; the resolved principal surfaces on the structured `routecraft.auth.principal` exchange header and via the `ex.principal` getter. For non-standard IdPs, pass `claims` mappers to `jwks()` / `jwt()`; see [Securing capabilities](/docs/advanced/securing-capabilities).

Passing `jwks(...)` straight to `auth` works identically. Reach for `oauth()` when you want `requiredScopes` enforcement or an explicit issuer.

**Custom verification (opaque tokens, introspection, etc.):**

```ts
import { mcpPlugin, oauth } from '@routecraft/ai'
import { jwtVerify, createRemoteJWKSet } from 'jose'

const jwks = createRemoteJWKSet(new URL('https://idp.example.com/.well-known/jwks.json'))

auth: oauth({
  issuer: 'https://idp.example.com',
  verify: async (token) => {
    const { payload } = await jwtVerify(token, jwks, {
      issuer: 'https://idp.example.com',
      audience: 'https://mcp.example.com',
    })
    if (typeof payload.exp !== 'number') throw new Error('token has no exp')
    return {
      kind: 'oauth',
      scheme: 'bearer',
      subject: payload.sub as string,
      clientId: payload['client_id'] as string,
      expiresAt: payload.exp,
      claims: payload as Record<string, unknown>,
    }
  },
})
```

`expiresAt` is required on a principal returned through `oauth()`: a principal without a finite numeric expiry has no bounded validity window and is refused. A principal whose expiry has already passed is refused at the gate whichever auth mode produced it. The boundary is inclusive and compared in whole seconds, so a principal whose `expiresAt` equals the current second is already expired, matching RFC 7519 section 4.1.4.

`verify` runs on **every request**. Revision 2026-07-28 is stateless, so there is no session in which a past verification could be cached; keep introspection calls fast or cache them yourself.

## Proxying client tools

The `proxy` option re-exposes tools from registered `clients` through this MCP server without a route per tool. Each entry is a ref string or a config object:

| Ref form | Meaning |
|----------|---------|
| `'server:tool'` | Proxy one tool from a registered client |
| `'server:*'` or `'server'` | Proxy every tool the client advertises |
| `{ ref, name?, description?, annotations? }` | Proxy one tool with overrides |

**`McpProxyToolConfig`:**

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `ref` | `string` | Yes | Tool ref; the server id must be a key of `clients` |
| `name` | `string` | No | Exposed tool name override (`[A-Za-z0-9_-]{1,64}`); invalid on wildcard refs |
| `description` | `string` | No | Description override for `tools/list`; invalid on wildcard refs |
| `annotations` | `McpToolAnnotations` | No | Merged over the remote tool's annotations (per key) |
| `guard` | `ToolGuard` | No | Runs before dispatch with `(args, ctx)`; throw to reject the call as an `isError` result. `ctx.principal` carries the MCP caller's identity (HTTP `auth`). On wildcard refs the guard attaches to every expanded tool. Same contract as the agent's `tools([{ name, guard }])`, except the args are the caller's raw input (no local schema validation runs before a proxy guard). |

Refs are validated when the plugin is created: unknown clients, malformed refs, wildcard renames, and statically duplicate exposed names all throw. Colons beyond the first split stay in the tool segment (matching the agent's `MCP(server:tool)` grammar), so a remote tool named `ns:tool` is addressable as `'server:ns:tool'`. Resolution against the tool registry is live, so wildcard entries follow tool refresh and stdio restarts, and a client whose initial listing failed starts serving as soon as its tools appear.

An exact ref and a wildcard covering the same remote tool compose: the exact entry's overrides and guard apply regardless of config order. On a collision between different remote tools, a local `.from(mcp())` route wins over a proxied tool, and earlier `proxy` entries win over later ones; both log a warning once per registry change. Exposed names must match `[A-Za-z0-9_-]{1,64}`; a remote tool whose own name does not conform is skipped with a warning unless renamed via an exact entry's `name` override.

Proxied calls dispatch over the client's registered transport and auth, and the remote result (`content`, `structuredContent`, `isError`) passes through unchanged, apart from the protocol-revision projection applied to every result so it still matches the schema this server advertised. The caller's authenticated principal is not forwarded, and no route pipeline runs (no `authorize()`, validation, or resilience wrappers); a per-entry `guard` covers identity and role checks. Reserve raw `proxy` entries for simple, read-only tools; put anything needing stateful guardrails behind a `.from(mcp())` route. See [Calling an MCP, re-exposing a client's tools](/docs/advanced/call-an-mcp#re-exposing-a-clients-tools).

See [Expose to an agent](/docs/introduction/expose-to-an-agent), [Calling an MCP](/docs/advanced/call-an-mcp), and [Securing capabilities](/docs/advanced/securing-capabilities) for usage guides.
