-
Notifications
You must be signed in to change notification settings - Fork 13
Description
Add HTTP transport support to the existing MCP server infrastructure by implementing transport selection via environment variables and creating necessary configuration DTOs.
Implementation Plan
1. HttpTransportConfig DTO
File: src/McpServer/HttpTransportConfig.php
<?php
declare(strict_types=1);
namespace Butschster\ContextGenerator\McpServer;
final readonly class HttpTransportConfig
{
public function __construct(
public string $host = 'localhost',
public int $port = 8080,
public string $sessionStore = 'file',
public ?string $sessionStorePath = null,
public bool $corsEnabled = true,
public array $corsOrigins = ['*'],
public int $maxRequestSize = 10485760, // 10MB
) {}
public function getAddress(): string
{
return "http://{$this->host}:{$this->port}";
}
public function getSessionStoreConfig(): array
{
return [
'type' => $this->sessionStore,
'path' => $this->sessionStorePath,
];
}
}
2. McpConfig Updates
Add HTTP transport configuration section and accessor methods:
// Add to protected array $config
'transport' => [
'type' => 'stdio', // stdio|http
'http' => [
'host' => 'localhost',
'port' => 8080,
'session_store' => 'file',
'session_store_path' => null,
'cors_enabled' => true,
'cors_origins' => ['*'],
'max_request_size' => 10485760,
],
],
// Add accessor methods
public function getTransportType(): string
{
return $this->config['transport']['type'] ?? 'stdio';
}
public function isHttpTransport(): bool
{
return $this->getTransportType() === 'http';
}
public function getHttpTransportConfig(): HttpTransportConfig
{
$http = $this->config['transport']['http'] ?? [];
return new HttpTransportConfig(
host: $http['host'] ?? 'localhost',
port: (int)($http['port'] ?? 8080),
sessionStore: $http['session_store'] ?? 'file',
sessionStorePath: $http['session_store_path'] ?? null,
corsEnabled: (bool)($http['cors_enabled'] ?? true),
corsOrigins: $http['cors_origins'] ?? ['*'],
maxRequestSize: (int)($http['max_request_size'] ?? 10485760),
);
}
3. McpServerBootloader Updates
Add HTTP transport environment variables to init
method:
// Add to $this->config->setDefaults() array
'transport' => [
'type' => $env->get('MCP_TRANSPORT', 'stdio'),
'http' => [
'host' => $env->get('MCP_HTTP_HOST', 'localhost'),
'port' => (int) $env->get('MCP_HTTP_PORT', 8080),
'session_store' => $env->get('MCP_HTTP_SESSION_STORE', 'file'),
'session_store_path' => $env->get('MCP_HTTP_SESSION_STORE_PATH'),
'cors_enabled' => (bool) $env->get('MCP_HTTP_CORS_ENABLED', true),
'cors_origins' => array_filter(explode(',', $env->get('MCP_HTTP_CORS_ORIGINS', '*'))),
'max_request_size' => (int) $env->get('MCP_HTTP_MAX_REQUEST_SIZE', 10485760),
],
],
4. Server.php Updates
Modify the run
method to use appropriate runner based on transport configuration:
public function run(string $name): void
{
$server = new McpServer(name: $name, logger: $this->logger);
$this->configureServer($server);
$initOptions = $server->createInitializationOptions();
// Determine transport type from environment or config
$transportType = $_ENV['MCP_TRANSPORT'] ?? 'stdio';
if ($transportType === 'http') {
// Use HTTP runner with configuration
$httpOptions = [
'host' => $_ENV['MCP_HTTP_HOST'] ?? 'localhost',
'port' => (int) ($_ENV['MCP_HTTP_PORT'] ?? 8080),
];
$runner = new \Mcp\Server\HttpServerRunner(
server: $server,
initOptions: $initOptions,
httpOptions: $httpOptions,
logger: $this->logger
);
// Handle HTTP request if in HTTP mode
$response = $runner->handleRequest();
$runner->sendResponse($response);
} else {
// Use STDIO runner (default)
$runner = new \Mcp\Server\ServerRunner(
server: $server,
initOptions: $initOptions,
logger: $this->logger
);
$runner->run();
}
}
Environment Variables
Transport Selection
MCP_TRANSPORT=stdio|http
- Transport type (default: stdio)
HTTP Transport Options
MCP_HTTP_HOST=localhost
- HTTP server host (default: localhost)MCP_HTTP_PORT=8080
- HTTP server port (default: 8080)MCP_HTTP_SESSION_STORE=file|memory
- Session storage type (default: file)MCP_HTTP_SESSION_STORE_PATH=/path/to/sessions
- Session storage pathMCP_HTTP_CORS_ENABLED=true
- Enable CORS (default: true)MCP_HTTP_CORS_ORIGINS=*
- CORS allowed origins (comma-separated)MCP_HTTP_MAX_REQUEST_SIZE=10485760
- Max request size in bytes (default: 10MB)
Usage Examples
STDIO Mode (Default)
php app.php server
# or explicitly
MCP_TRANSPORT=stdio php app.php server
HTTP Mode
MCP_TRANSPORT=http php app.php server
# or with custom options
MCP_TRANSPORT=http MCP_HTTP_HOST=0.0.0.0 MCP_HTTP_PORT=3000 php app.php server
Metadata
Metadata
Assignees
Labels
mcpMCP server componentsMCP server components
Type
Projects
Status
Done