-
-
Notifications
You must be signed in to change notification settings - Fork 124
[MCP Bundle] Make MonologBundle optional #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[MCP Bundle] Make MonologBundle optional #864
Conversation
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
WebMamba
left a comment
There was a problem hiding this 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.
| if (!class_exists(MonologBundle::class)) { | ||
| $this->markTestSkipped('MonologBundle is not installed'); | ||
| } | ||
|
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/mcp-bundle/config/services.php
Outdated
| ->call('setPaginationLimit', [param('mcp.pagination_limit')]) | ||
| ->call('setInstructions', [param('mcp.instructions')]) | ||
| ->call('setLogger', [service('monolog.logger.mcp')]) | ||
| ->call('setLogger', [service('monolog.logger.mcp')->nullOnInvalid()]) |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
|
I tend more and more to require the logger |
Me too, I feel like we’re making things unnecessarily complicated 😂 |
There was a problem hiding this 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, |
There was a problem hiding this comment.
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 ?
|
Makes sense, open for a PR bringing in some tests? |
| $transport = $this->logger | ||
| ? new StdioTransport(logger: $this->logger) | ||
| : new StdioTransport(); |
There was a problem hiding this comment.
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 ?
| $transport = $this->logger | |
| ? new StdioTransport(logger: $this->logger) | |
| : new StdioTransport(); | |
| $transport = new StdioTransport(logger: $this->logger === null ? NullLogger() : $this->logger) |
There was a problem hiding this comment.
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 😅
|
But yes, to sum up my thought, you can either:
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 |
@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? |
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
monolog.logger.mcpservice creation inclass_exists()check->nullOnInvalid()tosetLogger()call in services.phpmonolog.logger.mcpwithNULL_ON_INVALID_REFERENCECredits
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.