forked from modelcontextprotocol/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceTemplate.php
More file actions
134 lines (123 loc) · 4.94 KB
/
Copy pathResourceTemplate.php
File metadata and controls
134 lines (123 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?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\Schema;
use Mcp\Exception\InvalidArgumentException;
/**
* A template description for resources available on the server.
*
* @phpstan-import-type AnnotationsData from Annotations
*
* @phpstan-type ResourceTemplateData array{
* uriTemplate: string,
* name: string,
* title?: string,
* description?: string|null,
* mimeType?: string|null,
* annotations?: AnnotationsData|null,
* _meta?: array<string, mixed>
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class ResourceTemplate implements \JsonSerializable
{
/**
* Resource name pattern regex - must contain only alphanumeric characters, underscores, and hyphens.
*/
private const RESOURCE_NAME_PATTERN = '/^[a-zA-Z0-9_-]+$/';
/**
* URI Template pattern regex - requires a valid scheme followed by colon and path with at least one placeholder (RFC 3986).
* Example patterns: file://{path}/contents.txt, db://{table}/{id}, config:{key}, etc.
*/
private const URI_TEMPLATE_PATTERN = '/^[a-zA-Z][a-zA-Z0-9+.-]*:.*{[^{}]+}.*/';
/**
* @param string $uriTemplate a URI template (according to RFC 6570) that can be used to construct resource URIs
* @param string $name a short identifier for this resource template type
* @param ?string $title optional human-readable title for display in UI
* @param ?string $description a description to help the LLM understand available resources
* @param ?string $mimeType the MIME type for all resources that match this template, if uniform
* @param ?Annotations $annotations optional annotations for the client
* @param ?array<string, mixed> $meta optional metadata
*/
public function __construct(
public readonly string $uriTemplate,
public readonly string $name,
public readonly ?string $title = null,
public readonly ?string $description = null,
public readonly ?string $mimeType = null,
public readonly ?Annotations $annotations = null,
public readonly ?array $meta = null,
) {
if (!preg_match(self::RESOURCE_NAME_PATTERN, $name)) {
throw new InvalidArgumentException(\sprintf('Invalid resource name "%s": must contain only alphanumeric characters, underscores, and hyphens.', $name));
}
if (!preg_match(self::URI_TEMPLATE_PATTERN, $uriTemplate)) {
throw new InvalidArgumentException(\sprintf('Invalid URI template : "%s" must be a valid URI template with at least one placeholder.', $uriTemplate));
}
}
/**
* @param ResourceTemplateData $data
*/
public static function fromArray(array $data): self
{
if (empty($data['uriTemplate']) || !\is_string($data['uriTemplate'])) {
throw new InvalidArgumentException('Invalid or missing "uriTemplate" in ResourceTemplate data.');
}
if (empty($data['name']) || !\is_string($data['name'])) {
throw new InvalidArgumentException('Invalid or missing "name" in ResourceTemplate data.');
}
if (!empty($data['_meta']) && !\is_array($data['_meta'])) {
throw new InvalidArgumentException('Invalid "_meta" in ResourceTemplate data.');
}
return new self(
uriTemplate: $data['uriTemplate'],
name: $data['name'],
title: isset($data['title']) && \is_string($data['title']) ? $data['title'] : null,
description: $data['description'] ?? null,
mimeType: $data['mimeType'] ?? null,
annotations: isset($data['annotations']) ? Annotations::fromArray($data['annotations']) : null,
meta: isset($data['_meta']) ? $data['_meta'] : null
);
}
/**
* @return array{
* uriTemplate: string,
* name: string,
* title?: string,
* description?: string,
* mimeType?: string,
* annotations?: Annotations,
* _meta?: array<string, mixed>
* }
*/
public function jsonSerialize(): array
{
$data = [
'uriTemplate' => $this->uriTemplate,
'name' => $this->name,
];
if (null !== $this->title) {
$data['title'] = $this->title;
}
if (null !== $this->description) {
$data['description'] = $this->description;
}
if (null !== $this->mimeType) {
$data['mimeType'] = $this->mimeType;
}
if (null !== $this->annotations) {
$data['annotations'] = $this->annotations;
}
if (null !== $this->meta) {
$data['_meta'] = $this->meta;
}
return $data;
}
}