Skip to content

refactor: Adapt to php-mcp/server v1.1.0 changes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
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
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require": {
"php": "^8.1",
"laravel/framework": "^9.46 || ^10.34 || ^11.29 || ^12.0",
"php-mcp/server": "^1.0"
"php-mcp/server": "^1.1.0"
},
"require-dev": {
"laravel/pint": "^1.13",
Expand All @@ -30,8 +30,7 @@
"pestphp/pest-plugin-laravel": "^2.0",
"mockery/mockery": "^1.6",
"phpunit/phpunit": "^10.0 || ^11.0",
"react/http": "^1.11",
"php-mcp/react-transport": "^0.1.0"
"react/http": "^1.11"
},
"autoload": {
"psr-4": {
Expand Down
79 changes: 39 additions & 40 deletions samples/basic/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMcp\Laravel\Server\Commands;

use Illuminate\Console\Command;
use PhpMcp\Server\Server;
use PhpMcp\Server\Transports\StdioTransportHandler;
use Psr\Log\LoggerInterface;

Expand All @@ -30,14 +31,17 @@ class ServeCommand extends Command
* The StdioTransportHandler's start() method contains the blocking loop
* for reading STDIN and writing to STDOUT.
*/
public function handle(StdioTransportHandler $handler, LoggerInterface $logger): int
public function handle(Server $server): int
{
if (! config('mcp.transports.stdio.enabled', false)) {
$this->error('MCP STDIO transport is disabled. Cannot run mcp:serve.');

return Command::FAILURE;
}

$handler = new StdioTransportHandler($server);
$logger = app(LoggerInterface::class);

$logger->info('Starting MCP server via mcp:serve (STDIO)...');
$this->info('MCP server starting via STDIO. Listening for requests...');

Expand Down
16 changes: 10 additions & 6 deletions src/Http/Controllers/McpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use PhpMcp\Server\State\TransportState;
use PhpMcp\Server\Server;
use PhpMcp\Server\Transports\HttpTransportHandler;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Throwable;

class McpController
{
private HttpTransportHandler $handler;

private LoggerInterface $logger;

/**
* MCP Controller Constructor
*
* Inject dependencies resolved by the service container.
*/
public function __construct(
private readonly HttpTransportHandler $handler,
private readonly TransportState $state,
private readonly LoggerInterface $logger
) {}
public function __construct(Server $server)
{
$this->handler = new HttpTransportHandler($server);
$this->logger = app(LoggerInterface::class);
}

/**
* Handle client message (HTTP POST endpoint).
Expand Down
48 changes: 11 additions & 37 deletions src/McpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
use PhpMcp\Laravel\Server\Events\ResourcesListChanged;
use PhpMcp\Laravel\Server\Events\ToolsListChanged;
use PhpMcp\Laravel\Server\Listeners\McpNotificationListener;
use PhpMcp\Server\Processor;
use PhpMcp\Server\Registry;
use PhpMcp\Server\Contracts\ConfigurationRepositoryInterface;
use PhpMcp\Server\Server;
use PhpMcp\Server\State\TransportState;
use PhpMcp\Server\Transports\HttpTransportHandler;
use PhpMcp\Server\Transports\StdioTransportHandler;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;

class McpServiceProvider extends ServiceProvider
{
Expand All @@ -46,51 +44,27 @@ public function register(): void
$this->mergeConfigFrom(__DIR__.'/../config/mcp.php', 'mcp');

$this->app->singleton(Server::class, function (Application $app) {
$config = $app['config'];

$mcpConfig = new ConfigAdapter($config);
$cacheStore = $app['cache']->store($config->get('mcp.cache.store'));
$logger = $app['log']->channel($config->get('mcp.logging.channel'));

$server = Server::make()
->withContainer($app)
->withConfig($mcpConfig)
->withBasePath(base_path())
->withLogger($logger)
->withCache($cacheStore);
->withScanDirectories($app['config']->get('mcp.discovery.directories', ['app/Mcp']));

if (! $this->app->environment('production')) {
$server->discover();
}

return $server;
});

$this->app->bind(Processor::class, fn (Application $app) => $app->make(Server::class)->getProcessor());
$this->app->bind(Registry::class, fn (Application $app) => $app->make(Server::class)->getRegistry());
$this->app->bind(TransportState::class, fn (Application $app) => $app->make(Server::class)->getStateManager());

$this->app->bind(HttpTransportHandler::class, function (Application $app) {
return new HttpTransportHandler(
$app->make(Processor::class),
$app->make(TransportState::class),
$app['log']
);
});

$this->app->bind(StdioTransportHandler::class, function (Application $app) {
return new StdioTransportHandler(
$app->make(Processor::class),
$app->make(TransportState::class),
$app['log']
);
});
$registry = $server->getRegistry();

$this->app->afterResolving(Registry::class, function (Registry $registry) {
$registry->setToolsChangedNotifier(fn () => ToolsListChanged::dispatch());
$registry->setResourcesChangedNotifier(fn () => ResourcesListChanged::dispatch());
$registry->setPromptsChangedNotifier(fn () => PromptsListChanged::dispatch());

return $server;
});

$this->app->bind(ConfigurationRepositoryInterface::class, fn (Application $app) => new ConfigAdapter($app['config']));
$this->app->bind(LoggerInterface::class, fn (Application $app) => $app['log']->channel($app['config']->get('mcp.logging.channel')));
$this->app->bind(CacheInterface::class, fn (Application $app) => $app['cache']->store($app['config']->get('mcp.cache.store')));
}

public function boot(): void
Expand Down