77 * file for configuration (same format as the C++ auth example).
88 */
99
10- import { Server } from " @modelcontextprotocol/sdk/server/index.js" ;
10+ import { Server } from ' @modelcontextprotocol/sdk/server/index.js' ;
1111import {
1212 CallToolRequestSchema ,
1313 ListToolsRequestSchema ,
14- } from " @modelcontextprotocol/sdk/types.js" ;
15- import { GopherAuth } from " @gopher.security/gopher-mcp-js" ;
16- import express , { Request , Response } from " express" ;
17- import cors from " cors" ;
18- import bodyParser from " body-parser" ;
19- import path from " path" ;
20- import { getWeather } from " ./tools/get-weather.js" ;
21- import { getForecast } from " ./tools/get-forecast.js" ;
22- import { getAlerts } from " ./tools/get-alerts.js" ;
23- import { MCPServer } from " ./server.js" ;
14+ } from ' @modelcontextprotocol/sdk/types.js' ;
15+ import { GopherAuth } from ' @gopher.security/gopher-mcp-js' ;
16+ import express , { Request , Response } from ' express' ;
17+ import cors from ' cors' ;
18+ import bodyParser from ' body-parser' ;
19+ import path from ' path' ;
20+ import { getWeather } from ' ./tools/get-weather.js' ;
21+ import { getForecast } from ' ./tools/get-forecast.js' ;
22+ import { getAlerts } from ' ./tools/get-alerts.js' ;
23+ import { MCPServer } from ' ./server.js' ;
2424
2525// Load config from server.config file
26- const configPath = process . argv [ 2 ] || path . join (
27- path . dirname ( new URL ( import . meta. url ) . pathname ) , '..' , 'server.config'
28- ) ;
26+ const configPath =
27+ process . argv [ 2 ] ||
28+ path . join (
29+ path . dirname ( new URL ( import . meta. url ) . pathname ) ,
30+ '..' ,
31+ 'server.config'
32+ ) ;
2933
3034// Initialize GopherAuth from config file
3135const auth = new GopherAuth ( { configPath } ) ;
@@ -34,16 +38,19 @@ auth.initialize();
3438// Read config values for server setup
3539const SERVER_PORT = auth . nativeConfig ?. getInt ( 'port' ) ?? 3001 ;
3640const SERVER_HOST = auth . nativeConfig ?. getString ( 'host' ) ?? '0.0.0.0' ;
37- const SERVER_URL = auth . nativeConfig ?. getString ( 'server_url' ) ?? `http://localhost:${ SERVER_PORT } ` ;
38- const ALLOWED_SCOPES = auth . nativeConfig ?. getString ( 'allowed_scopes' ) ?? 'openid profile email' ;
41+ const SERVER_URL =
42+ auth . nativeConfig ?. getString ( 'server_url' ) ??
43+ `http://localhost:${ SERVER_PORT } ` ;
44+ const ALLOWED_SCOPES =
45+ auth . nativeConfig ?. getString ( 'allowed_scopes' ) ?? 'openid profile email' ;
3946const MCP_SCOPES = ALLOWED_SCOPES . split ( ' ' ) . filter ( Boolean ) ;
4047
4148// Factory: create a new MCP Server instance per session
4249function createMcpServer ( ) : Server {
4350 const server = new Server (
4451 {
45- name : " js-auth-mcp-server" ,
46- version : " 1.0.0" ,
52+ name : ' js-auth-mcp-server' ,
53+ version : ' 1.0.0' ,
4754 } ,
4855 {
4956 capabilities : {
@@ -61,11 +68,11 @@ function createMcpServer(): Server {
6168 server . setRequestHandler ( CallToolRequestSchema , async ( request ) => {
6269 const toolName = request . params . name ;
6370 switch ( toolName ) {
64- case " get-weather" :
71+ case ' get-weather' :
6572 return getWeather . handler ( request ) ;
66- case " get-forecast" :
73+ case ' get-forecast' :
6774 return getForecast . handler ( request ) ;
68- case " get-weather-alerts" :
75+ case ' get-weather-alerts' :
6976 return getAlerts . handler ( request ) ;
7077 default :
7178 throw new Error ( `Unknown tool: ${ toolName } ` ) ;
@@ -82,15 +89,17 @@ const mcpServer = new MCPServer(createMcpServer);
8289async function startServer ( ) {
8390 const app = express ( ) ;
8491
85- app . use ( cors ( {
86- exposedHeaders : [ 'mcp-session-id' , 'Mcp-Session-Id' ] ,
87- } ) ) ;
92+ app . use (
93+ cors ( {
94+ exposedHeaders : [ 'mcp-session-id' , 'Mcp-Session-Id' ] ,
95+ } )
96+ ) ;
8897 app . use ( bodyParser . json ( ) ) ;
8998
9099 // Health check
91- app . get ( " /health" , ( _req : Request , res : Response ) => {
100+ app . get ( ' /health' , ( _req : Request , res : Response ) => {
92101 res . json ( {
93- status : " healthy" ,
102+ status : ' healthy' ,
94103 timestamp : new Date ( ) . toISOString ( ) ,
95104 activeSessions : mcpServer . getActiveSessions ( ) ,
96105 } ) ;
@@ -103,15 +112,15 @@ async function startServer() {
103112 } ) ;
104113
105114 // MCP endpoint with auth + StreamableHTTP
106- const MCP_ENDPOINT = " /mcp" ;
115+ const MCP_ENDPOINT = ' /mcp' ;
107116
108117 app . all (
109118 MCP_ENDPOINT ,
110119 auth . expressMiddleware ( {
111120 publicMethods : [ 'initialize' ] ,
112121 toolScopes : {
113- " get-forecast" : MCP_SCOPES ,
114- " get-weather-alerts" : MCP_SCOPES ,
122+ ' get-forecast' : MCP_SCOPES ,
123+ ' get-weather-alerts' : MCP_SCOPES ,
115124 } ,
116125 } ) ,
117126 async ( req : Request , res : Response ) => {
@@ -120,26 +129,26 @@ async function startServer() {
120129 ) ;
121130
122131 app . listen ( SERVER_PORT , SERVER_HOST , ( ) => {
123- console . log ( " ========================================" ) ;
124- console . log ( " JS Auth MCP Server" ) ;
125- console . log ( " ========================================" ) ;
132+ console . log ( ' ========================================' ) ;
133+ console . log ( ' JS Auth MCP Server' ) ;
134+ console . log ( ' ========================================' ) ;
126135 console . log ( `🚀 Server: http://${ SERVER_HOST } :${ SERVER_PORT } ` ) ;
127136 console . log ( `📡 MCP: ${ SERVER_URL } ${ MCP_ENDPOINT } ` ) ;
128137 console . log ( `🔐 OAuth: ${ SERVER_URL } /.well-known/oauth-protected-resource` ) ;
129138 console . log ( `💚 Health: ${ SERVER_URL } /health` ) ;
130139 console . log ( `📄 Config: ${ configPath } ` ) ;
131140 console . log ( `🔑 Auth: ${ auth . isDisabled ? 'DISABLED' : 'ENABLED' } ` ) ;
132- console . log ( "" ) ;
141+ console . log ( '' ) ;
133142 } ) ;
134143}
135144
136- process . on ( " SIGINT" , ( ) => {
137- console . log ( " \nShutting down..." ) ;
145+ process . on ( ' SIGINT' , ( ) => {
146+ console . log ( ' \nShutting down...' ) ;
138147 auth . shutdown ( ) ;
139148 process . exit ( 0 ) ;
140149} ) ;
141150
142151startServer ( ) . catch ( ( error ) => {
143- console . error ( " Server error:" , error ) ;
152+ console . error ( ' Server error:' , error ) ;
144153 process . exit ( 1 ) ;
145154} ) ;
0 commit comments