[Server] Log JSON-RPC payloads at debug level only - #525
Open
aton-of-data wants to merge 1 commit into
Open
aton-of-data wants to merge 1 commit into
aton-of-data wants to merge 1 commit into
Conversation
Protocol logged the raw input and the full request, client response and notification objects as context at info level, so a server logging at INFO wrote every tool argument and every elicitation/sampling reply to its log. Info records now carry only the method and id; the raw message is logged once at debug level. Fixes modelcontextprotocol#524
aton-of-data
requested review from
CodeWithKyrian,
Nyholm,
chr-hertel and
soyuka
as code owners
October 2, 2026 00:50
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #524
Problem. At
3175614,src/Server/Protocol.phpputs whole JSON-RPC payloads into the log context at info level::181info('Received message to process.', ['message' => $input]), the raw wire input:260info('Handling request.', ['request' => $request]):364info('Handling response from client.', ['response' => $response]), which carries sampling and elicitation replies:384info('Handling notification.', ['notification' => $notification])docs/run/server-builder.md:241and:271set up a MonologStreamHandleratLogger::INFO. With that setup every tool argument, and every elicitation or sampling reply, is written to disk twice per message.The rest of the SDK already keeps payloads at debug:
CallToolHandler.php:69logs tool arguments at debug and:155tool failures, the client logs its raw input at debug (src/Client/Protocol.php:576), andStatelessProtocollogs no payloads at all.Protocolwas the one place that did not follow that split.Change. The four info records now carry identifiers only:
methodandrequest_idfor requests,message_idfor client responses,methodfor notifications.Received message to process.stays at info with no context, and a new debug record,Received message payload., carries the raw input under the samemessagekey. That raw input holds every request, response and notification in a batch, so debug output still has the full payload.Reproduction. The documented configuration (Monolog
StreamHandler('php://stdout', Logger::INFO)),Server::builder()->setLogger($logger)->addTool(fn (string $username, string $password) => 'ok', 'login'), overInMemoryTransport, sending initialize,notifications/initialized, thentools/callwith{"username":"alice","password":"hunter2"}.Before, excerpt:
After:
hunter2appears twice in the INFO log before and not at all after. With the handler at DEBUG it still appears, inReceived message payload.and inCallToolHandler'sExecuting tool.Test.
ProtocolTest::testMessagePayloadsAreOnlyLoggedAtDebugLevelruns three cases (atools/callrequest, a client response to an elicitation, anotifications/cancellednotification) and asserts for each that no record at info or above contains the payload, that the method or id is still logged at info, and that the payload is present at debug. It fails 3 of 3 on3175614and passes 3 of 3 with the change. No existing test was changed.Verified on PHP 8.3.6, PHPUnit 10.5.65:
phpunit --testsuite=unitphpstanphp-cs-fixer fix --dry-run --diff(no cache)Not verified.
DualEraElicitationTesthangs in my environment on the base commit as well, so it was skipped. In the inspector suite the 44 HTTP tests error on base and patched alike (fetch failed ... invalid onRequestStart methodfrom the npx inspector); the stdio inspector tests pass. The conformance tests need Docker and were not run.Left alone, same family, failure paths only.
src/Capability/Discovery/SchemaValidator.php:89-92logs the tool arguments at error, but only when the validator itself throws;Protocol.php:562-566logs a client reply at error when it cannot be reconstructed from the session. These are diagnostics at warning or error, not per-message info logging, so I kept them out of this change. The first is worth a follow-up if you agree.AI disclosure: Claude (Anthropic) was used to diagnose this, write the change and the test, and run the verification. Every result quoted here was executed.