Make logger nullable in Transport constructors (#152)
- BaseTransport: Accept ?LoggerInterface parameter and convert null to NullLogger - StdioTransport: Change logger parameter to nullable - StreamableHttpTransport: Change logger parameter to nullable - InMemoryTransport: Fix missing parent::__construct() call and add logging
This commit is contained in:
parent
7cd281d0c7
commit
470237ad0b
4 changed files with 14 additions and 7 deletions
|
|
@ -14,6 +14,7 @@ namespace Mcp\Server\Transport;
|
|||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
/**
|
||||
|
|
@ -38,9 +39,11 @@ abstract class BaseTransport
|
|||
*/
|
||||
protected ?\Fiber $sessionFiber = null;
|
||||
|
||||
public function __construct(
|
||||
protected readonly LoggerInterface $logger,
|
||||
) {
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(?LoggerInterface $logger = null)
|
||||
{
|
||||
$this->logger = $logger ?? new NullLogger();
|
||||
}
|
||||
|
||||
public function initialize(): void
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Mcp\Server\Transport;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
/**
|
||||
|
|
@ -27,7 +28,9 @@ class InMemoryTransport extends BaseTransport implements TransportInterface
|
|||
*/
|
||||
public function __construct(
|
||||
private readonly array $messages = [],
|
||||
?LoggerInterface $logger = null,
|
||||
) {
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
public function initialize(): void
|
||||
|
|
@ -51,10 +54,13 @@ class InMemoryTransport extends BaseTransport implements TransportInterface
|
|||
*/
|
||||
public function listen(): mixed
|
||||
{
|
||||
$this->logger->info('InMemoryTransport is processing messages...');
|
||||
|
||||
foreach ($this->messages as $message) {
|
||||
$this->handleMessage($message, $this->sessionId);
|
||||
}
|
||||
|
||||
$this->logger->info('InMemoryTransport finished processing.');
|
||||
$this->handleSessionEnd($this->sessionId);
|
||||
|
||||
$this->sessionId = null;
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ namespace Mcp\Server\Transport;
|
|||
|
||||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* @implements TransportInterface<int>
|
||||
|
|
@ -29,7 +28,7 @@ class StdioTransport extends BaseTransport implements TransportInterface
|
|||
public function __construct(
|
||||
private $input = \STDIN,
|
||||
private $output = \STDOUT,
|
||||
LoggerInterface $logger = new NullLogger(),
|
||||
?LoggerInterface $logger = null,
|
||||
) {
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ use Psr\Http\Message\ResponseInterface;
|
|||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\StreamFactoryInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
/**
|
||||
|
|
@ -45,7 +44,7 @@ class StreamableHttpTransport extends BaseTransport implements TransportInterfac
|
|||
?ResponseFactoryInterface $responseFactory = null,
|
||||
?StreamFactoryInterface $streamFactory = null,
|
||||
array $corsHeaders = [],
|
||||
LoggerInterface $logger = new NullLogger(),
|
||||
?LoggerInterface $logger = null,
|
||||
) {
|
||||
parent::__construct($logger);
|
||||
$sessionIdString = $this->request->getHeaderLine('Mcp-Session-Id');
|
||||
|
|
|
|||
Loading…
Reference in a new issue