|
21 | 21 | - [Low-Level Server](#low-level-server)
|
22 | 22 | - [Writing MCP Clients](#writing-mcp-clients)
|
23 | 23 | - [Server Capabilities](#server-capabilities)
|
| 24 | + - [Proxy OAuth Server](#proxy-authorization-requests-upstream) |
24 | 25 |
|
25 | 26 | ## Overview
|
26 | 27 |
|
@@ -489,6 +490,52 @@ const result = await client.callTool({
|
489 | 490 | });
|
490 | 491 | ```
|
491 | 492 |
|
| 493 | +### Proxy Authorization Requests Upstream |
| 494 | + |
| 495 | +You can proxy OAuth requests to an external OAuth provider while adding custom validation and client management: |
| 496 | + |
| 497 | +```typescript |
| 498 | +import express from 'express'; |
| 499 | +import { ProxyOAuthServerProvider, mcpAuthRouter } from '@modelcontextprotocol/sdk'; |
| 500 | + |
| 501 | +const app = express(); |
| 502 | + |
| 503 | +const proxyProvider = new ProxyOAuthServerProvider({ |
| 504 | + endpoints: { |
| 505 | + authorizationUrl: "https://auth.external.com/oauth2/v1/authorize", |
| 506 | + tokenUrl: "https://auth.external.com/oauth2/v1/token", |
| 507 | + revocationUrl: "https://auth.external.com/oauth2/v1/revoke", |
| 508 | + }, |
| 509 | + verifyAccessToken: async (token) => { |
| 510 | + return { |
| 511 | + token, |
| 512 | + clientId: "123", |
| 513 | + scopes: ["openid", "email", "profile"], |
| 514 | + } |
| 515 | + }, |
| 516 | + getClient: async (client_id) => { |
| 517 | + return { |
| 518 | + client_id, |
| 519 | + redirect_uris: ["http://localhost:3000/callback"], |
| 520 | + } |
| 521 | + } |
| 522 | +}) |
| 523 | + |
| 524 | +app.use(mcpAuthRouter({ |
| 525 | + provider: proxyProvider, |
| 526 | + issuerUrl: new URL("http://auth.external.com"), |
| 527 | + baseUrl: new URL("http://mcp.example.com"), |
| 528 | + serviceDocumentationUrl: new URL("https://docs.example.com/"), |
| 529 | +})) |
| 530 | +``` |
| 531 | + |
| 532 | +This setup allows you to: |
| 533 | +- Forward OAuth requests to an external provider |
| 534 | +- Add custom token validation logic |
| 535 | +- Manage client registrations |
| 536 | +- Provide custom documentation URLs |
| 537 | +- Maintain control over the OAuth flow while delegating to an external provider |
| 538 | + |
492 | 539 | ## Documentation
|
493 | 540 |
|
494 | 541 | - [Model Context Protocol documentation](https://modelcontextprotocol.io)
|
|
0 commit comments