Skip to content

Commit 0e76f95

Browse files
committed
feat: update protected-route middleware for compatability with express v5
1 parent 8bdc9f8 commit 0e76f95

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

packages/mcp-express/src/middlewares/protected-route.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,28 @@ import {McpAuthOptions, PROTECTED_RESOURCE_URL, validateToken} from '@asgardeo/m
2020
import {NextFunction, Request, Response} from 'express';
2121

2222
export default function protectedRoute(options: McpAuthOptions) {
23-
return async function protectedMiddleware(
24-
req: Request,
25-
res: Response,
26-
next: NextFunction,
27-
): Promise<Response<any, Record<string, any>> | undefined> {
23+
return async function protectedMiddleware(req: Request, res: Response, next: NextFunction): Promise<void> {
2824
const authHeader: string | undefined = req.headers.authorization;
2925

3026
if (!authHeader) {
3127
res.setHeader(
3228
'WWW-Authenticate',
3329
`Bearer resource_metadata="${req.protocol}://${req.get('host')}${PROTECTED_RESOURCE_URL}"`,
3430
);
35-
return res.status(401).json({
31+
res.status(401).json({
3632
error: 'unauthorized',
3733
error_description: 'Missing authorization token',
3834
});
35+
return;
3936
}
4037

4138
const parts: string[] = authHeader.split(' ');
4239
if (parts.length !== 2 || parts[0] !== 'Bearer') {
43-
return res.status(401).json({
40+
res.status(401).json({
4441
error: 'invalid_token',
4542
error_description: 'Authorization header must be in format: Bearer [token]',
4643
});
44+
return;
4745
}
4846

4947
const token: string = parts[1];
@@ -69,9 +67,8 @@ export default function protectedRoute(options: McpAuthOptions) {
6967
try {
7068
await validateToken(token, TOKEN_VALIDATION_CONFIG.jwksUri, TOKEN_VALIDATION_CONFIG.options);
7169
next();
72-
return undefined;
7370
} catch (error: any) {
74-
return res.status(401).json({
71+
res.status(401).json({
7572
error: 'invalid_token',
7673
error_description: error.message || 'Invalid or expired token',
7774
});

0 commit comments

Comments
 (0)