@@ -47,6 +47,12 @@ public function __construct(TransportInterface $transport)
4747 }
4848
4949 /**
50+ * Establishes a connection and processes incoming messages from the transport layer.
51+ *
52+ * The method starts the transport, continuously checks for incoming messages while connected,
53+ * handles each message appropriately, and disconnects when the connection ends.
54+ *
55+ * @return void
5056 * @throws Exception
5157 */
5258 public function connect (): void
@@ -68,26 +74,58 @@ public function connect(): void
6874 $ this ->disconnect ();
6975 }
7076
77+ /**
78+ * Sends a message using the configured transport.
79+ *
80+ * @param string|array $message The message to be sent, either as a string or an array.
81+ * @return void
82+ */
7183 public function send (string |array $ message ): void
7284 {
7385 $ this ->transport ->send (message: $ message );
7486 }
7587
88+ /**
89+ * Disconnects the current transport by closing the connection.
90+ *
91+ * @return void
92+ */
7693 public function disconnect (): void
7794 {
7895 $ this ->transport ->close ();
7996 }
8097
98+ /**
99+ * Registers a request handler to manage incoming requests.
100+ *
101+ * @param RequestHandler $handler The request handler instance to be registered.
102+ * @return void
103+ */
81104 public function registerRequestHandler (RequestHandler $ handler ): void
82105 {
83106 $ this ->requestHandlers [] = $ handler ;
84107 }
85108
109+ /**
110+ * Registers a notification handler to handle incoming notifications.
111+ *
112+ * @param NotificationHandler $handler The notification handler instance to be registered.
113+ * @return void
114+ */
86115 public function registerNotificationHandler (NotificationHandler $ handler ): void
87116 {
88117 $ this ->notificationHandlers [] = $ handler ;
89118 }
90119
120+ /**
121+ * Handles an incoming JSON-RPC message from a client, processes it as either a request or notification,
122+ * and manages error responses when necessary.
123+ *
124+ * @param string $clientId The unique identifier for the client sending the message
125+ * @param array $message The JSON-RPC message data to process
126+ * @return void
127+ * @throws JsonRpcErrorException
128+ */
91129 public function handleMessage (string $ clientId , array $ message ): void
92130 {
93131 $ messageId = $ message ['id ' ] ?? null ;
@@ -143,7 +181,7 @@ private function handleRequestProcess(string $clientId, RequestData $requestData
143181 } catch (JsonRpcErrorException $ e ) {
144182 $ this ->pushMessage (clientId: $ clientId , message: new JsonRpcErrorResource (exception: $ e , id: $ messageId ));
145183 } catch (ToolParamsValidatorException $ e ) {
146- $ jsonRpcErrorException = new JsonRpcErrorException (message: $ e ->getMessage (). implode (', ' , $ e ->getErrors ()), code: JsonRpcErrorCode::INVALID_PARAMS );
184+ $ jsonRpcErrorException = new JsonRpcErrorException (message: $ e ->getMessage () . ' ' . implode (', ' , $ e ->getErrors ()), code: JsonRpcErrorCode::INVALID_PARAMS );
147185 $ this ->pushMessage (clientId: $ clientId , message: new JsonRpcErrorResource (exception: $ jsonRpcErrorException , id: $ messageId ));
148186 } catch (Exception $ e ) {
149187 $ jsonRpcErrorException = new JsonRpcErrorException (message: $ e ->getMessage (), code: JsonRpcErrorCode::INTERNAL_ERROR );
0 commit comments