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
8 changes: 2 additions & 6 deletions src/Resource/BlobResourceContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ public function __construct(
* Converts the blob resource contents to an array format.
* Includes 'uri', 'blob', and 'mimeType'.
*
* @return array{uri: string, blob: string, mimeType?: string} The array representation.
* @return array{uri: string, blob: string, mimeType: string} The array representation.
*/
public function toArray(): array
{
$data = ['uri' => $this->uri, 'blob' => $this->blob];
// $this->mimeType is guaranteed by constructor, but check doesn't hurt for consistency
if ($this->mimeType !== null) {
$data['mimeType'] = $this->mimeType;
}
$data = ['uri' => $this->uri, 'blob' => $this->blob, 'mimeType' => $this->mimeType];
return $data;
}
}
2 changes: 1 addition & 1 deletion src/Resource/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected function text(string $text, ?string $mimeType = null, array $parameter
return new TextResourceContents(
$this->resolveUri($this->getUri(), $parameters),
$text,
$mimeType
$mimeType ?? 'text/plain'
);
}

Expand Down
22 changes: 19 additions & 3 deletions src/Resource/ResourceContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,33 @@
*/
abstract class ResourceContents
{
/**
* The MIME type of the resource content.
* @var string
*/
public readonly string $mimeType;

/**
* Constructs a ResourceContents instance.
*
* @param string $uri The URI of the resource content. This might be a resolved URI if the resource URI was a template.
* @param string|null $mimeType The MIME type of the resource content, if applicable.
* @param string $mimeType The MIME type of the resource content.
*/
public function __construct(
/** The URI of the resource content. */
public readonly string $uri,
/** The MIME type of the resource content, if applicable. */
public readonly ?string $mimeType = null
string $mimeType
) {
$this->mimeType = $mimeType;
}

/**
* Converts the resource contents to an array format.
*
* @return array{uri: string, mimeType: string} The array representation.
*/
public function toArray(): array
{
return ['uri' => $this->uri, 'mimeType' => $this->mimeType];
}
}
16 changes: 7 additions & 9 deletions src/Resource/TextResourceContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,26 @@ class TextResourceContents extends ResourceContents
*
* @param string $uri The URI of the resource.
* @param string $text The text content.
* @param string|null $mimeType The MIME type of the content. Defaults to "text/plain" if null.
* @param string $mimeType The MIME type of the resource. Defaults to 'text/plain'.
*/
public function __construct(
string $uri,
public readonly string $text,
?string $mimeType = null
string $mimeType = 'text/plain'
) {
parent::__construct($uri, $mimeType ?? 'text/plain');
parent::__construct($uri, $mimeType);
}

/**
* Converts the text resource contents to an array format.
* Includes 'uri', 'text', and 'mimeType'.
* Includes 'uri', 'mimeType', and 'text'.
*
* @return array{uri: string, text: string, mimeType?: string} The array representation.
* @return array{uri: string, mimeType: string, text: string} The array representation.
*/
public function toArray(): array
{
$data = ['uri' => $this->uri, 'text' => $this->text];
if ($this->mimeType !== null) {
$data['mimeType'] = $this->mimeType;
}
$data = parent::toArray(); // Gets 'uri' and 'mimeType' from ResourceContents
$data['text'] = $this->text;
return $data;
}
}
28 changes: 28 additions & 0 deletions tests/Resource/BlobResourceContentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace MCP\Tests\Server\Resource;

use MCP\Server\Resource\BlobResourceContents;
use PHPUnit\Framework\TestCase;

class BlobResourceContentsTest extends TestCase
{
public function testToArrayReturnsCorrectStructureAndValues(): void
{
$uri = 'test://blob/123';
$blobData = base64_encode('Hello World');
$mimeType = 'application/octet-stream';

$blobContents = new BlobResourceContents($uri, $blobData, $mimeType);

$expectedArray = [
'uri' => $uri,
'blob' => $blobData,
'mimeType' => $mimeType,
];

$this->assertSame($expectedArray, $blobContents->toArray());
}
}
52 changes: 52 additions & 0 deletions tests/Resource/TextResourceContentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace MCP\Tests\Server\Resource;

use MCP\Server\Resource\TextResourceContents;
use PHPUnit\Framework\TestCase;

class TextResourceContentsTest extends TestCase
{
public function testConstructorAndToArrayWithExplicitMimeType(): void
{
$uri = 'test://text/1';
$text = 'Hello world';
$mimeType = 'application/xml'; // Explicitly different from default

$contents = new TextResourceContents($uri, $text, $mimeType);

$this->assertSame($uri, $contents->uri);
$this->assertSame($text, $contents->text);
$this->assertSame($mimeType, $contents->mimeType);

$expectedArray = [
'uri' => $uri,
'mimeType' => $mimeType,
'text' => $text,
];
$this->assertSame($expectedArray, $contents->toArray());
}

public function testConstructorAndToArrayWithDefaultMimeType(): void
{
$uri = 'test://text/2';
$text = 'Another message';
$defaultMimeType = 'text/plain'; // Expected default

// Instantiate without providing mimeType to use the default
$contents = new TextResourceContents($uri, $text);

$this->assertSame($uri, $contents->uri);
$this->assertSame($text, $contents->text);
$this->assertSame($defaultMimeType, $contents->mimeType);

$expectedArray = [
'uri' => $uri,
'mimeType' => $defaultMimeType,
'text' => $text,
];
$this->assertSame($expectedArray, $contents->toArray());
}
}