Skip to content

Commit d424c2d

Browse files
committed
refactor
1 parent a691ca1 commit d424c2d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Mcp/Methods/CallToolWithExecutor.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@
55
namespace Laravel\Boost\Mcp\Methods;
66

77
use Laravel\Boost\Mcp\ToolExecutor;
8+
use Laravel\Mcp\Response;
89
use Laravel\Mcp\Server\Contracts\Errable;
910
use Laravel\Mcp\Server\Contracts\Method;
1011
use Laravel\Mcp\Server\Exceptions\JsonRpcException;
1112
use Laravel\Mcp\Server\Methods\Concerns\InteractsWithResponses;
1213
use Laravel\Mcp\Server\ServerContext;
1314
use Laravel\Mcp\Server\Transport\JsonRpcRequest;
1415
use Laravel\Mcp\Server\Transport\JsonRpcResponse;
16+
use Throwable;
1517

1618
class CallToolWithExecutor implements Method, Errable
1719
{
1820
use InteractsWithResponses;
1921

22+
public function __construct(protected ToolExecutor $executor)
23+
{
24+
//
25+
}
26+
2027
/**
2128
* Handle the JSON-RPC tool/call request with process isolation.
2229
*/
@@ -40,14 +47,16 @@ public function handle(JsonRpcRequest $request, ServerContext $context): JsonRpc
4047
$request->id,
4148
));
4249

43-
$executor = app(ToolExecutor::class);
44-
4550
$arguments = [];
4651
if (isset($request->params['arguments']) && is_array($request->params['arguments'])) {
4752
$arguments = $request->params['arguments'];
4853
}
4954

50-
$response = $executor->execute(get_class($tool), $arguments);
55+
try {
56+
$response = $this->executor->execute(get_class($tool), $arguments);
57+
} catch (Throwable $e) {
58+
$response = Response::error('Tool execution error: '.$e->getMessage());
59+
}
5160

5261
return $this->toJsonRpcResponse($request, $response, fn ($responses) => [
5362
'content' => $responses->map(fn ($response) => $response->content()->toTool($tool))->all(),

0 commit comments

Comments
 (0)