Skip to content

Commit 87c8ece

Browse files
authored
Split messages in newlines (#18)
1 parent d1d4fbc commit 87c8ece

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/Commands/LoopMcpServerStartCommand.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,38 @@ public function handle(McpHandler $mcpHandler): int
5656
$this->comment('Received data: '.$data);
5757
}
5858

59-
try {
60-
$message = (array) json_decode($data, true);
61-
$response = $mcpHandler->handle($message);
62-
63-
if ($this->option('debug')) {
64-
$this->comment('Response: '.json_encode($response));
59+
foreach (explode("\n", $data) as $line) {
60+
if (!json_validate($line)) {
61+
if ($this->option('debug')) {
62+
$this->comment('Invalid line: '. $line);
63+
}
64+
continue;
6565
}
6666

67-
if (isset($message['id'])) {
68-
$stdout->write(json_encode($response).PHP_EOL);
69-
}
70-
} catch (Exception $e) {
71-
$this->error('Error processing message: '.$e->getMessage());
67+
try {
68+
$message = (array) json_decode($line, true);
69+
$response = $mcpHandler->handle($message);
70+
71+
if ($this->option('debug')) {
72+
$this->comment('Response: '.json_encode($response));
73+
}
7274

73-
$response = $mcpHandler->formatErrorResponse(
74-
$message['id'] ?? '',
75-
ErrorCode::INTERNAL_ERROR,
76-
$e->getMessage()
77-
);
75+
if (isset($message['id'])) {
76+
$stdout->write(json_encode($response).PHP_EOL);
77+
}
78+
} catch (Exception $e) {
79+
$this->error('Error processing message: '.$e->getMessage());
7880

79-
$stdout->write(json_encode($response).PHP_EOL);
81+
$response = $mcpHandler->formatErrorResponse(
82+
$message['id'] ?? '',
83+
ErrorCode::INTERNAL_ERROR,
84+
$e->getMessage()
85+
);
86+
87+
$stdout->write(json_encode($response).PHP_EOL);
88+
}
8089
}
90+
8191
});
8292

8393
if ($this->option('debug')) {

0 commit comments

Comments
 (0)