AI
AgentOps MCP: validate protected resource metadata before reconnecting the agent
A production runbook for qualifying MCP OAuth discovery, audience, scopes and negative canaries before reconnecting an agent or weakening authentication.
A production agent can no longer call an MCP server after a URL rotation or authorization change. The server returns 401, the client loops through sign-in, and the team considers forwarding an available token or temporarily disabling audience validation.
Either workaround can turn an OAuth discovery failure into cross-service token replay. Before changing permissions, prove which resource the server protects, which authorization server can issue its token, which audience the backend validates, and which scopes the tool actually needs.
The running case is an operations agent calling https://mcp.ops.example.net/mcp. After the service moves behind a new gateway, both state reads and change preparation fail. The runbook must end with a decision: repair metadata, correct the client, keep the block in place, or return to the previous endpoint.
Freeze the contract before restarting authentication
Preserve one failed request, its UTC timestamp, exact MCP URL, client, requested tool, and correlation ID. Do not record a complete token or authorization code.
incident: INC-MCP-AUTH-117
agent_release: ops-agent-2026.11.01-2
mcp_endpoint: https://mcp.ops.example.net/mcp
tool: changes.prepare
expected_resource: https://mcp.ops.example.net/mcp
expected_authorization_server: https://login.example.net
expected_scopes:
- mcp.tools.read
- changes.prepare
observed:
status: 401
correlation_id: mcp-7e91c
first_failure_utc: 2026-11-01T16:42:00Z
rollback_endpoint: https://mcp-v1.ops.example.net/mcp The URL is part of the security contract, not just a network address. A hostname, path, or proxy change can alter the resource identifier requested by the client or the audience enforced by the server.
Read the 401 challenge as evidence
A protected MCP server acts as an OAuth resource server. A request without a token should return a WWW-Authenticate challenge that lets the client locate protected resource metadata.
MCP_URL="https://mcp.ops.example.net/mcp"
curl --silent --show-error \
--dump-header challenge.headers \
--output challenge.body \
--request POST "$MCP_URL" \
--header "Content-Type: application/json" \
--data '{"jsonrpc":"2.0","id":"auth-probe-1","method":"tools/list"}'
grep -i '^www-authenticate:' challenge.headers The expected challenge uses the Bearer scheme and advertises a resource_metadata URL. Verify four things before following it: HTTPS, the expected hostname, no redirect to an unexpected domain, and consistency with the endpoint being called.
A 401 without a usable challenge is not evidence that the client needs more scopes. It first shows a broken discovery contract among the MCP server, gateway, and client.
Qualify protected resource metadata
Fetch the URL advertised by the challenge. Do not reconstruct a presumed URL from an example: an MCP endpoint may be served below a path, and the deployment must publish its canonical value.
{
"resource": "https://mcp.ops.example.net/mcp",
"authorization_servers": [
"https://login.example.net"
],
"scopes_supported": [
"mcp.tools.read",
"changes.prepare"
],
"bearer_methods_supported": [
"header"
]
} The resource value must identify the server that will receive the token. The listed authorization_servers must belong to the approved set for the environment. Published scopes describe the available surface; they do not replace backend authorization or tool guardrails.
Freeze the TLS certificate, HTTP status, content type, and redirects as well. Correct metadata retrieved from the wrong virtual host is still the wrong evidence.
Trace resource, audience, and scopes end to end
The client must request a token for the MCP resource and present that token only to the resource. Inspect the authorization or token request without capturing secrets, then inspect only the claims needed for diagnosis in a protected local process.
REQUEST
resource = https://mcp.ops.example.net/mcp
scopes = mcp.tools.read changes.prepare
client = approved production agent client
TOKEN CLAIMS TO RECORD
iss exact approved issuer
aud exact MCP resource or documented audience
scope no broader than the requested tool path
azp/client_id approved client
exp/iat plausible for the incident window
jti/hash correlation-safe identifier, never the token
BACKEND DECISION
resource accepted
audience accepted
scope accepted
tool authorization accepted or denied explicitly Do not conclude from locally decoding a JWT alone. The server must validate its signature, issuer, audience, lifetime, and business authorization. A token valid for a neighboring API must remain invalid for MCP.
If the authorization server ignores the resource parameter, stop the reconnection. Fixing only aud in the backend or accepting multiple unowned audiences silently expands the replay boundary.
Separate discovery failure from authorization failure
Interpret failures in this order:
401withoutresource_metadata: the server or gateway lost or built the challenge incorrectly;- metadata unavailable or redirected: publication, TLS, DNS, or document routing;
- unapproved issuer: environment configuration or inconsistent metadata;
- token issued but audience rejected: mismatch among
resource, token request, and backend validation; - audience accepted but scope rejected: consent, client configuration, or tool policy;
- scope accepted but action rejected: business authorization, identity, or target parameters;
403on the expected negative canary: healthy behavior to preserve.
This order prevents broader roles from being assigned when the client did not request a token for the correct resource in the first place.
Run positive and negative canaries
Validate a side-effect-free read tool first, then a sensitive tool in preparation mode if the server separates preparation from execution. Every success must be paired with at least one expected denial.
CANARY EXPECTED
No token 401 plus trusted resource_metadata
Token for another internal API 401 invalid_token
MCP token without changes.prepare 403 insufficient_scope
MCP token with read scope, tools/list success, no state change
Approved agent with changes.prepare prepared change only
Unapproved target with valid MCP token explicit authorization denial
Expired token 401, no silent refresh loop
Same canaries through production gateway identical security decisions The negative canary proves the control was not bypassed during repair. Measure attempt count as well: an unbounded client authentication loop can create noise, lock an account, or hide the original error.
Correlate without exposing tokens
Traces should connect the agent request, challenge, metadata document, token issuance, and MCP server decision. Retain identifiers or nonreversible hashes, never raw credentials.
trace_id
agent_release
mcp_endpoint
tool_name
resource_metadata_url
resource_identifier
authorization_server
token_issuer
token_audience
granted_scope_set
authorization_decision
deny_reason
backend_operation_id
configuration_revision The gateway and MCP server must tell the same story. If the gateway accepts the token and the backend rejects it, compare their audiences and configuration caches. If both accept it while the cross-API canary succeeds, the repair is unsafe.
Decide promotion, hold, or rollback
PROMOTE
401 challenge advertises canonical metadata over HTTPS
Resource, issuer, audience, and scopes align
Read canary succeeds
Tokens for other resources remain rejected
Unapproved target remains rejected
Traces connect configuration, token, and decision
HOLD THE BLOCK
Redirect or issuer is unexpected
Resource and audience cannot be reconciled
Authorization server ignores the requested resource
Gateway and backend make different decisions
Any negative canary succeeds
ROLL BACK
Restore the previous metadata and gateway configuration
Return to the previously qualified MCP endpoint
Clear only identified configuration caches
Revoke tokens issued for the bad configuration when required
Replay the full matrix before reconnecting the agent Do not roll back by temporarily accepting every audience. A safe return restores the last qualified triplet: endpoint, metadata, and token validation.
Conclusion
An MCP 401 is not an invitation to forward more credentials. It is a signal to trace from the challenge to the business decision through resource, issuer, audience, and scopes.
Reconnect the agent only when the positive canary works and the negative canaries remain blocked. If the resource identity is still ambiguous, keep the tool offline or return to the previously qualified endpoint.