MCP servers and AI agents
Use your tenant as the authorization server for MCP servers and AI agents: self-registering clients, resource-bound tokens and the SDK's protected-resource helper.
Your tenant can be the authorization server for your MCP servers and for other APIs that AI agents call. An MCP client finds your tenant from the MCP server, registers itself, sends the person to your hosted login, and gets an access token that only your MCP server accepts. 9in follows the MCP authorization specification (revision 2026-07-28).
#How it fits together
- Your MCP server is an API in 9in. Its identifier is the server's URL, such as
https://mcp.example.com/mcp. - An MCP client calls the server without a token. The server answers
401withWWW-Authenticate: Bearer resource_metadata="…", pointing to its protected resource metadata (RFC 9728), which names your tenant as the authorization server. - The client reads your tenant's metadata at
/.well-known/oauth-authorization-server(RFC 8414). It then registers at/oauth/register(RFC 7591), or uses the URL of its own Client ID Metadata Document as its client ID. - The client sends the person to
/oauth/authorizewith PKCE andresource=https://mcp.example.com/mcp(RFC 8707). The person signs in and sees the consent screen. - The client exchanges the code, names the resource again, and calls the MCP server with the access token. The token's
audis the server's URL, so other APIs refuse it.
#Set up your tenant
- APIs & Scopes → Define API. Use the MCP server's URL as the identifier, without a trailing slash, and add the scopes its tools need, such as
files:readandfiles:write. Write the scope descriptions for people: they appear on the consent screen. - AI Agents & MCP. Turn on how apps may introduce themselves:
- Dynamic client registration (RFC 7591). Most MCP clients use this today. The current MCP specification keeps it for compatibility.
- Client ID Metadata Documents. The client ID is an https URL where the app publishes its name and redirect URIs, and 9in reads it. This is the method the current MCP specification prefers.
- On the same page, choose the APIs these apps may get tokens for and the scopes they may ask for. Both are empty or minimal until you choose: a registered app can't get tokens for an API you haven't listed.
The same settings in the tenant configuration:
"clientRegistration": {
"dynamic": true,
"metadataDocuments": true,
"apis": ["https://mcp.example.com/mcp"],
"scopes": ["openid", "profile", "offline_access", "files:read", "files:write"]
}Settings apply per tenant, and registered apps are kept per environment: an app registered on your sandbox issuer doesn't exist in production.
#Protect your MCP server
createProtectedResource from @9in-io/sdk/server does the MCP server's part: it serves the metadata, answers requests without a valid token with the right challenge, and checks that a token was issued for this server with the scopes a call needs.
This example follows the stateless Streamable HTTP pattern of the MCP TypeScript SDK, with Express:
import express from "express";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { createProtectedResource } from "@9in-io/sdk/server";
const mcp = createProtectedResource({
resource: "https://mcp.example.com/mcp", // the API identifier in 9in
issuer: "https://acme.9in.io",
scopes: ["files:read", "files:write"],
resourceName: "Acme Files",
});
const app = express();
app.use(express.json());
// GET /.well-known/oauth-protected-resource/mcp
app.get(mcp.metadataPath, (req, res) => {
res.set("Access-Control-Allow-Origin", "*").json(mcp.metadata());
});
app.post("/mcp", async (req, res) => {
const auth = await mcp.authenticate(req, { scope: "files:read" });
if (!auth.ok) {
return res.status(auth.status).set("WWW-Authenticate", auth.wwwAuthenticate).json(auth.body);
}
const server = new McpServer({ name: "acme-files", version: "1.0.0" });
server.registerTool("whoami", { description: "Who is using this server" }, async () => ({
content: [{ type: "text", text: `user ${auth.claims.sub}, through ${auth.claims.client_id}` }],
}));
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
res.on("close", () => {
transport.close();
server.close();
});
await server.connect(transport);
await transport.handleRequest(req, res, req.body);
});
app.listen(3000);In a fetch handler (Cloudflare Workers, Deno, Bun, Hono), the helper returns ready-made responses:
export default {
async fetch(request: Request): Promise<Response> {
const metadata = mcp.handleMetadata(request);
if (metadata) return metadata;
const auth = await mcp.authenticate(request, { scope: "files:read" });
if (!auth.ok) return auth.response; // 401 or 403 with WWW-Authenticate
return handleMcp(request, auth.claims); // your MCP server
},
};What the helper sends:
| Request | Answer |
|---|---|
| No token | 401, WWW-Authenticate: Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource/mcp", scope="files:read" |
| Expired, revoked (see below), badly signed, or for another API | 401 with error="invalid_token" |
| A token without a scope the call needs | 403 with error="insufficient_scope" and the scopes needed, so the client can ask the person again for more |
A malformed Authorization header | 400 with error="invalid_request" |
Tokens are checked locally against your tenant's keys by default. To make revocations count at once, use mode: "hybrid" with a confidential application's clientId and clientSecret, as in Protecting your API. Every option is in the SDK reference.
Don't pass the token your MCP server receives on to other APIs: the MCP specification forbids it, and those APIs refuse it because of its audience. Give your server its own credentials for them, such as a machine-to-machine application.
#What these apps can do
Apps that register themselves or use a metadata document are always:
- Public clients. They have no secret, use PKCE with
S256, and get the authorization code and refresh token grants only. They can't use client credentials, the device flow, the password grant or Management API scopes. - Third-party. People always see the consent screen, even when the app only asks who they are. The screen says your tenant hasn't verified the app, names the API it wants to use with its scopes, and says where the person goes next: the redirect URI's host, or "an app on this device" for loopback and app-scheme redirects. Consent is remembered per app, scope and API.
- Never sent anywhere unasked. The app chose its own redirect URI, so an error before the consent screen (an unknown scope or resource, a missing PKCE challenge) shows on your hosted error page with its code, such as
invalid_scope, instead of redirecting. After the consent screen, the answer goes to the app as usual, includingaccess_deniedwhen the person declines. - Limited to what you allow. Only the APIs and scopes on the AI Agents & MCP page. An app that registered with a
scopeis also limited to those. - Short-lived when unused. Apps that never get a token are deleted after 7 days. An app that comes back later registers again.
Refresh tokens rotate on every use, as OAuth 2.1 requires for public clients, and the API's setting for refresh tokens applies. The iss parameter is in every authorization response (RFC 9207).
Redirect URIs must be https://…, http:// on 127.0.0.1, [::1] or localhost, or a private-use scheme with a dot, such as com.example.app:/callback. Loopback redirects may use any port at sign-in (RFC 8252). See POST /oauth/register for every field and error.
#Client ID Metadata Documents
When an authorization request's client_id is an https URL, 9in reads the document at that URL:
- The URL must use https with no port, have a path, and name a host, not an IP address or
localhost. - 9in sends one GET with a 5-second timeout, follows no redirects, and accepts JSON of up to 10 KB.
- The document's
client_idmust equal its URL.client_nameandredirect_urisare required, and the rules above apply.token_endpoint_auth_methodmust benone:private_key_jwtisn't supported. - 9in keeps the document for its
Cache-Controlmax-age, between 5 minutes and 24 hours (1 hour without one), and reads it again after that.
The consent screen also shows the host that publishes the document.
#Limits
| Registration attempts per IP address | 30 per hour |
| Registrations per environment | 1,000 per day |
| Registered apps per environment | 10,000 |
A refused registration gets 429 with Retry-After. The registration endpoint is called by software, without a browser, so Turnstile can't apply there. People still sign in through your hosted pages, with your bot protection, before any app gets a token.
#Taking access back
- People see the apps they've allowed under Apps with access on the account page (
/account) and can remove one. That deletes their consent and revokes the app's refresh tokens and opaque access tokens for them. - You see every registered app under AI Agents & MCP, with when it registered, when it last got a token and how many people allowed it. Deleting one revokes its tokens and everyone's consent.
- Turning off dynamic registration or metadata documents stops the apps that used it at once: their client IDs become unknown.
JWT access tokens issued before a removal fail introspection and hybrid verification from then on. A server that only checks tokens locally accepts them until they expire (1 hour by default), so use hybrid mode if that matters.
#What isn't supported yet
- Token exchange (RFC 8693). An MCP server can't yet trade the token it received for a token for another API through 9in. Use the server's own credentials for other APIs.
- One token for several resources. Each token has one audience. Ask for a token per MCP server.
- Reading or updating a registration (RFC 7592). Apps register again instead.
private_key_jwtfor metadata-document clients.
#How 9in maps to the specification
| MCP authorization requirement | 9in |
|---|---|
| Authorization server metadata (RFC 8414) or OpenID Connect Discovery | Both, at /.well-known/oauth-authorization-server and /.well-known/openid-configuration |
PKCE with S256, and code_challenge_methods_supported | Yes; public clients must use it |
| Client ID Metadata Documents | Yes, when you turn them on |
| Dynamic client registration (RFC 7591) | Yes, when you turn it on |
| Resource indicators (RFC 8707) and audience-bound tokens | Yes, one resource per token |
Protected resource metadata (RFC 9728) and WWW-Authenticate challenges | createProtectedResource in @9in-io/sdk/server |
iss in authorization responses (RFC 9207) | Yes |
| Refresh token rotation for public clients | Yes |
| Exact redirect URI matching; https or loopback redirect URIs | Yes, with any port for loopback (RFC 8252) and private-use schemes for native apps |