-
Notifications
You must be signed in to change notification settings - Fork 79
[Server] Implemented MCP logging specification with auto-injection #104
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
Open
Adebayo120
wants to merge
14
commits into
modelcontextprotocol:main
Choose a base branch
from
Adebayo120:feature/mcp-logging-specification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c51af7e
Implemented MCP logging specification with auto-injection
Adebayo120 4a1b62c
Added doc
Adebayo120 77a774f
Modified doc
Adebayo120 eb998cf
Fixed lint issues
Adebayo120 9362f6c
Fixed issues found in pipeline
Adebayo120 cd27bac
Fixed issue with McpLogger compatibility with LoggerInterface
Adebayo120 2fdb8d8
Updated with latest Server method interface
Adebayo120 4a8b33e
Exclude Auto injected client logger from generated input-schema
Adebayo120 b701bb1
Streamlined examples/stdio-logging-showcase
Adebayo120 11cfb16
Simplified docs
Adebayo120 4614dc5
Renamed McpLogger to ClientLogger
Adebayo120 e21a81d
Enabled logging by default
Adebayo120 0eecd64
Resolved conflict
Adebayo120 e082fd3
Rmoved unnecessary line of codes
Adebayo120 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
examples/stdio-logging-showcase/LoggingShowcaseHandlers.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mcp\Example\StdioLoggingShowcase; | ||
|
|
||
| use Mcp\Capability\Attribute\McpTool; | ||
| use Mcp\Capability\Logger\ClientLogger; | ||
|
|
||
| /** | ||
| * Example handlers showcasing auto-injected MCP logging capabilities. | ||
| * | ||
| * This demonstrates how handlers can receive ClientLogger automatically | ||
| * without any manual configuration - just declare it as a parameter! | ||
| */ | ||
| final class LoggingShowcaseHandlers | ||
| { | ||
| /** | ||
| * Tool that demonstrates different logging levels with auto-injected ClientLogger. | ||
| * | ||
| * @param string $message The message to log | ||
| * @param string $level The logging level (debug, info, warning, error) | ||
| * @param ClientLogger $logger Auto-injected MCP logger | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| #[McpTool(name: 'log_message', description: 'Demonstrates MCP logging with different levels')] | ||
| public function logMessage(string $message, string $level, ClientLogger $logger): array | ||
| { | ||
| $logger->info('🚀 Starting log_message tool', [ | ||
| 'requested_level' => $level, | ||
| 'message_length' => \strlen($message), | ||
| ]); | ||
|
|
||
| switch (strtolower($level)) { | ||
| case 'debug': | ||
| $logger->debug("Debug: $message", ['tool' => 'log_message']); | ||
| break; | ||
| case 'info': | ||
| $logger->info("Info: $message", ['tool' => 'log_message']); | ||
| break; | ||
| case 'notice': | ||
| $logger->notice("Notice: $message", ['tool' => 'log_message']); | ||
| break; | ||
| case 'warning': | ||
| $logger->warning("Warning: $message", ['tool' => 'log_message']); | ||
| break; | ||
| case 'error': | ||
| $logger->error("Error: $message", ['tool' => 'log_message']); | ||
| break; | ||
| case 'critical': | ||
| $logger->critical("Critical: $message", ['tool' => 'log_message']); | ||
| break; | ||
| case 'alert': | ||
| $logger->alert("Alert: $message", ['tool' => 'log_message']); | ||
| break; | ||
| case 'emergency': | ||
| $logger->emergency("Emergency: $message", ['tool' => 'log_message']); | ||
| break; | ||
| default: | ||
| $logger->warning("Unknown level '$level', defaulting to info"); | ||
| $logger->info("Info: $message", ['tool' => 'log_message']); | ||
| } | ||
|
|
||
| $logger->debug('log_message tool completed successfully'); | ||
|
|
||
| return [ | ||
| 'message' => "Logged message with level: $level", | ||
| 'logged_at' => date('Y-m-d H:i:s'), | ||
| 'level_used' => $level, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| require_once dirname(__DIR__).'/bootstrap.php'; | ||
| chdir(__DIR__); | ||
|
|
||
| use Mcp\Server; | ||
| use Mcp\Server\Transport\StdioTransport; | ||
|
|
||
| logger()->info('Starting MCP Stdio Logging Showcase Server...'); | ||
|
|
||
| // Create server with auto-discovery of MCP capabilities and ENABLE MCP LOGGING | ||
| $server = Server::builder() | ||
| ->setServerInfo('Stdio Logging Showcase', '1.0.0', 'Demonstration of auto-injected MCP logging in capability handlers.') | ||
| ->setContainer(container()) | ||
| ->setLogger(logger()) | ||
| ->setDiscovery(__DIR__, ['.']) | ||
| ->build(); | ||
|
|
||
| $transport = new StdioTransport(logger: logger()); | ||
|
|
||
| $server->run($transport); | ||
|
|
||
| logger()->info('Logging Showcase Server is ready!'); | ||
| logger()->info('This example demonstrates auto-injection of ClientLogger into capability handlers.'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -409,6 +409,10 @@ private function parseParametersInfo(\ReflectionMethod|\ReflectionFunction $refl | |||||||
| $parametersInfo = []; | ||||||||
|
|
||||||||
| foreach ($reflection->getParameters() as $rp) { | ||||||||
| if ($this->isAutoInjectedParameter($rp)) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| $paramName = $rp->getName(); | ||||||||
| $paramTag = $paramTags['$'.$paramName] ?? null; | ||||||||
|
|
||||||||
|
|
@@ -784,4 +788,25 @@ private function mapSimpleTypeToJsonSchema(string $type): string | |||||||
| default => \in_array(strtolower($type), ['datetime', 'datetimeinterface']) ? 'string' : 'object', | ||||||||
| }; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Determines if a parameter was auto-injected and should be excluded from schema generation. | ||||||||
| * | ||||||||
| * Parameters that are auto-injected by the framework (like ClientLogger) should not appear | ||||||||
| * in the tool schema since they're not provided by the client. | ||||||||
| */ | ||||||||
| private function isAutoInjectedParameter(\ReflectionParameter $parameter): bool | ||||||||
| { | ||||||||
| $type = $parameter->getType(); | ||||||||
|
|
||||||||
| if (!$type instanceof \ReflectionNamedType) { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| $typeName = $type->getName(); | ||||||||
|
|
||||||||
| // Auto-inject for ClientLogger or LoggerInterface types | ||||||||
| return 'Mcp\\Capability\\Logger\\ClientLogger' === $typeName | ||||||||
| || 'Psr\\Log\\LoggerInterface' === $typeName; | ||||||||
|
Comment on lines
+809
to
+810
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually you could simplify this to sth like
Suggested change
|
||||||||
| } | ||||||||
| } | ||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.