|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Example\HttpUserProfileExample; |
| 13 | + |
| 14 | +use Mcp\Capability\Attribute\CompletionProvider; |
| 15 | +use Mcp\Capability\Attribute\McpPrompt; |
| 16 | +use Mcp\Capability\Attribute\McpResource; |
| 17 | +use Mcp\Capability\Attribute\McpResourceTemplate; |
| 18 | +use Mcp\Capability\Attribute\McpTool; |
| 19 | +use Psr\Log\LoggerInterface; |
| 20 | + |
| 21 | +class McpElements |
| 22 | +{ |
| 23 | + // Simulate a simple user database |
| 24 | + private array $users = [ |
| 25 | + '101' => [ 'name' => 'Alice', 'email' => '[email protected]', 'role' => 'admin'], |
| 26 | + '102' => [ 'name' => 'Bob', 'email' => '[email protected]', 'role' => 'user'], |
| 27 | + '103' => [ 'name' => 'Charlie', 'email' => '[email protected]', 'role' => 'user'], |
| 28 | + ]; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private LoggerInterface $logger, |
| 32 | + ) { |
| 33 | + $this->logger->debug('HttpUserProfileExample McpElements instantiated.'); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Retrieves the profile data for a specific user. |
| 38 | + * |
| 39 | + * @param string $userId the ID of the user (from URI) |
| 40 | + * |
| 41 | + * @return array user profile data |
| 42 | + * |
| 43 | + * @throws McpServerException if the user is not found |
| 44 | + */ |
| 45 | + #[McpResourceTemplate( |
| 46 | + uriTemplate: 'user://{userId}/profile', |
| 47 | + name: 'user_profile', |
| 48 | + description: 'Get profile information for a specific user ID.', |
| 49 | + mimeType: 'application/json' |
| 50 | + )] |
| 51 | + public function getUserProfile( |
| 52 | + #[CompletionProvider(values: ['101', '102', '103'])] |
| 53 | + string $userId, |
| 54 | + ): array { |
| 55 | + $this->logger->info('Reading resource: user profile', ['userId' => $userId]); |
| 56 | + if (!isset($this->users[$userId])) { |
| 57 | + // Throwing an exception that Processor can turn into an error response |
| 58 | + throw McpServerException::invalidParams("User profile not found for ID: {$userId}"); |
| 59 | + } |
| 60 | + |
| 61 | + return $this->users[$userId]; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Retrieves a list of all known user IDs. |
| 66 | + * |
| 67 | + * @return array list of user IDs |
| 68 | + */ |
| 69 | + #[McpResource( |
| 70 | + uri: 'user://list/ids', |
| 71 | + name: 'user_id_list', |
| 72 | + description: 'Provides a list of all available user IDs.', |
| 73 | + mimeType: 'application/json' |
| 74 | + )] |
| 75 | + public function listUserIds(): array |
| 76 | + { |
| 77 | + $this->logger->info('Reading resource: user ID list'); |
| 78 | + |
| 79 | + return array_keys($this->users); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Sends a welcome message to a user. |
| 84 | + * (This is a placeholder - in a real app, it might queue an email). |
| 85 | + * |
| 86 | + * @param string $userId the ID of the user to message |
| 87 | + * @param string|null $customMessage an optional custom message part |
| 88 | + * |
| 89 | + * @return array status of the operation |
| 90 | + */ |
| 91 | + #[McpTool(name: 'send_welcome')] |
| 92 | + public function sendWelcomeMessage(string $userId, ?string $customMessage = null): array |
| 93 | + { |
| 94 | + $this->logger->info('Executing tool: send_welcome', ['userId' => $userId]); |
| 95 | + if (!isset($this->users[$userId])) { |
| 96 | + return ['success' => false, 'error' => "User ID {$userId} not found."]; |
| 97 | + } |
| 98 | + $user = $this->users[$userId]; |
| 99 | + $message = "Welcome, {$user['name']}!"; |
| 100 | + if ($customMessage) { |
| 101 | + $message .= ' '.$customMessage; |
| 102 | + } |
| 103 | + // Simulate sending |
| 104 | + $this->logger->info("Simulated sending message to {$user['email']}: {$message}"); |
| 105 | + |
| 106 | + return ['success' => true, 'message_sent' => $message]; |
| 107 | + } |
| 108 | + |
| 109 | + #[McpTool(name: 'test_tool_without_params')] |
| 110 | + public function testToolWithoutParams(): array |
| 111 | + { |
| 112 | + return ['success' => true, 'message' => 'Test tool without params']; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Generates a prompt to write a bio for a user. |
| 117 | + * |
| 118 | + * @param string $userId the user ID to generate the bio for |
| 119 | + * @param string $tone Desired tone (e.g., 'formal', 'casual'). |
| 120 | + * |
| 121 | + * @return array prompt messages |
| 122 | + * |
| 123 | + * @throws McpServerException if user not found |
| 124 | + */ |
| 125 | + #[McpPrompt(name: 'generate_bio_prompt')] |
| 126 | + public function generateBio( |
| 127 | + #[CompletionProvider(provider: UserIdCompletionProvider::class)] |
| 128 | + string $userId, |
| 129 | + string $tone = 'professional', |
| 130 | + ): array { |
| 131 | + $this->logger->info('Executing prompt: generate_bio', ['userId' => $userId, 'tone' => $tone]); |
| 132 | + if (!isset($this->users[$userId])) { |
| 133 | + throw McpServerException::invalidParams("User not found for bio prompt: {$userId}"); |
| 134 | + } |
| 135 | + $user = $this->users[$userId]; |
| 136 | + |
| 137 | + return [ |
| 138 | + ['role' => 'user', 'content' => "Write a short, {$tone} biography for {$user['name']} (Role: {$user['role']}, Email: {$user['email']}). Highlight their role within the system."], |
| 139 | + ]; |
| 140 | + } |
| 141 | +} |
0 commit comments