Skip to content

Commit 329c9a6

Browse files
Introduce new resource and issuer mandatory configurations
1 parent 7646bac commit 329c9a6

File tree

9 files changed

+3492
-4233
lines changed

9 files changed

+3492
-4233
lines changed

examples/express-mcp-server/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const port = process.env.PORT || 3000;
1212
const mcpAuthServer = new McpAuthServer({
1313
baseUrl: process.env.BASE_URL as string,
1414
issuer: process.env.ISSUER as string,
15+
resource: process.env.RESOURCE as string,
1516
});
1617

1718
app.use(express.json());

examples/express-mcp-vet-ai-assist-app/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@
3232
"@asgardeo/mcp-express": "workspace:*",
3333
"@modelcontextprotocol/inspector": "^0.11.0",
3434
"@modelcontextprotocol/sdk": "^1.11.0",
35+
"cors": "^2.8.5",
3536
"dotenv": "^16.3.1",
3637
"express": "^4.18.2",
3738
"tailwindcss": "^4.1.5",
3839
"zod": "^3.24.4"
3940
},
4041
"devDependencies": {
41-
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?4ee6f6be232d7631999d709a86b91612f1d34ce7",
42-
"@wso2/prettier-config": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/prettier-config?4ee6f6be232d7631999d709a86b91612f1d34ce7",
42+
"@types/cors": "^2.8.17",
4343
"@types/express": "^4.17.21",
4444
"@types/node": "^20.10.0",
45-
"nodemon": "^3.0.2",
45+
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?4ee6f6be232d7631999d709a86b91612f1d34ce7",
46+
"@wso2/prettier-config": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/prettier-config?4ee6f6be232d7631999d709a86b91612f1d34ce7",
4647
"eslint": "8.57.0",
48+
"nodemon": "^3.0.2",
4749
"ts-node": "^10.9.2",
4850
"typescript": "^5.3.3"
4951
}

examples/express-mcp-vet-ai-assist-app/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {McpAuthServer} from '@asgardeo/mcp-express';
2121
import {McpServer} from '@modelcontextprotocol/sdk/server/mcp';
2222
import {StreamableHTTPServerTransport} from '@modelcontextprotocol/sdk/server/streamableHttp';
2323
import {isInitializeRequest} from '@modelcontextprotocol/sdk/types';
24+
import cors from 'cors';
2425
import {config} from 'dotenv';
2526
import express, {Express, Request, Response} from 'express';
2627
import {z} from 'zod';
@@ -32,8 +33,10 @@ const app: Express = express();
3233
const mcpAuthServer: McpAuthServer = new McpAuthServer({
3334
baseUrl: process.env.BASE_URL as string,
3435
issuer: process.env.ISSUER as string,
36+
resource: process.env.RESOURCE as string,
3537
});
3638

39+
app.use(cors());
3740
app.use(express.json());
3841
app.use(mcpAuthServer.router());
3942

packages/mcp-express/src/middlewares/bearerAuthMiddleware.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ export default function bearerAuthMiddleware(options: McpAuthOptions) {
4646

4747
const token: string = parts[1];
4848

49-
const issuerBase: string | undefined = options?.baseUrl;
49+
const baseUrl: string | undefined = options?.baseUrl;
50+
const issuer: string | undefined = options?.issuer;
51+
const endpoints:
52+
| {
53+
jwks?: string | undefined;
54+
}
55+
| undefined = options?.endpoints;
5056

5157
const TOKEN_VALIDATION_CONFIG: {
5258
jwksUri: string;
@@ -56,11 +62,11 @@ export default function bearerAuthMiddleware(options: McpAuthOptions) {
5662
issuer: string;
5763
};
5864
} = {
59-
jwksUri: `${issuerBase}/oauth2/jwks`,
65+
jwksUri: `${baseUrl}${endpoints?.jwks ?? '/oauth2/jwks'}`,
6066
options: {
6167
audience: options?.audience,
6268
clockTolerance: 60,
63-
issuer: `${options.issuer}`,
69+
issuer,
6470
},
6571
};
6672

packages/mcp-express/src/routes/auth.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,34 @@ import {getProtectedResourceMetadata} from '../controllers/protected-resource';
2323

2424
export default function AuthRouter(options: McpAuthOptions): express.Router {
2525
const router: express.Router = express.Router();
26-
const {baseUrl, issuer} = options;
26+
const {baseUrl, issuer, resource} = options;
2727
if (!baseUrl) {
2828
throw new Error('baseUrl must be provided');
2929
}
3030

31+
if (!issuer) {
32+
throw new Error('issuer must be provided');
33+
}
34+
35+
if (!resource) {
36+
throw new Error('resource must be provided');
37+
}
38+
39+
const resourceUrlPathComponent: string | undefined = undefined; // new URL(options?.resource).pathname;
40+
3141
router.use(
32-
PROTECTED_RESOURCE_URL,
42+
resourceUrlPathComponent ? PROTECTED_RESOURCE_URL + resourceUrlPathComponent : PROTECTED_RESOURCE_URL,
3343
getProtectedResourceMetadata({
3444
authorizationServers: [issuer],
35-
resource: 'https://api.example.com',
45+
resource,
3646
}),
3747
);
3848

3949
router.use(
4050
AUTHORIZATION_SERVER_METADATA_URL,
4151
getAuthorizationServerMetadata({
4252
baseUrl,
53+
issuer,
4354
}),
4455
);
4556

packages/mcp-node/src/models/authorization-server.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ export interface AuthorizationServerMetadata {
5555
export interface AuthorizationServerMetadataOptions {
5656
/** The base URL of the authorization server */
5757
baseUrl: string;
58+
endpoints?: {
59+
// URL path of the authorization endpoint
60+
authorize?: string;
61+
// URL of the JWK Set document
62+
jwks?: string;
63+
// URL path of the token endpoint
64+
token?: string;
65+
};
66+
// URL path of the issuer endpoint
67+
issuer: string;
5868
/** Optional URL pointing to the service documentation */
5969
serviceDocumentation?: string;
6070
}

packages/mcp-node/src/models/mcp-auth.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
export interface McpAuthOptions {
2020
audience?: string;
2121
baseUrl: string;
22+
endpoints?: {
23+
jwks?: string;
24+
token?: string;
25+
};
2226
issuer: string;
27+
resource: string;
2328
}

packages/mcp-node/src/utils/generate-authorization-server-metadata.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ import {AuthorizationServerMetadata, AuthorizationServerMetadataOptions} from '.
2828
export default function generateAuthorizationServerMetadata(
2929
options: AuthorizationServerMetadataOptions,
3030
): AuthorizationServerMetadata {
31+
const {issuer, endpoints} = options;
32+
3133
const metadata: AuthorizationServerMetadata = {
32-
authorization_endpoint: `${options.baseUrl}/oauth2/authorize`,
33-
issuer: `${options.baseUrl}/oauth2/token`,
34+
authorization_endpoint: `${options.baseUrl}${endpoints?.authorize ?? '/oauth2/authorize'}`,
35+
issuer: `${issuer}`,
3436
response_types_supported: ['code'],
35-
token_endpoint: `${options.baseUrl}/oauth2/token`,
37+
token_endpoint: `${options.baseUrl}${endpoints?.token ?? '/oauth2/token'}`,
3638
};
3739

3840
// TODO: Check this further.
@@ -57,7 +59,7 @@ export default function generateAuthorizationServerMetadata(
5759
}
5860

5961
// TODO: Check this further.
60-
metadata.jwks_uri = `${options.baseUrl}/oauth/jwks`;
62+
metadata.jwks_uri = `${options.baseUrl}${endpoints?.jwks ?? '/oauth/jwks'}`;
6163

6264
return metadata;
6365
}

0 commit comments

Comments
 (0)