Skip to content

[MCP SDK] Example update to use proper logging #331

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/mcp-sdk/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^11.5",
"symfony/console": "^6.4 || ^7.0",
"psr/cache": "^3.0"
"psr/cache": "^3.0",
"monolog/monolog": "^3.9"
},
"suggest": {
"symfony/console": "To use SymfonyConsoleTransport for STDIO",
Expand Down
13 changes: 11 additions & 2 deletions src/mcp-sdk/examples/cli/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@

require __DIR__.'/vendor/autoload.php';

use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Symfony\Component\Console as SymfonyConsole;
use Symfony\Component\Console\Output\OutputInterface;

$debug = (bool) ($_SERVER['DEBUG'] ?? false);

// Setup input, output and logger
// Setup input, output
$input = new SymfonyConsole\Input\ArgvInput($argv);
$output = new SymfonyConsole\Output\ConsoleOutput($debug ? OutputInterface::VERBOSITY_VERY_VERBOSE : OutputInterface::VERBOSITY_NORMAL);
$logger = new SymfonyConsole\Logger\ConsoleLogger($output);

// Setup *Monolog* logger, logger must output JSON to stream to STDERR
// **WARNING** this will work only on the development stage with @modelcontextprotocol/inspector, but not in a real client
Copy link
Preview

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment uses Markdown formatting (Monolog and WARNING) which is not appropriate for PHP code comments. Consider using plain text formatting for better readability in the source code.

Suggested change
// **WARNING** this will work only on the development stage with @modelcontextprotocol/inspector, but not in a real client
// Setup Monolog logger, logger must output JSON to stream to STDERR
// WARNING: this will work only on the development stage with @modelcontextprotocol/inspector, but not in a real client

Copilot uses AI. Check for mistakes.

// For real clients please consider using other types of logs (file, client logging, etc.)
$handler = new StreamHandler('php://stderr');
$handler->setFormatter(new JsonFormatter());
$logger = new Logger('mcp', [$handler]);

// Configure the JsonRpcHandler and build the functionality
$jsonRpcHandler = new Symfony\AI\McpSdk\Server\JsonRpcHandler(
Expand Down