Skip to content

Conversation

@camilleislasse
Copy link
Contributor

@camilleislasse camilleislasse commented Nov 13, 2025

Q A
Bug fix? yes
New feature? no
Docs? no
Issues Fix #830
License MIT

This PR makes MonologBundle optional dependency for McpBundle. Without it installed, the container compilation was failing with "Parent definition' monolog.logger_prototype' does not exist."

Changes

  • Wrap monolog.logger.mcp service creation in class_exists() check
  • Add ->nullOnInvalid() to setLogger() call in services.php
  • Uniformize logger references: both McpCommand and McpController now use monolog.logger.mcp with NULL_ON_INVALID_REFERENCE
  • Update test to skip when MonologBundle is not installed

Credits

Builds on the initial work in #849 by @WebMamba. That PR protected service creation but the service was still referenced without protection. This PR completes the fix by also protecting the service usage.

Fixes symfony#830

- Wrap monolog.logger.mcp service creation in class_exists() check
- Add ->nullOnInvalid() to setLogger() call
- Uniformize logger references in McpCommand and McpController
- Update test to skip when MonologBundle is not installed

Builds on symfony#849 by @WebMamba
@carsonbot carsonbot added Bug Something isn't working Status: Needs Review labels Nov 13, 2025
@carsonbot carsonbot changed the title Make MonologBundle optional in McpBundle Make MonologBundle optional in McpBundle Nov 13, 2025
@OskarStark OskarStark added the MCP Bundle Issues & PRs about the MCP SDK integration bundle label Nov 13, 2025
@carsonbot carsonbot changed the title Make MonologBundle optional in McpBundle [MCP Bundle] Make MonologBundle optional in McpBundle Nov 13, 2025
@OskarStark OskarStark changed the title [MCP Bundle] Make MonologBundle optional in McpBundle [MCP Bundle] Make MonologBundle optional Nov 13, 2025
Copy link

@WebMamba WebMamba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @camilleislasse to give me a hand on this.

Comment on lines +53 to +56
if (!class_exists(MonologBundle::class)) {
$this->markTestSkipped('MonologBundle is not installed');
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since monolog is never installed on the ci, this test will be always skipped, so the rest of the test is useless. What do you think about requiring monolog on dev then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->call('setPaginationLimit', [param('mcp.pagination_limit')])
->call('setInstructions', [param('mcp.instructions')])
->call('setLogger', [service('monolog.logger.mcp')])
->call('setLogger', [service('monolog.logger.mcp')->nullOnInvalid()])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function setLogger can't have a nullable as argument see:
https://github.com/modelcontextprotocol/php-sdk/blob/7a3b473c405411aca8be14c0c6e12d2493752d61/src/Server/Builder.php#L254.
So I don't think this can work... Maybe to have an if condition before the call can be better ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and another error probably here :

private readonly LoggerInterface $logger,

Copy link
Contributor Author

@camilleislasse camilleislasse Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely happy with this solution ecea6d6 - conditionally instantiating transports with/without the logger parameter feels verbose.

The issue: MCP SDK has LoggerInterface $logger = new NullLogger() (non-nullable), so we can't pass null even though there's a default, if you have a better idea @WebMamba

Can change if modelcontextprotocol/php-sdk#152 is correct

…ts, and handle nullable logger in McpCommand/McpController by conditionally passing it to Transports.

  When MonologBundle is absent, Server and Transports use their default NullLogger.
@OskarStark
Copy link
Contributor

I tend more and more to require the logger

@camilleislasse
Copy link
Contributor Author

I tend more and more to require the logger

Me too, I feel like we’re making things unnecessarily complicated 😂

Copy link

@WebMamba WebMamba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it’s not really the point of this PR, but one of the key parts of this bundle is to build/instantiate the mcp.server.builder, and we don’t have any tests to secure this part. It seems important to me to add tests to cover it. The first commit of this PR had the CI green even if the builder wasn’t working.

$this->httpMessageFactory->createRequest($request),
$this->responseFactory,
$this->streamFactory,
logger: $this->logger,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here you can pass the NullLogger instead ?

@OskarStark
Copy link
Contributor

Makes sense, open for a PR bringing in some tests?

Comment on lines +34 to +36
$transport = $this->logger
? new StdioTransport(logger: $this->logger)
: new StdioTransport();
Copy link

@WebMamba WebMamba Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you thinks instead of having the NullLogger logic here: https://github.com/modelcontextprotocol/php-sdk/pull/152/files to have it here ?

Suggested change
$transport = $this->logger
? new StdioTransport(logger: $this->logger)
: new StdioTransport();
$transport = new StdioTransport(logger: $this->logger === null ? NullLogger() : $this->logger)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry the reviews are in the wrong order I miss clicked 😅

@WebMamba
Copy link

But yes, to sum up my thought, you can either:

  • require Monolog
  • or if Monolog is not installed, instantiate a NullLogger

After some reflection, I’m leaning more toward the NullLogger option 😇

@camilleislasse
Copy link
Contributor Author

But yes, to sum up my thought, you can either:

  • require Monolog
  • or if Monolog is not installed, instantiate a NullLogger

After some reflection, I’m leaning more toward the NullLogger option 😇

Maybe wait the review of modelcontextprotocol/php-sdk#152 ? If it's merged, I can pass the logger (which can be null via ContainerInterface::NULL_ON_INVALID_REFERENCE) directly to the transport without ternary operators.

@camilleislasse
Copy link
Contributor Author

camilleislasse commented Nov 16, 2025

Makes sense, open for a PR bringing in some tests?

@OskarStark Shouldn't lint:container have caught this? I'm not sure if it's possible to run it independently on each bundle though - they each have their own dependencies, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working MCP Bundle Issues & PRs about the MCP SDK integration bundle Status: Reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MCP Bundle] missing requirement to monolog

4 participants