Skip to content

Commit 3435cc5

Browse files
committed
refactor: remove HttpServerDriver and related classes, consolidate transport to Swoole implementation
1 parent 72d4fe2 commit 3435cc5

File tree

11 files changed

+703
-482
lines changed

11 files changed

+703
-482
lines changed

src/McpServer/Config/HttpTransportConfig.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/McpServer/Config/SwooleTransportConfig.php

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
final readonly class SwooleTransportConfig extends HttpTransportConfig
1111
{
1212
public function __construct(
13-
// Inherit base HTTP config
14-
string $host = 'localhost',
15-
int $port = 8080,
16-
string $sessionStore = 'file',
17-
?string $sessionStorePath = null,
18-
bool $corsEnabled = true,
19-
array $corsOrigins = ['*'],
20-
int $maxRequestSize = 10485760,
13+
public string $host = 'localhost',
14+
public int $port = 8080,
15+
public string $sessionStore = 'file',
16+
public ?string $sessionStorePath = null,
17+
public bool $corsEnabled = true,
18+
public array $corsOrigins = ['*'],
19+
public int $maxRequestSize = 10485760, // 10MB
2120

2221
// SSE-specific configuration
2322
public bool $sseEnabled = true,
@@ -44,17 +43,7 @@ public function __construct(
4443
public bool $enableStats = true,
4544
public int $statsInterval = 60,
4645
public bool $logRequests = false,
47-
) {
48-
parent::__construct(
49-
host: $host,
50-
port: $port,
51-
sessionStore: $sessionStore,
52-
sessionStorePath: $sessionStorePath,
53-
corsEnabled: $corsEnabled,
54-
corsOrigins: $corsOrigins,
55-
maxRequestSize: $maxRequestSize,
56-
);
57-
}
46+
) {}
5847

5948
/**
6049
* Get Swoole server configuration array
@@ -91,19 +80,4 @@ public function getSseConfig(): array
9180
'buffer_size' => $this->sseBufferSize,
9281
];
9382
}
94-
95-
/**
96-
* Get HTTP options array for Swoole integration
97-
*/
98-
public function toSwooleHttpOptions(): array
99-
{
100-
return \array_merge(
101-
parent::toHttpOptions(),
102-
[
103-
'enable_sse' => $this->sseEnabled,
104-
'stats_enabled' => $this->enableStats,
105-
'log_requests' => $this->logRequests,
106-
],
107-
);
108-
}
10983
}

src/McpServer/McpServerBootloader.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
use Butschster\ContextGenerator\McpServer\Routing\RouteRegistrar;
5050
use Butschster\ContextGenerator\McpServer\Server\Runner;
5151
use Butschster\ContextGenerator\McpServer\Server\RunnerInterface;
52-
use Butschster\ContextGenerator\McpServer\Server\ServerDriverFactory;
5352
use Butschster\ContextGenerator\McpServer\Tool\McpToolBootloader;
5453
use League\Route\Router;
5554
use League\Route\Strategy\StrategyInterface;
@@ -87,43 +86,36 @@ public function init(EnvironmentInterface $env): void
8786
'document_name_format' => $env->get('MCP_DOCUMENT_NAME_FORMAT', '[{path}] {description}'),
8887
'transport' => [
8988
'type' => $env->get('MCP_TRANSPORT', 'stdio'),
90-
'http' => [
91-
'host' => $env->get('MCP_HTTP_HOST', 'localhost'),
89+
'swoole' => [
90+
'host' => $env->get('MCP_HTTP_HOST', '127.0.0.1'),
9291
'port' => (int) $env->get('MCP_HTTP_PORT', 8080),
9392
'session_store' => $env->get('MCP_HTTP_SESSION_STORE', 'file'),
9493
'session_store_path' => $env->get('MCP_HTTP_SESSION_STORE_PATH'),
9594
'cors_enabled' => (bool) $env->get('MCP_HTTP_CORS_ENABLED', true),
96-
'cors_origins' => \array_filter(\explode(',', (string) $env->get('MCP_HTTP_CORS_ORIGINS', '*'))),
95+
'cors_origins' => \array_filter(
96+
\explode(',', (string) $env->get('MCP_HTTP_CORS_ORIGINS', '*')),
97+
),
9798
'max_request_size' => (int) $env->get('MCP_HTTP_MAX_REQUEST_SIZE', 10485760),
98-
],
99-
'swoole' => [
100-
'host' => $env->get('MCP_SWOOLE_HOST', 'localhost'),
101-
'port' => (int) $env->get('MCP_SWOOLE_PORT', 8080),
102-
'session_store' => $env->get('MCP_SWOOLE_SESSION_STORE', 'file'),
103-
'session_store_path' => $env->get('MCP_SWOOLE_SESSION_STORE_PATH'),
104-
'cors_enabled' => (bool) $env->get('MCP_SWOOLE_CORS_ENABLED', true),
105-
'cors_origins' => \array_filter(\explode(',', (string) $env->get('MCP_SWOOLE_CORS_ORIGINS', '*'))),
106-
'max_request_size' => (int) $env->get('MCP_SWOOLE_MAX_REQUEST_SIZE', 10485760),
10799

108100
// SSE-specific configuration
109-
'sse_enabled' => (bool) $env->get('MCP_SWOOLE_SSE_ENABLED', true),
110-
'max_sse_connections' => (int) $env->get('MCP_SWOOLE_MAX_SSE_CONNECTIONS', 1000),
111-
'sse_heartbeat_interval' => (int) $env->get('MCP_SWOOLE_SSE_HEARTBEAT_INTERVAL', 30),
112-
'sse_reconnect_timeout' => (int) $env->get('MCP_SWOOLE_SSE_RECONNECT_TIMEOUT', 5),
113-
'sse_buffer_size' => (int) $env->get('MCP_SWOOLE_SSE_BUFFER_SIZE', 8192),
101+
'sse_enabled' => (bool) $env->get('MCP_HTTP_SSE_ENABLED', true),
102+
'max_sse_connections' => (int) $env->get('MCP_HTTP_MAX_SSE_CONNECTIONS', 1000),
103+
'sse_heartbeat_interval' => (int) $env->get('MCP_HTTP_SSE_HEARTBEAT_INTERVAL', 30),
104+
'sse_reconnect_timeout' => (int) $env->get('MCP_HTTP_SSE_RECONNECT_TIMEOUT', 5),
105+
'sse_buffer_size' => (int) $env->get('MCP_HTTP_SSE_BUFFER_SIZE', 8192),
114106

115107
// Swoole server configuration
116108
'worker_num' => (int) $env->get('MCP_SWOOLE_WORKER_NUM', 4),
117109
'task_worker_num' => (int) $env->get('MCP_SWOOLE_TASK_WORKER_NUM', 2),
118-
'max_connections' => (int) $env->get('MCP_SWOOLE_MAX_CONNECTIONS', 10000),
110+
'max_connections' => (int) $env->get('MCP_SWOOLE_MAX_CONNECTIONS', 1024),
119111
'connection_timeout' => (int) $env->get('MCP_SWOOLE_CONNECTION_TIMEOUT', 300),
120112
'buffer_size' => (int) $env->get('MCP_SWOOLE_BUFFER_SIZE', 32768),
121113
'package_max_length' => (int) $env->get('MCP_SWOOLE_PACKAGE_MAX_LENGTH', 2097152),
122114

123115
// Performance settings
124116
'enable_coroutine' => (bool) $env->get('MCP_SWOOLE_ENABLE_COROUTINE', true),
125117
'enable_static_handler' => (bool) $env->get('MCP_SWOOLE_ENABLE_STATIC_HANDLER', false),
126-
'max_coroutines' => (int) $env->get('MCP_SWOOLE_MAX_COROUTINES', 100000),
118+
'max_coroutines' => (int) $env->get('MCP_SWOOLE_MAX_COROUTINES', 1024),
127119
'socket_buffer_size' => (int) $env->get('MCP_SWOOLE_SOCKET_BUFFER_SIZE', 2097152),
128120

129121
// Logging and monitoring

src/McpServer/Server/Driver/HttpServerDriver.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/McpServer/Server/Driver/SwooleServerDriver.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace Butschster\ContextGenerator\McpServer\Server\Driver;
66

77
use Butschster\ContextGenerator\McpServer\Config\SwooleTransportConfig;
8+
use Butschster\ContextGenerator\McpServer\Server\MessageParser;
89
use Butschster\ContextGenerator\McpServer\Server\Swoole\SwooleServerRunner;
910
use Mcp\Server\InitializationOptions;
1011
use Mcp\Server\Server as McpServer;
1112
use Psr\Log\LoggerInterface;
13+
use Spiral\Exceptions\ExceptionReporterInterface;
1214
use Swoole\Http\Request;
1315
use Swoole\Http\Response;
1416
use Swoole\Http\Server;
@@ -25,6 +27,8 @@ final class SwooleServerDriver implements ServerDriverInterface
2527
public function __construct(
2628
private readonly SwooleTransportConfig $config,
2729
private readonly LoggerInterface $logger,
30+
private readonly ExceptionReporterInterface $reporter,
31+
private readonly MessageParser $messageParser,
2832
) {}
2933

3034
public function run(McpServer $server, InitializationOptions $initOptions): void
@@ -39,6 +43,7 @@ public function run(McpServer $server, InitializationOptions $initOptions): void
3943
$server,
4044
$initOptions,
4145
$this->config,
46+
$this->messageParser,
4247
$this->logger,
4348
);
4449

@@ -268,6 +273,7 @@ private function handleRequest(Request $request, Response $response): void
268273
$this->sendErrorResponse($response, 'Server not properly initialized', 500);
269274
}
270275
} catch (\Throwable $e) {
276+
$this->reporter->report($e);
271277
$this->logger->error('Error handling request', [
272278
'error' => $e->getMessage(),
273279
'trace' => $e->getTraceAsString(),
@@ -506,6 +512,7 @@ private function cleanup(): void
506512
try {
507513
$this->runner->stop();
508514
} catch (\Throwable $e) {
515+
$this->reporter->report($e);
509516
$this->logger->error('Error stopping runner', [
510517
'error' => $e->getMessage(),
511518
]);

0 commit comments

Comments
 (0)