Skip to content

Commit 05b9258

Browse files
fix(server): Make logging opt-in by default
1 parent 42b93e7 commit 05b9258

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ You can install the package via Composer:
4242
composer require php-mcp/server
4343
```
4444

45-
> **Note:** For Laravel applications, consider using the dedicated [`php-mcp/laravel-server`](https://github.com/php-mcp/laravel) package. It builds upon this core library, providing helpful integrations, configuration options, and Artisan commands specifically tailored for the Laravel framework.
45+
> **Note:** For Laravel applications, consider using the dedicated [`php-mcp/laravel`](https://github.com/php-mcp/laravel) package. It builds upon this core library, providing helpful integrations, configuration options, and Artisan commands specifically tailored for the Laravel framework.
4646
4747
## Getting Started: A Simple `stdio` Server
4848

@@ -384,9 +384,9 @@ class SummarizePrompt {
384384
The `PhpMcp\Server\Server` class is the main entry point for configuring and running your MCP server. It provides a fluent interface (method chaining) to set up dependencies, parameters, and manually register elements.
385385

386386
* **`Server::make(): self`**: Static factory method to create a new server instance. It initializes the server with default implementations for core services (Logger, Cache, Config, Container).
387-
* **`->withLogger(LoggerInterface $logger): self`**: Provide a PSR-3 compliant logger implementation.
388-
* **If using the default `BasicContainer`:** This method replaces the default `StreamLogger` instance and updates the registration within the `BasicContainer`.
389-
* **If using a custom container:** This method *only* sets an internal property on the `Server` instance. It **does not** affect the custom container. You should register your desired `LoggerInterface` directly within your container setup.
387+
* **`->withLogger(LoggerInterface $logger): self`**: Provide a PSR-3 compliant logger implementation. By default, logging is disabled unless a logger is explicitly provided here (when using the default container) or registered in a custom container.
388+
* **If using the default `BasicContainer`:** This method replaces the default no-op logger and updates the registration within the `BasicContainer`, enabling logging.
389+
* **If using a custom container:** This method *only* sets an internal property on the `Server` instance. It **does not** affect the custom container. You should register your desired `LoggerInterface` directly within your container setup to enable logging.
390390
* **`->withCache(CacheInterface $cache): self`**: Provide a PSR-16 compliant cache implementation.
391391
* **If using the default `BasicContainer`:** This method replaces the default `FileCache` instance and updates the registration within the `BasicContainer`.
392392
* **If using a custom container:** This method *only* sets an internal property on the `Server` instance. It **does not** affect the custom container. You should register your desired `CacheInterface` directly within your container setup.
@@ -420,17 +420,17 @@ The `Server` relies on a PSR-11 `ContainerInterface` for two main purposes:
420420
**Default Behavior (No `withContainer` Call):**
421421

422422
If you *do not* call `->withContainer()`, the server uses its internal `PhpMcp\Server\Defaults\BasicContainer`. This basic container comes pre-configured with default implementations:
423-
* `LoggerInterface` -> `PhpMcp\Server\Defaults\StreamLogger` (writes to `STDERR`)
423+
* `LoggerInterface` -> `Psr\Log\NullLogger` (Logging is effectively disabled)
424424
* `CacheInterface` -> `PhpMcp\Server\Defaults\FileCache` (writes to `../cache/mcp_cache` relative to the package directory)
425425
* `ConfigurationRepositoryInterface` -> `PhpMcp\Server\Defaults\ArrayConfigurationRepository` (uses built-in default configuration values)
426426

427-
In this default mode, you *can* use the `->withLogger()`, `->withCache()`, and `->withConfig()` methods to replace these defaults. These methods will update the instance used by the server and also update the registration within the internal `BasicContainer`.
427+
In this default mode, you *can* use the `->withLogger()`, `->withCache()`, and `->withConfig()` methods to replace these defaults. These methods update the instance used by the server and also update the registration within the internal `BasicContainer`.
428428

429429
**Using a Custom Container (`->withContainer(MyContainer $c)`):**
430430

431431
If you provide your own PSR-11 container instance using `->withContainer()`, the responsibility shifts entirely to you:
432432

433-
* **You MUST ensure your container is configured to provide implementations for `LoggerInterface`, `CacheInterface`, and `ConfigurationRepositoryInterface`.** The server will attempt to fetch these using `$container->get(...)` and will fail if they are not available.
433+
* **You MUST ensure your container is configured to provide implementations for `LoggerInterface`, `CacheInterface`, and `ConfigurationRepositoryInterface`.** The server will attempt to fetch these using `$container->get(...)` and will fail if they are not available. Providing a `NullLogger` for `LoggerInterface` will keep logging disabled.
434434
* Your container will also be used to instantiate your handler classes, so ensure all their dependencies are also properly configured within your container.
435435
* When using a custom container, the `->withLogger()`, `->withCache()`, and `->withConfig()` methods on the `Server` instance become largely ineffective for modifying the dependencies the server *actually uses* during request processing, as the server will always defer to retrieving these services from *your provided container*. Configure these services directly in your container's setup.
436436

src/Server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpMcp\Server\Defaults\ArrayConfigurationRepository;
1111
use PhpMcp\Server\Defaults\BasicContainer;
1212
use PhpMcp\Server\Defaults\FileCache;
13-
use PhpMcp\Server\Defaults\StreamLogger;
1413
use PhpMcp\Server\Definitions\PromptDefinition;
1514
use PhpMcp\Server\Definitions\ResourceDefinition;
1615
use PhpMcp\Server\Definitions\ResourceTemplateDefinition;
@@ -21,6 +20,7 @@
2120
use PhpMcp\Server\Transports\StdioTransportHandler;
2221
use Psr\Container\ContainerInterface;
2322
use Psr\Log\LoggerInterface;
23+
use Psr\Log\NullLogger;
2424
use Psr\SimpleCache\CacheInterface;
2525
use ReflectionClass;
2626
use ReflectionMethod;
@@ -47,7 +47,7 @@ public function __construct()
4747
$container = new BasicContainer;
4848

4949
$config = new ArrayConfigurationRepository($this->getDefaultConfigValues());
50-
$logger = new StreamLogger(STDERR, 'debug');
50+
$logger = new NullLogger;
5151
$cache = new FileCache(__DIR__.'/../cache/mcp_cache');
5252

5353
$container->set(ConfigurationRepositoryInterface::class, $config);

0 commit comments

Comments
 (0)