PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.8
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.8
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / lib / php-mcp-schema / src / Common / Protocol / Factory / ServerRequestFactory.php

ServerRequestFactory.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.8, at lib/php-mcp-schema/src/Common/Protocol/Factory/ServerRequestFactory.php

90 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace WP\McpSchema\Common\Protocol\Factory;
6
7 use WP\McpSchema\Common\Protocol\Union\ServerRequestInterface;
8 use WP\McpSchema\Common\Protocol\DTO\PingRequest;
9 use WP\McpSchema\Client\Sampling\DTO\CreateMessageRequest;
10 use WP\McpSchema\Client\Roots\DTO\ListRootsRequest;
11 use WP\McpSchema\Client\Elicitation\DTO\ElicitRequest;
12 use WP\McpSchema\Common\Tasks\DTO\GetTaskRequest;
13 use WP\McpSchema\Common\Protocol\DTO\GetTaskPayloadRequest;
14 use WP\McpSchema\Common\Tasks\DTO\ListTasksRequest;
15 use WP\McpSchema\Common\Tasks\DTO\CancelTaskRequest;
16
17 /**
18 * Factory for creating ServerRequest union type instances.
19 *
20 * @mcp-domain Common
21 * @mcp-subdomain Protocol
22 * @mcp-version 2025-11-25
23 */
24 final class ServerRequestFactory
25 {
26 /**
27 * Registry mapping discriminator values to implementation classes.
28 *
29 * @var array<string, class-string<ServerRequestInterface>>
30 */
31 public const REGISTRY = [
32 'ping' => PingRequest::class,
33 'sampling/createMessage' => CreateMessageRequest::class,
34 'roots/list' => ListRootsRequest::class,
35 'elicitation/create' => ElicitRequest::class,
36 'tasks/get' => GetTaskRequest::class,
37 'tasks/result' => GetTaskPayloadRequest::class,
38 'tasks/list' => ListTasksRequest::class,
39 'tasks/cancel' => CancelTaskRequest::class,
40 ];
41
42 /**
43 * Creates an instance from an array.
44 *
45 * @param array<string, mixed> $data
46 * @return ServerRequestInterface
47 * @throws \InvalidArgumentException
48 */
49 public static function fromArray(array $data): ServerRequestInterface
50 {
51 if (!isset($data['method'])) {
52 throw new \InvalidArgumentException('Missing discriminator field: method');
53 }
54
55 /** @var string $method */
56 $method = $data['method'];
57 if (!isset(self::REGISTRY[$method])) {
58 throw new \InvalidArgumentException(sprintf(
59 "Unknown method value '%s'. Valid values: %s",
60 $method,
61 implode(', ', array_keys(self::REGISTRY))
62 ));
63 }
64
65 $class = self::REGISTRY[$method];
66 return $class::fromArray($data);
67 }
68
69 /**
70 * Checks if a method value is supported by this factory.
71 *
72 * @param string $method
73 * @return bool
74 */
75 public static function supports(string $method): bool
76 {
77 return isset(self::REGISTRY[$method]);
78 }
79
80 /**
81 * Returns all supported method values.
82 *
83 * @return array<string>
84 */
85 public static function methods(): array
86 {
87 return array_keys(self::REGISTRY);
88 }
89 }
90