[Server] Fix: ensure JSON-RPC error responses include message IDs (#77)

* fix: ensure JSON-RPC error responses include message IDs

* fix: replace method_exists with instanceof for message ID extraction
This commit is contained in:
Kyrian Obikwelu 2025-09-24 20:45:50 +01:00 • committed by GitHub
commit 32b8b2ea61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 35 deletions

View file

@ -23,6 +23,7 @@ use Mcp\Exception\NotFoundExceptionInterface;
use Mcp\Schema\Implementation;
use Mcp\Schema\JsonRpc\Error;
use Mcp\Schema\JsonRpc\HasMethodInterface;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Schema\JsonRpc\Response;
use Mcp\Schema\Request\InitializeRequest;
use Mcp\Server\MethodHandlerInterface;
@ -94,9 +95,6 @@ class Handler
/**
* @return iterable<array{string|null, array<string, mixed>}>
*
* @throws ExceptionInterface When a handler throws an exception during message processing
* @throws \JsonException When JSON encoding of the response fails
*/
public function process(string $input, ?Uuid $sessionId): iterable
{
@ -163,7 +161,7 @@ class Handler
foreach ($messages as $message) {
if ($message instanceof InvalidInputMessageException) {
$this->logger->warning('Failed to create message.', ['exception' => $message]);
$error = Error::forInvalidRequest($message->getMessage(), 0);
$error = Error::forInvalidRequest($message->getMessage());
yield [$this->encodeResponse($error), []];
continue;
}
@ -172,6 +170,8 @@ class Handler
'method' => $message->getMethod(),
]);
$messageId = $message instanceof Request ? $message->getId() : 0;
try {
$response = $this->handle($message, $session);
yield [$this->encodeResponse($response), ['session_id' => $session->getId()]];
@ -183,17 +183,17 @@ class Handler
['exception' => $e],
);
$error = Error::forMethodNotFound($e->getMessage());
$error = Error::forMethodNotFound($e->getMessage(), $messageId);
yield [$this->encodeResponse($error), []];
} catch (\InvalidArgumentException $e) {
$this->logger->warning(\sprintf('Invalid argument: %s', $e->getMessage()), ['exception' => $e]);
$error = Error::forInvalidParams($e->getMessage());
$error = Error::forInvalidParams($e->getMessage(), $messageId);
yield [$this->encodeResponse($error), []];
} catch (\Throwable $e) {
$this->logger->critical(\sprintf('Uncaught exception: %s', $e->getMessage()), ['exception' => $e]);
$error = Error::forInternalError($e->getMessage());
$error = Error::forInternalError($e->getMessage(), $messageId);
yield [$this->encodeResponse($error), []];
}
}
@ -202,7 +202,7 @@ class Handler
}
/**
* @throws \JsonException When JSON encoding fails
* Encodes a response to JSON, handling encoding errors gracefully.
*/
private function encodeResponse(Response|Error|null $response): ?string
{
@ -214,11 +214,26 @@ class Handler
$this->logger->info('Encoding response.', ['response' => $response]);
if ($response instanceof Response && [] === $response->result) {
return json_encode($response, \JSON_THROW_ON_ERROR | \JSON_FORCE_OBJECT);
}
try {
if ($response instanceof Response && [] === $response->result) {
return json_encode($response, \JSON_THROW_ON_ERROR | \JSON_FORCE_OBJECT);
}
return json_encode($response, \JSON_THROW_ON_ERROR);
return json_encode($response, \JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
$this->logger->error('Failed to encode response to JSON.', [
'message_id' => $response->getId(),
'exception' => $e,
]);
$fallbackError = new Error(
id: $response->getId(),
code: Error::INTERNAL_ERROR,
message: 'Response could not be encoded to JSON'
);
return json_encode($fallbackError, \JSON_THROW_ON_ERROR);
}
}
/**

View file

@ -44,19 +44,12 @@ final class Server
]);
$transport->onMessage(function (string $message, ?Uuid $sessionId) use ($transport) {
try {
foreach ($this->jsonRpcHandler->process($message, $sessionId) as [$response, $context]) {
if (null === $response) {
continue;
}
$transport->send($response, $context);
foreach ($this->jsonRpcHandler->process($message, $sessionId) as [$response, $context]) {
if (null === $response) {
continue;
}
} catch (\JsonException $e) {
$this->logger->error('Failed to encode response to JSON.', [
'message' => $message,
'exception' => $e,
]);
$transport->send($response, $context);
}
});