[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

@ -231,7 +231,7 @@ class GetPromptHandlerTest extends TestCase
public function testHandlePromptNotFoundExceptionReturnsError(): void
{
$request = $this->createGetPromptRequest('nonexistent_prompt');
$exception = new PromptNotFoundException($request);
$exception = new PromptNotFoundException('nonexistent_prompt');
$this->referenceProvider
->expects($this->once())
@ -243,14 +243,14 @@ class GetPromptHandlerTest extends TestCase
$this->assertInstanceOf(Error::class, $response);
$this->assertEquals($request->getId(), $response->id);
$this->assertEquals(Error::METHOD_NOT_FOUND, $response->code);
$this->assertEquals('Prompt not found for name: "nonexistent_prompt".', $response->message);
$this->assertEquals(Error::RESOURCE_NOT_FOUND, $response->code);
$this->assertEquals('Prompt not found: "nonexistent_prompt".', $response->message);
}
public function testHandlePromptGetExceptionReturnsError(): void
{
$request = $this->createGetPromptRequest('failing_prompt');
$exception = new PromptGetException($request, new \RuntimeException('Failed to get prompt'));
$exception = new PromptGetException('Failed to get prompt');
$this->referenceProvider
->expects($this->once())
@ -263,7 +263,7 @@ class GetPromptHandlerTest extends TestCase
$this->assertInstanceOf(Error::class, $response);
$this->assertEquals($request->getId(), $response->id);
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
$this->assertEquals('Error while handling prompt: Handling prompt "failing_prompt" failed with error: "Failed to get prompt".', $response->message);
$this->assertEquals('Failed to get prompt', $response->message);
}
public function testHandlePromptGetWithComplexArguments(): void