Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
7 changes: 7 additions & 0 deletions examples/server/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@ public function log($level, Stringable|string $message, array $context = []): vo
return;
}

$exception = $context['exception'] ?? null;
unset($context['exception']);

$logMessage = sprintf(
"[%s] %s %s\n",
strtoupper($level),
$message,
([] === $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 {
Expand Down
14 changes: 7 additions & 7 deletions src/Capability/Discovery/Discoverer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function discover(string $basePath, array $directories, array $excludeDir
}
} 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(),
]);
}
Expand Down Expand Up @@ -183,12 +183,12 @@ private function processFile(SplFileInfo $file, array &$discoveredCount, array &
}
}
} 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(),
]);
}
Expand Down Expand Up @@ -287,9 +287,9 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun
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()]);
}
}

Expand Down Expand Up @@ -338,7 +338,7 @@ private function getClassFromFile(SplFileInfo $file): ?string
$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;
Expand All @@ -354,7 +354,7 @@ private function getClassFromFile(SplFileInfo $file): ?string
$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;
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Handler/Request/CallToolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function handle(Request $request, SessionInterface $session): Response|Er
} 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());
Expand Down