|
| 1 | +<?php |
| 2 | + |
| 3 | +use Laravel\Mcp\Response; |
| 4 | +use Laravel\Mcp\ResponseFactory; |
| 5 | +use Laravel\Mcp\Server; |
| 6 | +use Laravel\Mcp\Server\Tool; |
| 7 | +use PHPUnit\Framework\ExpectationFailedException; |
| 8 | + |
| 9 | +class BookingServer extends Server |
| 10 | +{ |
| 11 | + protected array $tools = [ |
| 12 | + GetBookingTool::class, |
| 13 | + GetBookingWithoutStructuredContentTool::class, |
| 14 | + ]; |
| 15 | +} |
| 16 | + |
| 17 | +class GetBookingTool extends Tool |
| 18 | +{ |
| 19 | + protected string $name = 'booking/get'; |
| 20 | + |
| 21 | + protected string $title = 'Get booking'; |
| 22 | + |
| 23 | + protected string $description = 'Get a booking'; |
| 24 | + |
| 25 | + public function handle(): ResponseFactory |
| 26 | + { |
| 27 | + return Response::structured([ |
| 28 | + 'type' => 'booking', |
| 29 | + 'id' => 123, |
| 30 | + 'status' => 'confirmed', |
| 31 | + ]); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class GetBookingWithoutStructuredContentTool extends Tool |
| 36 | +{ |
| 37 | + protected string $name = 'booking/get/no-structured'; |
| 38 | + |
| 39 | + protected string $title = 'Get booking'; |
| 40 | + |
| 41 | + protected string $description = 'Get a booking'; |
| 42 | + |
| 43 | + public function handle(): Response |
| 44 | + { |
| 45 | + return Response::text('This is the confirmed booking 123'); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +it('may assert the structured content', function (): void { |
| 50 | + $response = BookingServer::tool(GetBookingTool::class); |
| 51 | + |
| 52 | + $response->assertStructuredContent([ |
| 53 | + 'type' => 'booking', |
| 54 | + 'id' => 123, |
| 55 | + 'status' => 'confirmed', |
| 56 | + ]); |
| 57 | +}); |
| 58 | + |
| 59 | +it('fails to assert the structured content is wrong', function (): void { |
| 60 | + $response = BookingServer::tool(GetBookingTool::class); |
| 61 | + |
| 62 | + $response->assertStructuredContent([ |
| 63 | + 'type' => 'booking', |
| 64 | + 'id' => 124, |
| 65 | + 'status' => 'pending', |
| 66 | + ]); |
| 67 | +})->throws(ExpectationFailedException::class); |
| 68 | + |
| 69 | +it('fails to assert the structured content is not present', function (): void { |
| 70 | + $response = BookingServer::tool(GetBookingWithoutStructuredContentTool::class); |
| 71 | + |
| 72 | + $response->assertStructuredContent([ |
| 73 | + 'type' => 'booking', |
| 74 | + 'id' => 123, |
| 75 | + 'status' => 'confirmed', |
| 76 | + ]); |
| 77 | +})->throws(ExpectationFailedException::class); |
0 commit comments