Skip to content

Commit fe19a07

Browse files
authored
Merge pull request #21 from laravel/cleanup_tools_for_consistency
Refactor Tools to improve consistency and readability
2 parents ab09251 + 8f90e94 commit fe19a07

15 files changed

+82
-92
lines changed

src/Mcp/Methods/CallToolWithExecutor.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,43 @@
44

55
namespace Laravel\Boost\Mcp\Methods;
66

7-
use Generator;
87
use Illuminate\Support\ItemNotFoundException;
98
use Laravel\Boost\Mcp\ToolExecutor;
109
use Laravel\Mcp\Server\Contracts\Methods\Method;
1110
use Laravel\Mcp\Server\ServerContext;
1211
use Laravel\Mcp\Server\Tools\ToolResult;
13-
use Laravel\Mcp\Server\Transport\JsonRpcNotification;
1412
use Laravel\Mcp\Server\Transport\JsonRpcRequest;
1513
use Laravel\Mcp\Server\Transport\JsonRpcResponse;
14+
use Throwable;
1615

1716
class CallToolWithExecutor implements Method
1817
{
1918
/**
2019
* Handle the JSON-RPC tool/call request with process isolation.
2120
*
22-
* @return JsonRpcResponse|Generator<JsonRpcNotification|JsonRpcResponse>
21+
* @param JsonRpcRequest $request
22+
* @param ServerContext $context
23+
* @return JsonRpcResponse
2324
*/
24-
public function handle(JsonRpcRequest $request, ServerContext $context)
25+
public function handle(JsonRpcRequest $request, ServerContext $context): JsonRpcResponse
2526
{
2627
try {
27-
$tool = $context->tools()
28-
->firstOrFail(fn ($tool) => $tool->name() === $request->params['name']);
29-
} catch (ItemNotFoundException $e) {
28+
$tool = $context->tools()->firstOrFail(fn ($tool) => $tool->name() === $request->params['name']);
29+
} catch (ItemNotFoundException) {
3030
return JsonRpcResponse::create(
3131
$request->id,
3232
ToolResult::error('Tool not found')
3333
);
34-
} catch (\Throwable $e) {
34+
} catch (Throwable $e) {
3535
return JsonRpcResponse::create(
3636
$request->id,
3737
ToolResult::error('Error finding tool: '.$e->getMessage())
3838
);
3939
}
4040

4141
try {
42-
// Use ToolExecutor instead of calling tool directly
4342
$executor = app(ToolExecutor::class);
4443

45-
// Safely get arguments
4644
$arguments = [];
4745
if (isset($request->params['arguments']) && is_array($request->params['arguments'])) {
4846
$arguments = $request->params['arguments'];
@@ -52,7 +50,7 @@ public function handle(JsonRpcRequest $request, ServerContext $context)
5250

5351
return JsonRpcResponse::create($request->id, $result);
5452

55-
} catch (\Throwable $e) {
53+
} catch (Throwable $e) {
5654
return JsonRpcResponse::create(
5755
$request->id,
5856
ToolResult::error('Tool execution error: '.$e->getMessage())

src/Mcp/Tools/BrowserLogs.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
namespace Laravel\Boost\Mcp\Tools;
66

7-
use Illuminate\Support\Facades\Log;
87
use Laravel\Boost\Concerns\ReadsLogs;
98
use Laravel\Mcp\Server\Tool;
109
use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly;
1110
use Laravel\Mcp\Server\Tools\ToolInputSchema;
1211
use Laravel\Mcp\Server\Tools\ToolResult;
1312

14-
#[IsReadOnly()]
13+
#[IsReadOnly]
1514
class BrowserLogs extends Tool
1615
{
1716
use ReadsLogs;
@@ -41,7 +40,7 @@ public function handle(array $arguments): ToolResult
4140
return ToolResult::error('The "entries" argument must be greater than 0.');
4241
}
4342

44-
// Locate the correct log file using shared helper.
43+
// Locate the correct log file using the shared helper.
4544
$logFile = storage_path('logs/browser.log');
4645

4746
if (! file_exists($logFile)) {

src/Mcp/Tools/DatabaseConnections.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Laravel\Mcp\Server\Tools\ToolInputSchema;
1010
use Laravel\Mcp\Server\Tools\ToolResult;
1111

12-
#[IsReadOnly()]
12+
#[IsReadOnly]
1313
class DatabaseConnections extends Tool
1414
{
1515
public function description(): string
@@ -19,7 +19,6 @@ public function description(): string
1919

2020
public function schema(ToolInputSchema $schema): ToolInputSchema
2121
{
22-
// No inputs required for this tool.
2322
return $schema;
2423
}
2524

src/Mcp/Tools/DatabaseQuery.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly;
1010
use Laravel\Mcp\Server\Tools\ToolInputSchema;
1111
use Laravel\Mcp\Server\Tools\ToolResult;
12+
use Throwable;
1213

13-
#[IsReadOnly()]
14+
#[IsReadOnly]
1415
class DatabaseQuery extends Tool
1516
{
1617
/**
@@ -56,9 +57,9 @@ public function handle(array $arguments): ToolResult
5657
'EXPLAIN',
5758
'DESCRIBE',
5859
'DESC',
59-
'WITH', // Common-table expressions, must be followed by SELECT
60+
'WITH', // SELECT must follow Common-table expressions
6061
'VALUES', // Returns literal values
61-
'TABLE', // PostgreSQL shorthand for SELECT *
62+
'TABLE', // PostgresSQL shorthand for SELECT *
6263
];
6364

6465
$isReadOnly = in_array($firstWord, $allowList, true);
@@ -80,7 +81,7 @@ public function handle(array $arguments): ToolResult
8081
return ToolResult::json(
8182
DB::connection($connectionName)->select($query)
8283
);
83-
} catch (\Throwable $e) {
84+
} catch (Throwable $e) {
8485
return ToolResult::error('Query failed: '.$e->getMessage());
8586
}
8687
}

src/Mcp/Tools/DatabaseSchema.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Laravel\Boost\Mcp\Tools;
66

7+
use Exception;
8+
use Illuminate\Support\Arr;
79
use Illuminate\Support\Facades\Cache;
810
use Illuminate\Support\Facades\DB;
911
use Illuminate\Support\Facades\Log;
@@ -14,7 +16,7 @@
1416
use Laravel\Mcp\Server\Tools\ToolInputSchema;
1517
use Laravel\Mcp\Server\Tools\ToolResult;
1618

17-
#[IsReadOnly()]
19+
#[IsReadOnly]
1820
class DatabaseSchema extends Tool
1921
{
2022
public function description(): string
@@ -42,7 +44,7 @@ public function handle(array $arguments): ToolResult
4244
{
4345
$connection = $arguments['database'] ?? config('database.default');
4446
$filter = $arguments['filter'] ?? '';
45-
$cacheKey = "boost:mcp:database-schema:{$connection}:{$filter}";
47+
$cacheKey = "boost:mcp:database-schema:$connection:$filter";
4648

4749
$schema = Cache::remember($cacheKey, 20, function () use ($connection, $filter) {
4850
return $this->getDatabaseStructure($connection, $filter);
@@ -53,13 +55,11 @@ public function handle(array $arguments): ToolResult
5355

5456
protected function getDatabaseStructure(?string $connection, string $filter = ''): array
5557
{
56-
$structure = [
58+
return [
5759
'engine' => DB::connection($connection)->getDriverName(),
5860
'tables' => $this->getAllTablesStructure($connection, $filter),
5961
'global' => $this->getGlobalStructure($connection),
6062
];
61-
62-
return $structure;
6363
}
6464

6565
protected function getAllTablesStructure(?string $connection, string $filter = ''): array
@@ -102,7 +102,7 @@ protected function getTableStructure(?string $connection, string $tableName): ar
102102
'triggers' => $triggers,
103103
'check_constraints' => $checkConstraints,
104104
];
105-
} catch (\Exception $e) {
105+
} catch (Exception $e) {
106106
Log::error('Failed to get table structure for: '.$tableName, [
107107
'error' => $e->getMessage(),
108108
'trace' => $e->getTraceAsString(),
@@ -136,15 +136,15 @@ protected function getTableIndexes(?string $connection, string $tableName): arra
136136

137137
foreach ($indexes as $index) {
138138
$indexDetails[$index['name']] = [
139-
'columns' => $index['columns'],
140-
'type' => $index['type'] ?? null,
141-
'is_unique' => $index['unique'] ?? false,
142-
'is_primary' => $index['primary'] ?? false,
139+
'columns' => Arr::get($index, 'columns'),
140+
'type' => Arr::get($index, 'type'),
141+
'is_unique' => Arr::get($index, 'unique', false),
142+
'is_primary' => Arr::get($index, 'primary', false),
143143
];
144144
}
145145

146146
return $indexDetails;
147-
} catch (\Exception $e) {
147+
} catch (Exception) {
148148
return [];
149149
}
150150
}
@@ -153,7 +153,7 @@ protected function getTableForeignKeys(?string $connection, string $tableName):
153153
{
154154
try {
155155
return Schema::connection($connection)->getForeignKeys($tableName);
156-
} catch (\Exception $e) {
156+
} catch (Exception) {
157157
return [];
158158
}
159159
}

src/Mcp/Tools/DatabaseSchema/MySQLSchemaDriver.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Laravel\Boost\Mcp\Tools\DatabaseSchema;
66

7+
use Exception;
78
use Illuminate\Support\Facades\DB;
89

910
class MySQLSchemaDriver extends DatabaseSchemaDriver
@@ -16,7 +17,7 @@ public function getViews(): array
1617
FROM information_schema.VIEWS
1718
WHERE TABLE_SCHEMA = DATABASE()
1819
');
19-
} catch (\Exception $e) {
20+
} catch (Exception) {
2021
return [];
2122
}
2223
}
@@ -25,7 +26,7 @@ public function getStoredProcedures(): array
2526
{
2627
try {
2728
return DB::connection($this->connection)->select('SHOW PROCEDURE STATUS WHERE Db = DATABASE()');
28-
} catch (\Exception $e) {
29+
} catch (Exception) {
2930
return [];
3031
}
3132
}
@@ -34,7 +35,7 @@ public function getFunctions(): array
3435
{
3536
try {
3637
return DB::connection($this->connection)->select('SHOW FUNCTION STATUS WHERE Db = DATABASE()');
37-
} catch (\Exception $e) {
38+
} catch (Exception) {
3839
return [];
3940
}
4041
}
@@ -47,7 +48,7 @@ public function getTriggers(?string $table = null): array
4748
}
4849

4950
return DB::connection($this->connection)->select('SHOW TRIGGERS');
50-
} catch (\Exception $e) {
51+
} catch (Exception) {
5152
return [];
5253
}
5354
}
@@ -61,7 +62,7 @@ public function getCheckConstraints(string $table): array
6162
WHERE CONSTRAINT_SCHEMA = DATABASE()
6263
AND TABLE_NAME = ?
6364
', [$table]);
64-
} catch (\Exception $e) {
65+
} catch (Exception) {
6566
return [];
6667
}
6768
}

src/Mcp/Tools/DatabaseSchema/PostgreSQLSchemaDriver.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Laravel\Boost\Mcp\Tools\DatabaseSchema;
66

7+
use Exception;
78
use Illuminate\Support\Facades\DB;
89

910
class PostgreSQLSchemaDriver extends DatabaseSchemaDriver
@@ -12,11 +13,11 @@ public function getViews(): array
1213
{
1314
try {
1415
return DB::connection($this->connection)->select("
15-
SELECT schemaname, viewname, definition
16-
FROM pg_views
16+
SELECT schemaname, viewname, definition
17+
FROM pg_views
1718
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
1819
");
19-
} catch (\Exception $e) {
20+
} catch (Exception) {
2021
return [];
2122
}
2223
}
@@ -31,7 +32,7 @@ public function getStoredProcedures(): array
3132
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
3233
AND prokind = 'p'
3334
");
34-
} catch (\Exception $e) {
35+
} catch (Exception) {
3536
return [];
3637
}
3738
}
@@ -46,7 +47,7 @@ public function getFunctions(): array
4647
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
4748
AND prokind = 'f'
4849
");
49-
} catch (\Exception $e) {
50+
} catch (Exception) {
5051
return [];
5152
}
5253
}
@@ -66,7 +67,7 @@ public function getTriggers(?string $table = null): array
6667
}
6768

6869
return DB::connection($this->connection)->select($sql);
69-
} catch (\Exception $e) {
70+
} catch (Exception) {
7071
return [];
7172
}
7273
}
@@ -76,11 +77,11 @@ public function getCheckConstraints(string $table): array
7677
try {
7778
return DB::connection($this->connection)->select("
7879
SELECT conname, pg_get_constraintdef(oid) as definition
79-
FROM pg_constraint
80-
WHERE contype = 'c'
80+
FROM pg_constraint
81+
WHERE contype = 'c'
8182
AND conrelid = ?::regclass
8283
", [$table]);
83-
} catch (\Exception $e) {
84+
} catch (Exception) {
8485
return [];
8586
}
8687
}
@@ -93,7 +94,7 @@ public function getSequences(): array
9394
FROM information_schema.sequences
9495
WHERE sequence_schema = current_schema()
9596
');
96-
} catch (\Exception $e) {
97+
} catch (Exception) {
9798
return [];
9899
}
99100
}

src/Mcp/Tools/DatabaseSchema/SQLiteSchemaDriver.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Laravel\Boost\Mcp\Tools\DatabaseSchema;
66

7+
use Exception;
78
use Illuminate\Support\Facades\DB;
89

910
class SQLiteSchemaDriver extends DatabaseSchemaDriver
@@ -12,11 +13,11 @@ public function getViews(): array
1213
{
1314
try {
1415
return DB::connection($this->connection)->select("
15-
SELECT name, sql
16-
FROM sqlite_master
16+
SELECT name, sql
17+
FROM sqlite_master
1718
WHERE type = 'view'
1819
");
19-
} catch (\Exception $e) {
20+
} catch (Exception) {
2021
return [];
2122
}
2223
}
@@ -42,7 +43,7 @@ public function getTriggers(?string $table = null): array
4243
}
4344

4445
return DB::connection($this->connection)->select($sql);
45-
} catch (\Exception $e) {
46+
} catch (Exception) {
4647
return [];
4748
}
4849
}

0 commit comments

Comments
 (0)