@tereo-io/mcp
v0.2.1
Published
Expose a Tereo app service's tools over the Model Context Protocol, authenticated by Tereo
Readme
@tereo-io/mcp
Serve an app service's tools over the Model Context Protocol, signed in through tereo. You define tools and name the Tereo permission each needs; the package handles the OAuth resource-server side, permission gating, error handling, logging, rate limiting and the HTTP server, and runs your handler as a known user.
npm install @tereo-io/mcpIt is ESM only and built on Node.js 22. Peer dependencies: @tereo-io/sdk, express 5, zod
4.2 or later (the MCP SDK needs it) and ioredis; @modelcontextprotocol/client is an optional
peer, needed only for @tereo-io/mcp/testing.
Tools and resources are supported; prompts are not.
import { createTereoMcp, ToolError } from "@tereo-io/mcp";
import * as z from "zod/v4";
const mcp = createTereoMcp({
name: "query-service", // MCP server name shown to clients
version: "1.0.0",
core, // CoreClient from createCoreClient(), already started
publicUrl: "https://mcp.example.com", // this server's public origin
allowedClients: ["claude-connector", "chatgpt-connector"],
redis, // for the rate limit; omit only with rateLimit: false
provisioning: document, // optional: the service's provisioning document
});
mcp.define({
name: "list_connections",
description: "List the database connections you can query",
permission: "query.connections.read",
readOnly: true,
handler: async (_input, ctx) => listConnections(ctx.auth.userId),
});
mcp.define({
name: "describe_connection",
description: "Show a connection's tables",
permission: "query.connections.read",
readOnly: true,
input: z.object({ connectionId: z.string() }),
handler: async (input, ctx) => // input: { connectionId: string }
describeConnection(input.connectionId, { signal: ctx.signal }),
});
mcp.define({
name: "run_query",
description: "Run a read-only query against a connection",
permission: "query.queries.run",
readOnly: true,
input: async (ctx) => querySchemaFor(ctx.auth.userId), // per-user schema
handler: async (input, ctx) => runQuery(ctx.auth, input), // input: unknown
});
const server = await mcp.listen(4002); // or: app.use(await mcp.router())
await server.close();Clients sign in through Tereo, so each one needs a login client created there first; see connecting.
createTereoMcp(options)
| Option | Required | |
|--------|----------|---|
| name, version | yes | Reported to MCP clients |
| instructions | no | A non-empty string, or a function of the caller returning one, sent to clients at initialize: how to use this server's tools. See Instructions and titles |
| core | yes | The app's CoreClient. Its service slug is the audience core checks at introspection |
| publicUrl | yes | Public origin of this server, with no path; https unless the host is loopback. The MCP endpoint is ${publicUrl}${path} |
| allowedClients | yes | Non-empty list of client_ids; tokens issued to any other client are refused |
| path | no | Default /mcp. Must start with / |
| redis | unless rateLimit: false | An ioredis client, owned by the caller |
| rateLimit | no | { perMinute } (default 120; a positive integer) or false |
| allowServiceAccounts | no | Default false: client_credentials tokens are refused |
| allowedOrigins | no | Browser origins allowed to call the endpoint, such as https://app.example.com (listen only). See Serving |
| scopes | no | OAuth scopes advertised to clients, typically ["openid", "profile", "email"]; advertised only, never required. See Scopes |
| provisioning | no | The service's provisioning document, for the start-time permission check |
| coreUrl | no | Core's base URL, for fetching its authorization-server metadata at start. Default: the URL core was created with (core.url, which a client from an @tereo-io/sdk before 0.5.2 lacks), else CORE_URL |
| logger | no | A Pino logger. Default a new one named @tereo-io/mcp |
| maxResultBytes | no | Default 1 000 000; a positive integer. A larger result becomes a tool error instead of flooding the model |
| toolTimeoutMs | no | Default 60 000; a positive integer. How long a tool call may run. See Timeouts and cancellation |
mcp.define(tool)
| Field | Required | |
|-------|----------|---|
| name | yes | [a-z0-9_], unique; a duplicate throws |
| title | no | A non-empty display name for people; clients show it in place of name |
| description | yes | Shown to the model |
| permission | yes | A non-empty Tereo permission name, or null for any signed-in user. The hierarchy is the SDK's: write includes read, admin includes all |
| readOnly | yes | Sets readOnlyHint. Required so an author has to state it |
| destructive, idempotent | no | destructiveHint, idempotentHint |
| input | no | z.object(...), or (ctx) => z.object(...) (may be async) for a per-user schema. Omitted: no arguments |
| output | no | z.object(...), static only. The result is validated and sent as structured content too. See Structured results |
| handler | yes | (input, ctx) => value. A string is returned as text; any other JSON value as JSON text |
| timeoutMs | no | A positive integer; overrides toolTimeoutMs for this tool |
| ui | no | { resourceUri, visibility? }: links the tool to a ui:// resource (MCP Apps). See Resources and tool UIs |
| rateLimit | no | { perMinute } (a positive integer) for the tool's own limit, or false for none. Unset: the server-wide limit. See Rate limit |
define throws once the server has started (router() or listen() was called), and when a
field above is out of range; the message names the tool and the field. It also throws for a static
input or an output that JSON Schema cannot express (a transform on the output side,
z.date(), z.bigint(), a custom type): the MCP SDK converts both when it lists tools, and one
such tool would fail tools/list for every caller who can see it.
The handler's input is typed from a static input: with input: z.object({ sides: z.number() })
the handler sees { sides: number } without an annotation, and with no input it sees
undefined. A function input is known only per request, so there the input is unknown unless
you annotate the handler's parameter. With output, the handler's return value is checked
against the schema at compile time.
An input function runs for every protocol message the caller sends (initialize, tools/list,
tools/call), not only for calls, because the server is built per request. Keep it cheap and free
of side effects. If it throws, returns anything but a z.object(...), or returns one JSON Schema
cannot express, the error is logged as MCP tool input schema failed and that tool is left out
for that request; the caller's other tools still list and run.
ctx
| Member | |
|--------|---|
| auth | The SDK's AuthContext: userId, email, name, userType, permissions, clientId, scope, organizationId, … |
| core | The CoreClient, for lookups (groups, users) and outbox.audit |
| requestId | Also on every log line for the call |
| logger | A Pino child logger carrying tool, user_id and request_id |
| signal | An AbortSignal for the call. See Timeouts and cancellation |
Instructions and titles
instructions is sent to clients when they connect, on both protocol eras; clients typically put
it in the model's context, so say there how the tools fit together. A tool's title is listed
with the tool in tools/list for clients to show people; the model still calls the tool by
name.
instructions can also be a function of the caller, for text that depends on who is asking:
const mcp = createTereoMcp({
// …
instructions: async (ctx) => {
// ctx: auth, core, requestId, logger, signal
const sources = await listSourcesFor(ctx.auth.userId, ctx.signal);
return `Query only these sources: ${sources.join(", ")}.`;
},
});The function runs on every initialize (and server/discover) and is not rate-limited, so cache
expensive work. It does not run for other messages, and ctx.signal aborts after 2 000 ms. If it
throws, runs over or returns anything but a non-empty string, the failure is logged and the client
is sent no instructions; the connection still works.
Resources and tool UIs
mcp.resource(def) serves an MCP resource. Any authenticated caller may list and read it: there is
no permission check, so a resource must hold nothing a signed-in user should not see. The server
advertises the resources capability only when at least one resource is defined. uri, name and
mimeType are required, and uri is unique and an absolute URL as URL parsing writes it
(ui://app/card.html, not ui://app/a b.html); resource throws, like define, once the server has
started. title, description and meta (sent as _meta on the listing and on the contents) are
optional, and checked when the resource is defined. read(ctx) gets the same ctx as a tool and
returns { text } or { blob } (base64).
A read is bounded as a tool call is: it counts toward the caller's server-wide rate limit, ends
after toolTimeoutMs (when ctx.signal aborts, as it does when the client disconnects or a
listen server begins closing), and is refused above maxResultBytes. A read that throws, or
returns anything but { text } or { blob } holding a string, is logged at error and the client
gets a generic MCP error; a read that is cancelled, times out or is refused is logged at info or
warn.
mcp.resource({
uri: "ui://query/chart.html",
name: "chart",
mimeType: "text/html;profile=mcp-app",
read: async () => ({ text: chartHtml }),
});A tool's ui links it to such a resource for MCP Apps, and is listed as _meta.ui (with
resourceUri also under the flat _meta["ui/resourceUri"] key that older hosts read). Its
resourceUri starts with ui:// and must be the uri of a defined resource. visibility is
["model"], ["app"] or both; the client hides an ["app"] tool from the model, but the server
still lists it, checks its permission and counts it toward the rate limit.
mcp.define({
name: "show_chart",
description: "Show a saved chart",
permission: "query.queries.run",
readOnly: true,
input: z.object({ id: z.string() }),
ui: { resourceUri: "ui://query/chart.html" },
handler: async ({ id }) => loadChart(id),
});Serving
router() and listen() are async and run the start checks once.
await mcp.router()returns an Express router with the protected-resource metadata routes and the MCP endpoint behind bearer auth. Mount it at the app root (app.use(await mcp.router()), notapp.use("/x", …)): the401challenge and the/.well-knownroutes assumepublicUrlmaps to/. The host app owns Host and Origin validation and body limits; use this package'shostHeaderValidationandoriginValidation(see below). It also owns error handling: install a final JSON error handler, or Express answers a malformed body with its HTML error page, which outside production carries the stack. Mount the router ahead of an app-widecore.auth.requireAuth: its/.well-knownroutes take no token, and its endpoint checks the token itself.await mcp.listen(port, host?, options?)builds a standalone app, bound to0.0.0.0by default, that accepts the host ofpublicUrl(plus loopback names whenhostis loopback), and addsGET /health. It answers a request it rejects (a malformed JSON body, say) with JSON:{ "error": "invalid_request" }and the 4xx status, or500 { "error": "server_error" }. It resolves{ port, url, close }(typeListening):portis the bound port (pass0to pick one),urlthe endpoint as reached locally, andclose()stops accepting connections, waits for in-flight calls to finish, and closes the MCP handler. Calls still running aftershutdownTimeoutMshave their connections dropped andclose()resolves anyway.close()also removes the signal listeners thatlisteninstalled.
Host and Origin validation
hostHeaderValidation(allowedHostnames) and originValidation(allowedOrigins) are Express
middleware for an app that mounts router(), with the checks listen applies. Given the hostname
of publicUrl and the allowedOrigins passed to createTereoMcp, they accept and refuse what
listen does on a non-loopback bind:
import { createTereoMcp, hostHeaderValidation, originValidation } from "@tereo-io/mcp";
const publicUrl = "https://mcp.example.com";
const allowedOrigins = ["https://app.example.com"];
const mcp = createTereoMcp({ publicUrl, allowedOrigins, /* … */ });
app.use(hostHeaderValidation([new URL(publicUrl).hostname]));
app.use(originValidation(allowedOrigins));
app.use(express.json({ limit: "1mb" }));
app.use(await mcp.router());Without allowedOrigins, listen on a non-loopback bind checks no Origin; leave
originValidation out to match.
| Parameter | Each entry is | Compared with |
|-----------|---------------|---------------|
| allowedHostnames | A hostname as URL parsing writes it: lowercase, IPv6 in brackets (mcp.example.com, 127.0.0.1, [::1]), with no scheme, port or path | The Host header's hostname; the port is not compared |
| allowedOrigins | An http or https origin as createTereoMcp takes it (https://app.example.com), a hostname as above, or <scheme>://* for every origin of a non-web scheme (moz-extension://*) | The Origin header's hostname, or its scheme for a <scheme>://* entry; an origin entry's scheme and port are not compared |
Any other entry (https://mcp.example.com as a hostname, a port, a path, https://*, an empty
string) throws when the middleware is made, with a message naming it. hostHeaderValidation
refuses a request whose Host is missing or not listed; originValidation lets a request without
Origin through (only browsers send one) and refuses one that is not listed or cannot be parsed,
such as null. Either answers 403 with a JSON-RPC error body. The lists are copied when the
middleware is made.
Unlike listen, they add nothing to the lists: to accept localhost, 127.0.0.1 and [::1] too,
as listen does on a loopback bind, list them in both. Mount them ahead of the router, and ahead
of anything else that should be protected the same way.
req.auth in the host app
@tereo-io/sdk declares req.auth as its AuthContext, and importing @tereo-io/mcp leaves that
alone. @modelcontextprotocol/express, the MCP SDK's Express package, declares req.auth as its
own AuthInfo, so in a project that imports it directly (for its own middleware, say) that
declaration wins: req.auth.userId does not compile on the app's own routes, and with
skipLibCheck off @types/express-serve-static-core reports TS2430. The value there is still the
AuthContext that core.auth.requireAuth set. Use this package's validation helpers rather than
the MCP SDK's, or, where the import is needed, read the context with
core.auth.validateUser(req), which is typed AuthContext, rather than casting req.auth. On the
MCP endpoint req.auth is the MCP SDK's; a tool gets the AuthContext as ctx.auth.
Shutdown
listen's third parameter (type ListenOptions) controls shutdown:
| Option | Default | |
|--------|---------|---|
| signals | ["SIGTERM", "SIGINT"] | Signals that shut the server down. false: install no listeners; the host handles signals itself and calls close() |
| shutdownTimeoutMs | 25000 | How long close() waits for in-flight calls before dropping their connections; also bounds onShutdown. A positive integer |
| onShutdown | none | Runs after the server has closed, before the process exits: stop the core client, close Redis |
shutdownTimeoutMs bounds the drain and onShutdown separately, so a shutdown can take about
twice that value (50 seconds by default): size a Kubernetes terminationGracePeriodSeconds above
it, or lower shutdownTimeoutMs.
With signals handled, listen owns the exit: on the first signal it logs MCP server shutting down,
runs close(), then onShutdown, and then exits the process with code 0. The exit code is
1 if the drain deadline was hit, if close() or onShutdown threw (logged at error as
MCP server shutdown failed), or if onShutdown had not settled within shutdownTimeoutMs. A
second signal during shutdown exits with 1 at once. Wire it like this:
const server = await mcp.listen(4002, undefined, {
onShutdown: async () => { await core.stop(); redis.disconnect(); },
});The SDK's request handler stops itself on the same signals and leaves the exit to any host that
listens for them, so without this a served process either lingers with its core client stopped,
refusing every token, or is killed mid-call. Tool handlers still running during the drain can no
longer call core, because the handler refuses new work once stopped, so such a call ends as an
internal error. router() installs no listeners and never exits: the host app owns its shutdown.
Browsers send an Origin header; other MCP clients do not, and a request without one is allowed.
With allowedOrigins, listen allows a browser request only from those origins, compared by
hostname (the MCP SDK's rule, so the scheme and port are not checked), plus the loopback hostnames
when host is loopback. Without it, a loopback bind allows only loopback origins and any other bind
checks no origin.
Request flow
- Host and Origin are checked by the standalone app (
listen) or by the host app (router). - Bearer token. No token, or a bad one, answers
401withWWW-Authenticate: Bearer … resource_metadata="…", which is how a client finds Tereo. - Verification. The token is introspected through
core.auth.introspectToken. Core reports a token active to this service only when its audience includes the service's slug. The token is refused when it is inactive, was issued to a client not inallowedClients, belongs to a service account (unlessallowServiceAccounts), or has expired or carries no valid expiry. Every refusal answers401with the same message,Invalid or expired token; the real reason (inactive,client_not_allowed,service_account,expired) is logged at info asMCP token refused. - Per-request server. A server is built for each request with only the tools whose permission
the caller holds. A tool the caller lacks is absent from
tools/list, and calling it is indistinguishable from calling a tool that does not exist. - Tool call. Rate limit, then the handler, then result mapping, then one log line. Input that fails the schema never reaches the handler; the MCP SDK answers with a tool error.
A revoked token or a removed permission keeps working for up to the SDK's introspection cache TTL,
auth.introspectionCacheTtlSeconds on createCoreClient (60 seconds by default). Lower it if that
is too long.
No scopes are required. The protected-resource metadata, at
/.well-known/oauth-protected-resource${path}, names Tereo as the authorization server.
Scopes
The server advertises no scopes by default, so MCP clients request none. Set scopes
(typically ["openid", "profile", "email"]) and they are listed as scopes_supported in the
metadata, so clients request them at sign-in. That is what makes ctx.auth.name and
ctx.auth.email non-null: Tereo puts name in a token's details only with the profile scope and
email only with email.
- The scopes are advertised, not required: a token without them is still accepted, and the
401challenge carries noscopeparameter. - The login client in Tereo must list them in its
scopes, or core will not grant them. offline_accessis never advertised; a resource server must not, andscopesrefuses it.- Without the scopes, look the user up with
ctx.core.auth.getUser(ctx.auth.userId).
Start checks
router() and listen() throw, with a message naming the tool or option, when:
publicUrlis not a valid URL, has a path, query or fragment, or is http on a non-loopback host;pathdoes not start with/;allowedClientsis empty;redisis missing andrateLimitis notfalse;- a tool sets its own
rateLimitandredisis missing; rateLimit.perMinute,maxResultBytesortoolTimeoutMsis given and is not a positive integer;instructionsis given and is neither a non-empty string nor a function;- an
allowedOriginsentry is not an http or https origin (scheme, host and optional port only); - a
scopesentry is empty, contains whitespace, a quote or a backslash, is repeated, or isoffline_access; - with
provisioning, a tool needs a permission the document's service does not declare; - a tool's
ui.resourceUriis not theuriof a resource defined withmcp.resource(); - none of
coreUrl,core.urlandCORE_URLis set, or Tereo's authorization-server metadata cannot be fetched from${coreUrl}/.well-known/oauth-authorization-server; - the metadata's
issueris not https (plain http is allowed onlocalhostand127.0.0.1, as the MCP SDK requires) or has a query or fragment.
A failed start is not cached: the next router() or listen() runs the checks again.
Timeouts and cancellation
Each call has a deadline: the tool's timeoutMs, else toolTimeoutMs, else 60 seconds. When it
passes, the call answers Tool timed out after <n> ms as a tool error, logged at warn with
outcome timeout. Whatever the handler returns or throws afterwards is discarded. The call
still counts toward the rate limit.
ctx.signal aborts:
- at the deadline, with a
TimeoutErroras itsreason; - when the client disconnects, on both protocol eras (the MCP SDK's per-request signal);
- when the client cancels the call, on the 2026 protocol era only. A 2026-era client cancels by
aborting the call's own HTTP stream, which reaches the call. A 2025-era (legacy) client cancels
by sending
notifications/cancelledin a separate POST; since a server is built per request, that notification reaches a fresh server with no such call and is dropped. The handler runs on until it returns, throws or reaches its deadline, and is logged with that outcome; - when a
listenserver'sclose()begins, including the shutdown on a signal, with anAbortError.router()has noclose(), so there the signal aborts only at the deadline, on a disconnect or on a 2026-era cancel.
A handler that rejects with the abort, after a disconnect, a cancel or close() (the signal's
reason, or any AbortError, as fetch throws), ends as outcome cancelled, logged at info with
no err; a client still connected gets the tool error Tool call cancelled. Any other error stays
an internal error.
A timeout answers the client; it cannot stop a handler that ignores ctx.signal, which runs on in
the background, holding whatever connections and locks it has. Pass the signal to database and
HTTP calls (fetch(url, { signal: ctx.signal }), a query's abort option) so the work stops with
the call.
In an input function ctx.signal is the server's closing signal, shared by every request, not
one for the call: read aborted if you need to, but do not attach listeners to it there, or they
pile up for the life of the server.
Structured results
With output, the tool is listed with that schema as its outputSchema, and the handler's return
value is parsed with it. A successful result is sent twice: as structuredContent (the parsed
value; a plain z.object drops unknown keys) and as its JSON in a text block, for clients that
read only text. maxResultBytes applies to that JSON.
mcp.define({
name: "count_rows",
description: "Count the rows in a table",
permission: "query.connections.read",
readOnly: true,
input: z.object({ table: z.string() }),
output: z.object({ table: z.string(), rows: z.number().int() }),
handler: async ({ table }, ctx) => ({ table, rows: await countRows(table, ctx.signal) }),
});A return value that does not match is an internal error: the model sees
Internal error (request <id>), and the log line's err names the failing paths and issue
codes, never the values. A path names only fields the schema declares: a key of a z.record, a
catchall or a z.map comes from the result, so it is written *. Error results (ToolError,
timeouts, cancellations, the rate limit) carry no structuredContent.
The schema must accept its own parsed value, because the MCP SDK validates structuredContent
again with the same schema; one that does not (an .overwrite() that changes the value on every
parse, say) is reported the same way, so the SDK's own message, which may quote values, never
reaches the client. A schema JSON Schema cannot express is refused by define, as above.
Output validation runs after the handler, outside the tool's timeout. Keep output schemas synchronous and cheap: no async refinements, no lookups.
Errors
| Thrown by the handler | The model sees | Logged |
|-----------------------|----------------|--------|
| ToolError(message) | message, as a tool error | info, outcome tool_error |
| An SDK HttpError with status below 500 (ForbiddenError, NotFoundError, …) | Its message, as a tool error | info, outcome tool_error |
| Anything else | Internal error (request <id>), as a tool error | error, with err, outcome internal_error |
A result over maxResultBytes is a tool error too (outcome tool_error), as is a call that runs
past its timeout (outcome timeout, logged at warn) and one cancelled by a disconnect, a
2026-era cancel or close() (outcome cancelled, logged at info; see
Timeouts and cancellation). The wrapper exists so that a database
error or a stack detail never reaches the model.
Logging
One line per tool call, object first: tool, user_id, client_id, org_id, request_id,
outcome (ok, tool_error, internal_error, rate_limited, timeout, cancelled) and
duration_ms. Inputs and
results are never logged; they may be the app's data.
Audit
Tool calls are not written to Tereo's audit log: its actions are change actions only (CREATE
through RESTORE), so a call has nothing to record. Every call is a log line, as above. A handler
that changes data writes its own entry:
handler: async (input, ctx) => {
await prisma.$transaction(async (tx) => {
const item = await tx.item.create({ data: input });
await ctx.core.outbox.audit(tx, {
userId: ctx.auth.userId,
action: "CREATE",
entityType: "Item",
entityId: item.id,
});
});
return "created";
},Rate limit
A fixed one-minute window per user id, counted in Redis under @tereo-io/mcp:<name>:rl:<user>,
120 calls by default. Over the limit the call returns a tool error saying how many seconds to wait
and the handler does not run. Only tools/call and resources/read are counted (a read over the
limit answers an MCP error, and always counts toward the server-wide limit): input functions, which run for every
protocol message, are not limited, and neither is a call whose arguments fail the schema, which the
MCP SDK refuses before the limit is checked. If Redis fails the call goes through and the failure is
logged at error.
A tool can set its own limit instead:
| rateLimit on the tool | |
|-------------------------|---|
| unset | The server-wide limit applies |
| { perMinute } | Its own per-user counter, under @tereo-io/mcp:<name>:rl:<user>:<tool>. Its calls do not count toward the server-wide limit |
| false | Never limited; Redis is not touched |
A tool with its own limit needs redis even when the server-wide rateLimit is false. The
answer over the limit, the outcome and the Redis failure behaviour are the same as above.
Testing
@tereo-io/mcp/testing gives an app's own tests an in-process MCP client and a stub core. It needs
@modelcontextprotocol/client installed and a core client in the SDK's
testMode, so
test tokens are accepted without a core:
import { createTestClient, startStubCore } from "@tereo-io/mcp/testing";
const stubCore = await startStubCore();
const mcp = createTereoMcp({
name: "query-service",
version: "1.0.0",
core, // createCoreClient({ …, testMode: true })
publicUrl: "https://mcp.example.com",
allowedClients: ["claude-connector"],
rateLimit: false,
coreUrl: stubCore.coreUrl,
});
mcp.define(/* … */);
const { client, close } = await createTestClient(mcp, {
token: "test-token-user-1|client=claude-connector|perms=query.connections.read",
});
const { tools } = await client.listTools();
const result = await client.callTool({ name: "list_connections", arguments: {} });
await close();
await stubCore.close();startStubCore() serves only core's authorization-server metadata, on a loopback port, so the start
checks pass; it answers nothing else. createTestClient calls
mcp.listen(0, "127.0.0.1", { signals: false }), so the start checks run, including the fetch of
that metadata, and the test process gains no signal listeners. close() stops the client and the server.
