|
| 1 | +/** |
| 2 | + * The web-standard counterpart of `examples/bearer-auth`: the same |
| 3 | + * Resource-Server-only auth built entirely from `@modelcontextprotocol/server` |
| 4 | + * exports — `requireBearerAuth` gating the MCP handler, behind the same |
| 5 | + * DNS-rebinding guards the Express sibling gets from `createMcpExpressApp` — |
| 6 | + * composed as one `fetch(request)` handler. |
| 7 | + * |
| 8 | + * On Cloudflare Workers, Deno, or Bun that handler is the whole server |
| 9 | + * (`export default { fetch: fetchHandler }`); on Node, `toNodeHandler` bridges |
| 10 | + * it onto `node:http`. HTTP-only by definition. |
| 11 | + */ |
| 12 | +import { createServer } from 'node:http'; |
| 13 | + |
| 14 | +import { parseExampleArgs } from '@mcp-examples/shared'; |
| 15 | +import { toNodeHandler } from '@modelcontextprotocol/node'; |
| 16 | +import type { AuthInfo, McpServerFactory, OAuthTokenVerifier } from '@modelcontextprotocol/server'; |
| 17 | +import { |
| 18 | + createMcpHandler, |
| 19 | + hostHeaderValidationResponse, |
| 20 | + localhostAllowedHostnames, |
| 21 | + localhostAllowedOrigins, |
| 22 | + McpServer, |
| 23 | + OAuthError, |
| 24 | + OAuthErrorCode, |
| 25 | + originValidationResponse, |
| 26 | + requireBearerAuth |
| 27 | +} from '@modelcontextprotocol/server'; |
| 28 | +import * as z from 'zod/v4'; |
| 29 | + |
| 30 | +const buildServer: McpServerFactory = ctx => { |
| 31 | + const server = new McpServer({ name: 'bearer-auth-web-example', version: '1.0.0' }); |
| 32 | + server.registerTool('whoami', { description: 'Returns the authenticated subject.', inputSchema: z.object({}) }, async () => ({ |
| 33 | + content: [{ type: 'text', text: `client=${ctx.authInfo?.clientId ?? 'anon'}` }] |
| 34 | + })); |
| 35 | + return server; |
| 36 | +}; |
| 37 | + |
| 38 | +const { port } = parseExampleArgs(); |
| 39 | + |
| 40 | +// Replace with JWT verification, RFC 7662 introspection, etc. |
| 41 | +const staticTokenVerifier: OAuthTokenVerifier = { |
| 42 | + async verifyAccessToken(token): Promise<AuthInfo> { |
| 43 | + if (token !== 'demo-token') { |
| 44 | + throw new OAuthError(OAuthErrorCode.InvalidToken, 'unknown token'); |
| 45 | + } |
| 46 | + return { token, clientId: 'demo-client', scopes: ['mcp'], expiresAt: Math.floor(Date.now() / 1000) + 3600 }; |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +const gate = requireBearerAuth({ verifier: staticTokenVerifier, requiredScopes: ['mcp'] }); |
| 51 | +const handler = createMcpHandler(buildServer); |
| 52 | + |
| 53 | +async function fetchHandler(request: Request): Promise<Response> { |
| 54 | + const rejected = |
| 55 | + hostHeaderValidationResponse(request, localhostAllowedHostnames()) ?? originValidationResponse(request, localhostAllowedOrigins()); |
| 56 | + if (rejected) { |
| 57 | + return rejected; |
| 58 | + } |
| 59 | + const auth = await gate(request); |
| 60 | + if (auth instanceof Response) { |
| 61 | + return auth; |
| 62 | + } |
| 63 | + return handler.fetch(request, { authInfo: auth }); |
| 64 | +} |
| 65 | + |
| 66 | +// On a web-standard runtime the composition above is the whole server; |
| 67 | +// `toNodeHandler` accepts any `{ fetch }` shape and bridges it onto node:http. |
| 68 | +createServer(toNodeHandler({ fetch: fetchHandler })).listen(port, () => { |
| 69 | + console.error(`[server] listening on http://127.0.0.1:${port}/mcp`); |
| 70 | +}); |
0 commit comments