Adopt server examples

This commit is contained in:
Christopher Hertel 2025-08-17 02:46:01 +02:00
commit 2ef051a355
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3
5 changed files with 3639 additions and 756 deletions

View file

@ -0,0 +1,68 @@
<?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\Example;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
abstract class InspectorSnapshotTestCase extends TestCase
{
#[DataProvider('provideMethods')]
public function testResourcesListOutputMatchesSnapshot(string $method): void
{
$process = Process::fromShellCommandline(
\sprintf('npx @modelcontextprotocol/inspector --cli php %s --method %s', $this->getServerScript(), $method)
)->mustRun();
$output = $process->getOutput();
$snapshotFile = $this->getSnapshotFilePath($method);
if (!file_exists($snapshotFile)) {
file_put_contents($snapshotFile, $output.\PHP_EOL);
$this->markTestIncomplete("Snapshot created at $snapshotFile, please re-run tests.");
}
$expected = file_get_contents($snapshotFile);
$this->assertJsonStringEqualsJsonString($expected, $output);
}
/**
* List of methods to test.
*
* @return array<string, array{method: string}>
*/
abstract public static function provideMethods(): array;
abstract protected function getServerScript(): string;
/**
* @return array<string, array{method: string}>
*/
protected static function provideListMethods(): array
{
return [
'Prompt Listing' => ['method' => 'prompts/list'],
'Resource Listing' => ['method' => 'resources/list'],
// 'Resource Template Listing' => ['method' => 'resources/templates/list'],
'Tool Listing' => ['method' => 'tools/list'],
];
}
private function getSnapshotFilePath(string $method): string
{
$className = substr(static::class, strrpos(static::class, '\\') + 1);
return __DIR__.'/snapshots/'.$className.'-'.str_replace('/', '_', $method).'.json';
}
}

View file

@ -0,0 +1,30 @@
<?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\Example;
use PHPUnit\Framework\Attributes\Group;
#[Group('inspector')]
final class StdioCalculatorExampleTest extends InspectorSnapshotTestCase
{
public static function provideMethods(): array
{
return [
...parent::provideListMethods(),
];
}
protected function getServerScript(): string
{
return \dirname(__DIR__, 2).'/examples/01-discovery-stdio-calculator/server.php';
}
}

View file

@ -0,0 +1,4 @@
{
"prompts": []
}

View file

@ -0,0 +1,11 @@
{
"resources": [
{
"name": "calculator_config",
"uri": "config://calculator/settings",
"description": "Current settings for the calculator tool (precision, allow_negative).",
"mimeType": "application/json"
}
]
}

View file

@ -0,0 +1,51 @@
{
"tools": [
{
"name": "calculate",
"description": "Performs a calculation based on the operation.",
"inputSchema": {
"type": "object",
"properties": {
"a": {
"type": "number",
"description": "the first operand"
},
"b": {
"type": "number",
"description": "the second operand"
},
"operation": {
"type": "string",
"description": "the operation ('add', 'subtract', 'multiply', 'divide')"
}
},
"required": [
"a",
"b",
"operation"
]
}
},
{
"name": "update_setting",
"description": "Updates a specific configuration setting.",
"inputSchema": {
"type": "object",
"properties": {
"setting": {
"type": "string",
"description": "the setting key ('precision' or 'allow_negative')"
},
"value": {
"description": "the new value (int for precision, bool for allow_negative)"
}
},
"required": [
"setting",
"value"
]
}
}
]
}