first commit
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Laravel\Mcp\Console\Commands;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Arr;
|
||||
use Laravel\Mcp\Server\Registrar;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Process\PhpExecutableFinder;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mcp:inspector',
|
||||
description: 'Open the MCP Inspector tool to debug and test MCP Servers'
|
||||
)]
|
||||
class InspectorCommand extends Command
|
||||
{
|
||||
public function handle(Registrar $registrar): int
|
||||
{
|
||||
$handle = $this->argument('handle');
|
||||
|
||||
if (! is_string($handle)) {
|
||||
$this->components->error('Please pass a valid MCP server handle');
|
||||
|
||||
return static::FAILURE;
|
||||
}
|
||||
|
||||
$this->components->info("Starting the MCP Inspector for server [{$handle}]");
|
||||
|
||||
$localServer = $registrar->getLocalServer($handle);
|
||||
$route = $registrar->getWebServer($handle);
|
||||
|
||||
$servers = $registrar->servers();
|
||||
if ($servers === []) {
|
||||
$this->components->error('No MCP servers found. Please run `php artisan make:mcp-server [name]`');
|
||||
|
||||
return static::FAILURE;
|
||||
}
|
||||
|
||||
// Only one server, we should just run it for them
|
||||
if (count($servers) === 1) {
|
||||
$server = array_shift($servers);
|
||||
[$localServer, $route] = match (true) {
|
||||
is_callable($server) => [$server, null],
|
||||
$server::class === Route::class => [null, $server],
|
||||
default => [null, null],
|
||||
};
|
||||
}
|
||||
|
||||
if (is_null($localServer) && is_null($route)) {
|
||||
$availableServers = Arr::map(array_keys($servers), fn ($server): string => "[{$server}]");
|
||||
$this->components->error('MCP Server with name ['.$handle.'] not found. Available servers: '.Arr::join($availableServers, ', '));
|
||||
|
||||
return static::FAILURE;
|
||||
}
|
||||
|
||||
$env = [];
|
||||
|
||||
if (is_string($host = $this->option('host'))) {
|
||||
$env['HOST'] = $host;
|
||||
}
|
||||
|
||||
if (is_string($port = $this->option('port'))) {
|
||||
$env['CLIENT_PORT'] = $port;
|
||||
}
|
||||
|
||||
if ($localServer !== null) {
|
||||
$artisanPath = base_path('artisan');
|
||||
|
||||
$command = [
|
||||
'npx',
|
||||
'@modelcontextprotocol/inspector',
|
||||
'--transport',
|
||||
'stdio',
|
||||
$this->phpBinary(),
|
||||
$artisanPath,
|
||||
"mcp:start {$handle}",
|
||||
];
|
||||
|
||||
$guidance = [
|
||||
'Transport Type' => 'STDIO',
|
||||
'Command' => $this->phpBinary(),
|
||||
'Arguments' => implode(' ', [
|
||||
str_replace('\\', '/', $artisanPath),
|
||||
'mcp:start',
|
||||
$handle,
|
||||
]),
|
||||
];
|
||||
} else {
|
||||
$serverUrl = url($route->uri());
|
||||
if (parse_url($serverUrl, PHP_URL_SCHEME) === 'https') {
|
||||
$env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
|
||||
}
|
||||
|
||||
$command = [
|
||||
'npx',
|
||||
'@modelcontextprotocol/inspector',
|
||||
'--transport',
|
||||
'http',
|
||||
'--server-url',
|
||||
$serverUrl,
|
||||
];
|
||||
|
||||
$guidance = [
|
||||
'Transport Type' => 'Streamable HTTP',
|
||||
'URL' => $serverUrl,
|
||||
'Secure' => 'Your project must be accessible on HTTP for this to work due to how node manages SSL trust',
|
||||
];
|
||||
}
|
||||
|
||||
$process = new Process($command, null, $env);
|
||||
$process->setTimeout(null);
|
||||
|
||||
try {
|
||||
foreach ($guidance as $guidanceKey => $guidanceValue) {
|
||||
$this->info(sprintf('%s => %s', $guidanceKey, $guidanceValue));
|
||||
}
|
||||
|
||||
$this->newLine();
|
||||
|
||||
$process->mustRun(function (int|string $type, string $buffer): void {
|
||||
echo $buffer;
|
||||
});
|
||||
} catch (Exception $exception) {
|
||||
$this->components->error('Failed to start MCP Inspector: '.$exception->getMessage());
|
||||
|
||||
return static::FAILURE;
|
||||
}
|
||||
|
||||
return static::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<int, string|int>>
|
||||
*/
|
||||
protected function getArguments(): array
|
||||
{
|
||||
return [
|
||||
['handle', InputArgument::REQUIRED, 'The handle or route of the MCP server to inspect.'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<int, string|int|null>>
|
||||
*/
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
['host', null, InputOption::VALUE_OPTIONAL, 'The host the inspector should bind to'],
|
||||
['port', null, InputOption::VALUE_OPTIONAL, 'The port the inspector should bind to'],
|
||||
];
|
||||
}
|
||||
|
||||
protected function phpBinary(): string
|
||||
{
|
||||
return (new PhpExecutableFinder)->find(false) ?: 'php';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Laravel\Mcp\Console\Commands;
|
||||
|
||||
use Illuminate\Console\GeneratorCommand;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'make:mcp-prompt',
|
||||
description: 'Create a new MCP prompt class'
|
||||
)]
|
||||
class MakePromptCommand extends GeneratorCommand
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'Prompt';
|
||||
|
||||
protected function getStub(): string
|
||||
{
|
||||
return file_exists($customPath = $this->laravel->basePath('stubs/mcp-prompt.stub'))
|
||||
? $customPath
|
||||
: __DIR__.'/../../../stubs/mcp-prompt.stub';
|
||||
}
|
||||
|
||||
protected function getDefaultNamespace($rootNamespace): string
|
||||
{
|
||||
return "{$rootNamespace}\\Mcp\\Prompts";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<int, string|int>>
|
||||
*/
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the prompt already exists'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Laravel\Mcp\Console\Commands;
|
||||
|
||||
use Illuminate\Console\GeneratorCommand;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'make:mcp-resource',
|
||||
description: 'Create a new MCP resource class'
|
||||
)]
|
||||
class MakeResourceCommand extends GeneratorCommand
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'Resource';
|
||||
|
||||
protected function getStub(): string
|
||||
{
|
||||
return file_exists($customPath = $this->laravel->basePath('stubs/mcp-resource.stub'))
|
||||
? $customPath
|
||||
: __DIR__.'/../../../stubs/mcp-resource.stub';
|
||||
}
|
||||
|
||||
protected function getDefaultNamespace($rootNamespace): string
|
||||
{
|
||||
return "{$rootNamespace}\\Mcp\\Resources";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<int, string|int>>
|
||||
*/
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the resource already exists'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Laravel\Mcp\Console\Commands;
|
||||
|
||||
use Illuminate\Console\GeneratorCommand;
|
||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'make:mcp-server',
|
||||
description: 'Create a new MCP server class'
|
||||
)]
|
||||
class MakeServerCommand extends GeneratorCommand
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'Server';
|
||||
|
||||
protected function getStub(): string
|
||||
{
|
||||
return file_exists($customPath = $this->laravel->basePath('stubs/mcp-server.stub'))
|
||||
? $customPath
|
||||
: __DIR__.'/../../../stubs/mcp-server.stub';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $rootNamespace
|
||||
*/
|
||||
protected function getDefaultNamespace($rootNamespace): string
|
||||
{
|
||||
return "{$rootNamespace}\\Mcp\\Servers";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<int, string|int>>
|
||||
*/
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the server already exists'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
protected function buildClass($name): string
|
||||
{
|
||||
$stub = parent::buildClass($name);
|
||||
|
||||
$className = class_basename($name);
|
||||
|
||||
$serverDisplayName = trim((string) preg_replace('/(?<!^)([A-Z])/', ' $1', $className));
|
||||
|
||||
return str_replace('{{ serverDisplayName }}', $serverDisplayName, $stub);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Laravel\Mcp\Console\Commands;
|
||||
|
||||
use Illuminate\Console\GeneratorCommand;
|
||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'make:mcp-tool',
|
||||
description: 'Create a new MCP tool class'
|
||||
)]
|
||||
class MakeToolCommand extends GeneratorCommand
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'Tool';
|
||||
|
||||
protected function getStub(): string
|
||||
{
|
||||
return file_exists($customPath = $this->laravel->basePath('stubs/mcp-tool.stub'))
|
||||
? $customPath
|
||||
: __DIR__.'/../../../stubs/mcp-tool.stub';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $rootNamespace
|
||||
*/
|
||||
protected function getDefaultNamespace($rootNamespace): string
|
||||
{
|
||||
return "{$rootNamespace}\\Mcp\\Tools";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<int, string|int>>
|
||||
*/
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the tool already exists'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
protected function buildClass($name): string
|
||||
{
|
||||
$stub = parent::buildClass($name);
|
||||
|
||||
$className = class_basename($name);
|
||||
$title = Str::headline($className);
|
||||
|
||||
return str_replace(
|
||||
'{{ title }}',
|
||||
$title,
|
||||
$stub,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Laravel\Mcp\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Laravel\Mcp\Server\Registrar;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mcp:start',
|
||||
description: 'Start the MCP Server for a given handle'
|
||||
)]
|
||||
class StartCommand extends Command
|
||||
{
|
||||
public function handle(Registrar $registrar): int
|
||||
{
|
||||
$handle = $this->argument('handle');
|
||||
|
||||
assert(is_string($handle));
|
||||
|
||||
$server = $registrar->getLocalServer($handle);
|
||||
|
||||
if ($server === null) {
|
||||
$this->components->error("MCP Server with name [{$handle}] not found. Did you register it using [Mcp::local()]?");
|
||||
|
||||
return static::FAILURE;
|
||||
}
|
||||
|
||||
$server();
|
||||
|
||||
return static::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<int, string|int>>
|
||||
*/
|
||||
protected function getArguments(): array
|
||||
{
|
||||
return [
|
||||
['handle', InputArgument::REQUIRED, 'The handle of the MCP server to start.'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user