|
8 | 8 | use Laravel\Boost\Mcp\ToolRegistry; |
9 | 9 | use Laravel\Mcp\Request; |
10 | 10 | use Laravel\Mcp\Response; |
| 11 | +use Laravel\Mcp\Server\Tool; |
| 12 | +use Throwable; |
11 | 13 |
|
12 | 14 | class ExecuteToolCommand extends Command |
13 | 15 | { |
@@ -37,30 +39,34 @@ public function handle(): int |
37 | 39 | return 1; |
38 | 40 | } |
39 | 41 |
|
40 | | - try { |
41 | | - // Execute the tool |
42 | | - $tool = app($toolClass); |
43 | | - |
44 | | - $request = new Request($arguments ?? []); |
45 | | - $response = $tool->handle($request); |
| 42 | + /** @var Tool $tool */ |
| 43 | + $tool = app($toolClass); |
46 | 44 |
|
47 | | - // Output the result as JSON for the parent process |
48 | | - echo json_encode([ |
49 | | - 'isError' => $response->isError(), |
50 | | - 'content' => (string) $response->content(), |
51 | | - ]); |
| 45 | + $request = new Request($arguments ?? []); |
52 | 46 |
|
53 | | - return 0; |
54 | | - |
55 | | - } catch (\Throwable $e) { |
56 | | - // Output error result |
| 47 | + try { |
| 48 | + /** @var Response $response */ |
| 49 | + $response = $tool->handle($request); |
| 50 | + } catch (Throwable $e) { |
57 | 51 | $errorResult = Response::error("Tool execution failed (E_THROWABLE): {$e->getMessage()}"); |
| 52 | + |
58 | 53 | $this->error(json_encode([ |
59 | 54 | 'isError' => true, |
60 | | - 'content' => (string) $errorResult->content(), |
| 55 | + 'content' => [ |
| 56 | + $errorResult->content()->toTool($tool), |
| 57 | + ], |
61 | 58 | ])); |
62 | 59 |
|
63 | | - return 1; |
| 60 | + return static::FAILURE; |
64 | 61 | } |
| 62 | + |
| 63 | + echo json_encode([ |
| 64 | + 'isError' => $response->isError(), |
| 65 | + 'content' => [ |
| 66 | + $response->content()->toTool($tool), |
| 67 | + ], |
| 68 | + ]); |
| 69 | + |
| 70 | + return static::SUCCESS; |
65 | 71 | } |
66 | 72 | } |
0 commit comments