-
Notifications
You must be signed in to change notification settings - Fork 68
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
11
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
11 commits
Select commit
Hold shift + click to select a range
edc9cde
Implemented MCP logging specification with auto-injection
Adebayo120 36695ac
Added doc
Adebayo120 f68e110
Modified doc
Adebayo120 49ebb27
Fixed lint issues
Adebayo120 21cf288
Fixed issues found in pipeline
Adebayo120 30f8f42
Fixed issue with McpLogger compatibility with LoggerInterface
Adebayo120 ec1e63b
Updated with latest Server method interface
Adebayo120 11e74b5
Exclude Auto injected client logger from generated input-schema
Adebayo120 997d819
Streamlined examples/stdio-logging-showcase
Adebayo120 5da6e21
Simplified docs
Adebayo120 5bd3452
Renamed McpLogger to ClientLogger
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,6 +13,7 @@ | |||||
|
||||||
use Mcp\Capability\Attribute\McpResource; | ||||||
use Mcp\Capability\Attribute\McpTool; | ||||||
use Mcp\Capability\Logger\ClientLogger; | ||||||
use Psr\Log\LoggerInterface; | ||||||
use Psr\Log\NullLogger; | ||||||
|
||||||
|
@@ -40,14 +41,15 @@ public function __construct( | |||||
* Supports 'add', 'subtract', 'multiply', 'divide'. | ||||||
* Obeys the 'precision' and 'allow_negative' settings from the config resource. | ||||||
* | ||||||
* @param float $a the first operand | ||||||
* @param float $b the second operand | ||||||
* @param string $operation the operation ('add', 'subtract', 'multiply', 'divide') | ||||||
* @param float $a the first operand | ||||||
* @param float $b the second operand | ||||||
* @param string $operation the operation ('add', 'subtract', 'multiply', 'divide') | ||||||
* @param ClientLogger $logger Auto-injected MCP logger | ||||||
* | ||||||
* @return float|string the result of the calculation, or an error message string | ||||||
*/ | ||||||
#[McpTool(name: 'calculate')] | ||||||
public function calculate(float $a, float $b, string $operation): float|string | ||||||
public function calculate(float $a, float $b, string $operation, ClientLogger $logger): float|string | ||||||
{ | ||||||
$this->logger->info(\sprintf('Calculating: %f %s %f', $a, $operation, $b)); | ||||||
|
||||||
|
@@ -65,25 +67,48 @@ public function calculate(float $a, float $b, string $operation): float|string | |||||
break; | ||||||
case 'divide': | ||||||
if (0 == $b) { | ||||||
$logger->warning('Division by zero attempted', [ | ||||||
'operand_a' => $a, | ||||||
'operand_b' => $b, | ||||||
]); | ||||||
|
||||||
return 'Error: Division by zero.'; | ||||||
} | ||||||
$result = $a / $b; | ||||||
break; | ||||||
default: | ||||||
$logger->error('Unknown operation requested', [ | ||||||
'operation' => $operation, | ||||||
'supported_operations' => ['add', 'subtract', 'multiply', 'divide'], | ||||||
]); | ||||||
|
||||||
return "Error: Unknown operation '{$operation}'. Supported: add, subtract, multiply, divide."; | ||||||
} | ||||||
|
||||||
if (!$this->config['allow_negative'] && $result < 0) { | ||||||
$logger->warning('Negative result blocked by configuration', [ | ||||||
'result' => $result, | ||||||
'allow_negative_setting' => false, | ||||||
]); | ||||||
|
||||||
return 'Error: Negative results are disabled.'; | ||||||
} | ||||||
|
||||||
return round($result, $this->config['precision']); | ||||||
$finalResult = round($result, $this->config['precision']); | ||||||
$logger->info('✅ Calculation completed successfully', [ | ||||||
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.
Suggested change
|
||||||
'result' => $finalResult, | ||||||
'precision' => $this->config['precision'], | ||||||
]); | ||||||
|
||||||
return $finalResult; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Provides the current calculator configuration. | ||||||
* Can be read by clients to understand precision etc. | ||||||
* | ||||||
* @param ClientLogger $logger Auto-injected MCP logger for demonstration | ||||||
* | ||||||
* @return Config the configuration array | ||||||
*/ | ||||||
#[McpResource( | ||||||
|
@@ -92,9 +117,12 @@ public function calculate(float $a, float $b, string $operation): float|string | |||||
description: 'Current settings for the calculator tool (precision, allow_negative).', | ||||||
mimeType: 'application/json', | ||||||
)] | ||||||
public function getConfiguration(): array | ||||||
public function getConfiguration(ClientLogger $logger): array | ||||||
{ | ||||||
$this->logger->info('Resource config://calculator/settings read.'); | ||||||
$logger->info('📊 Resource config://calculator/settings accessed via auto-injection!', [ | ||||||
'current_config' => $this->config, | ||||||
'auto_injection_demo' => 'ClientLogger was automatically injected into this resource handler', | ||||||
]); | ||||||
|
||||||
return $this->config; | ||||||
} | ||||||
|
@@ -103,8 +131,9 @@ public function getConfiguration(): array | |||||
* Updates a specific configuration setting. | ||||||
* Note: This requires more robust validation in a real app. | ||||||
* | ||||||
* @param string $setting the setting key ('precision' or 'allow_negative') | ||||||
* @param mixed $value the new value (int for precision, bool for allow_negative) | ||||||
* @param string $setting the setting key ('precision' or 'allow_negative') | ||||||
* @param mixed $value the new value (int for precision, bool for allow_negative) | ||||||
* @param ClientLogger $logger Auto-injected MCP logger | ||||||
* | ||||||
* @return array{ | ||||||
* success: bool, | ||||||
|
@@ -113,18 +142,32 @@ public function getConfiguration(): array | |||||
* } success message or error | ||||||
*/ | ||||||
#[McpTool(name: 'update_setting')] | ||||||
public function updateSetting(string $setting, mixed $value): array | ||||||
public function updateSetting(string $setting, mixed $value, ClientLogger $logger): array | ||||||
{ | ||||||
$this->logger->info(\sprintf('Setting tool called: setting=%s, value=%s', $setting, var_export($value, true))); | ||||||
if (!\array_key_exists($setting, $this->config)) { | ||||||
$logger->error('Unknown setting requested', [ | ||||||
'setting' => $setting, | ||||||
'available_settings' => array_keys($this->config), | ||||||
]); | ||||||
|
||||||
return ['success' => false, 'error' => "Unknown setting '{$setting}'."]; | ||||||
} | ||||||
|
||||||
if ('precision' === $setting) { | ||||||
if (!\is_int($value) || $value < 0 || $value > 10) { | ||||||
$logger->warning('Invalid precision value provided', [ | ||||||
'value' => $value, | ||||||
'valid_range' => '0-10', | ||||||
]); | ||||||
|
||||||
return ['success' => false, 'error' => 'Invalid precision value. Must be integer between 0 and 10.']; | ||||||
} | ||||||
$this->config['precision'] = $value; | ||||||
$logger->info('✅ Precision setting updated', [ | ||||||
'new_precision' => $value, | ||||||
'previous_config' => $this->config, | ||||||
]); | ||||||
|
||||||
// In real app, notify subscribers of config://calculator/settings change | ||||||
// $registry->notifyResourceChanged('config://calculator/settings'); | ||||||
|
@@ -138,10 +181,19 @@ public function updateSetting(string $setting, mixed $value): array | |||||
} elseif (\in_array(strtolower((string) $value), ['false', '0', 'no', 'off'])) { | ||||||
$value = false; | ||||||
} else { | ||||||
$logger->warning('Invalid allow_negative value provided', [ | ||||||
'value' => $value, | ||||||
'expected_type' => 'boolean', | ||||||
]); | ||||||
|
||||||
return ['success' => false, 'error' => 'Invalid allow_negative value. Must be boolean (true/false).']; | ||||||
} | ||||||
} | ||||||
$this->config['allow_negative'] = $value; | ||||||
$logger->info('✅ Allow negative setting updated', [ | ||||||
'new_allow_negative' => $value, | ||||||
'updated_config' => $this->config, | ||||||
]); | ||||||
|
||||||
// $registry->notifyResourceChanged('config://calculator/settings'); | ||||||
return ['success' => true, 'message' => 'Allow negative results set to '.($value ? 'true' : 'false').'.']; | ||||||
|
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,35 @@ | ||
#!/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()) | ||
->enableClientLogging() // Enable MCP logging capability and auto-injection! | ||
->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
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.
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.
this could remain to be a server-side log message