@@ -45,12 +45,17 @@ class McpStreamMiddleware
4545 /**
4646 * CORS headers for MCP endpoints
4747 */
48- private const CORS_HEADERS = [
49- 'Access-Control-Allow-Origin ' => '* ' ,
50- 'Access-Control-Allow-Methods ' => 'GET, POST, DELETE, OPTIONS ' ,
51- 'Access-Control-Allow-Headers ' => 'Content-Type, Authorization, mcp-session-id ' ,
52- 'Access-Control-Expose-Headers ' => 'WWW-Authenticate ' ,
53- ];
48+ // MCP clients (Claude Desktop, etc.) are external — CORS must be permissive.
49+ // Endpoints are protected by requiring a DreamFactory session/Bearer token.
50+ private static function corsHeaders (): array
51+ {
52+ return [
53+ 'Access-Control-Allow-Origin ' => '* ' ,
54+ 'Access-Control-Allow-Methods ' => 'GET, POST, DELETE, OPTIONS ' ,
55+ 'Access-Control-Allow-Headers ' => 'Content-Type, Authorization, mcp-session-id ' ,
56+ 'Access-Control-Expose-Headers ' => 'WWW-Authenticate ' ,
57+ ];
58+ }
5459
5560 /**
5661 * Handle an incoming request.
@@ -76,7 +81,7 @@ public function handle(Request $request, Closure $next)
7681
7782 // Handle OPTIONS preflight for any MCP path
7883 if ($ method === 'OPTIONS ' ) {
79- return response ('' , 200 )->withHeaders (self ::CORS_HEADERS );
84+ return response ('' , 200 )->withHeaders (self ::corsHeaders () );
8085 }
8186
8287 $ mcpService = $ matches [1 ];
@@ -100,7 +105,7 @@ public function handle(Request $request, Closure $next)
100105
101106 // Add CORS headers to response
102107 if ($ response ) {
103- foreach (self ::CORS_HEADERS as $ key => $ value ) {
108+ foreach (self ::corsHeaders () as $ key => $ value ) {
104109 $ response ->headers ->set ($ key , $ value );
105110 }
106111 return $ response ;
@@ -116,7 +121,7 @@ public function handle(Request $request, Closure $next)
116121 private function handleRfc8414WellKnown (Request $ request , Closure $ next , string $ wellKnownType , string $ mcpService , string $ method )
117122 {
118123 if ($ method === 'OPTIONS ' ) {
119- return response ('' , 200 )->withHeaders (self ::CORS_HEADERS );
124+ return response ('' , 200 )->withHeaders (self ::corsHeaders () );
120125 }
121126
122127 $ controllerMethod = self ::RFC8414_WELL_KNOWN [$ wellKnownType ] ?? null ;
@@ -129,7 +134,7 @@ private function handleRfc8414WellKnown(Request $request, Closure $next, string
129134 $ response = $ controller ->$ controllerMethod ($ request , $ mcpService );
130135
131136 if ($ response ) {
132- foreach (self ::CORS_HEADERS as $ key => $ value ) {
137+ foreach (self ::corsHeaders () as $ key => $ value ) {
133138 $ response ->headers ->set ($ key , $ value );
134139 }
135140 return $ response ;
0 commit comments