Skip to content

Commit a0f896f

Browse files
committed
Global req object customization, better error handling
1 parent dc73fb8 commit a0f896f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/next/auth-wrapper.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { InvalidTokenError, InsufficientScopeError, ServerError } from '@modelcontextprotocol/sdk/server/auth/errors';
22
import { AuthInfo } from '@modelcontextprotocol/sdk/server/auth/types';
33

4+
// Extend the Request type to include auth info
5+
declare global {
6+
interface Request {
7+
auth?: AuthInfo;
8+
}
9+
}
410
export interface McpAuthOptions {
511
/**
612
* Optional, scopes that the token must have.
@@ -33,7 +39,15 @@ export function withMcpAuth(
3339
throw new InvalidTokenError("Invalid Authorization header format, expected 'Bearer TOKEN'");
3440
}
3541

36-
const authInfo = await verifyToken(req, token);
42+
let authInfo: AuthInfo;
43+
try {
44+
authInfo = await verifyToken(req, token);
45+
} catch (error) {
46+
// Handle any error from verifyToken as a 401
47+
throw new InvalidTokenError(
48+
error instanceof Error ? error.message : "Failed to verify token"
49+
);
50+
}
3751

3852
// Check if token has the required scopes (if any)
3953
if (options.requiredScopes?.length) {
@@ -52,12 +66,13 @@ export function withMcpAuth(
5266
}
5367

5468
// Set auth info on the request object after successful verification
55-
(req as any).auth = authInfo;
69+
req.auth = authInfo;
5670

5771
return handler(req);
5872
} catch (error) {
5973
const origin = new URL(req.url).origin;
6074
const resourceMetadataUrl = options.resourceMetadataPath || `${origin}/.well-known/oauth-protected-resource`;
75+
6176
if (error instanceof InvalidTokenError) {
6277
return new Response(JSON.stringify(error.toResponseObject()), {
6378
status: 401,

src/next/mcp-api-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export function initializeMcpApiHandler(
311311
url: req.url,
312312
headers: Object.fromEntries(req.headers),
313313
body: bodyContent,
314-
auth: (req as any).auth, // Use the auth info that should already be set by withMcpAuth
314+
auth: req.auth, // Use the auth info that should already be set by withMcpAuth
315315
});
316316

317317

0 commit comments

Comments
 (0)