[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:
parent
8885d29a4e
commit
ca18caf29d
2 changed files with 278 additions and 185 deletions
|
|
@ -16,6 +16,9 @@ use Mcp\Capability\Registry\PromptReference;
|
|||
use Mcp\Capability\Registry\ResourceReference;
|
||||
use Mcp\Capability\Registry\ResourceTemplateReference;
|
||||
use Mcp\Capability\Registry\ToolReference;
|
||||
use Mcp\Exception\PromptNotFoundException;
|
||||
use Mcp\Exception\ResourceNotFoundException;
|
||||
use Mcp\Exception\ToolNotFoundException;
|
||||
use Mcp\Schema\Prompt;
|
||||
use Mcp\Schema\Resource;
|
||||
use Mcp\Schema\ResourceTemplate;
|
||||
|
|
@ -45,10 +48,12 @@ class RegistryProviderTest extends TestCase
|
|||
$this->assertFalse($toolRef->isManual);
|
||||
}
|
||||
|
||||
public function testGetToolReturnsNullForUnregisteredTool(): void
|
||||
public function testGetToolThrowsExceptionForUnregisteredTool(): void
|
||||
{
|
||||
$toolRef = $this->registry->getTool('non_existent_tool');
|
||||
$this->assertNull($toolRef);
|
||||
$this->expectException(ToolNotFoundException::class);
|
||||
$this->expectExceptionMessage('Tool not found: "non_existent_tool".');
|
||||
|
||||
$this->registry->getTool('non_existent_tool');
|
||||
}
|
||||
|
||||
public function testGetResourceReturnsRegisteredResource(): void
|
||||
|
|
@ -65,10 +70,12 @@ class RegistryProviderTest extends TestCase
|
|||
$this->assertFalse($resourceRef->isManual);
|
||||
}
|
||||
|
||||
public function testGetResourceReturnsNullForUnregisteredResource(): void
|
||||
public function testGetResourceThrowsExceptionForUnregisteredResource(): void
|
||||
{
|
||||
$resourceRef = $this->registry->getResource('test://non_existent');
|
||||
$this->assertNull($resourceRef);
|
||||
$this->expectException(ResourceNotFoundException::class);
|
||||
$this->expectExceptionMessage('Resource not found for uri: "test://non_existent".');
|
||||
|
||||
$this->registry->getResource('test://non_existent');
|
||||
}
|
||||
|
||||
public function testGetResourceMatchesResourceTemplate(): void
|
||||
|
|
@ -84,15 +91,17 @@ class RegistryProviderTest extends TestCase
|
|||
$this->assertEquals($handler, $resourceRef->handler);
|
||||
}
|
||||
|
||||
public function testGetResourceWithIncludeTemplatesFalse(): void
|
||||
public function testGetResourceWithIncludeTemplatesFalseThrowsException(): void
|
||||
{
|
||||
$template = $this->createValidResourceTemplate('test://{id}');
|
||||
$handler = fn (string $id) => "content for {$id}";
|
||||
|
||||
$this->registry->registerResourceTemplate($template, $handler);
|
||||
|
||||
$resourceRef = $this->registry->getResource('test://123', false);
|
||||
$this->assertNull($resourceRef);
|
||||
$this->expectException(ResourceNotFoundException::class);
|
||||
$this->expectExceptionMessage('Resource not found for uri: "test://123".');
|
||||
|
||||
$this->registry->getResource('test://123', false);
|
||||
}
|
||||
|
||||
public function testGetResourcePrefersDirectResourceOverTemplate(): void
|
||||
|
|
@ -125,10 +134,12 @@ class RegistryProviderTest extends TestCase
|
|||
$this->assertFalse($templateRef->isManual);
|
||||
}
|
||||
|
||||
public function testGetResourceTemplateReturnsNullForUnregisteredTemplate(): void
|
||||
public function testGetResourceTemplateThrowsExceptionForUnregisteredTemplate(): void
|
||||
{
|
||||
$templateRef = $this->registry->getResourceTemplate('test://{non_existent}');
|
||||
$this->assertNull($templateRef);
|
||||
$this->expectException(ResourceNotFoundException::class);
|
||||
$this->expectExceptionMessage('Resource not found for uri: "test://{non_existent}".');
|
||||
|
||||
$this->registry->getResourceTemplate('test://{non_existent}');
|
||||
}
|
||||
|
||||
public function testGetPromptReturnsRegisteredPrompt(): void
|
||||
|
|
@ -145,10 +156,12 @@ class RegistryProviderTest extends TestCase
|
|||
$this->assertFalse($promptRef->isManual);
|
||||
}
|
||||
|
||||
public function testGetPromptReturnsNullForUnregisteredPrompt(): void
|
||||
public function testGetPromptThrowsExceptionForUnregisteredPrompt(): void
|
||||
{
|
||||
$promptRef = $this->registry->getPrompt('non_existent_prompt');
|
||||
$this->assertNull($promptRef);
|
||||
$this->expectException(PromptNotFoundException::class);
|
||||
$this->expectExceptionMessage('Prompt not found: "non_existent_prompt".');
|
||||
|
||||
$this->registry->getPrompt('non_existent_prompt');
|
||||
}
|
||||
|
||||
public function testGetToolsReturnsAllRegisteredTools(): void
|
||||
|
|
|
|||
|
|
@ -13,6 +13,9 @@ namespace Mcp\Tests\Unit\Capability\Registry;
|
|||
|
||||
use Mcp\Capability\Completion\EnumCompletionProvider;
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Exception\PromptNotFoundException;
|
||||
use Mcp\Exception\ResourceNotFoundException;
|
||||
use Mcp\Exception\ToolNotFoundException;
|
||||
use Mcp\Schema\Prompt;
|
||||
use Mcp\Schema\Resource;
|
||||
use Mcp\Schema\ResourceTemplate;
|
||||
|
|
@ -264,23 +267,36 @@ class RegistryTest extends TestCase
|
|||
$this->registry->registerResourceTemplate($manualTemplate, fn () => 'manual', [], true);
|
||||
$this->registry->registerResourceTemplate($discoveredTemplate, fn () => 'discovered', [], false);
|
||||
|
||||
$this->logger
|
||||
->expects($this->once())
|
||||
->method('debug')
|
||||
->with('Removed 4 discovered elements from internal registry.');
|
||||
// Test that all elements exist
|
||||
$this->registry->getTool('manual_tool');
|
||||
$this->registry->getResource('test://manual');
|
||||
$this->registry->getPrompt('manual_prompt');
|
||||
$this->registry->getResourceTemplate('manual://{id}');
|
||||
$this->registry->getTool('discovered_tool');
|
||||
$this->registry->getResource('test://discovered');
|
||||
$this->registry->getPrompt('discovered_prompt');
|
||||
$this->registry->getResourceTemplate('discovered://{id}');
|
||||
|
||||
$this->registry->clear();
|
||||
|
||||
$this->assertNotNull($this->registry->getTool('manual_tool'));
|
||||
$this->assertNull($this->registry->getTool('discovered_tool'));
|
||||
$this->assertNotNull($this->registry->getResource('test://manual'));
|
||||
$this->assertNull(
|
||||
$this->registry->getResource('test://discovered', false),
|
||||
); // Don't include templates to avoid debug log
|
||||
$this->assertNotNull($this->registry->getPrompt('manual_prompt'));
|
||||
$this->assertNull($this->registry->getPrompt('discovered_prompt'));
|
||||
$this->assertNotNull($this->registry->getResourceTemplate('manual://{id}'));
|
||||
$this->assertNull($this->registry->getResourceTemplate('discovered://{id}'));
|
||||
// Manual elements should still exist
|
||||
$this->registry->getTool('manual_tool');
|
||||
$this->registry->getResource('test://manual');
|
||||
$this->registry->getPrompt('manual_prompt');
|
||||
$this->registry->getResourceTemplate('manual://{id}');
|
||||
|
||||
// Test that all discovered elements throw exceptions
|
||||
$this->expectException(ToolNotFoundException::class);
|
||||
$this->registry->getTool('discovered_tool');
|
||||
|
||||
$this->expectException(ResourceNotFoundException::class);
|
||||
$this->registry->getResource('test://discovered');
|
||||
|
||||
$this->expectException(PromptNotFoundException::class);
|
||||
$this->registry->getPrompt('discovered_prompt');
|
||||
|
||||
$this->expectException(ResourceNotFoundException::class);
|
||||
$this->registry->getResourceTemplate('discovered://{id}');
|
||||
}
|
||||
|
||||
public function testClearLogsNothingWhenNoDiscoveredElements(): void
|
||||
|
|
@ -294,7 +310,7 @@ class RegistryTest extends TestCase
|
|||
|
||||
$this->registry->clear();
|
||||
|
||||
$this->assertNotNull($this->registry->getTool('manual_tool'));
|
||||
$this->registry->getTool('manual_tool');
|
||||
}
|
||||
|
||||
public function testRegisterToolHandlesStringHandler(): void
|
||||
|
|
|
|||
Loading…
Reference in a new issue