Skip to content

Commit b9bc340

Browse files
refactor: Enhance MCP server command handling and update README for transport options
1 parent 48556b7 commit b9bc340

File tree

5 files changed

+89
-68
lines changed

5 files changed

+89
-68
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
coverage: none
3333

3434
- name: Install Composer dependencies
35-
run: composer update --no-interaction --prefer-dist
35+
run: composer install --no-interaction --prefer-dist
3636

3737
- name: Run Tests
3838
run: composer test

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ All MCP server settings are managed in `config/mcp.php`. Here are the key sectio
6363
### Transport Configuration
6464
* **`transports`**: Available communication methods
6565
* **`stdio`**: CLI-based transport
66-
* `enabled`: Enable the `mcp:serve` command
66+
* `enabled`: Enable the `mcp:serve` command with `stdio` option.
6767
* **`http_dedicated`**: Standalone HTTP server
68-
* `enabled`, `host`, `port`, `path_prefix` settings
68+
* `enabled`: Enable the `mcp:serve` command with `http` option.
69+
* `host`, `port`, `path_prefix` settings
6970
* **`http_integrated`**: Laravel route-based server
7071
* `enabled`: Serve through Laravel routes
71-
* `prefix`: URL prefix (default: 'mcp')
72+
* `route_prefix`: URL prefix (default: 'mcp')
7273
* `middleware`: Applied middleware (default: 'web')
7374
7475
### Cache & Performance

src/Commands/ServeCommand.php

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,20 @@ class ServeCommand extends Command
3838
* for reading STDIN and writing to STDOUT.
3939
*/
4040
public function handle(Server $server): int
41+
{
42+
$transportOption = $this->getTransportOption();
43+
44+
return match ($transportOption) {
45+
'stdio' => $this->handleStdioTransport($server),
46+
'http' => $this->handleHttpTransport($server),
47+
default => $this->handleInvalidTransport($transportOption),
48+
};
49+
}
50+
51+
private function getTransportOption(): string
4152
{
4253
$transportOption = $this->option('transport');
54+
4355
if ($transportOption === null) {
4456
if ($this->input->isInteractive()) {
4557
$transportOption = select(
@@ -55,62 +67,71 @@ public function handle(Server $server): int
5567
}
5668
}
5769

58-
$host = $this->option('host');
59-
$port = $this->option('port');
60-
$pathPrefix = $this->option('path-prefix');
70+
return $transportOption;
71+
}
6172

62-
if ($transportOption === 'stdio') {
63-
if (! config('mcp.transports.stdio.enabled', true)) {
64-
$this->error('MCP STDIO transport is disabled in config/mcp.php.');
73+
private function handleStdioTransport(Server $server): int
74+
{
75+
if (! config('mcp.transports.stdio.enabled', true)) {
76+
$this->error('MCP STDIO transport is disabled in config/mcp.php.');
6577

66-
return Command::FAILURE;
67-
}
78+
return Command::FAILURE;
79+
}
6880

69-
$this->info('Starting MCP server with STDIO transport...');
81+
$this->info('Starting MCP server with STDIO transport...');
7082

71-
try {
72-
$transport = new StdioServerTransport;
73-
$server->listen($transport);
74-
} catch (\Exception $e) {
75-
$this->error("Failed to start MCP server with STDIO transport: {$e->getMessage()}");
83+
try {
84+
$transport = new StdioServerTransport;
85+
$server->listen($transport);
86+
} catch (\Exception $e) {
87+
$this->error("Failed to start MCP server with STDIO transport: {$e->getMessage()}");
7688

77-
return Command::FAILURE;
78-
}
79-
} elseif ($transportOption === 'http') {
80-
if (! config('mcp.transports.http_dedicated.enabled', true)) {
81-
$this->error('Dedicated MCP HTTP transport is disabled in config/mcp.php.');
89+
return Command::FAILURE;
90+
}
8291

83-
return Command::FAILURE;
84-
}
92+
$this->info("MCP Server (STDIO) stopped.");
8593

86-
$host = $this->option('host') ?? config('mcp.transports.http_dedicated.host', '127.0.0.1');
87-
$port = (int) ($this->option('port') ?? config('mcp.transports.http_dedicated.port', 8090));
88-
$pathPrefix = $this->option('path-prefix') ?? config('mcp.transports.http_dedicated.path_prefix', 'mcp_server');
89-
$sslContextOptions = config('mcp.transports.http_dedicated.ssl_context_options'); // For HTTPS
90-
91-
$this->info("Starting MCP server with dedicated HTTP transport on http://{$host}:{$port} (prefix: /{$pathPrefix})...");
92-
$transport = new HttpServerTransport(
93-
host: $host,
94-
port: $port,
95-
mcpPathPrefix: $pathPrefix,
96-
sslContext: $sslContextOptions
97-
);
98-
99-
try {
100-
$server->listen($transport);
101-
} catch (\Exception $e) {
102-
$this->error("Failed to start MCP server with dedicated HTTP transport: {$e->getMessage()}");
103-
104-
return Command::FAILURE;
105-
}
106-
} else {
107-
$this->error("Invalid transport specified: {$transportOption}. Use 'stdio' or 'http'.");
94+
return Command::SUCCESS;
95+
}
10896

109-
return Command::INVALID;
97+
private function handleHttpTransport(Server $server): int
98+
{
99+
if (! config('mcp.transports.http_dedicated.enabled', true)) {
100+
$this->error('Dedicated MCP HTTP transport is disabled in config/mcp.php.');
101+
102+
return Command::FAILURE;
110103
}
111104

112-
$this->info("MCP Server ({$transportOption}) stopped.");
105+
$host = $this->option('host') ?? config('mcp.transports.http_dedicated.host', '127.0.0.1');
106+
$port = (int) ($this->option('port') ?? config('mcp.transports.http_dedicated.port', 8090));
107+
$pathPrefix = $this->option('path-prefix') ?? config('mcp.transports.http_dedicated.path_prefix', 'mcp_server');
108+
$sslContextOptions = config('mcp.transports.http_dedicated.ssl_context_options'); // For HTTPS
109+
110+
$this->info("Starting MCP server with dedicated HTTP transport on http://{$host}:{$port} (prefix: /{$pathPrefix})...");
111+
$transport = new HttpServerTransport(
112+
host: $host,
113+
port: $port,
114+
mcpPathPrefix: $pathPrefix,
115+
sslContext: $sslContextOptions
116+
);
117+
118+
try {
119+
$server->listen($transport);
120+
} catch (\Exception $e) {
121+
$this->error("Failed to start MCP server with dedicated HTTP transport: {$e->getMessage()}");
122+
123+
return Command::FAILURE;
124+
}
125+
126+
$this->info("MCP Server (HTTP) stopped.");
113127

114128
return Command::SUCCESS;
115129
}
130+
131+
private function handleInvalidTransport(string $transportOption): int
132+
{
133+
$this->error("Invalid transport specified: {$transportOption}. Use 'stdio' or 'http'.");
134+
135+
return Command::INVALID;
136+
}
116137
}

src/Http/Controllers/McpController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ public function handleSse(Request $request): Response
121121
}
122122

123123
static $keepAliveCounter = 0;
124-
if (($keepAliveCounter++ % (15 / $pollInterval)) == 0) {
124+
$keepAliveInterval = (int) round(15 / $pollInterval);
125+
if (($keepAliveCounter++ % $keepAliveInterval) == 0) {
125126
echo ": keep-alive\n\n";
126127
$this->flushOutput();
127128
}

src/McpServiceProvider.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Contracts\Foundation\Application;
88
use Illuminate\Contracts\Support\DeferrableProvider;
9+
use Illuminate\Support\Facades\Event;
910
use Illuminate\Support\Facades\Route;
1011
use Illuminate\Support\ServiceProvider;
1112
use PhpMcp\Laravel\Commands\DiscoverCommand;
@@ -22,23 +23,6 @@
2223

2324
class McpServiceProvider extends ServiceProvider implements DeferrableProvider
2425
{
25-
/**
26-
* The event listener mappings for the application.
27-
*
28-
* @var array<class-string, array<int, class-string>>
29-
*/
30-
protected $listen = [
31-
ToolsListChanged::class => [
32-
McpNotificationListener::class,
33-
],
34-
ResourcesListChanged::class => [
35-
McpNotificationListener::class,
36-
],
37-
PromptsListChanged::class => [
38-
McpNotificationListener::class,
39-
],
40-
];
41-
4226
public function register(): void
4327
{
4428
$this->mergeConfigFrom(__DIR__ . '/../config/mcp.php', 'mcp');
@@ -55,7 +39,12 @@ public function register(): void
5539
*/
5640
public function provides(): array
5741
{
58-
return [Server::class, LaravelHttpTransport::class];
42+
return [
43+
McpRegistrar::class,
44+
Server::class,
45+
Registry::class,
46+
LaravelHttpTransport::class,
47+
];
5948
}
6049

6150
public function boot(): void
@@ -64,6 +53,7 @@ public function boot(): void
6453
$this->buildServer();
6554
$this->bootConfig();
6655
$this->bootRoutes();
56+
$this->bootEvents();
6757
$this->bootCommands();
6858
$this->bootEventListeners();
6959
}
@@ -166,6 +156,14 @@ protected function bootCommands(): void
166156
}
167157
}
168158

159+
protected function bootEvents(): void
160+
{
161+
Event::listen(
162+
[ToolsListChanged::class, ResourcesListChanged::class, PromptsListChanged::class],
163+
McpNotificationListener::class,
164+
);
165+
}
166+
169167
protected function bootEventListeners(): void
170168
{
171169
$server = $this->app->make(Server::class);

0 commit comments

Comments
 (0)