Skip to content

MCP OAuth scope config is not applied to authorization flow #26301

@joshuapbritz

Description

@joshuapbritz

In full transparency, this ticket was created using an AI agent. I am not sure if there is anything useful here, but I was hoping that I could at least get pointed in the correct direction. My debug session is here: https://opncd.ai/share/LeFwxgyK

I am happy to move or update the issue log. The issue I am having is that a specific MCP server I am trying to connect to is not working. The server is https://docs.medusajs.com/learn/introduction/build-with-llms-ai/mcp-server but when trying to connect I get an error message: OAuth Failure: Missing required state parameter - potential CSRF attack.

The below was output by my agent (Gpt 5.5 xhigh).

Summary

OpenCode exposes an MCP OAuth config option:

"oauth": {
  "scope": "openid email profile"
}

But the configured scope does not appear to be included in the OAuth client metadata used by the MCP TypeScript SDK. As a result, the generated authorization URL can omit scope, which prevents users from working around MCP servers that require explicit scopes.

Environment

  • OpenCode version observed in logs: 1.14.40
  • Current source checked: v1.14.41
  • OS: macOS
  • MCP server tested: https://docs.medusajs.com/mcp

Reproduction

Configure a remote MCP server with an explicit OAuth scope:

{
  "mcp": {
    "medusa": {
      "type": "remote",
      "enabled": true,
      "url": "https://docs.medusajs.com/mcp",
      "oauth": {
        "scope": "openid email profile"
      }
    }
  }
}

Run:

opencode mcp auth medusa

Inspect logs in:

~/.local/share/opencode/log/

Actual Behavior

The generated authorization URL does not include a scope parameter.

Example log shape:

https://cloud.medusajs.com/oauth/authorize
  ?response_type=code
  &client_id=medusa-mcp
  &code_challenge=<redacted>
  &code_challenge_method=S256
  &redirect_uri=http%3A%2F%2F127.0.0.1%3A19876%2Fmcp%2Foauth%2Fcallback
  &state=<redacted>
  &resource=https%3A%2F%2Fdocs.medusajs.com%2F

There is no:

scope=openid%20email%20profile

Expected Behavior

If mcp.<server>.oauth.scope is configured, OpenCode should pass it through so the authorization request includes that scope.

Likely Cause

packages/opencode/src/mcp/index.ts passes oauthConfig?.scope into McpOAuthProvider, but packages/opencode/src/mcp/oauth-provider.ts does not appear to expose that value through clientMetadata.

The MCP TypeScript SDK uses provider.clientMetadata.scope as a fallback when no scope is supplied by the WWW-Authenticate header or protected resource metadata.

Suggested Fix

In packages/opencode/src/mcp/oauth-provider.ts, include configured scope in clientMetadata:

get clientMetadata(): OAuthClientMetadata {
  return {
    redirect_uris: [this.redirectUrl],
    client_name: "OpenCode",
    client_uri: "https://opencode.ai",
    grant_types: ["authorization_code", "refresh_token"],
    response_types: ["code"],
    token_endpoint_auth_method: this.config.clientSecret ? "client_secret_post" : "none",
    ...(this.config.scope ? { scope: this.config.scope } : {}),
  }
}

Why This Matters

Some MCP OAuth servers require explicit scopes but may not advertise MCP protected-resource scopes_supported. OpenCode already has a config option that appears intended to handle this case, but it currently cannot be used as a workaround if it is not included in client metadata.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions