Cognito 如何用 CIMD 接入 AI 助手,避免 MCP 伺服器出現大量應用程式用戶端
Letting AI assistants into your MCP server without app client sprawl
作者説明如何以 Cognito 的 CIMD,讓不同 AI 助手登入 MCP 伺服器,而毋須為每個助手建立應用程式用戶端。CIMD 以 HTTPS URL 作為 client_id,並由文件描述用戶端;Cognito 再透過政策和基礎應用程式用戶端限制可用的 URL、grant types 和 scopes。
Picture this: your MCP server on Lambda finally returns real data, so it needs sign-in. Then the first users ask whether they can connect Claude, the second team wants ChatGPT, and someone else uses Copilot. Every one of those assistants needs a client_id in your Cognito user pool. Who creates them, and who cleans them up later?
Until recently, I had no answer I really liked. With Cognito's new support for client ID metadata documents (CIMD), announced on November 19, 2026, that changed. I walk through a complete, tested setup in Secure an MCP server with Cognito: CIMD instead of DCR; here is the short version of why it matters and how it fits together.
Two old options, both with a catch
MCP uses OAuth 2.1, but unlike a classic web app you don't know your clients in advance. Any assistant your users like may show up.
- Pre-registered app clients are clean and controllable, but every new assistant means manual admin work and a waiting vendor.
-
Dynamic client registration (DCR) lets clients register themselves via
/oauth2/register. The downside: per the Cognito docs, registered and regular clients share one quota of 10,000 app clients per user pool, and registered clients stick around until you delete them. Open registration means strangers can fill up your pool. The current MCP specification (2026-07-28) only keeps DCR as a deprecated fallback.
How CIMD flips the model
With CIMD the client describes itself. Its client_id is an HTTPS URL, and at that URL it hosts a small JSON document with its name, redirect URIs and grant types. During sign-in, Cognito fetches and validates that document.
You stay in control with two building blocks:
- One base app client that caps what any CIMD client can get: grant types and scopes are the intersection of app client, document and request.
- A CIMD policy with URL patterns that decides which document URLs are allowed in.
So nothing piles up in your user pool, and a client can write whatever it likes into its document without ever exceeding your app client's limits. A few constraints are worth knowing: CIMD clients are always public (no secret, so no client credentials grant), you get up to ten policies per user pool, and the feature requires the Essentials or Plus plan, not Lite.
The part your server still owns
Cognito handles sign-in and tokens, but your MCP server is the resource server and has three duties: publish protected resource metadata (RFC 9728) under /.well-known/oauth-protected-resource, answer requests without a valid token with a 401 and a WWW-Authenticate header pointing there, and verify that each token was actually meant for it.
That last point is where I see the biggest risk. MCP clients send the resource parameter, and Cognito writes it into the aud claim (resource binding, RFC 8707). If you don't check it, your server happily accepts any valid token from the same user pool, including one issued for a completely different service. A simplified version of the verifier:
import { JwtVerifier } from "aws-jwt-verify";
const verifier = JwtVerifier.create({
issuer: process.env.ISSUER!,
audience: process.env.MCP_RESOURCE!, // the URL of your MCP endpoint
scope: "bookstore-mcp/tools.read",
jwksUri: process.env.ISSUER + "/.well-known/jwks.json",
customJwtCheck: ({ payload }) => {
if (payload.token_use !== "access") throw new Error("Access tokens only");
},
});
I also like distinguishing 401 from 403: a missing scope returns insufficient_scope, which lets a client request more scopes instead of forcing a fresh sign-in.
Where I stay cautious
CIMD moves trust to the client's DNS and web server. Wildcard patterns such as https://*.example.com/client.json are convenient but admit anyone controlling a subdomain, so I keep patterns narrow. And Cognito caches documents for at least a day, which you need to know when a client changes its redirect URIs. Finally, a clean sign-in doesn't protect your tools from manipulated content; that's a separate hardening job.
What the full article covers
- A complete Serverless Framework stack with user pool, resource server and base app client
- Full TypeScript code for token verification, metadata and a minimal MCP handler
- Six local tests that run without AWS access, including the foreign-audience case
- CLI steps to create the CIMD policy and check the server from outside
- Common mistakes around caching, wildcards and the Lite plan, plus an FAQ
Read the full walkthrough on my site: Secure an MCP server with Cognito: CIMD instead of DCR.
📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.
👉 Next step: Not sure which AWS service fits your use case? My AWS service finder asks a few questions and points you to the right one.
📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).
Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en
來源:DEV Community · dev.to