Salesforce MCP - the hidden gotcha of incorrectly issued Access Token
If you are testing Salesforce Hosted MCP Servers locally using @modelcontextprotocol/inspector, you will likely hit two frustrating roadblocks before seeing a single tool schema:
- Salesforce rejects plain
http://127.0.0.1callback URLs. - Once you get HTTPS working and complete OAuth, MCP Inspector immediately fails with
Auth challenge: token_expiredandUnexpected: auth() returned AUTHORIZED without authorization code- even though your network tab shows tokens arriving cleanly.
Here is the exact local HTTPS setup and the one Salesforce OAuth checkbox that fixes the handshake loop.
Serve MCP Inspector over Local HTTPS with mkcert
Salesforce strictly enforces HTTPS on external callback URLs (only exempting http://localhost in narrow contexts, while rejecting http://127.0.0.1, which MCP Inspector binds to by default).
Use mkcert to generate a locally trusted certificate for 127.0.0.1 and pass it into MCP Inspector via its TLS environment variables:
# 1. Install the local CA (run once)
mkcert -install
# 2. Generate certs for 127.0.0.1 and localhost
mkdir -p ~/.mcp-certs
mkcert -key-file ~/.mcp-certs/key.pem -cert-file ~/.mcp-certs/cert.pem 127.0.0.1 localhost
# 3. Launch MCP Inspector with HTTPS enabled
INSPECTOR_TLS_KEY_PATH=~/.mcp-certs/key.pem \
INSPECTOR_TLS_CERT_PATH=~/.mcp-certs/cert.pem \
npx @modelcontextprotocol/inspector
Your callback URL is now https://127.0.0.1:6274/oauth/callback.
Configure the Salesforce External Client App
Salesforce's hosted MCP servers require an External Client App (legacy Connected Apps are not supported for hosted MCP).
In Setup → External Client App Manager → New External Client App, configure the OAuth settings as follows:
- Callback URL:
https://127.0.0.1:6274/oauth/callback
OAuth Scopes:
- Access the identity URL service (
id,profile,email,address,phone) - Full access (
full) - Perform requests at any time (
refresh_token,offline_access) - Access unique user identifiers (
openid) - Access Salesforce hosted MCP servers (
mcp_api)
Flow Enablement:
- Enable Authorization Code and Credentials Flow
Security:
- Require secret for Refresh Token Flow
- Require Proof Key for Code Exchange (PKCE) extension for Supported Authorization Flows
- Enable Refresh Token Rotation
- Limit Idle Refresh Token Time-to-Live (TTL) to 30 Days
- Configure ID token (Include standard claims)
- Issue JSON Web Token (JWT)-based access tokens for named users (Critical - do not skip)
Note: When you save with JWT-based access tokens enabled, Salesforce will take noticeably longer than usual to respond while it provisions a signing key pair for your app.
Troubleshooting: Why OAuth Succeeds but MCP Fails with token_expired
If you miss the Issue JSON Web Token (JWT)-based access tokens for named users checkbox, your browser login will succeed and MCP Inspector will receive tokens, only to fail immediately with:
Authentication for "SalesforceObjects" needs attention. Auth challenge: token_expired Unexpected: auth() returned AUTHORIZED without authorization code
What is actually happening under the hood:
- Opaque tokens vs. JWTs: By default, Salesforce issues opaque session-based access tokens (
00D...!...). Standard REST APIs validate these against Salesforce's internal session store, whereas Salesforce's hosted MCP gateway expects a self-contained JWT it can verify statelessly via your org's JWKS keys. When the gateway receives an opaque string instead of a three-part JWT, it rejects the header with a 401 and surfaces a misleadingtoken_expiredchallenge. - Inspector's recovery loop: MCP Inspector catches that 401 and invokes its internal
auth()recovery handler. Because valid tokens are already sitting in Inspector's local store from the redirect you just completed, its state machine sees an AUTHORIZED session without a fresh authorization code and throws the second error.
Enabling JWT-based access tokens for named users switches the issued access_token from an opaque string to a signed JWT, allowing the MCP gateway to validate the mcp_api scope on the first try.
Comments
No comments yet. Start the discussion.