22
33namespace OPGG \LaravelMcpServer ;
44
5- use Illuminate \Support \Facades \Config ;
65use Illuminate \Support \Facades \Route ;
76use OPGG \LaravelMcpServer \Console \Commands \MakeMcpNotificationCommand ;
87use OPGG \LaravelMcpServer \Console \Commands \MakeMcpPromptCommand ;
@@ -49,7 +48,9 @@ public function register(): void
4948 {
5049 parent ::register ();
5150
52- $ provider = match (Config::get ('mcp-server.server_provider ' )) {
51+ $ this ->registerConfiguration ();
52+
53+ $ provider = match ($ this ->getConfig ('mcp-server.server_provider ' )) {
5354 'streamable_http ' => StreamableHttpServiceProvider::class,
5455 default => SseServiceProvider::class,
5556 };
@@ -70,7 +71,7 @@ public function boot(): void
7071 protected function registerRoutes (): void
7172 {
7273 // Skip route registration if the server is disabled
73- if (! Config:: get ('mcp-server.enabled ' , true )) {
74+ if (! $ this -> getConfig ('mcp-server.enabled ' , true )) {
7475 return ;
7576 }
7677
@@ -79,10 +80,10 @@ protected function registerRoutes(): void
7980 return ;
8081 }
8182
82- $ path = Config:: get ('mcp-server.default_path ' );
83- $ middlewares = Config:: get ('mcp-server.middlewares ' , []);
84- $ domain = Config:: get ('mcp-server.domain ' );
85- $ provider = Config:: get ('mcp-server.server_provider ' );
83+ $ path = $ this -> getConfig ('mcp-server.default_path ' );
84+ $ middlewares = $ this -> getConfig ('mcp-server.middlewares ' , []);
85+ $ domain = $ this -> getConfig ('mcp-server.domain ' );
86+ $ provider = $ this -> getConfig ('mcp-server.server_provider ' );
8687
8788 // Handle multiple domains support
8889 $ domains = $ this ->normalizeDomains ($ domain );
@@ -121,25 +122,89 @@ protected function normalizeDomains($domain): array
121122 */
122123 protected function registerRoutesForDomain (?string $ domain , string $ path , array $ middlewares , string $ provider ): void
123124 {
125+ $ router = $ this ->app ->make ('router ' );
126+
127+ if ($ this ->isLumenRouter ($ router )) {
128+ $ this ->registerLumenRoutes ($ router , $ domain , $ path , $ middlewares , $ provider );
129+
130+ return ;
131+ }
132+
124133 // Build route configuration
125- $ router = Route::middleware ($ middlewares );
134+ $ routeRegistrar = Route::middleware ($ middlewares );
126135
127136 // Apply domain restriction if specified
128137 if ($ domain !== null ) {
129- $ router = $ router ->domain ($ domain );
138+ $ routeRegistrar = $ routeRegistrar ->domain ($ domain );
130139 }
131140
132141 // Register provider-specific routes
133142 switch ($ provider ) {
134143 case 'sse ' :
135- $ router ->get ("{$ path }/sse " , [SseController::class, 'handle ' ]);
136- $ router ->post ("{$ path }/message " , [MessageController::class, 'handle ' ]);
144+ $ routeRegistrar ->get ("{$ path }/sse " , [SseController::class, 'handle ' ]);
145+ $ routeRegistrar ->post ("{$ path }/message " , [MessageController::class, 'handle ' ]);
137146 break ;
138147
139148 case 'streamable_http ' :
140- $ router ->get ($ path , [StreamableHttpController::class, 'getHandle ' ]);
141- $ router ->post ($ path , [StreamableHttpController::class, 'postHandle ' ]);
149+ $ routeRegistrar ->get ($ path , [StreamableHttpController::class, 'getHandle ' ]);
150+ $ routeRegistrar ->post ($ path , [StreamableHttpController::class, 'postHandle ' ]);
142151 break ;
143152 }
144153 }
154+
155+ protected function registerConfiguration (): void
156+ {
157+ if ($ this ->isLumenApplication () && ! $ this ->app ['config ' ]->has ('mcp-server ' )) {
158+ $ this ->app ->configure ('mcp-server ' );
159+ }
160+
161+ $ this ->mergeConfigFrom (__DIR__ .'/../config/mcp-server.php ' , 'mcp-server ' );
162+ }
163+
164+ protected function getConfig (string $ key , $ default = null )
165+ {
166+ if ($ this ->app ->bound ('config ' )) {
167+ return $ this ->app ['config ' ]->get ($ key , $ default );
168+ }
169+
170+ return $ default ;
171+ }
172+
173+ protected function isLumenApplication (): bool
174+ {
175+ return class_exists (\Laravel \Lumen \Application::class) && $ this ->app instanceof \Laravel \Lumen \Application;
176+ }
177+
178+ protected function isLumenRouter ($ router ): bool
179+ {
180+ return class_exists (\Laravel \Lumen \Routing \Router::class) && $ router instanceof \Laravel \Lumen \Routing \Router;
181+ }
182+
183+ protected function registerLumenRoutes ($ router , ?string $ domain , string $ path , array $ middlewares , string $ provider ): void
184+ {
185+ $ groupAttributes = [];
186+
187+ if (! empty ($ middlewares )) {
188+ $ groupAttributes ['middleware ' ] = $ middlewares ;
189+ }
190+
191+ if ($ domain !== null ) {
192+ $ groupAttributes ['domain ' ] = $ domain ;
193+ }
194+
195+ $ router ->group ($ groupAttributes , function ($ router ) use ($ path , $ provider ) {
196+ switch ($ provider ) {
197+ case 'sse ' :
198+ $ router ->get ("{$ path }/sse " , [SseController::class, 'handle ' ]);
199+ $ router ->post ("{$ path }/message " , [MessageController::class, 'handle ' ]);
200+ break ;
201+
202+ case 'streamable_http ' :
203+ default :
204+ $ router ->get ($ path , [StreamableHttpController::class, 'getHandle ' ]);
205+ $ router ->post ($ path , [StreamableHttpController::class, 'postHandle ' ]);
206+ break ;
207+ }
208+ });
209+ }
145210}
0 commit comments