Skip to content

OAuth strategy never attempted when an upstream accepts anonymous initialize but requires auth for tools/call #1271

Closed

Description

@krisiasty

Summary

An upstream that answers initialize/tools/list without credentials but requires a token for tools/call can never be authenticated by mcpproxy.

runAuthStrategies advances to the next strategy only when the current one fails. Because the no-auth strategy succeeds against such a server, the connection is marked
connected-without-auth and the OAuth strategy is never reached — even when the server has a fully populated oauth block with a valid, unexpired token already stored.
Every subsequent tool call then fails with transport error: authorization required.

This is not a misconfiguration: there is no configuration that reaches the OAuth strategy, because reaching it requires the no-auth probe to fail.

Environment

Item Value
mcpproxy v0.66.0 (personal) darwin/amd64
Transport http (streamable)
Upstream https://gmailmcp.googleapis.com/mcp/v1 (Google's Gmail MCP endpoint)
Auth server https://accounts.google.com/ (no DCR; static client_id/client_secret)

Reproduction

The upstream's split behaviour is reproducible with curl alone — no credentials, no mcpproxy:

$ curl -s -o /dev/null -w 'HTTP %{http_code}\n' -X POST https://gmailmcp.googleapis.com/mcp/v1 \
    -H 'Content-Type: application/json' -H 'Accept: application/json, text/event-stream' \
    -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"0"}}}'
HTTP 200
$ curl -s -X POST https://gmailmcp.googleapis.com/mcp/v1 \
    -H 'Content-Type: application/json' -H 'Accept: application/json, text/event-stream' \
    -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"list_labels","arguments":{}}}'
HTTP 401
{"id":2,"jsonrpc":"2.0","result":{"content":[{"text":"Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. ...","type":"text"}],"isError":true}}

Anonymous initialize returns 200; anonymous tools/call returns 401. tools/list is likewise anonymous-200, so all 23 tools are discovered and indexed normally.

Then, in mcpproxy:

  1. Add the server: {"name": "Gmail", "url": "https://gmailmcp.googleapis.com/mcp/v1", "protocol": "http"}
  2. Configure OAuth (Google requires a manually created client — no DCR):
"oauth": {
  "client_id": "<client-id>.apps.googleusercontent.com",
  "client_secret": "${keyring:gmail_client_secret}",
  "scopes": ["https://mail.google.com/"]
}
  1. mcpproxy auth login --server=Gmail → browser consent completes, token is persisted.
  2. Call any tool.

Observed

Tool calls fail, while auth status simultaneously reports the token as valid:

$ mcpproxy call tool-read --tool-name=Gmail:list_labels --json_args='{}'
Error: ... server 'Gmail' authentication failed for tool 'list_labels'. OAuth/token authentication required but not properly configured ...
  CallTool failed for 'list_labels': transport error: authorization required
$ mcpproxy auth status --server=Gmail -o json
"authenticated": true,
"connected": true,
"oauth": { "token_valid": true, "token_expires_at": "2026-09-12T18:00:10+02:00" },
"health": { "level": "degraded", "summary": "Sign-in required", "action": "login" }

The clearest signal is that a manual re-login is refused, because the daemon has concluded the server needs no auth:

$ mcpproxy auth login --server=Gmail
Error: failed to trigger OAuth login via daemon: API returned status 500:
{"success":false,"error":"Failed to trigger login: failed to start OAuth: failed to start OAuth flow:
 server connected without OAuth - no authentication required"}

A per-server restart, and a full daemon restart, both reproduce it. After the daemon restart the earlier no valid token available log line disappears — consistent with the connection
settling on no-auth and never consulting the token store at all.

Root cause

The strategy ladder is headers → no-auth → OAuth (internal/upstream/core/connection_http.go):

{"headers", c.tryHeadersAuth},
{"no-auth", c.tryNoAuth},
{"OAuth", c.tryOAuthAuth},

runAuthStrategies (same file) iterates and stops at the first strategy that returns nil. tryNoAuth returns nil whenever c.initialize(ctx) succeeds:

if err := c.initialize(ctx); err != nil {
    return fmt.Errorf("MCP initialize failed during no-auth strategy: %w", err)
}

The assumption is that a server needing auth will reject initialize. For per-method authorization — auth enforced at tools/call but not at initialize — that assumption does not hold.

The same assumption appears on the login path in internal/upstream/core/connection_oauth.go, which treats a successful initialize as proof that OAuth is unnecessary and turns it into an error:

err = c.initialize(ctx)
if err == nil {
    // No OAuth needed - server connected without auth
    return "", nil, "", "", fmt.Errorf("server connected without OAuth - no authentication required")
}

This directly contradicts the intent recorded on the config field itself in internal/config/config.go, where a present oauth block is documented as the signal that OAuth is required:

OAuth *OAuthConfig `json:"oauth" mapstructure:"oauth"` // OAuth configuration (keep even when empty to signal OAuth requirement)

That signal is currently ignored by the strategy ladder.

(Source quoted from main at time of filing; the local build under test is v0.66.0, so line numbers may differ.)

Impact

Any MCP server that authenticates per-method rather than at handshake is unusable through mcpproxy. Google's Gmail MCP endpoint is one such server, and other *.googleapis.com
MCP surfaces seem likely to behave the same way, since the 401 is emitted by Google's standard API frontend (server: ESF) rather than by MCP-specific logic.

The failure is also hard to diagnose from the outside: auth status says authenticated, the server shows as connected with its full tool list indexed, and only the tool call fails —
while auth login, the obvious remedy, refuses to run.

Suggested fix

  1. Honour the configured oauth block. When a server has one, either skip the no-auth strategy or do not let its success terminate the ladder — matching the documented
    "keep even when empty to signal OAuth requirement" contract.
  2. Do not treat a successful initialize as proof that no auth is required in handleOAuthAuthorization; gate that conclusion on the absence of an oauth block so manual
    auth login stays available.
  3. Optionally, re-probe on a 401 at call time. If a tool call fails with an auth error on a connection established via no-auth, invalidate the strategy and re-run the ladder
    from OAuth rather than surfacing authorization required to the caller.

A regression test would be a fake upstream that returns 200 for initialize/tools/list and 401 for tools/call, asserting the client ends up on the OAuth strategy.

Workaround

A static headers.Authorization: Bearer <token> works, since headers-auth is tried first — but Google access tokens expire in one hour, so this is not usable in practice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions