Agents

MCP gateway

Put Scute sign-in, harness guards and tool pinning in front of any MCP server, as a library or a CLI.

@scute/mcp-gateway sits between MCP clients and your MCP server (Streamable HTTP). It:

  • serves the protected resource metadata (RFC 9728) that sends clients to your app's OAuth server, and checks every access token: signature, issuer, audience and expiry;
  • trades each person's access token for a task token of the client's agent (RFC 8693), so every tools/call runs through the agent and person and task check and your harness guards;
  • shows in tools/list only the tools the task could ever use;
  • answers a missing scope with insufficient_scope, so the client asks the person for more access;
  • hashes tool definitions and holds new or changed ones for review (tool pinning);
  • forwards to your server with the gateway's own credential. The person's token never reaches it.

Before you run it#

  1. Turn on MCP sign-in for the app.
  2. Add the gateway's public URL as an MCP server, for example https://mcp.acme.io/mcp.
  3. Link MCP clients to an agent, or set a default agent. The agent's roles are the most a client can do.

Run the CLI#

SCUTE_APP_ID=app_xxx SCUTE_SECRET=YOUR_APP_SECRET \MCP_RESOURCE=https://mcp.acme.io/mcp \MCP_UPSTREAM=http://localhost:9000/mcp \MCP_UPSTREAM_AUTHORIZATION="Bearer UPSTREAM_KEY" \npx @scute/mcp-gateway

The package's binary is scute-mcp-gateway.

VariableRequiredMeaning
SCUTE_APP_IDyesThe app's id or public id
SCUTE_SECRETyesThe app's secret key. Used for token exchange and tool reports.
MCP_RESOURCEyesThe gateway's public MCP URL, exactly as registered in Scute
MCP_UPSTREAMyesThe MCP server behind the gateway
MCP_UPSTREAM_AUTHORIZATIONnoThe Authorization header the gateway sends upstream
MCP_RESOURCE_NAMEnoShown to clients in the resource metadata
SCUTE_BASE_URLnoDefault https://api.scute.io
PORTnoDefault 8787
TRUST_PROXYno1 to build the request URL from X-Forwarded-Proto and X-Forwarded-Host, behind a proxy you run

The CLI runs with the default guard, guards.permissions(). To add guards, use the library.

Use the library#

server.tsts
import { createGateway, nodeListener } from "@scute/mcp-gateway";import { guards } from "@scute/harness";import { createServer } from "node:http";const gateway = createGateway({  resource: "https://mcp.acme.io/mcp",  upstream: { url: "http://localhost:9000/mcp", headers: { authorization: `Bearer ${process.env.UPSTREAM_KEY}` } },  guards: [guards.permissions(), guards.content()],});createServer(nodeListener(gateway.handle)).listen(8787);

gateway.handle is a standard fetch handler, (request: Request) => Promise<Response>, so it runs in any runtime with fetch. In a Next.js route handler:

app/mcp/route.tsts
import { createGateway } from "@scute/mcp-gateway";const gateway = createGateway({  resource: "https://mcp.acme.io/mcp",  upstream: { url: process.env.MCP_UPSTREAM! },});export const POST = gateway.handle;export const GET = gateway.handle;export const DELETE = gateway.handle;

The gateway also answers its resource metadata at /.well-known/oauth-protected-resource plus the resource's path (here /.well-known/oauth-protected-resource/mcp). In a framework with file routes, send that path to the same handler.

nodeListener(handler, { trustProxy }) adapts it to node:http.

Config#

resourcestringrequired
The gateway's public MCP URL, as registered in Scute. It is the token audience.
upstreamobjectrequired
url of the MCP server behind the gateway, and headers the gateway sends it.
appIdstring
Default SCUTE_APP_ID.
secretstring
The app's secret key. Default SCUTE_SECRET. Without it the gateway cannot start tasks or report tools.
baseUrlstring
Default SCUTE_BASE_URL, then https://api.scute.io.
guardsGuard[]
Harness guards run on every tool call. Default [guards.permissions()]. See Harness.
toolsToolsConfig
Harness tool settings, such as which permission each tool needs.
modeMode
Harness mode for the guards.
storeStore
Harness store.
pinningboolean
Hold new or changed tool definitions for review. Default true.
hideUnavailableboolean
tools/list shows only tools the task could ever use. Default true.
humanToolsboolean
List the gateway's own tools for bringing the person in. Default true.
verificationMethodsstring[]
Methods scute_verify_person offers. Default email_otp, sms_otp, totp, entra_push.
resourceNamestring
Shown to clients in the resource metadata.
allowedHostsstring[] | "*"
DNS rebinding protection: requests must come to one of these hosts. Default the resource's own host. "*" turns the check off.
allowedOriginsstring[] | "*"
An Origin header, when sent, must be one of these. Default the resource's own origin.
fetchtypeof fetch
A fetch implementation, when the runtime has none.

What happens on a request#

  1. Discovery

    GET /.well-known/oauth-protected-resource (and the path-suffixed form) answers resource, authorization_servers (your app's issuer), scopes_supported and bearer_methods_supported. A request without a valid token answers 401 with WWW-Authenticate: Bearer resource_metadata="...", which starts the client's sign-in.

  2. Token exchange

    On the first tool call for an access token, the gateway calls POST /v1/oauth/:app_id/token with the token exchange grant and gets a task token for the client's agent. The task lives as long as the access token. A new task starts when it expires. See Token exchange for a task.

  3. Tool calls

    Each tools/call runs through the harness guards with the person's task. A call the guards refuse comes back as a tool error with a message for the model; the upstream never sees it. An allowed call is forwarded with the guards' arguments, and the result goes through the guards' after hooks.

  4. Tool lists

    tools/list drops held tools, drops tools whose permission is outside the task's ceiling (from whoami), and adds the gateway's own tools.

When the MCP server in Scute maps scopes to permissions (scope_actions), a call refused as outside_task whose permission belongs to a scope the person has not granted answers HTTP 403:

WWW-Authenticate: Bearer error="insufficient_scope", scope="tools:read tools:write", resource_metadata="..."

The client then asks the person again with the wider scope.

Bringing the person in#

Next to your server's tools, the gateway lists its own tools and answers them itself, never forwarding them upstream. Each works with the person's task and answers with a say line.

ToolDoes
scute_verify_personSends the person a code or a push
scute_submit_codePasses on the code they read out
scute_check_verificationChecks a push or a link
scute_approval_statusChecks a reviewer's answer
scute_whoamiWho the task works for, what it may do, and what needs verification or approval

When a call needs verification, the model is told to use scute_verify_person and try again. An upstream tool with one of these names is replaced by the gateway's. humanTools: false turns them off. Signing in is not a tool: the client signs the person in when it connects.

Limits and errors#

CaseAnswer
Wrong Host, or an unexpected Origin403
No token, or a token that is invalid, expired or for another server401 with WWW-Authenticate
A JSON-RPC batch that contains a tools/* request400. Send tools/* requests one at a time.
A body over 5,000,000 characters413
Token exchange failed (no agent linked, agent suspended)A tool error naming the reason
A failure while checking the upstream's answerA JSON-RPC error; the unchecked answer is never passed on
Any other gateway failure502 with a JSON-RPC error

Forwarded upstream: the gateway's upstream.headers, plus the client's Accept, Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, traceparent and tracestate. Server-sent event streams pass through event by event; only the event carrying the answer is changed.