[Server] Allow overriding the default file name pattern for Discovery (#292)
* Add optional namePatterns capability to discovery so that the default scan for `*.php` files can be overridden. * add default to namePatterns docs * add an error check if we manage to pass in an empty list of patterns * add unit tests (and a static entry to classmap for composer.json so it autoloads from a non .php file for the test) * run php-cs-fixer * change how default namePatterns are set and make sure all the annotations are correct * Fix bad whitespace Co-authored-by: Christopher Hertel <mail@christopher-hertel.de> * switch to a const default instead of nullable with a check in the function to convert null to a default * make sure we handle empty arrays * formatting fixes * Apply suggestion from @chr-hertel Co-authored-by: Christopher Hertel <mail@christopher-hertel.de> * Add changelog entry --------- Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
This commit is contained in:
parent
fb2c8c2ee4
commit
86bd845924
2 changed files with 109 additions and 37 deletions
|
|
@ -16,6 +16,7 @@ use Mcp\Capability\Completion\ListCompletionProvider;
|
|||
use Mcp\Capability\Discovery\Discoverer;
|
||||
use Mcp\Capability\Registry\ToolReference;
|
||||
use Mcp\Tests\Unit\Capability\Attribute\CompletionProviderFixture;
|
||||
use Mcp\Tests\Unit\Capability\Discovery\Fixtures\AlternativeFileNameToolHandler;
|
||||
use Mcp\Tests\Unit\Capability\Discovery\Fixtures\DiscoverableToolHandler;
|
||||
use Mcp\Tests\Unit\Capability\Discovery\Fixtures\InvocablePromptFixture;
|
||||
use Mcp\Tests\Unit\Capability\Discovery\Fixtures\InvocableResourceFixture;
|
||||
|
|
@ -34,10 +35,10 @@ class DiscoveryTest extends TestCase
|
|||
|
||||
public function testDiscoversAllElementTypesCorrectlyFromFixtureFiles(): void
|
||||
{
|
||||
$discovery = $this->discoverer->discover(__DIR__, ['Fixtures']);
|
||||
$discovery = $this->discoverer->discover(__DIR__, ['Fixtures'], [], ['*.php', '*.inc']);
|
||||
|
||||
$tools = $discovery->getTools();
|
||||
$this->assertCount(4, $tools);
|
||||
$this->assertCount(5, $tools);
|
||||
|
||||
$this->assertArrayHasKey('greet_user', $tools);
|
||||
$this->assertFalse($tools['greet_user']->isManual);
|
||||
|
|
@ -56,6 +57,9 @@ class DiscoveryTest extends TestCase
|
|||
$this->assertFalse($tools['InvokableCalculator']->isManual);
|
||||
$this->assertEquals([InvocableToolFixture::class, '__invoke'], $tools['InvokableCalculator']->handler);
|
||||
|
||||
$this->assertArrayHasKey('inc_file_name_tool', $tools);
|
||||
$this->assertEquals([AlternativeFileNameToolHandler::class, 'run'], $tools['inc_file_name_tool']->handler);
|
||||
|
||||
$this->assertArrayNotHasKey('private_tool_should_be_ignored', $tools);
|
||||
$this->assertArrayNotHasKey('protected_tool_should_be_ignored', $tools);
|
||||
$this->assertArrayNotHasKey('static_tool_should_be_ignored', $tools);
|
||||
|
|
@ -121,6 +125,25 @@ class DiscoveryTest extends TestCase
|
|||
$this->assertTrue($discovery->isEmpty());
|
||||
}
|
||||
|
||||
public function testHandlesDefaultAndOverriddenFileNamePatterns(): void
|
||||
{
|
||||
$discovery = $this->discoverer->discover(__DIR__, ['Fixtures']);
|
||||
$this->assertArrayHasKey('greet_user', $discovery->getTools());
|
||||
$this->assertArrayNotHasKey('inc_file_name_tool', $discovery->getTools());
|
||||
|
||||
$discovery = $this->discoverer->discover(__DIR__, ['Fixtures'], [], []);
|
||||
$this->assertArrayHasKey('greet_user', $discovery->getTools());
|
||||
$this->assertArrayNotHasKey('inc_file_name_tool', $discovery->getTools());
|
||||
|
||||
$discovery = $this->discoverer->discover(__DIR__, ['Fixtures'], [], ['*.php', '*.inc']);
|
||||
$this->assertArrayHasKey('greet_user', $discovery->getTools());
|
||||
$this->assertArrayHasKey('inc_file_name_tool', $discovery->getTools());
|
||||
|
||||
$discovery = $this->discoverer->discover(__DIR__, ['Fixtures'], [], ['*.inc']);
|
||||
$this->assertArrayNotHasKey('greet_user', $discovery->getTools());
|
||||
$this->assertArrayHasKey('inc_file_name_tool', $discovery->getTools());
|
||||
}
|
||||
|
||||
public function testCorrectlyInfersNamesAndDescriptionsFromMethodsOrClassesIfNotSetInAttribute(): void
|
||||
{
|
||||
$discovery = $this->discoverer->discover(__DIR__, ['Fixtures']);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
<?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\Discovery\Fixtures;
|
||||
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
class AlternativeFileNameToolHandler
|
||||
{
|
||||
#[McpTool(name: 'inc_file_name_tool')]
|
||||
public function run(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue