-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathStdioServerTransport.php
More file actions
243 lines (199 loc) · 8.38 KB
/
StdioServerTransport.php
File metadata and controls
243 lines (199 loc) · 8.38 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
declare(strict_types=1);
namespace PhpMcp\Server\Transports;
use Evenement\EventEmitterTrait;
use PhpMcp\Schema\JsonRpc\Parser;
use PhpMcp\Server\Contracts\LoggerAwareInterface;
use PhpMcp\Server\Contracts\LoopAwareInterface;
use PhpMcp\Server\Contracts\ServerTransportInterface;
use PhpMcp\Server\Exception\TransportException;
use PhpMcp\Schema\JsonRpc\Error;
use PhpMcp\Schema\JsonRpc\Message;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use React\ChildProcess\Process;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use React\Stream\ReadableResourceStream;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableResourceStream;
use React\Stream\WritableStreamInterface;
use Throwable;
use function React\Promise\reject;
/**
* Implementation of the STDIO server transport using ReactPHP Process and Streams.
* Listens on STDIN, writes to STDOUT, and emits events for the Protocol.
*/
class StdioServerTransport implements ServerTransportInterface, LoggerAwareInterface, LoopAwareInterface
{
use EventEmitterTrait;
protected LoggerInterface $logger;
protected LoopInterface $loop;
protected ?Process $process = null;
protected ?ReadableStreamInterface $stdin = null;
protected ?WritableStreamInterface $stdout = null;
protected string $buffer = '';
protected bool $closing = false;
protected bool $listening = false;
private const CLIENT_ID = 'stdio';
/**
* Constructor takes optional stream resources.
* Defaults to STDIN and STDOUT if not provided.
* Dependencies like Logger and Loop are injected via setters.
*
* @param resource|null $inputStreamResource The readable resource (e.g., STDIN).
* @param resource|null $outputStreamResource The writable resource (e.g., STDOUT).
*
* @throws TransportException If provided resources are invalid.
*/
public function __construct(
protected $inputStreamResource = STDIN,
protected $outputStreamResource = STDOUT
) {
if (str_contains(PHP_OS, 'WIN') && ($this->inputStreamResource === STDIN && $this->outputStreamResource === STDOUT)) {
$message = 'STDIN and STDOUT are not supported as input and output stream resources' .
'on Windows due to PHP\'s limitations with non blocking pipes.' .
'Please use WSL or HttpServerTransport, or if you are advanced, provide your own stream resources.';
throw new TransportException($message);
}
// if (str_contains(PHP_OS, 'WIN')) {
// $this->inputStreamResource = pclose(popen('winpty -c "'.$this->inputStreamResource.'"', 'r'));
// $this->outputStreamResource = pclose(popen('winpty -c "'.$this->outputStreamResource.'"', 'w'));
// }
if (! is_resource($this->inputStreamResource) || get_resource_type($this->inputStreamResource) !== 'stream') {
throw new TransportException('Invalid input stream resource provided.');
}
if (! is_resource($this->outputStreamResource) || get_resource_type($this->outputStreamResource) !== 'stream') {
throw new TransportException('Invalid output stream resource provided.');
}
$this->logger = new NullLogger();
$this->loop = Loop::get();
}
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
public function setLoop(LoopInterface $loop): void
{
$this->loop = $loop;
}
/**
* Starts listening on STDIN.
*
* @throws TransportException If already listening or streams cannot be opened.
*/
public function listen(): void
{
if ($this->listening) {
throw new TransportException('Stdio transport is already listening.');
}
if ($this->closing) {
throw new TransportException('Cannot listen, transport is closing/closed.');
}
try {
$this->stdin = new ReadableResourceStream($this->inputStreamResource, $this->loop);
$this->stdout = new WritableResourceStream($this->outputStreamResource, $this->loop);
} catch (Throwable $e) {
$this->logger->error('Failed to open STDIN/STDOUT streams.', ['exception' => $e]);
throw new TransportException("Failed to open standard streams: {$e->getMessage()}", 0, $e);
}
$this->stdin->on('data', function ($chunk) {
$this->buffer .= $chunk;
$this->processBuffer();
});
$this->stdin->on('error', function (Throwable $error) {
$this->logger->error('STDIN stream error.', ['error' => $error->getMessage()]);
$this->emit('error', [new TransportException("STDIN error: {$error->getMessage()}", 0, $error), self::CLIENT_ID]);
$this->close();
});
$this->stdin->on('close', function () {
$this->logger->info('STDIN stream closed.');
$this->emit('client_disconnected', [self::CLIENT_ID, 'STDIN Closed']);
$this->close();
});
$this->stdout->on('error', function (Throwable $error) {
$this->logger->error('STDOUT stream error.', ['error' => $error->getMessage()]);
$this->emit('error', [new TransportException("STDOUT error: {$error->getMessage()}", 0, $error), self::CLIENT_ID]);
$this->close();
});
try {
$signalHandler = function (int $signal) {
$this->logger->info("Received signal {$signal}, shutting down.");
$this->close();
};
$this->loop->addSignal(SIGTERM, $signalHandler);
$this->loop->addSignal(SIGINT, $signalHandler);
} catch (Throwable $e) {
$this->logger->debug('Signal handling not supported by current event loop.');
}
$this->logger->info('Server is up and listening on STDIN 🚀');
$this->listening = true;
$this->closing = false;
$this->emit('ready');
$this->emit('client_connected', [self::CLIENT_ID]);
}
/** Processes the internal buffer to find complete lines/frames. */
private function processBuffer(): void
{
while (str_contains($this->buffer, "\n")) {
$pos = strpos($this->buffer, "\n");
$line = substr($this->buffer, 0, $pos);
$this->buffer = substr($this->buffer, $pos + 1);
$trimmedLine = trim($line);
if (empty($trimmedLine)) {
continue;
}
try {
$message = Parser::parse($trimmedLine);
} catch (Throwable $e) {
$this->logger->error('Error parsing message', ['exception' => $e]);
$error = Error::forParseError("Invalid JSON: " . $e->getMessage());
$this->sendMessage($error, self::CLIENT_ID);
continue;
}
$this->emit('message', [$message, self::CLIENT_ID]);
}
}
/**
* Sends a raw, framed message to STDOUT.
*/
public function sendMessage(Message $message, string $sessionId, array $context = []): PromiseInterface
{
if ($this->closing || ! $this->stdout || ! $this->stdout->isWritable()) {
return reject(new TransportException('Stdio transport is closed or STDOUT is not writable.'));
}
$deferred = new Deferred();
$json = json_encode($message, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$written = $this->stdout->write($json . "\n");
if ($written) {
$deferred->resolve(null);
} else {
$this->logger->debug('STDOUT buffer full, waiting for drain.');
$this->stdout->once('drain', function () use ($deferred) {
$this->logger->debug('STDOUT drained.');
$deferred->resolve(null);
});
}
return $deferred->promise();
}
/**
* Stops listening and cleans up resources.
*/
public function close(): void
{
if ($this->closing) {
return;
}
$this->closing = true;
$this->listening = false;
$this->logger->info('Closing Stdio transport...');
$this->stdin?->close();
$this->stdout?->close();
$this->stdin = null;
$this->stdout = null;
$this->emit('close', ['StdioTransport closed.']);
$this->removeAllListeners();
}
}