Use the throwable object in $context['exception'] (#207)

* Use the throwable object in $context['excpetion']

* add changelog

* remove 'exception' from context

* revert debug
This commit is contained in:
Tobias Nyholm 2025-12-27 23:46:35 +01:00 • committed by GitHub
commit 7f02285753
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 8 deletions

View file

@ -6,6 +6,7 @@ All notable changes to `mcp/sdk` will be documented in this file.
-----
* Throw exception when trying to inject parameter with the unsupported names `$_session` or `$_request`.
* `Throwable` objects are passed to log context instead of the exception message.
0.2.1
-----

View file

@ -63,6 +63,9 @@ function logger(): LoggerInterface
return;
}
$exception = $context['exception'] ?? null;
unset($context['exception']);
$logMessage = sprintf(
"[%s] %s %s\n",
strtoupper($level),
@ -70,6 +73,10 @@ function logger(): LoggerInterface
([] === $context || !$debug) ? '' : json_encode($context),
);
if ($exception instanceof Throwable) {
$logMessage .= sprintf('> %s', $exception->getMessage())."\n";
}
if (($_SERVER['FILE_LOG'] ?? false) || !defined('STDERR')) {
file_put_contents('dev.log', $logMessage, \FILE_APPEND);
} else {

View file

@ -106,7 +106,7 @@ class Discoverer
}
} catch (\Throwable $e) {
$this->logger->error('Error during file finding process for MCP discovery'.json_encode($e->getTrace(), \JSON_PRETTY_PRINT), [
'exception' => $e->getMessage(),
'exception' => $e,
'trace' => $e->getTraceAsString(),
]);
}
@ -183,12 +183,12 @@ class Discoverer
}
}
} catch (\ReflectionException $e) {
$this->logger->error('Reflection error processing file for MCP discovery', ['file' => $file->getPathname(), 'class' => $className, 'exception' => $e->getMessage()]);
$this->logger->error('Reflection error processing file for MCP discovery', ['file' => $file->getPathname(), 'class' => $className, 'exception' => $e]);
} catch (\Throwable $e) {
$this->logger->error('Unexpected error processing file for MCP discovery', [
'file' => $file->getPathname(),
'class' => $className,
'exception' => $e->getMessage(),
'exception' => $e,
'trace' => $e->getTraceAsString(),
]);
}
@ -287,9 +287,9 @@ class Discoverer
break;
}
} catch (ExceptionInterface $e) {
$this->logger->error("Failed to process MCP attribute on {$className}::{$methodName}", ['attribute' => $attributeClassName, 'exception' => $e->getMessage(), 'trace' => $e->getPrevious() ? $e->getPrevious()->getTraceAsString() : $e->getTraceAsString()]);
$this->logger->error("Failed to process MCP attribute on {$className}::{$methodName}", ['attribute' => $attributeClassName, 'exception' => $e, 'trace' => $e->getPrevious() ? $e->getPrevious()->getTraceAsString() : $e->getTraceAsString()]);
} catch (\Throwable $e) {
$this->logger->error("Unexpected error processing attribute on {$className}::{$methodName}", ['attribute' => $attributeClassName, 'exception' => $e->getMessage(), 'trace' => $e->getTraceAsString()]);
$this->logger->error("Unexpected error processing attribute on {$className}::{$methodName}", ['attribute' => $attributeClassName, 'exception' => $e, 'trace' => $e->getTraceAsString()]);
}
}
@ -338,7 +338,7 @@ class Discoverer
$content = $file->getContents();
} catch (\Throwable $e) {
$this->logger->warning("Failed to read file content during class discovery: {$file->getPathname()}", [
'exception' => $e->getMessage(),
'exception' => $e,
]);
return null;
@ -354,7 +354,7 @@ class Discoverer
$tokens = token_get_all($content);
} catch (\Throwable $e) {
$this->logger->warning("Failed to tokenize file during class discovery: {$file->getPathname()}", [
'exception' => $e->getMessage(),
'exception' => $e,
]);
return null;

View file

@ -91,7 +91,7 @@ final class CallToolHandler implements RequestHandlerInterface
} catch (\Throwable $e) {
$this->logger->error('Unhandled error during tool execution', [
'name' => $toolName,
'exception' => $e->getMessage(),
'exception' => $e,
]);
return Error::forInternalError('Error while executing tool', $request->getId());