Skip to content

Commit 52bcbad

Browse files
author
klapaudius
committed
Add PHPDoc comments and minor concatenation fix
Added PHPDoc comments to improve code documentation and clarify method purposes and parameter types across routing and protocol files. Also fixed a minor string concatenation issue for error handling in `MCPProtocol`. These changes enhance code readability and maintainability.
1 parent f336c00 commit 52bcbad

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/Protocol/MCPProtocol.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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);

src/Resources/config/routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
use KLP\KlpMcpServer\Controllers\SseController;
55
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
66

7+
/**
8+
* Configures routes for the application.
9+
*
10+
* @param RoutingConfigurator $routes The routing configurator used to define routes.
11+
* @return void
12+
*/
713
return function (RoutingConfigurator $routes) {
814
$routes->add('sse_route', '/mcp/sse')
915
->controller([SseController::class, 'handle'])

0 commit comments

Comments
 (0)