Skip to content

Commit 784ae42

Browse files
committed
Use the throwable object in $context['excpetion']
1 parent df2563f commit 784ae42

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

examples/server/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function log($level, Stringable|string $message, array $context = []): vo
7070
([] === $context || !$debug) ? '' : json_encode($context),
7171
);
7272

73+
if (isset($context['exception']) && $context['exception'] instanceof Throwable) {
74+
$logMessage .= sprintf('> %s', $context['exception']->getMessage())."\n";
75+
}
76+
7377
if (($_SERVER['FILE_LOG'] ?? false) || !defined('STDERR')) {
7478
file_put_contents('dev.log', $logMessage, \FILE_APPEND);
7579
} else {

src/Capability/Discovery/Discoverer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function discover(string $basePath, array $directories, array $excludeDir
106106
}
107107
} catch (\Throwable $e) {
108108
$this->logger->error('Error during file finding process for MCP discovery'.json_encode($e->getTrace(), \JSON_PRETTY_PRINT), [
109-
'exception' => $e->getMessage(),
109+
'exception' => $e,
110110
'trace' => $e->getTraceAsString(),
111111
]);
112112
}
@@ -183,12 +183,12 @@ private function processFile(SplFileInfo $file, array &$discoveredCount, array &
183183
}
184184
}
185185
} catch (\ReflectionException $e) {
186-
$this->logger->error('Reflection error processing file for MCP discovery', ['file' => $file->getPathname(), 'class' => $className, 'exception' => $e->getMessage()]);
186+
$this->logger->error('Reflection error processing file for MCP discovery', ['file' => $file->getPathname(), 'class' => $className, 'exception' => $e]);
187187
} catch (\Throwable $e) {
188188
$this->logger->error('Unexpected error processing file for MCP discovery', [
189189
'file' => $file->getPathname(),
190190
'class' => $className,
191-
'exception' => $e->getMessage(),
191+
'exception' => $e,
192192
'trace' => $e->getTraceAsString(),
193193
]);
194194
}
@@ -287,9 +287,9 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun
287287
break;
288288
}
289289
} catch (ExceptionInterface $e) {
290-
$this->logger->error("Failed to process MCP attribute on {$className}::{$methodName}", ['attribute' => $attributeClassName, 'exception' => $e->getMessage(), 'trace' => $e->getPrevious() ? $e->getPrevious()->getTraceAsString() : $e->getTraceAsString()]);
290+
$this->logger->error("Failed to process MCP attribute on {$className}::{$methodName}", ['attribute' => $attributeClassName, 'exception' => $e, 'trace' => $e->getPrevious() ? $e->getPrevious()->getTraceAsString() : $e->getTraceAsString()]);
291291
} catch (\Throwable $e) {
292-
$this->logger->error("Unexpected error processing attribute on {$className}::{$methodName}", ['attribute' => $attributeClassName, 'exception' => $e->getMessage(), 'trace' => $e->getTraceAsString()]);
292+
$this->logger->error("Unexpected error processing attribute on {$className}::{$methodName}", ['attribute' => $attributeClassName, 'exception' => $e, 'trace' => $e->getTraceAsString()]);
293293
}
294294
}
295295

@@ -338,7 +338,7 @@ private function getClassFromFile(SplFileInfo $file): ?string
338338
$content = $file->getContents();
339339
} catch (\Throwable $e) {
340340
$this->logger->warning("Failed to read file content during class discovery: {$file->getPathname()}", [
341-
'exception' => $e->getMessage(),
341+
'exception' => $e,
342342
]);
343343

344344
return null;
@@ -354,7 +354,7 @@ private function getClassFromFile(SplFileInfo $file): ?string
354354
$tokens = token_get_all($content);
355355
} catch (\Throwable $e) {
356356
$this->logger->warning("Failed to tokenize file during class discovery: {$file->getPathname()}", [
357-
'exception' => $e->getMessage(),
357+
'exception' => $e,
358358
]);
359359

360360
return null;

src/Server/Handler/Request/CallToolHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function handle(Request $request, SessionInterface $session): Response|Er
9191
} catch (\Throwable $e) {
9292
$this->logger->error('Unhandled error during tool execution', [
9393
'name' => $toolName,
94-
'exception' => $e->getMessage(),
94+
'exception' => $e,
9595
]);
9696

9797
return Error::forInternalError('Error while executing tool', $request->getId());

0 commit comments

Comments
 (0)