File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -87,7 +87,29 @@ const HOST = process.env.MCP_HOST ?? 'localhost';
8787const PORT = process . env . MCP_PORT ? Number . parseInt ( process . env . MCP_PORT , 10 ) : 3001 ;
8888
8989const app = createMcpExpressApp ( { host : HOST } ) ;
90- app . use ( cors ( ) ) ;
90+ const DEFAULT_CORS_ORIGIN_REGEX = / ^ h t t p s ? : \/ \/ (?: l o c a l h o s t | 1 2 7 \. 0 \. 0 \. 1 ) (?: : \d + ) ? $ / ;
91+
92+ let corsOriginRegex = DEFAULT_CORS_ORIGIN_REGEX ;
93+ if ( process . env . MCP_CORS_ORIGIN_REGEX ) {
94+ try {
95+ corsOriginRegex = new RegExp ( process . env . MCP_CORS_ORIGIN_REGEX ) ;
96+ } catch ( error ) {
97+ const msg =
98+ error && typeof error === 'object' && 'message' in error ? String ( ( error as { message : unknown } ) . message ) : String ( error ) ;
99+ console . warn ( `Invalid MCP_CORS_ORIGIN_REGEX (${ process . env . MCP_CORS_ORIGIN_REGEX } ): ${ msg } ` ) ;
100+ corsOriginRegex = DEFAULT_CORS_ORIGIN_REGEX ;
101+ }
102+ }
103+
104+ app . use (
105+ cors ( {
106+ origin : ( origin , cb ) => {
107+ // Allow non-browser clients (no Origin header).
108+ if ( ! origin ) return cb ( null , true ) ;
109+ return cb ( null , corsOriginRegex . test ( origin ) ) ;
110+ }
111+ } )
112+ ) ;
91113
92114// Create event store for resumability
93115const eventStore = new InMemoryEventStore ( ) ;
You can’t perform that action at this time.
0 commit comments