Add tool name validation for SEP-986 (#140)

This commit is contained in:
Christopher Hertel 2025-11-13 21:42:30 +01:00 • committed by GitHub
commit 7a8fbe9b20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 96 additions and 9 deletions

View file

@ -140,7 +140,7 @@ class RegistryTest extends TestCase
$this->logger
->expects($this->once())
->method('debug')
->with("Ignoring discovered tool 'test_tool' as it conflicts with a manually registered one.");
->with('Ignoring discovered tool "test_tool" as it conflicts with a manually registered one.');
$this->registry->registerTool($discoveredTool, fn () => 'discovered', false);
@ -181,7 +181,7 @@ class RegistryTest extends TestCase
$this->logger
->expects($this->once())
->method('debug')
->with("Ignoring discovered resource 'test://resource' as it conflicts with a manually registered one.");
->with('Ignoring discovered resource "test://resource" as it conflicts with a manually registered one.');
$this->registry->registerResource($discoveredResource, fn () => 'discovered', false);
@ -210,7 +210,7 @@ class RegistryTest extends TestCase
$this->logger
->expects($this->once())
->method('debug')
->with("Ignoring discovered template 'test://{id}' as it conflicts with a manually registered one.");
->with('Ignoring discovered template "test://{id}" as it conflicts with a manually registered one.');
$this->registry->registerResourceTemplate($discoveredTemplate, fn () => 'discovered', [], false);
@ -239,7 +239,7 @@ class RegistryTest extends TestCase
$this->logger
->expects($this->once())
->method('debug')
->with("Ignoring discovered prompt 'test_prompt' as it conflicts with a manually registered one.");
->with('Ignoring discovered prompt "test_prompt" as it conflicts with a manually registered one.');
$this->registry->registerPrompt($discoveredPrompt, fn () => 'discovered', [], false);

View file

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Capability\Tool;
use Mcp\Capability\Tool\NameValidator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
final class NameValidatorTest extends TestCase
{
#[DataProvider('provideValidNames')]
public function testValidNames(string $name): void
{
$this->assertTrue((new NameValidator())->isValid($name));
}
public static function provideValidNames(): array
{
return [
['my_tool'],
['MyTool123'],
['my.tool'],
['my-tool'],
['my/tool'],
['my_tool-01.02'],
['my_long_toolname_that_is_exactly_sixty_four_characters_long_1234'],
];
}
#[DataProvider('provideInvalidNames')]
public function testInvalidNames(string $name): void
{
$this->assertFalse((new NameValidator())->isValid($name));
}
public static function provideInvalidNames(): array
{
return [
[''],
['my tool'],
['my@tool'],
['my!tool'],
['my_tool#1'],
['this_tool_name_is_way_too_long_because_it_exceeds_the_sixty_four_character_limit_set_by_the_validator'],
];
}
}