Skip to content

Instantly share code, notes, and snippets.

@ihsanberahim
Created May 18, 2025 10:03
Show Gist options
  • Save ihsanberahim/6506a5bfa9da1d848d9625cd5df88c76 to your computer and use it in GitHub Desktop.
Save ihsanberahim/6506a5bfa9da1d848d9625cd5df88c76 to your computer and use it in GitHub Desktop.
`tinkering` is a mcp tools
<?php
namespace App\Mcp\Tools;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Validator;
use OPGG\LaravelMcpServer\Services\ToolService\ToolInterface;
use Symfony\Component\Console\Output\BufferedOutput;
class TinkeringTool implements ToolInterface
{
/**
* Get the tool name.
*/
public function getName(): string
{
return 'tinkering';
}
/**
* Get the tool description.
*/
public function getDescription(): string
{
return <<<'TXT'
- Functions
- Use Laravel Eloquent Query Builder
- Database SQL Query
- Data mapping
- Data insertion
- Data update
- Output in JSON format
- Rules
- DO NOT include comments
- ALWAYS use `DB::transaction` to wrap the code
- ALWAYS new line after each statement
TXT;
}
/**
* Get the input schema for the tool.
*/
public function getInputSchema(): array
{
return [
'type' => 'object',
'properties' => [
'code' => [
'type' => 'string',
'description' => <<<TXT
Example:
- Laravel Eloquent Query Builder
- `\App\Models\User::all()`
- Database SQL Query
- `DB::select('SELECT * FROM users;')`
- Data Mapping
- Data Insertion
- `\App\Models\User::create(['name' => 'John Doe', 'email' => '[email protected]']);`
- Data Update
- `\App\Models\User::where('id', 1)->update(['name' => 'John Doe', 'email' => '[email protected]']);`
TXT,
],
// Add more parameters as needed
],
'required' => ['code'],
];
}
/**
* Get the tool annotations.
*/
public function getAnnotations(): array
{
return [
'title' => 'Tinkering Tool',
'readOnlyHint' => false,
'destructiveHint' => false,
'openWorldHint' => true,
];
}
/**
* Execute the tool.
*
* @param array $arguments Tool arguments
* @return mixed
*/
public function execute(array $arguments): string
{
Validator::make($arguments, [
'code' => ['required', 'string'],
// Add more validation rules as needed
])->validate();
$code = $arguments['code'] ?? '';
$output = new BufferedOutput;
// Implement your tool logic here
Artisan::call('tinker', ['--execute' => '
$result = '.$code.';
if(is_array($result)) {
echo json_encode($result);
} else {
echo $result;
}
'], $output);
return json_encode([
'output' => str(
$output->fetch()
)->replaceMatches('/^\[!\].*$/m', '')->trim()->value(),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment