|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OPGG\LaravelMcpServer\Services\ResourceService\Examples; |
| 4 | + |
| 5 | +use OPGG\LaravelMcpServer\Services\ResourceService\ResourceTemplate; |
| 6 | + |
| 7 | +/** |
| 8 | + * Example ResourceTemplate that demonstrates how to create dynamic user resources. |
| 9 | + * This solves the problem described in GitHub discussion #32. |
| 10 | + * |
| 11 | + * Usage: |
| 12 | + * - Template URI: "database://users/{id}" |
| 13 | + * - Client can request: "database://users/123" to get user with ID 123 |
| 14 | + * - The read() method will be called with ['id' => '123'] as parameters |
| 15 | + */ |
| 16 | +class UserResourceTemplate extends ResourceTemplate |
| 17 | +{ |
| 18 | + public string $uriTemplate = 'database://users/{id}'; |
| 19 | + |
| 20 | + public string $name = 'User by ID'; |
| 21 | + |
| 22 | + public ?string $description = 'Access individual user details by user ID'; |
| 23 | + |
| 24 | + public ?string $mimeType = 'application/json'; |
| 25 | + |
| 26 | + /** |
| 27 | + * List all available user resources. |
| 28 | + * |
| 29 | + * This method returns a list of concrete user resources that can be accessed |
| 30 | + * through this template. In a real implementation, you would query your database |
| 31 | + * to get all available users. |
| 32 | + * |
| 33 | + * @return array Array of user resource definitions |
| 34 | + */ |
| 35 | + public function list(): ?array |
| 36 | + { |
| 37 | + // In a real implementation, you would query your database: |
| 38 | + // $users = User::select(['id', 'name'])->get(); |
| 39 | + // |
| 40 | + // For this example, we'll return mock data: |
| 41 | + $users = [ |
| 42 | + ['id' => 1, 'name' => 'Alice'], |
| 43 | + ['id' => 2, 'name' => 'Bob'], |
| 44 | + ['id' => 3, 'name' => 'Charlie'], |
| 45 | + ]; |
| 46 | + |
| 47 | + $resources = []; |
| 48 | + foreach ($users as $user) { |
| 49 | + $resources[] = [ |
| 50 | + 'uri' => "database://users/{$user['id']}", |
| 51 | + 'name' => "User: {$user['name']}", |
| 52 | + 'description' => "Profile data for user {$user['name']} (ID: {$user['id']})", |
| 53 | + 'mimeType' => $this->mimeType, |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + return $resources; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Read user data for the specified user ID. |
| 62 | + * |
| 63 | + * In a real implementation, this would: |
| 64 | + * 1. Extract the user ID from the parameters |
| 65 | + * 2. Query the database to fetch user details |
| 66 | + * 3. Return the user data as JSON |
| 67 | + * |
| 68 | + * @param string $uri The full URI being requested (e.g., "database://users/123") |
| 69 | + * @param array $params Extracted parameters (e.g., ['id' => '123']) |
| 70 | + * @return array Resource content with uri, mimeType, and text/blob |
| 71 | + */ |
| 72 | + public function read(string $uri, array $params): array |
| 73 | + { |
| 74 | + $userId = $params['id'] ?? null; |
| 75 | + |
| 76 | + if ($userId === null) { |
| 77 | + return [ |
| 78 | + 'uri' => $uri, |
| 79 | + 'mimeType' => 'application/json', |
| 80 | + 'text' => json_encode(['error' => 'Missing user ID'], JSON_PRETTY_PRINT), |
| 81 | + ]; |
| 82 | + } |
| 83 | + |
| 84 | + // In a real implementation, you would query your database here: |
| 85 | + // $user = User::find($userId); |
| 86 | + // |
| 87 | + // For this example, we'll return mock data: |
| 88 | + $userData = [ |
| 89 | + 'id' => (int) $userId, |
| 90 | + 'name' => "User {$userId}", |
| 91 | + 'email' => "user{$userId}@example.com", |
| 92 | + 'created_at' => '2024-01-01T00:00:00Z', |
| 93 | + 'profile' => [ |
| 94 | + 'bio' => "This is the bio for user {$userId}", |
| 95 | + 'location' => 'Example City', |
| 96 | + ], |
| 97 | + ]; |
| 98 | + |
| 99 | + return [ |
| 100 | + 'uri' => $uri, |
| 101 | + 'mimeType' => $this->mimeType, |
| 102 | + 'text' => json_encode($userData, JSON_PRETTY_PRINT), |
| 103 | + ]; |
| 104 | + } |
| 105 | +} |
0 commit comments