Last active
September 9, 2026 11:52
-
-
Save NandoKstroNet/f1debe2f82a0fe0e82ef624799e9280a to your computer and use it in GitHub Desktop.
MCP Laravel: Tool Last 30 Days Sales & Resource Total of All Sales - https://youtube.com/[@]codeexperts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Mcp\Tools; | |
| use App\Models\Sale; | |
| use Carbon\CarbonImmutable; | |
| use Illuminate\Contracts\JsonSchema\JsonSchema; | |
| use Illuminate\JsonSchema\Types\Type; | |
| use Laravel\Mcp\Request; | |
| use Laravel\Mcp\Response; | |
| use Laravel\Mcp\Server\Attributes\Description; | |
| use Laravel\Mcp\Server\Attributes\Name; | |
| use Laravel\Mcp\Server\Tool; | |
| #[Name('last-thirty-days-sales')] | |
| #[Description('💵 Get the sales data for the last 30 days. 💵')] | |
| class LastThirtyDaysSalesTool extends Tool | |
| { | |
| /** | |
| * Handle the tool request. | |
| */ | |
| public function handle(Request $request, Sale $sale): Response | |
| { | |
| $startDate = new CarbonImmutable($request->get('start_date')); | |
| $endDate = $startDate->subDays(30); | |
| info('Dates MCP Teste', [ | |
| 'start_date' => $startDate->toDateString(), | |
| 'end_date' => $endDate->toDateString(), | |
| ]); | |
| \DB::listen(fn($sql) => info('SQL Query: ' . $sql->sql, $sql->bindings)); | |
| return Response::json([ | |
| 'sales' => $sale->whereBetween('created_at', [$endDate, $startDate])->orderBy('created_at', 'desc')->get(), | |
| ]); | |
| } | |
| /** | |
| * Get the tool's input schema. | |
| * | |
| * @return array<string, Type> | |
| */ | |
| public function schema(JsonSchema $schema): array | |
| { | |
| return [ | |
| 'start_date' => $schema | |
| ->string() | |
| ->format('date') | |
| ->description('The start date for the sales data.') | |
| ->default(''), | |
| ]; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Mcp\Resources; | |
| use App\Models\Sale; | |
| use Laravel\Mcp\Request; | |
| use Laravel\Mcp\Response; | |
| use Laravel\Mcp\Server\Attributes\Description; | |
| use Laravel\Mcp\Server\Resource; | |
| #[Description('💵 Total of Sales - Seller System 💵')] | |
| class TotalSalesResource extends Resource | |
| { | |
| /** | |
| * Handle the resource request. | |
| */ | |
| public function handle(Request $request, Sale $sale): Response | |
| { | |
| return Response::text('O total de vendas do Sistema Seller é: R$ ' . number_format($sale->sum('total_sale') / 2, 2, ',', '.')); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment