Skip to content

Commit b6594d7

Browse files
committed
chore: update protect method in McpAuthServer
1 parent 30a1395 commit b6594d7

File tree

2 files changed

+65
-36
lines changed

2 files changed

+65
-36
lines changed

packages/mcp-express/README.md

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,61 +32,72 @@ pnpm add @asgardeo/mcp-express
3232
- Built-in CORS support
3333
- Seamless integration with Asgardeo
3434

35+
## Usage
36+
3537
### Basic Setup
3638

3739
```typescript
3840
import express from 'express';
39-
import {McpAuth, protectedRoute} from '@asgardeo/mcp-express';
41+
import {McpAuthServer} from '@asgardeo/mcp-express';
4042

4143
const app = express();
4244

43-
// Initialize MCP authentication middleware with baseUrl
44-
app.use(
45-
McpAuth({
46-
baseUrl: process.env.BASE_URL as string,
47-
}),
48-
);
45+
// Initialize McpAuthServer with baseUrl
46+
const mcpAuthServer = new McpAuthServer({
47+
baseUrl: process.env.BASE_URL as string,
48+
});
49+
50+
app.use(express.json());
51+
app.use(mcpAuthServer.router());
4952

5053
// Protect your MCP endpoint
5154
app.post(
5255
'/mcp',
53-
protectedRoute({
54-
baseUrl: process.env.BASE_URL as string,
55-
}),
56-
async (req, res) => {
56+
mcpAuthServer.protect(async (req, res) => {
5757
// Your MCP handling logic here
58-
},
58+
}),
5959
);
6060
```
6161

6262
### API Reference
6363

64-
#### McpAuth(options)
64+
#### McpAuthServer(options)
6565

66-
Initializes the MCP authentication server middleware with the given configuration.
66+
Creates a new instance of the MCP authentication server with the given configuration.
6767

6868
```typescript
69-
import {McpAuth} from '@asgardeo/mcp-express';
69+
import {McpAuthServer} from '@asgardeo/mcp-express';
7070

71-
app.use(McpAuth({baseUrl: 'https://auth.example.com'}));
71+
const mcpAuthServer = new McpAuthServer({baseUrl: 'https://auth.example.com'});
7272
```
7373

74-
#### protectedRoute
74+
#### mcpAuthServer.router()
7575

76-
Middleware to protect routes that require authentication.
76+
Returns an Express router that sets up the necessary endpoints for MCP authentication.
7777

7878
```typescript
79-
import {protectedRoute} from '@asgardeo/mcp-express';
79+
app.use(mcpAuthServer.router());
80+
```
81+
82+
#### mcpAuthServer.protect(handler)
83+
84+
Returns middleware that protects routes requiring authentication, passing control to the provided handler function when authentication succeeds.
8085

81-
app.use('/api/protected', protectedRoute, protectedRoutes);
86+
```typescript
87+
app.post(
88+
'/api/protected',
89+
mcpAuthServer.protect(async (req, res) => {
90+
// Your protected route logic here
91+
})
92+
);
8293
```
8394

8495
### Configuration
8596

86-
The middleware can be configured with the following option:
97+
The server can be configured with the following option:
8798

8899
```typescript
89-
interface McpAuthOptions {
100+
interface McpAuthServerOptions {
90101
/** Base URL of the authorization server */
91102
baseUrl: string;
92103
}
@@ -98,7 +109,7 @@ Here's a complete example of setting up an Express server with MCP authenticatio
98109

99110
```typescript
100111
import {randomUUID} from 'node:crypto';
101-
import {McpAuth, protectedRoute} from '@asgardeo/mcp-express';
112+
import {McpAuthServer} from '@asgardeo/mcp-express';
102113
import {McpServer} from '@modelcontextprotocol/sdk/server/mcp';
103114
import {StreamableHTTPServerTransport} from '@modelcontextprotocol/sdk/server/streamableHttp';
104115
import {isInitializeRequest} from '@modelcontextprotocol/sdk/types';
@@ -109,12 +120,14 @@ import {z} from 'zod';
109120
config();
110121

111122
const app: Express = express();
123+
124+
// Initialize McpAuthServer
125+
const mcpAuthServer = new McpAuthServer({
126+
baseUrl: process.env.BASE_URL as string,
127+
});
128+
112129
app.use(express.json());
113-
app.use(
114-
McpAuth({
115-
baseUrl: process.env.BASE_URL as string,
116-
}),
117-
);
130+
app.use(mcpAuthServer.router());
118131

119132
// Session management
120133
interface TransportMap {
@@ -132,10 +145,7 @@ const isSessionExpired = (lastAccessTime: number): boolean => Date.now() - lastA
132145
// MCP endpoint with authentication
133146
app.post(
134147
'/mcp',
135-
protectedRoute({
136-
baseUrl: process.env.BASE_URL as string,
137-
}),
138-
async (req: Request, res: Response): Promise<void> => {
148+
mcpAuthServer.protect(async (req: Request, res: Response): Promise<void> => {
139149
try {
140150
const sessionId: string | undefined = req.headers['mcp-session-id'] as string | undefined;
141151
let transport: StreamableHTTPServerTransport;
@@ -204,7 +214,7 @@ app.post(
204214
} catch (error) {
205215
// Error handling
206216
}
207-
},
217+
}),
208218
);
209219

210220
const PORT: string | number = process.env.PORT || 3000;
@@ -241,4 +251,4 @@ pnpm lint
241251

242252
## License
243253

244-
Apache-2.0 - see the [LICENSE](LICENSE) file for details.
254+
Apache-2.0 - see the [LICENSE](LICENSE) file for details.

packages/mcp-express/src/McpAuthServer.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1-
import express, {Router, RequestHandler} from 'express';
1+
/**
2+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
219
import {McpAuthOptions} from '@asgardeo/mcp-node';
20+
import express, {Router, RequestHandler} from 'express';
321
import protectedRoute from './middlewares/protected-route';
422
import AuthRouter from './routes/auth';
523

624
export class McpAuthServer {
725
private options: McpAuthOptions;
26+
827
private routerInstance: Router;
928

1029
constructor(options: McpAuthOptions) {
@@ -20,7 +39,7 @@ export class McpAuthServer {
2039
}
2140

2241
public protect(handler: RequestHandler): Router {
23-
const protectedRouter = express.Router();
42+
const protectedRouter: Router = express.Router();
2443
protectedRouter.use(protectedRoute(this.options), handler);
2544
return protectedRouter;
2645
}

0 commit comments

Comments
 (0)