|
| 1 | +/** |
| 2 | + * Integration tests for the lazy-auth example mount. |
| 3 | + * |
| 4 | + * Verifies that the @modelcontextprotocol/server-lazy-auth app, mounted at |
| 5 | + * /lazy-auth, advertises URLs under the mount path, that RFC 8414/9728 |
| 6 | + * path-insertion well-known URLs at the host root reach the example, that the |
| 7 | + * host's own root OAuth endpoints are untouched, and that the full lazy-auth |
| 8 | + * flow (401 -> discovery -> PKCE -> token -> authed tool call) works |
| 9 | + * end-to-end over HTTP. |
| 10 | + */ |
| 11 | +import { createHash } from 'crypto'; |
| 12 | +import http from 'http'; |
| 13 | +import express from 'express'; |
| 14 | +import { AddressInfo } from 'net'; |
| 15 | +import { mountLazyAuthExample } from './lazy-auth.js'; |
| 16 | + |
| 17 | +interface HttpResult { |
| 18 | + status: number; |
| 19 | + headers: http.IncomingHttpHeaders; |
| 20 | + body: string; |
| 21 | +} |
| 22 | + |
| 23 | +function request(url: string, options: http.RequestOptions = {}, body?: string): Promise<HttpResult> { |
| 24 | + return new Promise((resolve, reject) => { |
| 25 | + const req = http.request(url, options, (res) => { |
| 26 | + let data = ''; |
| 27 | + res.on('data', (chunk) => (data += chunk)); |
| 28 | + res.on('end', () => resolve({ status: res.statusCode ?? 0, headers: res.headers, body: data })); |
| 29 | + }); |
| 30 | + req.on('error', reject); |
| 31 | + if (body !== undefined) req.write(body); |
| 32 | + req.end(); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function callTool(base: string, name: string, accessToken?: string): Promise<HttpResult> { |
| 37 | + return request( |
| 38 | + `${base}/lazy-auth/mcp`, |
| 39 | + { |
| 40 | + method: 'POST', |
| 41 | + headers: { |
| 42 | + 'content-type': 'application/json', |
| 43 | + accept: 'application/json, text/event-stream', |
| 44 | + ...(accessToken ? { authorization: `Bearer ${accessToken}` } : {}), |
| 45 | + }, |
| 46 | + }, |
| 47 | + JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name, arguments: {} } }) |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +describe('Lazy Auth example mount', () => { |
| 52 | + let server: http.Server; |
| 53 | + let base: string; |
| 54 | + |
| 55 | + beforeAll(async () => { |
| 56 | + const app = express(); |
| 57 | + // Mirror src/index.ts: the example mounts before the host's middleware |
| 58 | + // and routes. |
| 59 | + mountLazyAuthExample(app); |
| 60 | + app.use(express.json()); |
| 61 | + // Sentinels for the host's own root OAuth surface, which the example's |
| 62 | + // well-known rewrite must not shadow. |
| 63 | + app.get('/.well-known/oauth-authorization-server', (_req, res) => { |
| 64 | + res.json({ issuer: 'HOST-OWN-AS' }); |
| 65 | + }); |
| 66 | + app.get('/authorize', (_req, res) => { |
| 67 | + res.send('HOST-OWN-AUTHORIZE'); |
| 68 | + }); |
| 69 | + |
| 70 | + server = await new Promise((resolve) => { |
| 71 | + const s = app.listen(0, () => resolve(s)); |
| 72 | + }); |
| 73 | + base = `http://localhost:${(server.address() as AddressInfo).port}`; |
| 74 | + }); |
| 75 | + |
| 76 | + afterAll(async () => { |
| 77 | + await new Promise((resolve) => server.close(resolve)); |
| 78 | + }); |
| 79 | + |
| 80 | + it('serves public MCP requests without auth', async () => { |
| 81 | + const res = await request( |
| 82 | + `${base}/lazy-auth/mcp`, |
| 83 | + { |
| 84 | + method: 'POST', |
| 85 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 86 | + }, |
| 87 | + JSON.stringify({ |
| 88 | + jsonrpc: '2.0', |
| 89 | + id: 1, |
| 90 | + method: 'initialize', |
| 91 | + params: { protocolVersion: '2025-06-18', capabilities: {}, clientInfo: { name: 'test', version: '0' } }, |
| 92 | + }) |
| 93 | + ); |
| 94 | + expect(res.status).toBe(200); |
| 95 | + expect(res.body).toContain('Lazy Auth'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('answers 401 with resource_metadata under the mount path for protected tools', async () => { |
| 99 | + const res = await callTool(base, 'get_secret'); |
| 100 | + expect(res.status).toBe(401); |
| 101 | + expect(res.headers['www-authenticate']).toContain(`resource_metadata="${base}/lazy-auth/auth/prm"`); |
| 102 | + }); |
| 103 | + |
| 104 | + it('advertises mount-prefixed URLs in PRM and AS metadata', async () => { |
| 105 | + const prm = JSON.parse((await request(`${base}/lazy-auth/auth/prm`)).body); |
| 106 | + expect(prm.resource).toBe(`${base}/lazy-auth/mcp`); |
| 107 | + expect(prm.authorization_servers).toEqual([`${base}/lazy-auth`]); |
| 108 | + |
| 109 | + // RFC 8414 path-insertion form at the host root (the only form MCP SDK |
| 110 | + // clients try for an issuer with a path). |
| 111 | + const asRes = await request(`${base}/.well-known/oauth-authorization-server/lazy-auth`); |
| 112 | + expect(asRes.status).toBe(200); |
| 113 | + const as = JSON.parse(asRes.body); |
| 114 | + expect(as.issuer).toBe(`${base}/lazy-auth`); |
| 115 | + expect(as.authorization_endpoint).toBe(`${base}/lazy-auth/authorize`); |
| 116 | + expect(as.token_endpoint).toBe(`${base}/lazy-auth/token`); |
| 117 | + }); |
| 118 | + |
| 119 | + it('serves TTL-scoped PRM through the path-insertion form', async () => { |
| 120 | + const res = await request(`${base}/.well-known/oauth-protected-resource/lazy-auth/ttl/3600/mcp`); |
| 121 | + expect(res.status).toBe(200); |
| 122 | + expect(JSON.parse(res.body).resource).toBe(`${base}/lazy-auth/ttl/3600/mcp`); |
| 123 | + }); |
| 124 | + |
| 125 | + it('leaves the host root OAuth surface untouched', async () => { |
| 126 | + const as = await request(`${base}/.well-known/oauth-authorization-server`); |
| 127 | + expect(JSON.parse(as.body).issuer).toBe('HOST-OWN-AS'); |
| 128 | + const authorize = await request(`${base}/authorize`); |
| 129 | + expect(authorize.body).toBe('HOST-OWN-AUTHORIZE'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('completes the full lazy-auth flow: PKCE -> token -> authed tool call', async () => { |
| 133 | + const verifier = 'v'.repeat(43); |
| 134 | + const challenge = createHash('sha256').update(verifier).digest('base64url'); |
| 135 | + |
| 136 | + const authorizeUrl = new URL(`${base}/lazy-auth/authorize`); |
| 137 | + const params: Record<string, string> = { |
| 138 | + client_id: 'test-client', |
| 139 | + redirect_uri: 'http://localhost:1234/callback', |
| 140 | + code_challenge: challenge, |
| 141 | + code_challenge_method: 'S256', |
| 142 | + state: 'test-state', |
| 143 | + approved: '1', |
| 144 | + resource: `${base}/lazy-auth/mcp`, |
| 145 | + }; |
| 146 | + for (const [k, v] of Object.entries(params)) authorizeUrl.searchParams.set(k, v); |
| 147 | + |
| 148 | + const authorizeRes = await request(authorizeUrl.href); |
| 149 | + expect(authorizeRes.status).toBe(302); |
| 150 | + const redirect = new URL(authorizeRes.headers.location!); |
| 151 | + const code = redirect.searchParams.get('code')!; |
| 152 | + expect(code).toBeTruthy(); |
| 153 | + expect(redirect.searchParams.get('state')).toBe('test-state'); |
| 154 | + |
| 155 | + const tokenRes = await request( |
| 156 | + `${base}/lazy-auth/token`, |
| 157 | + { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' } }, |
| 158 | + new URLSearchParams({ |
| 159 | + grant_type: 'authorization_code', |
| 160 | + code, |
| 161 | + code_verifier: verifier, |
| 162 | + resource: `${base}/lazy-auth/mcp`, |
| 163 | + }).toString() |
| 164 | + ); |
| 165 | + expect(tokenRes.status).toBe(200); |
| 166 | + const token = JSON.parse(tokenRes.body); |
| 167 | + expect(token.access_token).toBeTruthy(); |
| 168 | + expect(token.refresh_token).toBeTruthy(); |
| 169 | + |
| 170 | + const secretRes = await callTool(base, 'get_secret', token.access_token); |
| 171 | + expect(secretRes.status).toBe(200); |
| 172 | + expect(secretRes.body).toContain('the-answer-is-42'); |
| 173 | + }); |
| 174 | +}); |
0 commit comments