You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-6Lines changed: 47 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -338,27 +338,68 @@ php artisan mcp:start demo
338
338
339
339
## Authentication
340
340
341
-
Web-based MCP servers can be protected using [Laravel Passport](https://laravel.com/docs/passport), turning your MCP server into an OAuth2 protected resource.
341
+
### OAuth 2.1
342
342
343
-
If you already have Passport set up for your app, all you need to do is add the `Mcp::oauthRoutes()` helper to your `routes/web.php` file. This registers the required OAuth2 discovery and client registration endpoints. The method accepts an optional route prefix, which defaults to `oauth`.
343
+
The recommended way to protect your web-based MCP servers is to
344
+
use [Laravel Passport](https://laravel.com/docs/passport), turning your MCP server into an OAuth2 protected resource.
345
+
346
+
If you already have Passport set up for your app, all you need to do is add the `Mcp::oauthRoutes()` helper to your
347
+
`routes/web.php` file. This registers the required OAuth2 discovery and client registration endpoints.
348
+
349
+
To secure, apply Passport's `auth:api` middleware to your server registration in `routes/ai.php`:
344
350
345
351
```php
352
+
use App\Mcp\Servers\WeatherExample;
346
353
use Laravel\Mcp\Facades\Mcp;
347
354
348
355
Mcp::oauthRoutes();
356
+
357
+
Mcp::web('/mcp/weather', WeatherExample::class)
358
+
->middleware('auth:api');
349
359
```
350
360
351
-
Then, apply the `auth:api` middleware to your server registration in `routes/ai.php`:
361
+
### Sanctum
362
+
363
+
If you'd like to protect your MCP server using Sanctum, simply add the Sanctum middleware to your server in
364
+
`routes/ai.php`. Make sure MCP clients pass the usual `Authorization: Bearer token` header.
352
365
353
366
```php
354
367
use App\Mcp\Servers\WeatherExample;
355
368
use Laravel\Mcp\Facades\Mcp;
356
369
357
-
Mcp::web('/mcp/weather', WeatherExample::class)
358
-
->middleware('auth:api');
370
+
Mcp::web('/mcp/demo', WeatherExample::class)
371
+
->middleware('auth:sanctum');
372
+
```
373
+
374
+
## Authorization
375
+
376
+
Type hint `Authenticatable` in your primitives, or use `$request->user()` to check authorization.
377
+
378
+
```php
379
+
public function handle(Request $request, Authenticatable $user) view.
380
+
{
381
+
if ($user->tokenCan('server:update') === false) {
382
+
return ToolResult::error('Permission denied');
383
+
}
384
+
385
+
if ($request->user()->can('check:weather') === false) {
386
+
return ToolResult::error('Permission denied');
387
+
}
388
+
...
389
+
}
359
390
```
360
391
361
-
Your MCP server is now protected using OAuth.
392
+
### Conditionally registering tools
393
+
394
+
You can hide tools from certain users without modifying your server config by using `shouldRegister`.
395
+
396
+
```php
397
+
/** UpdateServer tool **/
398
+
public function shouldRegister(Authenticatable $user): bool
0 commit comments