1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Extension\Skills\Server;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Resource\BlobResourceContents;
18: use Nexus\Mcp\Core\Schema\Resource\TextResourceContents;
19:
20: /**
21: * One file of a skill, held as the bytes served for it.
22: */
23: final readonly class SkillFile
24: {
25: /**
26: * @param non-empty-string $uri
27: * @param string $contents The file's raw bytes
28: * @param null|non-empty-string $mimeType
29: */
30: public function __construct(
31: public string $uri,
32: public string $contents,
33: public ?string $mimeType = null,
34: ) {
35: Assert::that($uri)->isNonEmptyString('skill file "uri" must be a non-empty string.');
36: Assert::that($mimeType)->nullOr()->isNonEmptyString('skill file "mimeType" must be a non-empty string or null.');
37: }
38:
39: /**
40: * The contents as text when the bytes are valid UTF-8 free of NUL, and as a base64 blob otherwise.
41: */
42: public function toResourceContents(): BlobResourceContents|TextResourceContents
43: {
44: if (preg_match('//u', $this->contents) === 1 && ! str_contains($this->contents, "\0")) {
45: return new TextResourceContents($this->uri, $this->contents, $this->mimeType);
46: }
47:
48: return new BlobResourceContents($this->uri, base64_encode($this->contents), $this->mimeType);
49: }
50: }
51: