Skip to content

Commit 5b01377

Browse files
committed
fix: laravel route domain() doesn't support array
1 parent 758399c commit 5b01377

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,36 @@ The MCP protocol also defines a "Streamable HTTP SSE" mode, but this package doe
231231

232232
### Domain Restriction
233233

234-
You can restrict the MCP server routes to specific domains by configuring the `domain` option in your `config/mcp-server.php` file:
234+
You can restrict MCP server routes to a specific domain for better security and organization:
235235

236236
```php
237237
// config/mcp-server.php
238238

239-
// Allow MCP routes on all domains (default)
239+
// Allow access from all domains (default)
240240
'domain' => null,
241241

242-
// Restrict to a specific domain
242+
// Restrict to a specific domain only
243243
'domain' => 'api.example.com',
244+
```
245+
246+
**When to use domain restriction:**
247+
- Running multiple applications on different subdomains
248+
- Separating API endpoints from your main application
249+
- Implementing multi-tenant architectures where each tenant has its own subdomain
250+
251+
**Example scenarios:**
252+
```php
253+
// API subdomain only
254+
'domain' => 'api.myapp.com',
255+
256+
// Admin panel subdomain
257+
'domain' => 'admin.myapp.com',
244258

245-
// Restrict to multiple domains (using array)
246-
'domain' => ['api.example.com', 'admin.example.com'],
259+
// Tenant-specific subdomain
260+
'domain' => 'tenant1.myapp.com',
247261
```
248262

249-
This feature allows you to control which domains can access your MCP server, providing an additional layer of security and flexibility in multi-domain applications.
263+
> **Note:** This feature uses Laravel's route `domain()` method. For multiple domain support, consider implementing custom middleware instead.
250264
251265
### Creating and Adding Custom Tools
252266

config/mcp-server.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,20 @@
8181
| Domain Restriction
8282
|--------------------------------------------------------------------------
8383
|
84-
| Specify which domain(s) should load the MCP routes. This can be:
85-
| - null: Routes will be registered for all domains (default)
86-
| - string: A specific domain name (e.g., 'api.example.com')
87-
| - array: Multiple domain names (e.g., ['api.example.com', 'admin.example.com'])
84+
| Restrict MCP server routes to a specific domain. This is useful when:
85+
| - Running multiple applications on different subdomains
86+
| - Separating API endpoints from main application
87+
| - Implementing multi-tenant architectures
88+
|
89+
| Options:
90+
| - null: Allow access from all domains (default)
91+
| - string: Restrict to a single domain (e.g., 'api.example.com')
92+
|
93+
| Example:
94+
| 'domain' => 'api.example.com', // Only api.example.com can access MCP routes
95+
|
96+
| Note: This uses Laravel's route domain() method internally.
97+
| For multiple domains, consider using middleware-based restrictions instead.
8898
|
8999
*/
90100
'domain' => null,

src/LaravelMcpServerServiceProvider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,18 @@ protected function registerRoutes(): void
7272
$path = Config::get('mcp-server.default_path');
7373
$middlewares = Config::get('mcp-server.middlewares', []);
7474
$domain = Config::get('mcp-server.domain');
75-
7675
$provider = Config::get('mcp-server.server_provider');
7776

78-
// Create a route registrar with domain configuration if specified
77+
// Build route configuration with optional domain restriction
7978
$router = Route::middleware($middlewares);
80-
if ($domain !== null) {
79+
80+
// Apply domain restriction if configured
81+
// This ensures MCP routes are only accessible from the specified domain
82+
if ($domain !== null && is_string($domain)) {
8183
$router = $router->domain($domain);
8284
}
8385

86+
// Register provider-specific routes
8487
if ($provider === 'sse') {
8588
$router->get("{$path}/sse", [SseController::class, 'handle']);
8689
$router->post("{$path}/message", [MessageController::class, 'handle']);

0 commit comments

Comments
 (0)