@@ -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
3840import express from ' express' ;
39- import {McpAuth , protectedRoute } from ' @asgardeo/mcp-express' ;
41+ import {McpAuthServer } from ' @asgardeo/mcp-express' ;
4042
4143const 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
5154app .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
100111import {randomUUID } from ' node:crypto' ;
101- import {McpAuth , protectedRoute } from ' @asgardeo/mcp-express' ;
112+ import {McpAuthServer } from ' @asgardeo/mcp-express' ;
102113import {McpServer } from ' @modelcontextprotocol/sdk/server/mcp' ;
103114import {StreamableHTTPServerTransport } from ' @modelcontextprotocol/sdk/server/streamableHttp' ;
104115import {isInitializeRequest } from ' @modelcontextprotocol/sdk/types' ;
@@ -109,12 +120,14 @@ import {z} from 'zod';
109120config ();
110121
111122const app: Express = express ();
123+
124+ // Initialize McpAuthServer
125+ const mcpAuthServer = new McpAuthServer ({
126+ baseUrl: process .env .BASE_URL as string ,
127+ });
128+
112129app .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
120133interface TransportMap {
@@ -132,10 +145,7 @@ const isSessionExpired = (lastAccessTime: number): boolean => Date.now() - lastA
132145// MCP endpoint with authentication
133146app .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
210220const 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.
0 commit comments