Skip to content

Commit 2476540

Browse files
committed
Add lazy-auth example server mounted at /lazy-auth
Mounts @modelcontextprotocol/server-lazy-auth as an additional example, served at /lazy-auth/mcp alongside the other example MCP App servers. Unlike the other examples (plain createServer() factories behind the stateless handler), lazy-auth's value is its HTTP layer: public tools work unauthenticated, protected tools answer 401 + WWW-Authenticate, and a built-in mock OAuth authorization server completes the flow. So it mounts its full Express app (createApp()) under /lazy-auth. - New src/modules/example-apps/lazy-auth.ts mounts the app before the host middleware and adds the RFC 8414/9728 path-insertion well-known rewrite (/.well-known/oauth-authorization-server/lazy-auth -> into the mount) that SDK clients require for discovery under a base path. - PUBLIC_URL for the example is derived from this server's own BASE_URI, so no extra deploy-time configuration is needed; an explicit PUBLIC_URL still wins (e.g. a tunnel). - Integration test covers the full flow: public initialize/tools/list, 401 on the protected tool, mount-prefixed discovery metadata, and the PKCE -> token -> authed get_secret round trip.
1 parent 8b919a9 commit 2476540

4 files changed

Lines changed: 240 additions & 2 deletions

File tree

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { config } from './config.js';
2020
import { AuthModule } from './modules/auth/index.js';
2121
import { MCPModule } from './modules/mcp/index.js';
2222
import { ExampleAppsModule, AVAILABLE_EXAMPLES } from './modules/example-apps/index.js';
23+
import { mountLazyAuthExample } from './modules/example-apps/lazy-auth.js';
2324
import { ExternalTokenValidator, InternalTokenValidator, ITokenValidator } from './interfaces/auth-validator.js';
2425
import { redisClient } from './modules/shared/redis.js';
2526
import { logger } from './modules/shared/logger.js';
@@ -48,6 +49,13 @@ async function main() {
4849
// This is required for rate limiting to work correctly with real client IPs
4950
app.set('trust proxy', true);
5051

52+
// Lazy-auth MCP App example: a full Express app with its own mock OAuth
53+
// authorization server, mounted at /lazy-auth. Registered before the host
54+
// middleware so its CORS and body handling stay self-contained.
55+
if (config.auth.mode !== 'auth_server') {
56+
mountLazyAuthExample(app, config.baseUri);
57+
}
58+
5159
// Basic middleware
5260
// Intentionally permissive CORS for public MCP reference server
5361
// This allows any MCP client to test against this reference implementation

src/modules/example-apps/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import { createServer as createTranscriptServer } from '@modelcontextprotocol/se
3333
import { createServer as createVideoResourceServer } from '@modelcontextprotocol/server-video-resource';
3434
import { createServer as createWikiExplorerServer } from '@modelcontextprotocol/server-wiki-explorer';
3535

36+
import { LAZY_AUTH_SLUG } from './lazy-auth.js';
37+
3638
declare module "express-serve-static-core" {
3739
interface Request {
3840
auth?: AuthInfo;
@@ -161,5 +163,8 @@ export class ExampleAppsModule {
161163
}
162164
}
163165

164-
// Export list of available examples for documentation
165-
export const AVAILABLE_EXAMPLES = Object.keys(EXAMPLE_SERVERS);
166+
// Export list of available examples for documentation. The lazy-auth example
167+
// is served at the same /:slug/mcp path shape, but as a full Express app with
168+
// its own OAuth endpoints rather than through the stateless handler above
169+
// (see ./lazy-auth.ts).
170+
export const AVAILABLE_EXAMPLES = [...Object.keys(EXAMPLE_SERVERS), LAZY_AUTH_SLUG];
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Lazy Auth Example - Mounts @modelcontextprotocol/server-lazy-auth at /lazy-auth
3+
*
4+
* Unlike the other example servers (plain createServer() factories in
5+
* ./index.ts), the lazy-auth example's whole point is its HTTP layer: public
6+
* tools work unauthenticated, protected tools answer 401 + WWW-Authenticate,
7+
* and a built-in mock OAuth authorization server completes the flow. So we
8+
* mount its full Express app under /lazy-auth instead of adding it to the
9+
* generic stateless handler.
10+
*
11+
* Call mountLazyAuthExample() BEFORE registering the host's own middleware
12+
* (CORS, body parsing, logging) so the example's responses stay fully
13+
* self-contained, and before any other /.well-known routes.
14+
*
15+
* The example advertises absolute OAuth URLs (issuer, authorize/token
16+
* endpoints, PRM resource, WWW-Authenticate resource_metadata). It resolves
17+
* them from PUBLIC_URL, falling back to the request Host header for loopback
18+
* hosts only. We derive PUBLIC_URL from this server's own public base URI -
19+
* already the source of truth for the host's OAuth issuer - so deployments
20+
* need no separate env var. An explicit PUBLIC_URL still wins (e.g. a tunnel);
21+
* omitting baseUri (e.g. tests on an ephemeral port) keeps the package's
22+
* per-request Host resolution.
23+
*/
24+
import { Express, Request, Response, NextFunction } from 'express';
25+
import { createApp as createLazyAuthApp } from '@modelcontextprotocol/server-lazy-auth';
26+
27+
export const LAZY_AUTH_SLUG = 'lazy-auth';
28+
29+
export function mountLazyAuthExample(app: Express, baseUri?: string): void {
30+
if (baseUri && !process.env.PUBLIC_URL) {
31+
process.env.PUBLIC_URL = `${baseUri.replace(/\/+$/, '')}/${LAZY_AUTH_SLUG}`;
32+
}
33+
34+
// RFC 8414/9728 place well-known discovery documents at the origin root,
35+
// with the resource path inserted after the well-known prefix
36+
// (e.g. /.well-known/oauth-authorization-server/lazy-auth), and MCP SDK
37+
// clients only try that insertion form. Rewrite those root paths into the
38+
// mount - rather than dispatching to the sub-app directly - so req.baseUrl
39+
// (and therefore every URL the example advertises) stays consistent.
40+
app.use((req: Request, _res: Response, next: NextFunction) => {
41+
const wellKnown = req.url.match(
42+
/^\/\.well-known\/(oauth-authorization-server|oauth-protected-resource)\/lazy-auth(\/.*)?$/
43+
);
44+
if (wellKnown) {
45+
req.url = `/${LAZY_AUTH_SLUG}/.well-known/${wellKnown[1]}${wellKnown[2] ?? ''}`;
46+
}
47+
next();
48+
});
49+
50+
app.use(`/${LAZY_AUTH_SLUG}`, createLazyAuthApp());
51+
}

0 commit comments

Comments
 (0)