Kick off the SDK based on Symfony's MCP SDK

This commit is contained in:
Christopher Hertel 2025-07-25 11:40:39 +02:00
commit ee0dffe369
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3
3 changed files with 3924 additions and 0 deletions

View file

@ -0,0 +1,52 @@
<?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\Message;
use Mcp\Message\Error;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
#[Small]
#[CoversClass(Error::class)]
final class ErrorTest extends TestCase
{
public function testWithIntegerId(): void
{
$error = new Error(1, -32602, 'Another error occurred');
$expected = [
'jsonrpc' => '2.0',
'id' => 1,
'error' => [
'code' => -32602,
'message' => 'Another error occurred',
],
];
$this->assertSame($expected, $error->jsonSerialize());
}
public function testWithStringId(): void
{
$error = new Error('abc', -32602, 'Another error occurred');
$expected = [
'jsonrpc' => '2.0',
'id' => 'abc',
'error' => [
'code' => -32602,
'message' => 'Another error occurred',
],
];
$this->assertSame($expected, $error->jsonSerialize());
}
}

View file

@ -0,0 +1,94 @@
<?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\Message;
use Mcp\Exception\InvalidInputMessageException;
use Mcp\Message\Factory;
use Mcp\Message\Notification;
use Mcp\Message\Request;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
#[Small]
#[CoversClass(Factory::class)]
final class FactoryTest extends TestCase
{
private Factory $factory;
protected function setUp(): void
{
$this->factory = new Factory();
}
/**
* @param iterable<mixed> $items
*/
private function first(iterable $items): mixed
{
foreach ($items as $item) {
return $item;
}
return null;
}
public function testCreateRequest(): void
{
$json = '{"jsonrpc": "2.0", "method": "test_method", "params": {"foo": "bar"}, "id": 123}';
$result = $this->first($this->factory->create($json));
$this->assertInstanceOf(Request::class, $result);
$this->assertSame('test_method', $result->method);
$this->assertSame(['foo' => 'bar'], $result->params);
$this->assertSame(123, $result->id);
}
public function testCreateNotification(): void
{
$json = '{"jsonrpc": "2.0", "method": "notifications/test_event", "params": {"foo": "bar"}}';
$result = $this->first($this->factory->create($json));
$this->assertInstanceOf(Notification::class, $result);
$this->assertSame('notifications/test_event', $result->method);
$this->assertSame(['foo' => 'bar'], $result->params);
}
public function testInvalidJson(): void
{
$this->expectException(\JsonException::class);
$this->first($this->factory->create('invalid json'));
}
public function testMissingMethod(): void
{
$result = $this->first($this->factory->create('{"jsonrpc": "2.0", "params": {}, "id": 1}'));
$this->assertInstanceOf(InvalidInputMessageException::class, $result);
$this->assertEquals('Invalid JSON-RPC request, missing "method".', $result->getMessage());
}
public function testBatchMissingMethod(): void
{
$results = $this->factory->create('[{"jsonrpc": "2.0", "params": {}, "id": 1}, {"jsonrpc": "2.0", "method": "notifications/test_event", "params": {}, "id": 2}]');
$results = iterator_to_array($results);
$result = array_shift($results);
$this->assertInstanceOf(InvalidInputMessageException::class, $result);
$this->assertEquals('Invalid JSON-RPC request, missing "method".', $result->getMessage());
$result = array_shift($results);
$this->assertInstanceOf(Notification::class, $result);
}
}

View file

@ -0,0 +1,46 @@
<?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\Message;
use Mcp\Message\Response;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
#[Small]
#[CoversClass(Response::class)]
final class ResponseTest extends TestCase
{
public function testWithIntegerId(): void
{
$response = new Response(1, ['foo' => 'bar']);
$expected = [
'jsonrpc' => '2.0',
'id' => 1,
'result' => ['foo' => 'bar'],
];
$this->assertSame($expected, $response->jsonSerialize());
}
public function testWithStringId(): void
{
$response = new Response('abc', ['foo' => 'bar']);
$expected = [
'jsonrpc' => '2.0',
'id' => 'abc',
'result' => ['foo' => 'bar'],
];
$this->assertSame($expected, $response->jsonSerialize());
}
}