Skip to content

Commit d2de1a4

Browse files
committed
Catch more exception
1 parent 886307f commit d2de1a4

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/mcp-sdk/src/Message/Error.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
public const INVALID_PARAMS = -32602;
1919
public const INTERNAL_ERROR = -32603;
2020
public const PARSE_ERROR = -32700;
21+
public const RESOURCE_NOT_FOUND = -32002;
2122

2223
public function __construct(
2324
public string|int $id,

src/mcp-sdk/src/Server/JsonRpcHandler.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Log\LoggerInterface;
1515
use Symfony\AI\McpSdk\Exception\HandlerNotFoundException;
1616
use Symfony\AI\McpSdk\Exception\NotFoundExceptionInterface;
17+
use Symfony\AI\McpSdk\Exception\ResourceNotFoundException;
1718
use Symfony\AI\McpSdk\Message\Error;
1819
use Symfony\AI\McpSdk\Message\Factory;
1920
use Symfony\AI\McpSdk\Message\Notification;
@@ -77,6 +78,10 @@ public function process(string $message): ?string
7778
: $this->encodeResponse($this->handleRequest($message));
7879
} catch (\DomainException) {
7980
return null;
81+
} catch (ResourceNotFoundException $e) {
82+
$this->logger->warning(\sprintf('Failed to find resource: %s', $e->getMessage()), ['exception' => $e]);
83+
84+
return $this->encodeResponse(new Error($message->id ?? 0, Error::RESOURCE_NOT_FOUND, $e->getMessage()));
8085
} catch (NotFoundExceptionInterface $e) {
8186
$this->logger->warning(\sprintf('Failed to create response: %s', $e->getMessage()), ['exception' => $e]);
8287

@@ -85,6 +90,10 @@ public function process(string $message): ?string
8590
$this->logger->warning(\sprintf('Invalid argument: %s', $e->getMessage()), ['exception' => $e]);
8691

8792
return $this->encodeResponse(Error::invalidParams($message->id ?? 0, $e->getMessage()));
93+
} catch (\Throwable $e) {
94+
$this->logger->critical(\sprintf('Uncaught exception: %s', $e->getMessage()), ['exception' => $e]);
95+
96+
return $this->encodeResponse(Error::internalError($message->id ?? 0, $e->getMessage()));
8897
}
8998
}
9099

0 commit comments

Comments
 (0)