[Server] Fix: standardize error handling across handlers with MCP specificatio… (#124)

* fix: standardize error handling across handlers with MCP specification compliance

* Update mcp-elements.md

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>

* docs: clarify error handling behavior for all element types

* docs: remove unnecessary whitespace in mcp-elements.md

* fix: simplify exception signatures to accept simple messages

* refactor: use specific exceptions in examples for better error handling

* feat: throw exceptions from reference provider instead of returning null

---------

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
This commit is contained in:
Kyrian Obikwelu 2025-10-30 17:30:58 +01:00 • committed by GitHub
commit ca18caf29d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -178,7 +178,7 @@ class ReadResourceHandlerTest extends TestCase
{
$uri = 'file://nonexistent/file.txt';
$request = $this->createReadResourceRequest($uri);
$exception = new ResourceNotFoundException($request);
$exception = new ResourceNotFoundException($uri);
$this->referenceProvider
->expects($this->once())
@ -194,14 +194,31 @@ class ReadResourceHandlerTest extends TestCase
$this->assertEquals('Resource not found for uri: "'.$uri.'".', $response->message);
}
public function testHandleResourceReadExceptionReturnsGenericError(): void
public function testHandleResourceReadExceptionReturnsActualErrorMessage(): void
{
$uri = 'file://corrupted/file.txt';
$request = $this->createReadResourceRequest($uri);
$exception = new ResourceReadException(
$request,
new \RuntimeException('Failed to read resource: corrupted data'),
);
$exception = new ResourceReadException('Failed to read resource: corrupted data');
$this->referenceProvider
->expects($this->once())
->method('getResource')
->with($uri)
->willThrowException($exception);
$response = $this->handler->handle($request, $this->session);
$this->assertInstanceOf(Error::class, $response);
$this->assertEquals($request->getId(), $response->id);
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
$this->assertEquals('Failed to read resource: corrupted data', $response->message);
}
public function testHandleGenericExceptionReturnsGenericError(): void
{
$uri = 'file://problematic/file.txt';
$request = $this->createReadResourceRequest($uri);
$exception = new \RuntimeException('Internal database connection failed');
$this->referenceProvider
->expects($this->once())
@ -382,7 +399,7 @@ class ReadResourceHandlerTest extends TestCase
{
$uri = 'file://custom/missing.txt';
$request = $this->createReadResourceRequest($uri);
$exception = new ResourceNotFoundException($request);
$exception = new ResourceNotFoundException($uri);
$this->referenceProvider
->expects($this->once())