Docs Packages

@procedures/mcp-server

packages/client-mcp/README.md on GitHub · Package map · Procedures

@procedures/mcp-server serves typed procedures as MCP tools to Claude and to other MCP clients. Its folder in the monorepo is packages/client-mcp.

  • createMcpHost() builds a complete MCP host from a registry, a tool surface and a host policy.
  • McpServerTransport is the MCP face of a ProcedureServer. The host uses it for each face.
  • A procedure can ask the person at the caller for input or a confirmation (MCP elicitation).
  • A procedure can tell the caller the progress of a long call. The caller can cancel a call.
  • A procedure can declare the fields of its output that hold text from outside the host. The host shows that text in envelopes.

Contents#

Install#

npm install @procedures/mcp-server @procedures/client

The package needs Node.js 22 or later.

Start a host#

createMcpHost() builds the registry, the expose rule, the host policy, the ProcedureServer and a transport for each face. It starts nothing: start() starts the faces.

import { createRegistry } from "@procedures/client";
import { createMcpHost } from "@procedures/mcp-server";

const host = createMcpHost({
  registry: createRegistry(appModule),
  surface: { namespaces: ["app"] },
  readOnly: process.argv.includes("--read-only"),
  name: "app",
  version: "0.1.0",
});
await host.start();
console.error(`${host.tools().length} tools over stdio`);

The options:

OptionDefaultFunction
registrythe global registryThe procedures of the host.
modulesnoneModules of procedures. Without registry, the host makes a registry of them. With registry, the host registers them in it.
surfaceeach procedureThe tools: a surface ({ namespaces, exclude }), keys and patterns (["git.status", "fs.*"]), or a function of the path. A list of keys gives a subset of tools.
excludenoneKeys that are not tools, also when the surface matches them.
readOnlyfalseWith true, the tools are only the procedures that change nothing. Each call has that capability.
policystrict callsMore of the host policy (hostPolicy() of @procedures/client): strictCalls, timeout, interceptors, capability, resources, token, browser. Refer to Strict calls.
interceptorsnoneInterceptors after policy.interceptors. A function gets the registry and the expose rule of the host.
serverOptionsnoneThe options of ProcedureServer that the policy does not give: autoRegister, collections, idempotency.
name, versionprocedure-server, 1.0.0The server info in the MCP handshake.
instructionsnoneThe instructions of the server. The MCP client gives them to the model.
faces{ stdio: true }The faces of the host. Refer to The faces.
elicitationask the personThe rules of the questions to the person. Refer to Questions to the person.
untrustedtag untrusted-contentThe envelopes of text from outside the host.
contentnoneA hook that gives other text blocks for a result.
jsonprettyThe JSON of the default text blocks: pretty (an indent of 2 spaces) or compact (one line, fewer tokens).
toolFilternoneMore filters of the tools: tags and internal procedures.
toolNamegit.statusThe name of the tool of a procedure, a function of the path. (path) => path.join("_") gives git_status. A server that moves to the host keeps its tool names.
streamLimits100 items, 30 secondsThe limits of a stream that can continue without an end.
debugfalseWith true, the host writes debug lines to stderr.

The host has these parts:

PartFunction
registry, policy, serverThe registry, the HostPolicy and the ProcedureServer of the host.
exposeThe expose rule: the procedures that are tools.
transportsOne McpServerTransport for each face.
start(), stop()These functions start and stop the faces. The calls in flight get the grace time. A host stops one time.
closedA promise. It resolves when the host stopped.
tools(), state()The tools of the host at this time, and the state of each face.

The host does not build the options of ProcedureServer by hand. hostPolicy() and procedureServerOptions() of @procedures/client give them. The dev-tools server (mark mcp) and mcp.serve use createMcpHost() too.

Strict calls#

Strict calls are the safe default of an MCP host. A procedure of code without metadata.calls can use only the tools of the host in its nested calls. Thus a tool cannot reach a procedure that the host does not expose. A procedure with metadata.calls can also use the procedures that it declares.

To turn strict calls off, give policy: { strictCalls: false }. Turn them off only for a host whose tools you trust with each procedure of the registry.

The faces#

Each face is one McpServerTransport on the server of the host. A host can have more than one face.

FaceFunction
stdio: trueThe standard input and output. Over stdio, console.log writes to stderr from start() on, because stdout carries the protocol.
stdio: { stdin, stdout, stopOnEnd, redirectConsole }Other streams, for a test or an embedding. With stopOnEnd: true (the default), the host stops when the input ends.
http: { path, allowedOrigins, allowedHosts, maxSessions, maxBodyBytes }Streamable HTTP on a listener of the app. Mount host.handleHttp on the listener. The face takes the browser checks of the policy, and the server checks the token of the policy. A deprecated token of the face becomes the token of the policy.
sdkOne or more MCP SDK transports, for example the InMemoryTransport of a test.

When the client closes the input of stdio, the host stops, and closed resolves.

Each request over Streamable HTTP has one check of its credentials (CLI-2b). The server of the host admits the request one time (admission()), with the bearer token, the cookies, the headers and the query of the request. The admission uses the request hook of the policy (principalOf), the token check and the authenticate function of the server. Each request gets it: initialize, a tool call, the GET stream and the DELETE of a session. Each tool call of the request uses the admitted credentials, so the server does not do its checks again for it.

When the server reads cookies, the face trusts a request with a cookie only from a named origin (CSRF). A call over stdio or over a given SDK transport is local: it has the local marker LOCAL_CREDENTIALS (CLI-1). The peer of stdio is the parent process. Thus a host with a token serves the calls of its stdio face.

The Streamable HTTP face checks each request before the request reaches the MCP SDK. The face has no token check of its own.

  • The browser checks come first, with guardMcpRequest. Each request must name a loopback host, an IP address or a host of allowedHosts (the default is browser.allowedHosts of the policy) in its Host header, on each listener. Thus DNS rebinding cannot reach the tools. An Origin header must name the server or a permitted origin (the default is browser.allowedOrigins of the policy).
  • Then the admission of the server checks the credentials. With a token, each request sends Authorization: Bearer <token>. The scheme has any case, for example bearer (RFC 7235). The check is tokenMatches of the core: its time does not tell the token. A refused request gets 401 with a WWW-Authenticate header.
  • The token of a host is the token of its policy. A token of the HTTP face of createMcpHost becomes the token of the policy, and two different tokens stop the host with an error. McpServerTransport refuses a token in its HTTP options: give the token to the server. Refer to Deprecated token options.
  • guardMcpRequest refuses a token option. The first call with it throws a TypeError that names the migration. Each later call with it gives a refusal with the status 500, without an error. Thus each request stays refused, and the log gets one error.
  • Only an initialize request starts a session. Thus a stray POST makes no MCP server. The body of each request has a limit of 4 MiB (maxBodyBytes).
  • The face keeps at most 256 sessions by default (maxSessions, CLI-51). A new session above the limit closes the session that was idle longest: no task in progress and no open response (a call or a GET stream). When each session is busy, the new session gets 503 with a Retry-After header. A client that sends to a closed session gets 404, and a client of the MCP specification then sends a new initialize.
  • Each live client of the MCP SDK keeps its GET stream open. Thus the limit does not close the session of a live client, and 256 is much more than the clients of one host. To serve more clients, give a larger maxSessions. maxSessions: Infinity turns the limit off.
  • assertLoopbackBind(host, allowRemote) refuses a listener on an address that is not loopback, unless allowRemote is true.

Deprecated token options#

The server of the face checks the credentials of each request one time (CLI-2b, decision D13). The face has no token check of its own. The token options of the face have these behaviors:

OptionBehaviorReplacement
faces.http.token of createMcpHost()It becomes the token of the policy. A different token in the policy throws an error.policy.token
httpOptions.token of McpServerTransportstart() throws an error.new ProcedureServer({ token })
token of mcpFace()A token that is not the token of the policy throws an error.hostPolicy({ token })
token of guardMcpRequestThe option is removed. The first call with it throws a TypeError, and each later call with it gets a refusal with the status 500. The function does only the browser checks.The token check of the server, and admission() of the server for each request

A host under the runner#

host.declare(svc) gives the host to a service of @mark1russell7/service (CLI-11). Then run() of the service starts the host, owns the signals, stops the host in stages and ends the process. The host does not start or stop by itself.

  • The server of the host is the transport part mcp. Each storage of the server is a dependency.
  • A host with a stdio face declares the trigger stdin-close. The end of the input starts the stop in stages, and the face does not stop the host by itself.
  • The HTTP face has no listener. Mount host.handleHttp on an httpTransport of @mark1russell7/service/http, and declare that transport on the service.
  • At the stop of the intake, the HTTP face ends the idle GET streams, as mcpFace() does (CLI-72). Thus the drain of the listener waits only for the calls and the tasks. Refer to The face of a procedures host.
  • The HTTP face stays on the listener through the drain, as mcpFace() does (CLI-74). host.handleHttp has until: "close transports", so mount the function itself. A function that wraps it loses until. Then give { until: "close transports" } to mount().
import { createServer } from "node:http";
import { ephemeral, localDev, service } from "@mark1russell7/service";
import { httpTransport } from "@mark1russell7/service/http";

// Over stdio: the profile ephemeral and the trigger stdin-close
const stdioHost = createMcpHost({ registry, faces: { stdio: true } });
await stdioHost.declare(service("my-mcp").profile(ephemeral)).run();

// Over Streamable HTTP: a listener of the service, and the face at /mcp on it
const httpHost = createMcpHost({ registry, policy: { token }, faces: { http: {} } });
const listener = httpTransport(createServer(), knobs, { listen: { port: 3003, host: "127.0.0.1" } });
// The function has the stage of its mount: the face stays on the listener through the drain
listener.mount(httpHost.handleHttp);
const svc = httpHost.declare(service("my-mcp").profile(localDev));
svc.server("my-mcp.listen", listener.knobs, listener);
await svc.run();

The localDev profile has no policies of readiness, liveness and startup. Declare them on the service before run(). The dev-tools server of @procedures/mcp-dev-tools (runDevToolsServer()) is an example of each mode.

Questions to the person#

A procedure can ask the person at the caller for input or a confirmation during a call. MCP elicitation is one face that answers. The shape is not specific to MCP: another face (a terminal) can answer the same questions.

import { elicitConfirmation } from "@procedures/client";

handler: async (input, ctx) => {
  const answer = await elicitConfirmation(ctx, `Did you give the URL ${input.url} yourself?`, {
    consent: "user-provided",
  });
  if (!answer.confirmed) return { released: false, note: answer.reason ?? "The person said no." };
  return { released: true, by: answer.by };
};

The parts in @procedures/client:

  • ctx.elicit is the function of the face. It is absent when no one can answer. Its modes tells which kinds of question the face can ask: form, url.
  • elicitInput(ctx, request) asks a question with a form of fields. The fields are the primitive fields of MCP: text, number, yes or no, and choices.
  • elicitConfirmation(ctx, question) asks a question of yes or no. confirmed is true only for an accepted yes.
  • The procedures that a procedure starts ask the same person.

Each answer has an action:

ActionMeaning
acceptThe person, or the consent of the operator, gave the answer. by tells who: person or operator.
declineThe person said no.
cancelThe person closed the question without a choice, or the call aborted.
unavailableNo one can answer. reason tells why. The helpers do not throw for this case.

The operator can give a consent when the server starts. Thus a server refuses input from the user without a consent of the operator. The host option elicitation has the rules:

OptionDefaultFunction
asktrueWith false, the face does not ask the person, also when the client can.
operatorConsentnonetrue for each question with a consent key, or a list of consent keys.
timeout5 minutesThe time that the face waits for the answer of the person.
const allow = process.argv.includes("--allow-user-provided") || process.env.POLITE_CRAWLER_ALLOW_USER_PROVIDED === "1";
const host = createMcpHost({
  registry,
  elicitation: { operatorConsent: allow ? ["user-provided"] : [] },
});

The order of the answers:

  1. When the client can ask (it declares elicitation), the face asks the person. A decline of the person stays a decline.
  2. When the question of the face fails (a timeout, a lost connection), the answer is unavailable. The operator does not answer: the person can see the question, and silence is not a consent.
  3. When the client cannot ask, the consent of the operator answers. The operator answers only a form question with a consent key that the operator gave.
  4. Else the answer is unavailable, with a reason.

Over Streamable HTTP, each question goes on the stream of its tool call.

Progress and cancel#

A procedure tells the caller the progress of a long call with ctx.progress:

handler: async (input, ctx) => {
  for (const [index, url] of input.urls.entries()) {
    await fetchPage(url, ctx.signal);
    await ctx.progress?.({ progress: index + 1, total: input.urls.length, message: url });
  }
  return { done: input.urls.length };
};
  • ctx.progress is present only when the client sent a progress token. Each update becomes a notifications/progress message.
  • Only the call of the host gets ctx.progress, not its nested calls. Thus the values of one call stay in order.
  • The function does not reject. A client that is gone does not stop the call.
  • A stream does not need it: the face gives each item of a stream as progress. When the procedure of a stream uses ctx.progress, the face gives only those updates.
  • When the client cancels a call (notifications/cancelled), ctx.signal of the procedure aborts.

Tasks#

A long call can run as an MCP task (MCP 2025-11-25). The client gets a task at once, polls its status, gets the result later, and can cancel the task. Two parts turn it on:

  • The procedure declares it in its metadata: taskSupport: "optional" (a task or a normal call) or taskSupport: "required" (only a task).
  • The host has a task store: the option tasks.
const crawlSite = createProcedure()
  .path(["crawl", "site"])
  .meta({ description: "Crawl a site", effects: { reads: ["net"], writes: ["store"] }, taskSupport: "optional" })
  // input, output and handler as for each procedure
  .build();

const host = createMcpHost({ modules: [crawlModule], tasks: { maxRunning: 4 } });
await host.start();
host.tasks?.running(); // the tasks that did not end, for a status page
  • The tool list gives execution.taskSupport of each such tool. Without the option tasks, the list does not give it, and each call is a normal request.
  • A progress of the procedure becomes the status message of its task, for example 2 of 5 done. The message holds only the counts, so no text of the work leaves its envelopes. A client that sent a progress token also gets the progress notifications.
  • A cancel of the task (tasks/cancel) aborts ctx.signal of the procedure. The close of a session cancels its tasks, and the stop of the host cancels each task.
  • Each task belongs to its session. A request of another session does not find it. A task without a session (stdio, an SDK transport) is visible only to the requests without a session. Such a request does not find the tasks of the HTTP sessions.
  • The store runs at most maxRunning tasks at one time (8 by default). A task holds its place until its procedure returns, also after a cancel. A task above the limit starts, and then fails at once. Thus the client gets a task, and the result of the task gives the reason. A full store is different: refer to maxStored below.
  • The time to keep a task (ttl) starts when the task ends. The store does not remove a task whose procedure still runs.
  • The store keeps at most 100 tasks for one session, and at most 1000 tasks for all the sessions together (maxStored, CLI-51). A new task removes the oldest task of its own session that ended. The store can be full, and the session can have no task that ended. Then the request of the new task gets the error -32600, and the store keeps nothing of it. Thus a client cannot remove the results of another session. maxStored: Infinity turns the total limit off.
  • The fair share (CLI-69) keeps one client from the whole store. The store keeps the last reserve slots (100 of 1000 by default) for the clients below their share. The rules follow this list.
  • The function shareKey gives the client of a task. The default mcpTaskShareKey gives the principal of the request, else the session. The token of the host is the same for each caller, so it does not identify a client.
  • The timers of the store use its clock. A test gives a fake clock.
  • The call of a task cannot ask the person: the procedure gets no ctx.elicit.
  • A normal call of an optional tool runs in the request, with the progress and the cancel of the request. A normal call of a tool with taskSupport: "required", or a task for a tool without task support, gets the error -32601.
  • To share one store with other parts, give tasks: { store: new McpTaskStore({ maxRunning: 4 }) }. The host closes only a store that it made.

The fair share applies these rules to a new task of a client:

  1. The share of the client is maxStored / (k + 1). k is the number of clients with tasks in the store, the new client included. The + 1 keeps a share for the client that comes next.
  2. When reserve slots or fewer are free, a client with its share or more gets no more tasks. Its new task replaces the oldest task of its session that ended. Without such a task, the request gets the error TASKS_FULL.
  3. A client below its share gets a task while the store has a free slot.

Thus one client keeps at most maxStored - reserve tasks, also with many sessions under one principal. A session of another client still gets a task. reserve: 0 turns the fair share off. The principal comes from identify of the handler of the face: the request hook, the token check and the authenticate function of the server.

// One key for each tenant: the tasks of all the users of a tenant count as one client
const host = createMcpHost({
  modules: [crawlModule],
  tasks: { shareKey: ({ principal, sessionId }) => String(principal?.claims?.["tenant"] ?? sessionId ?? "local") },
});

Text from outside the host#

A procedure that gives text from outside the host (a web page, a mail) declares the fields of its output that hold it. Then a model gets that text apart from the data, as data and not as instructions.

import { untrustedText } from "@procedures/client";

createProcedure()
  .path(["crawl", "read"])
  .meta({ description: "Read a stored page", untrusted: ["web_content"], effects: { reads: ["store"], writes: [] } })
  .handler(async (input) => ({
    doc: input.doc,
    web_content: [untrustedText(page.text, { url: page.url, capture: page.capture })],
  }));
  • The value of an untrusted field is a text, an UntrustedText with its source, or a list of them.
  • The first text block of the result has the data as JSON, without the untrusted fields.
  • The second text block has a notice, then one envelope for each text: <untrusted-content id="..." url="..." capture="...">.
  • A random id marks the start and the end of each envelope of one answer. A text cannot hold the name of the tag, in any case and with any dash or space. Thus a text cannot close its envelope.
  • In the structured content and the output schema, each untrusted field is a string: the text of its envelopes.
  • The host option untrusted changes the tag (tag: "untrusted-web-content") and the notice. Its wrap function replaces the envelopes.
  • createUntrustedEnvelopes() gives the same envelopes for other uses.

The untrusted fields of a procedure stay untrusted in the output of each caller: client.chain, a procedure of procedure.define, or a procedure of code.

  • The face finds these fields at each depth of the output. The first block has the data without them, and the second block has their envelopes.
  • The attribute field of each envelope gives the path of its field, for example pages.0.web_content.
  • In the structured content, each text of a nested field is in its envelope, and the field keeps its shape. Thus the content agrees with the output schema.
  • The HTTP and WebSocket faces give the output as it is.

The result of a tool#

  • A tool whose output schema is an object has an outputSchema. Each result then has structuredContent, and a text block for clients that do not read it.
  • The hints of a tool (readOnlyHint, destructiveHint, idempotentHint, openWorldHint) come from the declared effects of its procedure.
  • metadata.toolHints of a procedure can make its tool look more dangerous than its effects. Refer to Hint overrides.
  • metadata.title of a procedure gives the title of its tool.
  • A failed call gives isError with the code, the flag retryable and the reason in _meta. A failed stream gives its code and its flag too (CLI-48).
  • A call that a limit of the host refuses gives the code RATE_LIMITED, and _meta also has retryAfterMs: the wait before a retry (CLI-40).
  • The host option content can give other text blocks for a result that is a value. It gets the tool, the value and the default content, and it gives undefined for the default.
  • The host option json: "compact" gives the JSON of the default blocks on one line. Use it in place of a content hook for that change only.

Hint overrides#

The declared effects stay the truth: the host enforces the effects, not the hints. An override can only make a tool look more dangerous:

HintThe only value of an overrideMeaning
readOnlyHintfalseThe tool can change something.
idempotentHintfalseA second call can change more.
destructiveHinttrueThe tool can delete or overwrite data.
openWorldHinttrueThe tool can reach systems outside the host.
createProcedure()
  .path(["notes", "read"])
  .meta({ description: "Read a shared note", effects: { reads: ["store"], writes: [] }, toolHints: { openWorldHint: true } });

The registry refuses each other value when the procedure registers, also when the effects give the same value. The type ToolHintOverride refuses it before the registry does. toolHintsOf() of @procedures/client gives the hints of a tool with its override.

The transport#

McpServerTransport is the MCP face of a ProcedureServer. createMcpHost() makes one for each face. Use it directly to add an MCP face to a server of your own:

import { ProcedureServer, createRegistry } from "@procedures/client";
import { McpServerTransport } from "@procedures/mcp-server";

const registry = createRegistry(myModule);
const server = new ProcedureServer({ registry, autoRegister: true });
server.addTransport(new McpServerTransport(server, { registry, transport: "stdio", serverInfo: { name: "my-server", version: "1.0.0" } }));
await server.start();
  • The tools follow the registry. When the list of tools changes, the transport sends notifications/tools/list_changed.
  • The options instructions, stdio, elicitation, untrusted and content are the same as the options of the host.
  • toolFilter selects the tools: include (a function of the path), pathPrefix, includeTags, excludeTags and excludeInternal.
  • transport: "http" with httpOptions gives Streamable HTTP. The endpoint has no listener: mount handleHttp on a listener. sdkTransport connects an MCP SDK transport.
  • transport: "sse" is not implemented.

The face of a procedures host#

mcpFace() gives the MCP face of a procedures host (createProcedureHost() of @procedures/peer). The face serves Streamable HTTP at a path of a listener of the host, next to the other faces.

import { mcpFace } from "@procedures/mcp-server";

const faces = {
  http: true,
  mcp: mcpFace({ serverInfo: { name: "app", version: "0.1.0" } }),
  // A read-only MCP face of the same server
  mcpRead: mcpFace({ path: "/mcp-read", serverInfo: { name: "app", version: "0.1.0" }, narrowing: { effects: { writes: [] } } }),
};
OptionDefaultFunction
path/mcpThe path of the endpoint.
tokenthe token of the policyDeprecated. The server of the host checks the token of the policy. Another token stops the factory with an error (CLI-2b). Refer to Deprecated token options.
allowedOriginsthe origins of the policyThe browser origins that can send requests to the endpoint: a list, "*" or a function.
narrowingnoneA narrower capability for each call of the face, as a CapabilityRequest or a list of them.

The face adds narrowing to each call as one more source of the narrowing key (procedure-narrowing). Thus the server applies the meet of all the sources: the capability of the policy, the request hook and the key of the caller.

  • The read-only mode is { effects: { writes: [] } }. The face then lists and starts only the tools that change nothing, as mark mcp --read-only does.
  • A narrowing can only narrow. A part that asks for more than the policy gives has no effect.
  • The face lists only the tools that the narrowed capability permits. The time of a narrowing limits each call, not the tools.
  • A narrowing that is not valid stops the factory with an error. Examples are an unknown field and a clearance outside the order of the policy.

The face stops with the host (CLI-72). At the stop of the intake, the face does these steps:

  • It ends the GET stream of each session without a task in progress. The response ends in the protocol, so a client of the MCP SDK connects again, to the next server.
  • It keeps the GET stream of a session with a task in progress. The progress and the end of the task go on that stream. The stream ends with the work of the task.
  • It refuses a new session and a new GET stream with the status 503 and a Retry-After header. The other faces refuse new work in the same way. A POST or a DELETE of a session goes on, so a call in progress completes.

Thus the drain of the listener waits only for the calls and the tasks. Before, each GET stream stayed open until the close of the face. Thus each stop with a connected client waited for the full cap of the drain.

The face stays on the listener through the drain (CLI-74). Its handler has until: "close transports", as a RequestHandler of @mark1russell7/service/http. Thus the listener stays open until the stage close transports. During the drain, the face does these steps:

  • It gives the answer of an elicitation, a cancel and a ping to the session. Thus a call that asks the person can complete.
  • It refuses a new session and a new GET stream with its 503. A new tool call gets UNAVAILABLE from the server, and its procedure does not start.
  • It ends the SSE stream of a cancelled call. The SDK sends no answer to a cancel. Before, that stream held the drain of the listener until its deadline.

Each other path of the listener gets 503 with Retry-After, and a new connection gets an answer. The cap of the drain does not change. Before, the listener closed at the stop of the intake. Thus a call that waited for an answer of the person waited until the cooperative abort.

The procedures of this package#

ProcedureFunction
mcp.serveThis procedure starts an MCP host over stdio with createMcpHost(). Without namespaces, the tools come from mcpNamespaces and mcpExclude of the bundles (default @procedures/bundle-mcp). Without a list, no procedure is a tool.
mcp.list-toolsThis procedure lists the procedures of the registry of the caller as MCP tools.

exposeRule(), mergeSurfaces() and importBundles() give the tool surface of some bundles.

Set up Claude#

@procedures/mcp-dev-tools is a ready MCP server with this package. Add it to the MCP configuration of Claude Code or Claude Desktop:

{
  "mcpServers": {
    "dev-tools": { "command": "npx", "args": ["-y", "@procedures/mcp-dev-tools"] }
  }
}

The README of mcp-dev-tools has the configuration.

License#

MIT