Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Server/McpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function register(): void

public function boot(): void
{
$this->registerMcpScope();
$this->registerRoutes();
$this->registerContainerCallbacks();

Expand Down Expand Up @@ -96,4 +97,11 @@ protected function registerCommands(): void
InspectorCommand::class,
]);
}

protected function registerMcpScope(): void
{
$this->app->booted(function (): void {
Registrar::ensureMcpScope();
});
}
}
12 changes: 10 additions & 2 deletions src/Server/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function servers(): array

public function oauthRoutes(string $oauthPrefix = 'oauth'): void
{
$this->maybeAddMcpScope();
static::ensureMcpScope();
Router::get('/.well-known/oauth-protected-resource/{path?}', fn (?string $path = '') => response()->json([
'resource' => url('/'.$path),
'authorization_servers' => [url('/'.$path)],
Expand All @@ -108,7 +108,7 @@ public function oauthRoutes(string $oauthPrefix = 'oauth'): void
/**
* @return array<string, string>
*/
protected function maybeAddMcpScope(): array
public static function ensureMcpScope(): array
{
if (class_exists('Laravel\Passport\Passport') === false) {
return [];
Expand All @@ -124,6 +124,14 @@ protected function maybeAddMcpScope(): array
return $current;
}

/**
* @return array<string, string>
*/
protected function maybeAddMcpScope(): array
{
return static::ensureMcpScope();
}

/**
* @param class-string<Server> $serverClass
* @param callable(): Transport $transportFactory
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Server/McpServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Laravel\Mcp\Server\McpServiceProvider;

it('registers mcp scope during boot', function (): void {
if (! class_exists('Laravel\Passport\Passport')) {
eval('
namespace Laravel\Passport;
class Passport {
public static $scopes = [];
public static function tokensCan($scopes) {
self::$scopes = $scopes;
}
}
');
}

\Laravel\Passport\Passport::$scopes = ['custom' => 'Custom scope'];

$provider = new McpServiceProvider($this->app);
$provider->register();
$provider->boot();

$this->app->boot();

expect(\Laravel\Passport\Passport::$scopes)->toHaveKey('mcp:use');
expect(\Laravel\Passport\Passport::$scopes['custom'])->toBe('Custom scope');
});