Skip to content

Commit fbcecbc

Browse files
committed
refactor: fix HTTP request handler
1 parent 8da739e commit fbcecbc

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

src/Transports/HttpServer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public function listen(callable $onRequest, callable $onClose): void
109109

110110
public function onTick(\Closure $onTick): void
111111
{
112-
$this->loop->futureTick($onTick($this));
112+
$this->loop->futureTick(function () use ($onTick): void {
113+
$onTick($this);
114+
});
113115
}
114116

115117
public function isClosing(): bool

src/Transports/HttpServerTransport.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpMcp\Schema\JsonRpc\Error;
1414
use PhpMcp\Schema\JsonRpc\Message;
1515
use PhpMcp\Schema\JsonRpc\Parser;
16+
use Psr\Http\Message\ResponseInterface;
1617
use Psr\Http\Message\ServerRequestInterface;
1718
use Psr\Log\LoggerInterface;
1819
use Psr\Log\NullLogger;
@@ -126,24 +127,22 @@ public function emit($event, array $arguments = [])
126127
return $this->httpServer->emit($event, $arguments);
127128
}
128129

129-
private function createRequestHandler(): callable
130+
private function createRequestHandler(ServerRequestInterface $request): ResponseInterface
130131
{
131-
return function (ServerRequestInterface $request) {
132-
$path = $request->getUri()->getPath();
133-
$method = $request->getMethod();
132+
$path = $request->getUri()->getPath();
133+
$method = $request->getMethod();
134134

135-
if ($method === 'GET' && $path === $this->ssePath) {
136-
return $this->handleSseRequest($request);
137-
}
135+
if ($method === 'GET' && $path === $this->ssePath) {
136+
return $this->handleSseRequest($request);
137+
}
138138

139-
if ($method === 'POST' && $path === $this->messagePath) {
140-
return $this->handleMessagePostRequest($request);
141-
}
139+
if ($method === 'POST' && $path === $this->messagePath) {
140+
return $this->handleMessagePostRequest($request);
141+
}
142142

143-
$this->logger->debug('404 Not Found', ['method' => $method, 'path' => $path]);
143+
$this->logger->debug('404 Not Found', ['method' => $method, 'path' => $path]);
144144

145-
return new Response(404, ['Content-Type' => 'text/plain'], 'Not Found');
146-
};
145+
return new Response(404, ['Content-Type' => 'text/plain'], 'Not Found');
147146
}
148147

149148
/**

src/Transports/StreamableHttpServerTransport.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpMcp\Schema\JsonRpc\Parser;
2020
use PhpMcp\Schema\JsonRpc\Request;
2121
use PhpMcp\Schema\JsonRpc\Response;
22+
use Psr\Http\Message\ResponseInterface;
2223
use Psr\Http\Message\ServerRequestInterface;
2324
use Psr\Log\LoggerInterface;
2425
use Psr\Log\NullLogger;
@@ -248,28 +249,26 @@ public function emit($event, array $arguments = [])
248249
return $this->httpServer->emit($event, $arguments);
249250
}
250251

251-
private function createRequestHandler(): callable
252+
private function createRequestHandler(ServerRequestInterface $request): ResponseInterface
252253
{
253-
return function (ServerRequestInterface $request) {
254-
$path = $request->getUri()->getPath();
255-
$method = $request->getMethod();
254+
$path = $request->getUri()->getPath();
255+
$method = $request->getMethod();
256256

257-
if ($path !== $this->httpServer->mcpPath()) {
258-
$error = Error::forInvalidRequest("Not found: {$path}");
259-
return new HttpResponse(404, ['Content-Type' => 'application/json'], \json_encode($error));
260-
}
257+
if ($path !== $this->httpServer->mcpPath()) {
258+
$error = Error::forInvalidRequest("Not found: {$path}");
259+
return new HttpResponse(404, ['Content-Type' => 'application/json'], \json_encode($error));
260+
}
261261

262-
try {
263-
return match ($method) {
264-
'GET' => $this->handleGetRequest($request),
265-
'POST' => $this->handlePostRequest($request),
266-
'DELETE' => $this->handleDeleteRequest($request),
267-
default => $this->handleUnsupportedRequest($request),
268-
};
269-
} catch (\Throwable $e) {
270-
return $this->handleRequestError($e, $request);
271-
}
272-
};
262+
try {
263+
return match ($method) {
264+
'GET' => $this->handleGetRequest($request),
265+
'POST' => $this->handlePostRequest($request),
266+
'DELETE' => $this->handleDeleteRequest($request),
267+
default => $this->handleUnsupportedRequest($request),
268+
};
269+
} catch (\Throwable $e) {
270+
return $this->handleRequestError($e, $request);
271+
}
273272
}
274273

275274
private function handleGetRequest(ServerRequestInterface $request): PromiseInterface

0 commit comments

Comments
 (0)