feat: Add MCP scope challenge (step-up authentication) support #50
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Implements step-up authentication support as specified in the MCP Specification 2025-11-25. This allows upstream MCP servers to request additional OAuth scopes from clients when they attempt to access resources requiring elevated permissions.
Addresses and resolves Issue #49
Changes
🔐 Authentication Middleware (src/authentication.ts)
Added
getScopeChallengeResponse()method to generate RFC 6750 compliant HTTP 403 responses:Features:
error="insufficient_scope"(fixed error code per RFC 6750)scope="scope1 scope2 ..."(space-separated required scopes)resource_metadata(OAuth protected resource metadata URI)error_description(optional human-readable message)🔄 HTTP Server (src/startHTTPServer.ts)
Added scope challenge error detection:
isScopeChallengeError()type guard to identifyInsufficientScopeErrorfrom upstream servershandleStreamRequest()to catch and convert scope challenge errors to HTTP 403 responsesauthMiddlewareto enable scope challenge response generationError flow:
✅ Tests (src/authentication.test.ts)
Added comprehensive test coverage for
getScopeChallengeResponse():Test results:
Example Response
When a client attempts to access a resource without sufficient scopes:
Specification Compliance
This implementation follows:
HTTP Status Code Usage
Backward Compatibility
Usage Example
For mcp-proxy Configuration
For Upstream MCP Servers
Upstream servers (e.g., FastMCP) can throw errors that mcp-proxy will automatically convert:
mcp-proxy will automatically detect this error and return an HTTP 403 response with the appropriate WWW-Authenticate header.
Testing
All tests pass:
Build successful:
pnpm run build # ✔ Build completeDocumentation
Related
This PR implements the mcp-proxy portion of step-up authentication. For full end-to-end support, upstream MCP servers (like FastMCP) need to implement the
InsufficientScopeErrorerror type as described in the documentation.