Skip to content

Commit b9bdd8d

Browse files
authored
Implement MCP scope registration in service provider (#131)
* Implement MCP scope registration in service provider and update route handling * Fix test * Remove the unused ` maybeAddMcpScope ` method from the Registrar class
1 parent 404fbe3 commit b9bdd8d

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/Server/McpServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function register(): void
2525

2626
public function boot(): void
2727
{
28+
$this->registerMcpScope();
2829
$this->registerRoutes();
2930
$this->registerContainerCallbacks();
3031

@@ -96,4 +97,11 @@ protected function registerCommands(): void
9697
InspectorCommand::class,
9798
]);
9899
}
100+
101+
protected function registerMcpScope(): void
102+
{
103+
$this->app->booted(function (): void {
104+
Registrar::ensureMcpScope();
105+
});
106+
}
99107
}

src/Server/Registrar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function servers(): array
8484

8585
public function oauthRoutes(string $oauthPrefix = 'oauth'): void
8686
{
87-
$this->maybeAddMcpScope();
87+
static::ensureMcpScope();
8888
Router::get('/.well-known/oauth-protected-resource/{path?}', fn (?string $path = '') => response()->json([
8989
'resource' => url('/'.$path),
9090
'authorization_servers' => [url('/'.$path)],
@@ -108,7 +108,7 @@ public function oauthRoutes(string $oauthPrefix = 'oauth'): void
108108
/**
109109
* @return array<string, string>
110110
*/
111-
protected function maybeAddMcpScope(): array
111+
public static function ensureMcpScope(): array
112112
{
113113
if (class_exists('Laravel\Passport\Passport') === false) {
114114
return [];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Laravel\Mcp\Server\McpServiceProvider;
6+
7+
it('registers mcp scope during boot', function (): void {
8+
if (! class_exists('Laravel\Passport\Passport')) {
9+
eval('
10+
namespace Laravel\Passport;
11+
class Passport {
12+
public static $scopes = [];
13+
public static function tokensCan($scopes) {
14+
self::$scopes = $scopes;
15+
}
16+
}
17+
');
18+
}
19+
20+
\Laravel\Passport\Passport::$scopes = ['custom' => 'Custom scope'];
21+
22+
$provider = new McpServiceProvider($this->app);
23+
$provider->register();
24+
$provider->boot();
25+
26+
$this->app->boot();
27+
28+
expect(\Laravel\Passport\Passport::$scopes)->toHaveKey('mcp:use');
29+
expect(\Laravel\Passport\Passport::$scopes['custom'])->toBe('Custom scope');
30+
});

0 commit comments

Comments
 (0)