Skip to content

@ts-pf/mcp

Opt-in MCP tools from a ts-pf contract. The contract defines the tools, an executor runs them (your app in-process, or a typed client), and the official MCP SDK v2 serves them. A projection plus an executor: no MCP wire code here, and no ts-pf HTTP or message handler is reused.

Agent skill: skills/ts-pf-mcp/. Sync with npx skills experimental_sync -y.

Terminal window
npm i @ts-pf/mcp @modelcontextprotocol/server # the SDK peer is only needed for ./sdk

Three entries:

  • @ts-pf/mcp/meta: mcp({ effect, description, aliases }), dependency-free, safe in contract files.
  • @ts-pf/mcp: the toolkit (createToolkit, all, bindTools, audit, and the host search factories bm25Search / denseSearch / hybridSearch). Depends on @ts-pf/contract, @ts-pf/docs, @ts-pf/server, @ts-pf/protocol.
  • @ts-pf/mcp/sdk: createMcpFactory, a per-request McpServer factory. Optional peer @modelcontextprotocol/server >=2.3.0 <3; keep one copy of it in the app (two copies break createMcpHandler’s instanceof McpServer check with an HTTP 500).

Every procedure becomes a tool named by its path (planet.find → planet_find); docs({ hidden: true }) procedures are skipped. context runs once per call and is typed from the app’s $context<C>().

import { createMcpFactory } from '@ts-pf/mcp/sdk'
export const factory = createMcpFactory(app, { name: 'planets', version: '1.0.0' })

Shape tools, compose toolsets, check coverage

Section titled “Shape tools, compose toolsets, check coverage”

Classify in the contract with .meta({ ...docs({ description }), ...mcp({ effect: 'read' }) }); unclassified tools count as write, and .effect() on the tool overrides mcp({ effect }). Descriptions come from .describe(), then mcp({ description }), then docs() description / summary. mcp({ aliases }) and docs({ tags }) are extra words for host search. Type the context with createToolkit(contract).$context<AppCtx>(): mappers and inject then see AppCtx, and bindTools requires a matching context.

import { all, bindTools, createToolkit } from '@ts-pf/mcp'
import { createMcpFactory } from '@ts-pf/mcp/sdk'
import { contract } from './contract.js'
const tk = createToolkit(contract)
const findPlanet = tk.planet.find
.describe('Get one planet by id. Call planet_list first if you only have a name.')
.errors({ NOT_FOUND: ({ data }) => `No planet ${data.id}. Call planet_list for ids.` })
export const planetsMcp = tk.$toolset({
tools: [all(tk.planet, { exclude: [tk.planet.find] }), { planet_find: findPlanet }],
})
// A new procedure fails this line until a toolset exposes it or `exclude` names it.
tk.$coverage({ toolsets: [planetsMcp] })
export const tools = bindTools(planetsMcp, { app, context: () => ({ db }) })
export const factory = createMcpFactory(tools, { name: 'planets', version: '1.0.0' })

Builder methods: .describe(text), .effect('read' | 'write' | 'destructive'), .inject({ key: fn }) (hidden from the model, filled per call; injected values win over anything the model sends under that key), .input(schema, map) (a new model input; exclusive with .inject), .output(map) / .output(schema, map) (mappers get { context, info, call }, call a typed client for joins; outputSchema and structuredContent only with an object-root schema, and results are then checked: a mismatch is INTERNAL to the model, with the issues logged), .errors({ CODE: fn }) (next-step text for declared codes only; fn gets { data, message, context }), .examples([...]) (appended to the description, and returned by host discovery at detail: 'full'), .visible((info) => boolean) (per-caller lists, below), .scopes([...]) (OAuth step-up, below). tk.$tool({ description, effect, input, run, visible?, scopes? }) is a composite tool over call.

all(node, { effect, tags, exclude, inject, visible, scopes }) makes default tools for a node (effect is one effect or an array; tags keeps tools with any listed tag; exclude takes tools or whole nodes); each inject key goes only to tools whose input has it (union and intersection / allOf members included; a record, loose, or catchall input takes every key), and a typo or a type conflict is a compile error naming the field. A key must be a declared field of some selected input (a record’s index signature does not count, so it cannot hide a typo); to inject into an input that is only a record, use .inject() on that tool (it accepts any key there, so spell it carefully). At runtime all() throws on such an undeclared key, and when inject is set and it cannot read a selected input’s keys (no JSON Schema, or a recursive $ref member); exclude that procedure and bind it with .input(schema, map) (or .inject()). tags and docs({ hidden: true }) filter in the types too when the contract writes them as literals (docs({ tags: ['billing'] })); a widened value (a string[] variable) keeps the tool in the type, and $coverage still checks at runtime.

Large groups go behind a host: tk.$host({ description, tools, split?, visible? }). description is required; the get_* tool’s description starts with it, then names the inner tools (up to 30; above that, their categories). split: 'effect' (default) lists get_<key>_query_capabilities / call_<key>_query_capability (read) and get_<key>_operations_capabilities / call_<key>_operations_capability (write + destructive), leaving out a bucket with no tools; split: false lists get_<key>_capabilities / call_<key>_capability. Pin hot tools flat beside the host. Flat tools do best up to about 15 per toolset; audit() warns above that.

Discovery (get_*) takes { query?, category?, names?, detail?, limit? }. It starts from names (exact; names that don’t exist or are hidden from the caller come back under unknown) or every tool, filters by category (the first path segment under the host’s common prefix: admin.users.ban → users), then ranks by query (below). limit defaults to 5 and is ignored with names. detail is 'name', 'summary' (default; adds description, effect, category), or 'full' (adds inputSchema and examples). The result is { scope, total, categories, tools, unknown? }. call_* takes { name, input }; name is an enum up to 100 tools. get_* is read-only; call_* is read-only only when every inner tool is read, and destructive when any is.

A query ranks each inner tool’s name, description, docs({ tags }), mcp({ aliases }), and category, and keeps only matches. The ranker is a binding option, bindTools(toolset, { …, search }), for every host in the toolset:

  • bm25Search() (default): keywords, in-process, well under a millisecond. A query that shares no word with a tool does not find it.
  • hybridSearch({ embed, minScore? }): BM25 and embeddings merged by reciprocal rank fusion, so “destroy world” finds a tool described as “Delete a planet permanently” and exact words still rank. If embed fails, discovery returns the BM25 results and the error goes to onError.
  • denseSearch({ embed, minScore? }): embeddings only; keeps tools whose cosine similarity to the query is above minScore (default 0). If embed fails, discovery returns an error asking the model to retry or pass names / category.

embed(texts, { kind, signal }) returns one vector per text (number[] or Float32Array, all one length). kind is 'document' for tool text and 'query' for queries, for models that embed the two differently (e5, bge); signal is the caller’s, on queries. Tool vectors are embedded on the first query and kept in memory (a failed attempt is retried on the next query), so bindTools stays synchronous and nothing is spent until someone searches. A query is one embedding plus one dot product per tool: no vector database or external service is needed.

The model can run in the Node process with transformers.js:

import { pipeline } from '@huggingface/transformers'
import { bindTools, type Embed, hybridSearch } from '@ts-pf/mcp'
const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', { dtype: 'q8' })
const embed: Embed = async (texts) =>
(await extractor([...texts], { pooling: 'mean', normalize: true })).tolist()
export const tools = bindTools(planetsMcp, { app, context, search: hybridSearch({ embed }) })

@huggingface/transformers installs onnxruntime-node, a native package of about 300 MB, so this runs on Node, not on edge runtimes. The model (about 25 MB) downloads on first use unless you ship it (env.cacheDir). With this model on a laptop, a cached load took under 100 ms and a query 1–8 ms (the first query also embeds the tools). A hosted embedding API plugs into embed the same way. Example: examples/mcp/src/host-vector-search.ts. A custom ranker is any SearchFactory: (documents) => ({ search(query, ctx) }), returning ids best first.

@modelcontextprotocol/server owns the transport. Over HTTP, pass the verified token as authInfo; it reaches context as info.authInfo. Request headers are not forwarded to procedures; context sees the Request as info.requestInfo and should derive identity from authInfo, not raw Authorization / Cookie. stdio never carries a token, so give identity from the process: serveStdio((ctx) => factory({ ...ctx, authInfo })) with serveStdio from @modelcontextprotocol/server/stdio.

import { createMcpHandler } from '@modelcontextprotocol/server'
import { createMcpFactory } from '@ts-pf/mcp/sdk'
const factory = createMcpFactory(app, { name: 'planets', version: '1.0.0' })
const mcpHandler = createMcpHandler(factory)
export const response = await mcpHandler.fetch(request)

An HTTP MCP server is an OAuth resource server. The SDK checks the token at the HTTP edge, before any MCP message is handled: a client starts sign-in only on a 401 with a WWW-Authenticate challenge, and an error from a tool call does not trigger it. ts-pf takes over from the verified AuthInfo. context maps your IdP’s claims into the app’s context, then the procedure’s own middleware runs as usual.

import {
createMcpHandler,
getOAuthProtectedResourceMetadataUrl,
OAuthError,
OAuthErrorCode,
type OAuthMetadata,
type OAuthTokenVerifier,
oauthMetadataResponse,
requireBearerAuth,
} from '@modelcontextprotocol/server'
import { createMcpFactory } from '@ts-pf/mcp/sdk'
import { PFError } from '@ts-pf/protocol'
const issuer = 'https://idp.example.com'
const resourceServerUrl = new URL('https://api.example.com/mcp')
// Your IdP's RFC 8414 metadata, re-served for clients that probe this origin.
// Fetched once at startup (fails fast if the IdP is down); hardcode it if you prefer.
// An IdP that serves only OIDC discovery has it at /.well-known/openid-configuration.
const oauthMetadata = (await (
await fetch(`${issuer}/.well-known/oauth-authorization-server`)
).json()) as OAuthMetadata
const invalid = (message: string) =>
new OAuthError(OAuthErrorCode.InvalidToken, message)
// Introspection (RFC 7662) as this server's own client; validating a JWT against the IdP's JWKS works too.
// Anything unexpected is `invalid_token` (a 401 challenge), never a crash.
const verifier: OAuthTokenVerifier = {
async verifyAccessToken(token) {
const response = await fetch(`${issuer}/oauth/introspect`, {
method: 'POST',
headers: {
authorization: `Basic ${btoa(`${env.MCP_CLIENT_ID}:${env.MCP_CLIENT_SECRET}`)}`,
},
body: new URLSearchParams({ token }),
})
if (!response.ok) throw invalid('Introspection failed')
const claims = (await response.json()) as {
active?: boolean
sub?: string
client_id?: string
scope?: string
exp?: number
aud?: string | string[]
}
if (!claims.active || typeof claims.sub !== 'string') {
throw invalid('Token is not active')
}
if (typeof claims.exp !== 'number') throw invalid('Token has no expiry')
if (![claims.aud ?? []].flat().includes(resourceServerUrl.href)) {
throw invalid('Token was issued for another resource')
}
return {
token,
clientId: claims.client_id ?? '',
scopes: claims.scope?.split(' ') ?? [],
expiresAt: claims.exp, // required: the SDK refuses tokens without it
resource: resourceServerUrl,
extra: { sub: claims.sub },
}
},
}
const gate = requireBearerAuth({
verifier,
requiredScopes: ['mcp'],
expectedResource: resourceServerUrl, // refuse tokens issued for another audience
resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(resourceServerUrl),
})
// IdP claims → the contract's context. Procedure middleware then sees AppCtx, as on any other transport.
const factory = createMcpFactory(app, {
name: 'planets',
version: '1.0.0',
context: async (info): Promise<AppCtx> => {
const sub = info.authInfo?.extra?.sub
if (typeof sub !== 'string') {
// `extra` is whatever the verifier set; stdio and tests have no token at all.
throw new PFError({ code: 'UNAUTHORIZED', message: 'Token has no subject' })
}
return { db, user: { id: sub } } // or look the user up by IdP subject
},
})
const mcpHandler = createMcpHandler(factory)
export async function fetchHandler(request: Request): Promise<Response> {
// `/.well-known/oauth-protected-resource/mcp` (RFC 9728) and the AS metadata.
const metadata = oauthMetadataResponse(request, { oauthMetadata, resourceServerUrl })
if (metadata) return metadata
const authInfo = await gate(request)
if (authInfo instanceof Response) return authInfo // 401 / 403 with the challenge
return mcpHandler.fetch(request, { authInfo })
}
  • With a toolkit, the same function goes on the binding: bindTools(toolset, { app, context: (info) => … }). .inject() and output mappers get both the mapped context and the raw info.authInfo.
  • A gateway that calls an upstream ts-pf API as the user builds a client per caller: bindTools(toolset, { client: (info) => createClient(…) }), with the token from info.authInfo (exchanged for an upstream one when your IdP requires it). The function runs once per tool call; mapper call shares that client. Upstream errors map as in-process ones: declared codes through .errors(), VALIDATION with the model-facing schema, anything else INTERNAL.
  • context can reject per tool (info.tool); the model gets that error as text. To have the client ask the user for a missing scope instead, declare it with .scopes() (below).
  • Enforce roles and scopes in procedure middleware. .visible() and .scopes() shape what the client shows and asks for; they are not the security boundary.

tk.admin.purge.scopes(['admin']) (also all(node, { scopes }) and $tool({ scopes })) declares the OAuth scopes a tool needs. Over HTTP, when the verified token lacks one, the SDK answers 403 insufficient_scope before the call runs, and the client re-authorizes and retries. The challenge lists the granted scopes plus the required ones, so re-consent keeps what the caller had, and resource_metadata comes from authInfo.resourceMetadataUrl (set by requireBearerAuth({ resourceMetadataUrl }); otherwise derived from authInfo.resource). A host’s call_* challenges per inner tool, so one dispatch tool can front procedures with different scopes; an inner tool hidden from the caller is never challenged (it answers Unknown capability). No token means no challenge (that is the authentication gate’s job); stdio has no OAuth and is never challenged, and tools.call() does not enforce scopes, so middleware still has to. Scopes come from the toolkit (bindTools, then createMcpFactory(tools, …)); createMcpFactory(app, …) declares none. A string that is not an OAuth scope token throws a TypeError when the tool is built. await tools.scopesFor?.(name, args, info) reports what a call needs.

import { all, bindTools, createToolkit, type McpCallInfo } from '@ts-pf/mcp'
import { contract } from './contract.js'
const tk = createToolkit(contract)
const isAdmin = (info: McpCallInfo) => info.authInfo?.scopes.includes('admin') ?? false
export const planetsMcp = tk.$toolset({
tools: [
all(tk.planet, { exclude: [tk.planet.create] }),
{ planet_create: tk.planet.create.visible(isAdmin) }, // only admins see it
],
})
export const tools = bindTools(planetsMcp, { app, context: () => ({ db }) })

.visible(predicate) (also all(node, { visible }), $tool({ visible }), and $host({ visible }) for a whole host) shows a tool only to callers for whom the predicate is true. It gets McpCallInfo (authInfo, requestInfo, era), never context, and may be async. A hidden tool is left out of that caller’s tools/list and is not registered on their McpServer (a call is -32602); tools.call() answers Unknown tool "x". Inside a host, discovery, the get_* description, the call_* name list and annotations follow what the caller may see, and a bucket with nothing visible is not listed. Hide a tool or step up into it with .scopes(), not both. The tools/list cache hint is always cacheScope: 'private'; keep listTtlMs low when visibility depends on scopes that can change.

tools.list(info) and tools.call(name, args, info) work without MCP, in tests or an agent framework. McpTools is { list, call, scopesFor? } (scopesFor is async). McpCallInfo is { authInfo?, requestInfo?, signal?, inputResponses?, era?, tool? }; tool ({ name, path }) is set before context runs, so a project that wants tool-level tracing in interceptors copies it into its own context type.

Call pipeline options live on the executor: bindTools(toolset, { app, context, interceptors?, onError?, maxResultChars?, search? }) or bindTools(toolset, { client, context?, onError?, maxResultChars?, search? }) (client may be (info) => client; context is required when the toolkit’s $context<C>() type has required keys). createMcpFactory(app, …) takes the same options except search (it builds no hosts), plus name, version, instructions, listTtlMs (the tools/list cache hint, default 0); createMcpFactory(tools, …) takes only those four. Unknown throws become INTERNAL: Internal server error. onError(error, info) receives every tool error (and failed host searches); without it, unknown throws and INTERNAL errors are logged with console.error, like FetchHandler (isUnexpectedError from @ts-pf/protocol is that filter, for a custom onError). Text results are cut at maxResultChars (default 25,000).

audit(toolsets, { maxTools?, maxNameLength?, missingDescription? }) returns AuditFinding[] ({ level, tool, message }) for CI: missing descriptions (missingDescription, default 'error'), unclassified tools, schema roots, more than maxTools (15) listed tools, and names over maxNameLength (60) characters or outside the MCP name charset.

Checked against MCP Inspector 2.12 (CLI, HTTP and stdio, both protocol eras) and Claude Code 2.1 (HTTP and stdio; it negotiates 2026-07-28). Tool lists, host tools, structuredContent, error text, truncation, OAuth sign-in, and scope step-up behave as described above. Client quirks worth knowing:

  • The Inspector coerces arguments to the schema’s type before sending ("x" for an integer becomes null), so the VALIDATION text reports the coerced value.
  • Claude Code does not show annotations to the model or use readOnlyHint to skip permission prompts, and it cannot send a value of the wrong JSON type, so the model mostly sees range and format VALIDATION errors. It passes structuredContent to the model as text.
  • mcp
  • createToolkit
  • all
  • bindTools
  • audit, AuditFinding, AuditOptions
  • bm25Search, denseSearch, hybridSearch, Embed, EmbedOptions, DenseSearchOptions, SearchFactory, SearchIndex, SearchDocument, SearchContext
  • createMcpFactory, McpFactoryOptions
  • McpTools
  • McpCallInfo
  • McpToolDefinition
  • McpToolResult
  • MapperOptions (the options argument of a tool .map() callback)

mcp (with McpEffect, McpMeta) is @ts-pf/mcp/meta. createMcpFactory and McpFactoryOptions are @ts-pf/mcp/sdk. The rest are @ts-pf/mcp, which also exports the types that appear in inferred toolkit values (Toolkit, ToolkitNode, ToolBuilder, Toolset, Host, AllTools, CompositeTool, …) so a toolset can be exported under declaration: true.