Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions tests/Inspector/Http/HttpClientCommunicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\Http;

final class HttpClientCommunicationTest extends HttpInspectorSnapshotTestCase
{
protected function setUp(): void
{
$this->markTestSkipped('Test skipped: SDK cannot handle logging/setLevel requests required by logging capability, and built-in PHP server does not support sampling.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any ideas how to deal with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the logging part, since logging doesn't need to wait for response, the Built in PHP server still works, we just need to implement handling of logging/setLevel method in the SDK and we're good to go (In another PR obviously)

As for sampling, I honestly don't know just yet.

}

public static function provideMethods(): array
{
return [
...parent::provideMethods(),
'Prepare Project Briefing (Simple)' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'prepare_project_briefing',
'toolArgs' => [
'projectName' => 'Website Redesign',
'milestones' => ['Discovery', 'Design', 'Development', 'Testing'],
],
],
'testName' => 'prepare_project_briefing_simple',
],
'Prepare Project Briefing (Complex)' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'prepare_project_briefing',
'toolArgs' => [
'projectName' => 'Mobile App Launch',
'milestones' => ['Market Research', 'UI/UX Design', 'MVP Development', 'Beta Testing', 'Marketing Campaign', 'Public Launch'],
],
],
'testName' => 'prepare_project_briefing_complex',
],
'Run Service Maintenance' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'run_service_maintenance',
'toolArgs' => [
'serviceName' => 'Payment Gateway API',
],
],
'testName' => 'run_service_maintenance',
],
];
}

protected function getServerScript(): string
{
return \dirname(__DIR__, 3).'/examples/http-client-communication/server.php';
}
}
50 changes: 50 additions & 0 deletions tests/Inspector/Http/HttpCombinedRegistrationTest.php
Original file line number Diff line number Diff line change
@@ -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\Http;

final class HttpCombinedRegistrationTest extends HttpInspectorSnapshotTestCase
{
public static function provideMethods(): array
{
return [
...parent::provideMethods(),
'Manual Greeter Tool' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'manualGreeter',
'toolArgs' => ['user' => 'HTTP Test User'],
],
'testName' => 'manual_greeter',
],
'Discovered Status Check Tool' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'discovered_status_check',
'toolArgs' => [],
],
'testName' => 'discovered_status_check',
],
'Read Priority Config (Manual Override)' => [
'method' => 'resources/read',
'options' => [
'uri' => 'config://priority',
],
'testName' => 'config_priority',
],
];
}

protected function getServerScript(): string
{
return \dirname(__DIR__, 3).'/examples/http-combined-registration/server.php';
}
}
87 changes: 87 additions & 0 deletions tests/Inspector/Http/HttpComplexToolSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Http;

final class HttpComplexToolSchemaTest extends HttpInspectorSnapshotTestCase
{
public static function provideMethods(): array
{
return [
...parent::provideMethods(),
'Schedule Event (Meeting with Time)' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'schedule_event',
'toolArgs' => [
'title' => 'Team Standup',
'date' => '2024-12-01',
'type' => 'meeting',
'time' => '09:00',
'priority' => 'normal',
'attendees' => ['[email protected]', '[email protected]'],
'sendInvites' => true,
],
],
'testName' => 'schedule_event_meeting_with_time',
],
'Schedule Event (All Day Reminder)' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'schedule_event',
'toolArgs' => [
'title' => 'Project Deadline',
'date' => '2024-12-15',
'type' => 'reminder',
'priority' => 'high',
],
],
'testName' => 'schedule_event_all_day_reminder',
],
'Schedule Event (Call with High Priority)' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'schedule_event',
'toolArgs' => [
'title' => 'Client Call',
'date' => '2024-12-02',
'type' => 'call',
'time' => '14:30',
'priority' => 'high',
'attendees' => ['[email protected]'],
'sendInvites' => false,
],
],
'testName' => 'schedule_event_high_priority',
],
'Schedule Event (Other Event with Low Priority)' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'schedule_event',
'toolArgs' => [
'title' => 'Office Party',
'date' => '2024-12-20',
'type' => 'other',
'time' => '18:00',
'priority' => 'low',
'attendees' => ['[email protected]'],
],
],
'testName' => 'schedule_event_low_priority',
],
];
}

protected function getServerScript(): string
{
return \dirname(__DIR__, 3).'/examples/http-complex-tool-schema/server.php';
}
}
72 changes: 72 additions & 0 deletions tests/Inspector/Http/HttpDiscoveryUserProfileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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\Http;

final class HttpDiscoveryUserProfileTest extends HttpInspectorSnapshotTestCase
{
public static function provideMethods(): array
{
return [
...parent::provideMethods(),
'Send Welcome Tool' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'send_welcome',
'toolArgs' => ['userId' => '101', 'customMessage' => 'Welcome to our platform!'],
],
'testName' => 'send_welcome',
],
'Test Tool Without Params' => [
'method' => 'tools/call',
'options' => [
'toolName' => 'test_tool_without_params',
'toolArgs' => [],
],
'testName' => 'test_tool_without_params',
],
'Read User Profile 101' => [
'method' => 'resources/read',
'options' => [
'uri' => 'user://101/profile',
],
'testName' => 'read_user_profile_101',
],
'Read User Profile 102' => [
'method' => 'resources/read',
'options' => [
'uri' => 'user://102/profile',
],
'testName' => 'read_user_profile_102',
],
'Read User ID List' => [
'method' => 'resources/read',
'options' => [
'uri' => 'user://list/ids',
],
'testName' => 'read_user_id_list',
],
'Generate Bio Prompt (Formal)' => [
'method' => 'prompts/get',
'options' => [
'promptName' => 'generate_bio_prompt',
'promptArgs' => ['userId' => '101', 'tone' => 'formal'],
],
'testName' => 'generate_bio_prompt',
],
];
}

protected function getServerScript(): string
{
return \dirname(__DIR__, 3).'/examples/http-discovery-userprofile/server.php';
}
}
102 changes: 102 additions & 0 deletions tests/Inspector/Http/HttpInspectorSnapshotTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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\Http;

use Mcp\Tests\Inspector\InspectorSnapshotTestCase;
use Symfony\Component\Process\Process;

abstract class HttpInspectorSnapshotTestCase extends InspectorSnapshotTestCase
{
private Process $serverProcess;
private int $serverPort;

protected function setUp(): void
{
parent::setUp();
$this->startServer();
}

protected function tearDown(): void
{
$this->stopServer();
parent::tearDown();
}

abstract protected function getServerScript(): string;

protected function getServerConnectionArgs(): array
{
return [\sprintf('http://127.0.0.1:%d', $this->serverPort)];
}

protected function getTransport(): string
{
return 'http';
}

private function startServer(): void
{
$this->serverPort = 8000 + (getmypid() % 1000);

$this->serverProcess = new Process([
'php',
'-S',
\sprintf('127.0.0.1:%d', $this->serverPort),
$this->getServerScript(),
]);

$this->serverProcess->start();

$timeout = 5; // seconds
$startTime = time();

while (time() - $startTime < $timeout) {
if ($this->serverProcess->isRunning() && $this->isServerReady()) {
return;
}
usleep(100000); // 100ms
}

$this->fail(\sprintf('Server failed to start on port %d within %d seconds', $this->serverPort, $timeout));
}

private function stopServer(): void
{
if (isset($this->serverProcess)) {
$this->serverProcess->stop(1, \SIGTERM);
}
}

private function isServerReady(): bool
{
$context = stream_context_create([
'http' => [
'timeout' => 1,
'method' => 'GET',
],
]);

// Try a simple health check - this will likely fail with MCP but should respond
$response = @file_get_contents(\sprintf('http://127.0.0.1:%d', $this->serverPort), false, $context);

// We don't care about the response content, just that the server is accepting connections
return false !== $response || false === str_contains(error_get_last()['message'] ?? '', 'Connection refused');
}

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';
}
}
Loading