-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathClientBuilder.php
More file actions
184 lines (140 loc) · 4.72 KB
/
ClientBuilder.php
File metadata and controls
184 lines (140 loc) · 4.72 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
namespace PhpMcp\Client;
use PhpMcp\Client\Contracts\MessageIdGeneratorInterface;
use PhpMcp\Client\Exception\ConfigurationException;
use PhpMcp\Client\Factory\TransportFactory; // Added use
use PhpMcp\Client\Model\Capabilities as ClientCapabilities;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
/**
* Builder class for creating Client instances, each configured for ONE server connection.
*
* This class provides a fluent interface for configuring and building Client instances.
*/
class ClientBuilder
{
protected ?string $name = null;
protected ?string $version = null;
protected ?ClientCapabilities $capabilities = null;
protected ?LoggerInterface $logger = null;
protected ?CacheInterface $cache = null;
protected ?EventDispatcherInterface $eventDispatcher = null;
protected ?MessageIdGeneratorInterface $idGenerator = null;
protected ?LoopInterface $loop = null;
protected ?ServerConfig $serverConfig = null;
protected int $definitionCacheTtl = 3600;
protected ?TransportFactory $transportFactory = null;
protected function __construct() {}
public static function make(): self
{
return new self;
}
/** @deprecated 1.0.1 Use withClientInfo() instead. */
public function withName(string $name): self
{
$this->name = $name;
return $this;
}
/** @deprecated 1.0.1 Use withClientInfo() instead. */
public function withVersion(string $version): self
{
$this->version = $version;
return $this;
}
public function withClientInfo(string $name, string $version): self
{
$this->name = $name;
$this->version = $version;
return $this;
}
public function withCapabilities(ClientCapabilities $capabilities): self
{
$this->capabilities = $capabilities;
return $this;
}
public function withLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
return $this;
}
public function withCache(CacheInterface $cache, int $definitionCacheTtl = 3600): self
{
$this->cache = $cache;
$this->definitionCacheTtl = $definitionCacheTtl > 0 ? $definitionCacheTtl : 3600;
return $this;
}
public function withEventDispatcher(EventDispatcherInterface $eventDispatcher): self
{
$this->eventDispatcher = $eventDispatcher;
return $this;
}
public function withIdGenerator(MessageIdGeneratorInterface $generator): self
{
$this->idGenerator = $generator;
return $this;
}
public function withLoop(LoopInterface $loop): self
{
$this->loop = $loop;
return $this;
}
/**
* Sets the configuration for the server this client will connect to.
* This is required before calling build().
*
* @param ServerConfig $config The configuration object for the server.
*/
public function withServerConfig(ServerConfig $config): self
{
$this->serverConfig = $config;
return $this;
}
/**
* [ADVANCED] Provide a custom TransportFactory instance.
* Useful for testing or advanced transport customization.
*/
public function withTransportFactory(TransportFactory $factory): self
{
$this->transportFactory = $factory;
return $this;
}
/**
* Builds the Client instance.
*
* @throws ConfigurationException If required configuration is missing.
*/
public function build(): Client
{
if ($this->name === null) {
throw new ConfigurationException('Name must be provided using withName().');
}
if ($this->version === null) {
throw new ConfigurationException('Version must be provided using withVersion().');
}
if ($this->serverConfig === null) {
throw new ConfigurationException('ServerConfig must be provided using withServerConfig().');
}
$capabilities = $this->capabilities ?? ClientCapabilities::forClient();
$loop = $this->loop ?? Loop::get();
$clientConfig = new ClientConfig(
name: $this->name,
version: $this->version,
capabilities: $capabilities,
logger: $this->logger,
cache: $this->cache,
eventDispatcher: $this->eventDispatcher,
loop: $loop,
definitionCacheTtl: $this->definitionCacheTtl,
idGenerator: $this->idGenerator
);
return new Client(
$this->serverConfig,
$clientConfig,
$this->transportFactory
);
}
}