Skip to content

Conversation

@ardaguclu
Copy link
Member

@ardaguclu ardaguclu commented Jul 9, 2025

Fixes #161
Fixes #160

After the in-depth review of model context protocol specification and pondering about what would be the best approach in Kubernetes, I decided to propose the following path to comply with OAuth in this MCP Server.

This PR proposes the following;

  • Introduce three new flags: --require-oauth and --authorization-url and --server-url (they are hidden for now)
  • If --require-oauth is set, authorization middleware for SSE and Streamable HTTP transport types will be enabled.
    This middleware basically;
    • checks the presence of the Authorization header (if not, returns 401 as defined in protocol)
    • extracts the JWT token offline and checks the expiration and audience fields of token
    • sends TokenReview request to API Server to validate the token online. Result of TokenReview is embedded into context of the HTTP Calls.
    • In every tool call, MCP Server creates a new configuration based on the token with custom user-agent and performs requested action.
  • If --require-oauth is set, MCP Server serves .well-known/oauth-protected-resource endpoint to serve the authorization url of the cluster. Authorization url can be specified via toml configuration or flag. If it is unspecified, API Server host name defined in the MCP Kubeconfig will be used.

@ardaguclu ardaguclu force-pushed the require-oauth branch 3 times, most recently from e2c19d2 to 9540bd8 Compare July 9, 2025 12:35
ardaguclu added 4 commits July 9, 2025 15:40
When this flag is enabled, authorization middleware will be turned on.
When this flag is enabled, Derived which is generated based on the client
token will not be used.
This commit adds authorization middleware. Additionally, this commit
rejects the requests if the bearer token is absent in Authorization
header of the request.
Per Model Context Protocol specification, MCP Servers must check the
audience field of the token to ensure that they are generated specifically
for them.

This commits parses the JWT token and asserts that audience is correct
and token is not expired.
This commit sends online token verification by sending request to
TokenReview endpoint of API Server with the token and expected audience.

If API Server returns the status as authenticated, that means this token
can be used to generate a new ad hoc token for MCP Server.

If API Server returns the status as not authenticated, that means this token
is invalid and MCP Server returns 401 to force the client to initiate OAuth flow.
@ardaguclu
Copy link
Member Author

This PR fixes #161, #160

@ardaguclu
Copy link
Member Author

/hold
I'll update the code.

@ardaguclu ardaguclu marked this pull request as ready for review July 10, 2025 07:04
@ardaguclu
Copy link
Member Author

@manusa this is ready for review, when you have a chance. Thanks.

@manusa manusa added this to the 0.1.0 milestone Jul 14, 2025 — with automated-tasks
@manusa manusa merged commit 275b91a into containers:main Jul 14, 2025
9 of 10 checks passed
@ardaguclu ardaguclu deleted the require-oauth branch July 14, 2025 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth 2.0: 401 - Unauthorized header OAuth 2.0: Protected Resource Metadata

2 participants