Azure 上 ASP.NET Core 遠端 MCP 伺服器的 OAuth 資源伺服器實作要點
Your remote MCP server is an OAuth resource server, notes for ASP.NET Core on Azure
文章説明如何在 Azure 上以 ASP.NET Core 將遠端 MCP 伺服器按 OAuth 2.1 資源伺服器處理,並使用 Microsoft Entra ID 作為授權伺服器。
This article was drafted with AI help and reviewed by the author.
A local MCP server that runs over stdio on a developer laptop mostly inherits the laptop's trust. A remote one is different. It listens on a public HTTPS endpoint, many clients connect to it, and it usually calls other APIs on behalf of real users.
At that point it is just an API with a new kind of caller. The MCP authorization specification says so directly: a protected MCP server acts as an OAuth 2.1 resource server.
This post goes through what that means for a server written in ASP.NET Core with the official MCP C# SDK, using Microsoft Entra ID as the authorization server. It focuses on the decisions and the failure cases, with example values throughout. The rules come from the specification dated 2026-07-28 and its security best practices page, as published on 27 November 2026. None of this was exercised against a real Entra tenant.
The shortcut to avoid
Say your MCP server exposes a tool that reads a user's open support cases from an internal API. The internal API already accepts Entra tokens. The easy path is to take the bearer token the MCP client sent and forward it unchanged to the internal API.
The specification calls this token passthrough and forbids it. The security best practices page gives the reasons. If the server forwards tokens it never validated:
- The downstream API can't tell the MCP server's calls apart from anyone else holding the token.
- Rate limits and audits keyed on the caller stop meaning anything.
- A token stolen from one place can be replayed through your server into another.
The rule is short. An MCP server must not accept any token that was not issued for it.
Step 1: give the server its own identity and audience
Register the MCP server in Entra as its own API, separate from the downstream API. It gets its own application ID URI, which becomes the audience it expects.
Example values used in the rest of the post:
- MCP server URL: https://cases-mcp.example.com
- MCP server audience: api://cases-mcp
- Scopes on the MCP server: cases.read and cases.write
- Downstream internal API audience: api://case-service
In ASP.NET Core, the JWT bearer handler validates issuer, lifetime, signature, and audience. The one to double-check is audience. It must match the MCP server's own audience exactly. The SDK's protected server sample turns audience validation on and notes that it follows the resource indicator guidance in RFC 8707.
Step 2: publish protected resource metadata
The spec requires MCP servers to implement OAuth 2.0 Protected Resource Metadata, RFC 9728. When an unauthenticated request arrives, the server answers 401 with a WWW-Authenticate header pointing to its metadata document. The client reads that document to learn which authorization server to use and which scopes exist.
The C# SDK's ASP.NET Core package includes an MCP authentication scheme for this challenge, configured with the resource metadata. In the SDK sample, JWT bearer is the default authenticate scheme and the MCP scheme is the default challenge scheme. Fill the metadata with the server URL, the Entra authority, and only the scopes the tools need.
Keep the scope list small. The best practices page lists wildcard scopes and advertising every possible scope as common mistakes.
Step 3: check scopes per tool, not per server
A valid token only proves the caller may talk to the server. Each tool still has to check what the caller may do.
A sensible example mapping:
- listmycases needs cases.read.
- get_case needs cases.read, and the case must belong to the caller.
- addcasenote needs cases.write.
- close_case needs cases.write and an application role such as case.closer.
When a token is valid but lacks the scope for a tool, the spec says to answer 403 with an insufficient_scope error and the required scope in the WWW-Authenticate header. Clients can then run a step-up flow and ask the user for more permission. That is better than one broad consent screen on day one.
Remember that tool annotations such as a read-only hint are hints for the client. The spec says clients must treat them as untrusted unless the server is trusted. Your server enforces permissions in code. The annotation describes, it doesn't protect.
Step 4: call downstream APIs with a new token
The MCP server still needs to reach api://case-service. There are two honest ways.
On-behalf-of. The server exchanges the incoming user token for a new token whose audience is the downstream API. The downstream API sees the MCP server as the client and the user as the subject. Per-user permissions keep working, and the logs say who called through what.
Managed identity. If the downstream call is not user-specific, for example a lookup of reference data, the server uses its own managed identity. On AKS that means workload identity, so no secret lives in the pod. In this mode your server is responsible for checking the user's rights before it makes the call, because the downstream API only sees the server.
Pick one per tool and write it down. Mixing them silently is how a read tool ends up returning another user's cases.
Four requests, four outcomes
Example requests to the same server, with invented tokens.
- No token. The client calls the tools list endpoint with no Authorization header. The server returns 401 with the metadata link. The client starts the OAuth flow.
- A token for the wrong audience. A user pastes a token whose audience is Microsoft Graph. Signature and issuer are fine, but audience doesn't match api://cases-mcp. The server returns 401. Nothing is forwarded.
- Right audience, missing scope. The token carries cases.read. The model calls addcasenote. The server returns 403 with insufficient_scope and names cases.write. The client can ask the user to grant it.
- Everything valid. The token has cases.write. The model calls addcasenote for case 5512. The server checks the case belongs to the user, gets an on-behalf-of token for api://case-service, writes the note, and logs the user's object ID, the tool name, the case ID, and the result.
Hosting notes for Azure
- Statelessness helps. The 2026-07-28 spec removed protocol sessions and the initialize handshake, so each request carries what the server needs. That makes horizontal scaling behind a load balancer simpler. Older clients may still use the previous behavior, so test with the ones you support.
- Origin checks. The Streamable HTTP transport requires servers to validate the Origin header. Only enable CORS if a browser client really needs it, and then with an explicit origin list.
- Logging. Log tool name, caller object ID, client ID, and outcome for every call. Don't log full tool arguments if they can hold personal data. Log a hash or the IDs.
- Rate limits. The tools page says servers must rate limit tool invocations. ASP.NET Core's rate limiting middleware can partition by caller object ID.
Limitations
- Entra's support for newer OAuth pieces used by MCP clients, such as client ID metadata documents, may lag the spec. Check what your clients and tenant support before choosing a registration approach.
- On-behalf-of only works when the downstream API accepts Entra tokens. Other APIs need their own credential flow.
- The C# SDK evolves quickly. Confirm API names against the version you install.
Takeaways
- A remote MCP server is a resource server. Validate audience, issuer, lifetime, and signature on every request.
- Publish protected resource metadata and return proper 401 and 403 challenges.
- Check scopes and ownership per tool. Annotations don't enforce anything.
- Never forward the client's token. Use on-behalf-of or a managed identity with your own checks.
- Log every tool call against the caller's identity.
For readers who want the non-developer view first, meaning what hosts, clients and servers are and why access control matters from day one, there is a clear overview of MCP servers written for business teams.
References
- MCP specification 2026-07-28: Authorization
- MCP specification: Security best practices, token passthrough and scope minimization
- MCP specification: Tools, annotations and security considerations
- MCP C# SDK repository and ProtectedMcpServer sample
- Microsoft identity platform: On-behalf-of flow
- Microsoft Entra Workload ID on AKS
- RFC 9728: OAuth 2.0 Protected Resource Metadata
來源:DEV Community · dev.to