[Server] Feat: Add comprehensive HTTP inspector tests with improved framework (#125)
* feat: add comprehensive HTTP inspector tests and improve test framework * chore: remove debug flag from server configuration
This commit is contained in:
parent
ca18caf29d
commit
757b959bfd
53 changed files with 1877 additions and 189 deletions
93
tests/Inspector/Stdio/StdioCachedDiscoveryTest.php
Normal file
93
tests/Inspector/Stdio/StdioCachedDiscoveryTest.php
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<?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 Inspector;
|
||||
|
||||
use Mcp\Tests\Inspector\Stdio\StdioInspectorSnapshotTestCase;
|
||||
|
||||
final class StdioCachedDiscoveryTest extends StdioInspectorSnapshotTestCase
|
||||
{
|
||||
public static function provideMethods(): array
|
||||
{
|
||||
return [
|
||||
...parent::provideMethods(),
|
||||
'Add Numbers Tool' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'add_numbers',
|
||||
'toolArgs' => ['a' => 5, 'b' => 3],
|
||||
],
|
||||
'testName' => 'add_numbers',
|
||||
],
|
||||
'Add Numbers (Negative)' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'add_numbers',
|
||||
'toolArgs' => ['a' => -10, 'b' => 7],
|
||||
],
|
||||
'testName' => 'add_numbers_negative',
|
||||
],
|
||||
'Multiply Numbers Tool' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'multiply_numbers',
|
||||
'toolArgs' => ['a' => 4, 'b' => 6],
|
||||
],
|
||||
'testName' => 'multiply_numbers',
|
||||
],
|
||||
'Multiply Numbers (Zero)' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'multiply_numbers',
|
||||
'toolArgs' => ['a' => 15, 'b' => 0],
|
||||
],
|
||||
'testName' => 'multiply_numbers_zero',
|
||||
],
|
||||
'Divide Numbers Tool' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'divide_numbers',
|
||||
'toolArgs' => ['a' => 20, 'b' => 4],
|
||||
],
|
||||
'testName' => 'divide_numbers',
|
||||
],
|
||||
'Divide Numbers (Decimal Result)' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'divide_numbers',
|
||||
'toolArgs' => ['a' => 7, 'b' => 2],
|
||||
],
|
||||
'testName' => 'divide_numbers_decimal',
|
||||
],
|
||||
'Power Tool' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'power',
|
||||
'toolArgs' => ['base' => 2, 'exponent' => 8],
|
||||
],
|
||||
'testName' => 'power',
|
||||
],
|
||||
'Power Tool (Zero Exponent)' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'power',
|
||||
'toolArgs' => ['base' => 5, 'exponent' => 0],
|
||||
],
|
||||
'testName' => 'power_zero_exponent',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getServerScript(): string
|
||||
{
|
||||
return \dirname(__DIR__, 3).'/examples/stdio-cached-discovery/server.php';
|
||||
}
|
||||
}
|
||||
77
tests/Inspector/Stdio/StdioCustomDependenciesTest.php
Normal file
77
tests/Inspector/Stdio/StdioCustomDependenciesTest.php
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?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 Inspector;
|
||||
|
||||
use Mcp\Tests\Inspector\Stdio\StdioInspectorSnapshotTestCase;
|
||||
|
||||
final class StdioCustomDependenciesTest extends StdioInspectorSnapshotTestCase
|
||||
{
|
||||
public static function provideMethods(): array
|
||||
{
|
||||
return [
|
||||
...parent::provideMethods(),
|
||||
'Add Task' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'add_task',
|
||||
'toolArgs' => ['userId' => 'alice', 'description' => 'Complete the project documentation'],
|
||||
],
|
||||
'testName' => 'add_task',
|
||||
],
|
||||
'List User Tasks' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'list_user_tasks',
|
||||
'toolArgs' => ['userId' => 'alice'],
|
||||
],
|
||||
'testName' => 'list_user_tasks',
|
||||
],
|
||||
'Complete Task' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'complete_task',
|
||||
'toolArgs' => ['taskId' => 1],
|
||||
],
|
||||
'testName' => 'complete_task',
|
||||
],
|
||||
'Read System Statistics Resource' => [
|
||||
'method' => 'resources/read',
|
||||
'options' => [
|
||||
'uri' => 'stats://system/overview',
|
||||
],
|
||||
'testName' => 'read_system_stats',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getServerScript(): string
|
||||
{
|
||||
return \dirname(__DIR__, 3).'/examples/stdio-custom-dependencies/server.php';
|
||||
}
|
||||
|
||||
protected function normalizeTestOutput(string $output, ?string $testName = null): string
|
||||
{
|
||||
return match ($testName) {
|
||||
'add_task' => preg_replace(
|
||||
'/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}/',
|
||||
'2025-01-01T00:00:00+00:00',
|
||||
$output
|
||||
),
|
||||
'read_system_stats' => preg_replace(
|
||||
'/\\\\"server_uptime_seconds\\\\": -?\d+\.?\d*/',
|
||||
'\\"server_uptime_seconds\\": 12345',
|
||||
$output
|
||||
),
|
||||
default => $output,
|
||||
};
|
||||
}
|
||||
}
|
||||
50
tests/Inspector/Stdio/StdioDiscoveryCalculatorTest.php
Normal file
50
tests/Inspector/Stdio/StdioDiscoveryCalculatorTest.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?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\Inspector\Stdio;
|
||||
|
||||
final class StdioDiscoveryCalculatorTest extends StdioInspectorSnapshotTestCase
|
||||
{
|
||||
public static function provideMethods(): array
|
||||
{
|
||||
return [
|
||||
...parent::provideMethods(),
|
||||
'Calculate Sum' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'calculate',
|
||||
'toolArgs' => ['a' => 12.5, 'b' => 7.3, 'operation' => 'add'],
|
||||
],
|
||||
'testName' => 'calculate_sum',
|
||||
],
|
||||
'Update Setting' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'update_setting',
|
||||
'toolArgs' => ['setting' => 'precision', 'value' => 3],
|
||||
],
|
||||
'testName' => 'update_setting',
|
||||
],
|
||||
'Read Config' => [
|
||||
'method' => 'resources/read',
|
||||
'options' => [
|
||||
'uri' => 'config://calculator/settings',
|
||||
],
|
||||
'testName' => 'read_config',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getServerScript(): string
|
||||
{
|
||||
return \dirname(__DIR__, 3).'/examples/stdio-discovery-calculator/server.php';
|
||||
}
|
||||
}
|
||||
55
tests/Inspector/Stdio/StdioEnvVariablesTest.php
Normal file
55
tests/Inspector/Stdio/StdioEnvVariablesTest.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?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 Inspector;
|
||||
|
||||
use Mcp\Tests\Inspector\Stdio\StdioInspectorSnapshotTestCase;
|
||||
|
||||
final class StdioEnvVariablesTest extends StdioInspectorSnapshotTestCase
|
||||
{
|
||||
public static function provideMethods(): array
|
||||
{
|
||||
return [
|
||||
...parent::provideMethods(),
|
||||
'Process Data (Default Mode)' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'process_data_by_mode',
|
||||
'toolArgs' => ['input' => 'test data'],
|
||||
],
|
||||
'testName' => 'process_data_default',
|
||||
],
|
||||
'Process Data (Debug Mode)' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'process_data_by_mode',
|
||||
'toolArgs' => ['input' => 'debug test'],
|
||||
'envVars' => ['APP_MODE' => 'debug'],
|
||||
],
|
||||
'testName' => 'process_data_debug',
|
||||
],
|
||||
'Process Data (Production Mode)' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'process_data_by_mode',
|
||||
'toolArgs' => ['input' => 'production data'],
|
||||
'envVars' => ['APP_MODE' => 'production'],
|
||||
],
|
||||
'testName' => 'process_data_production',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getServerScript(): string
|
||||
{
|
||||
return \dirname(__DIR__, 3).'/examples/stdio-env-variables/server.php';
|
||||
}
|
||||
}
|
||||
82
tests/Inspector/Stdio/StdioExplicitRegistrationTest.php
Normal file
82
tests/Inspector/Stdio/StdioExplicitRegistrationTest.php
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
<?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 Inspector;
|
||||
|
||||
use Mcp\Tests\Inspector\Stdio\StdioInspectorSnapshotTestCase;
|
||||
|
||||
final class StdioExplicitRegistrationTest extends StdioInspectorSnapshotTestCase
|
||||
{
|
||||
public static function provideMethods(): array
|
||||
{
|
||||
return [
|
||||
...parent::provideMethods(),
|
||||
'Echo Tool Call' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'echo_text',
|
||||
'toolArgs' => ['text' => 'Hello World!'],
|
||||
],
|
||||
'testName' => 'echo_text',
|
||||
],
|
||||
'Echo Tool with Special Characters' => [
|
||||
'method' => 'tools/call',
|
||||
'options' => [
|
||||
'toolName' => 'echo_text',
|
||||
'toolArgs' => ['text' => 'Test with emoji 🎉 and symbols @#$%'],
|
||||
],
|
||||
'testName' => 'echo_text_special_chars',
|
||||
],
|
||||
'Read App Version Resource' => [
|
||||
'method' => 'resources/read',
|
||||
'options' => [
|
||||
'uri' => 'app://version',
|
||||
],
|
||||
'testName' => 'read_app_version',
|
||||
],
|
||||
'Read Item Details (123)' => [
|
||||
'method' => 'resources/read',
|
||||
'options' => [
|
||||
'uri' => 'item://123/details',
|
||||
],
|
||||
'testName' => 'read_item_123_details',
|
||||
],
|
||||
'Read Item Details (ABC)' => [
|
||||
'method' => 'resources/read',
|
||||
'options' => [
|
||||
'uri' => 'item://ABC/details',
|
||||
],
|
||||
'testName' => 'read_item_ABC_details',
|
||||
],
|
||||
'Personalized Greeting Prompt (Alice)' => [
|
||||
'method' => 'prompts/get',
|
||||
'options' => [
|
||||
'promptName' => 'personalized_greeting',
|
||||
'promptArgs' => ['userName' => 'Alice'],
|
||||
],
|
||||
'testName' => 'personalized_greeting_alice',
|
||||
],
|
||||
'Personalized Greeting Prompt (Bob)' => [
|
||||
'method' => 'prompts/get',
|
||||
'options' => [
|
||||
'promptName' => 'personalized_greeting',
|
||||
'promptArgs' => ['userName' => 'Bob'],
|
||||
],
|
||||
'testName' => 'personalized_greeting_bob',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getServerScript(): string
|
||||
{
|
||||
return \dirname(__DIR__, 3).'/examples/stdio-explicit-registration/server.php';
|
||||
}
|
||||
}
|
||||
37
tests/Inspector/Stdio/StdioInspectorSnapshotTestCase.php
Normal file
37
tests/Inspector/Stdio/StdioInspectorSnapshotTestCase.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?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\Inspector\Stdio;
|
||||
|
||||
use Mcp\Tests\Inspector\InspectorSnapshotTestCase;
|
||||
|
||||
abstract class StdioInspectorSnapshotTestCase extends InspectorSnapshotTestCase
|
||||
{
|
||||
abstract protected function getServerScript(): string;
|
||||
|
||||
protected function getServerConnectionArgs(): array
|
||||
{
|
||||
return ['php', $this->getServerScript()];
|
||||
}
|
||||
|
||||
protected function getTransport(): string
|
||||
{
|
||||
return 'stdio';
|
||||
}
|
||||
|
||||
protected function getSnapshotFilePath(string $method, ?string $testName = null): string
|
||||
{
|
||||
$className = substr(static::class, strrpos(static::class, '\\') + 1);
|
||||
$suffix = $testName ? '-'.preg_replace('/[^a-zA-Z0-9_]/', '_', $testName) : '';
|
||||
|
||||
return __DIR__.'/snapshots/'.$className.'-'.str_replace('/', '_', $method).$suffix.'.json';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"prompts": []
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"resources": []
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"resourceTemplates": []
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "8"
|
||||
}
|
||||
],
|
||||
"isError": false
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "-3"
|
||||
}
|
||||
],
|
||||
"isError": false
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "5"
|
||||
}
|
||||
],
|
||||
"isError": false
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "3.5"
|
||||
}
|
||||
],
|
||||
"isError": false
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "24"
|
||||
}
|
||||
],
|
||||
"isError": false
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "0"
|
||||
}
|
||||
],
|
||||
"isError": false
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "256"
|
||||
}
|
||||
],
|
||||
"isError": false
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "1"
|
||||
}
|
||||
],
|
||||
"isError": false
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
{
|
||||
"tools": [
|
||||
{
|
||||
"name": "add_numbers",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "integer"
|
||||
},
|
||||
"b": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "multiply_numbers",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "integer"
|
||||
},
|
||||
"b": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "divide_numbers",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "integer"
|
||||
},
|
||||
"b": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "power",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"base": {
|
||||
"type": "integer"
|
||||
},
|
||||
"exponent": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"base",
|
||||
"exponent"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"prompts": []
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"resources": [
|
||||
{
|
||||
"name": "system_stats",
|
||||
"uri": "stats://system/overview",
|
||||
"description": "Provides current system statistics.",
|
||||
"mimeType": "application/json"
|
||||
}
|
||||
]
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue