@@ -56,28 +56,38 @@ public function handle(McpHandler $mcpHandler): int
56
56
$ this ->comment ('Received data: ' .$ data );
57
57
}
58
58
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 ;
65
65
}
66
66
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
+ }
72
74
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 () );
78
80
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
+ }
80
89
}
90
+
81
91
});
82
92
83
93
if ($ this ->option ('debug ' )) {
0 commit comments