|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App; |
| 4 | + |
| 5 | +use Mcp\Capability\Attribute\McpPrompt; |
| 6 | +use Mcp\Capability\Attribute\McpResource; |
| 7 | +use Mcp\Capability\Attribute\McpTool; |
| 8 | + |
| 9 | +class McpElements |
| 10 | +{ |
| 11 | + public function __construct() {} |
| 12 | + |
| 13 | + /** |
| 14 | + * Get the current server time |
| 15 | + */ |
| 16 | + #[McpTool(name: 'current_time')] |
| 17 | + public function getCurrentTime(string $format = 'Y-m-d H:i:s'): string |
| 18 | + { |
| 19 | + try { |
| 20 | + return date($format); |
| 21 | + } catch (\Exception $e) { |
| 22 | + return date('Y-m-d H:i:s'); // fallback |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Calculate simple math operations |
| 28 | + */ |
| 29 | + #[McpTool(name: 'calculate')] |
| 30 | + public function calculate(float $a, float $b, string $operation): float|string |
| 31 | + { |
| 32 | + return match (strtolower($operation)) { |
| 33 | + 'add', '+' => $a + $b, |
| 34 | + 'subtract', '-' => $a - $b, |
| 35 | + 'multiply', '*' => $a * $b, |
| 36 | + 'divide', '/' => $b != 0 ? $a / $b : 'Error: Division by zero', |
| 37 | + default => 'Error: Unknown operation. Use: add, subtract, multiply, divide' |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Server information resource |
| 43 | + */ |
| 44 | + #[McpResource( |
| 45 | + uri: 'info://server/status', |
| 46 | + name: 'server_status', |
| 47 | + description: 'Current server status and information', |
| 48 | + mimeType: 'application/json' |
| 49 | + )] |
| 50 | + public function getServerStatus(): array |
| 51 | + { |
| 52 | + return [ |
| 53 | + 'status' => 'running', |
| 54 | + 'timestamp' => time(), |
| 55 | + 'version' => '1.0.0', |
| 56 | + 'transport' => 'HTTP', |
| 57 | + 'uptime' => time() - $_SERVER['REQUEST_TIME'] |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Configuration resource |
| 63 | + */ |
| 64 | + #[McpResource( |
| 65 | + uri: 'config://app/settings', |
| 66 | + name: 'app_config', |
| 67 | + description: 'Application configuration settings', |
| 68 | + mimeType: 'application/json' |
| 69 | + )] |
| 70 | + public function getAppConfig(): array |
| 71 | + { |
| 72 | + return [ |
| 73 | + 'debug' => $_SERVER['DEBUG'] ?? false, |
| 74 | + 'environment' => $_SERVER['APP_ENV'] ?? 'production', |
| 75 | + 'timezone' => date_default_timezone_get(), |
| 76 | + 'locale' => 'en_US' |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Greeting prompt |
| 82 | + */ |
| 83 | + #[McpPrompt( |
| 84 | + name: 'greet', |
| 85 | + description: 'Generate a personalized greeting message' |
| 86 | + )] |
| 87 | + public function greetPrompt(string $firstName = 'World', string $timeOfDay = 'day'): array |
| 88 | + { |
| 89 | + $greeting = match (strtolower($timeOfDay)) { |
| 90 | + 'morning' => 'Good morning', |
| 91 | + 'afternoon' => 'Good afternoon', |
| 92 | + 'evening', 'night' => 'Good evening', |
| 93 | + default => 'Hello' |
| 94 | + }; |
| 95 | + |
| 96 | + return [ |
| 97 | + 'role' => 'user', |
| 98 | + 'content' => "# {$greeting}, {$firstName}!\n\nWelcome to our MCP HTTP Server example. This demonstrates how to use the Model Context Protocol over HTTP transport." |
| 99 | + ]; |
| 100 | + } |
| 101 | +} |
0 commit comments