|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mcp\StdioCalculatorExample; |
| 4 | + |
| 5 | +use PhpMcp\Server\Attributes\McpResource; |
| 6 | +use PhpMcp\Server\Attributes\McpTool; |
| 7 | + |
| 8 | +class McpElements |
| 9 | +{ |
| 10 | + private array $config = [ |
| 11 | + 'precision' => 2, |
| 12 | + 'allow_negative' => true, |
| 13 | + ]; |
| 14 | + |
| 15 | + /** |
| 16 | + * Performs a calculation based on the operation. |
| 17 | + * |
| 18 | + * Supports 'add', 'subtract', 'multiply', 'divide'. |
| 19 | + * Obeys the 'precision' and 'allow_negative' settings from the config resource. |
| 20 | + * |
| 21 | + * @param float $a The first operand. |
| 22 | + * @param float $b The second operand. |
| 23 | + * @param string $operation The operation ('add', 'subtract', 'multiply', 'divide'). |
| 24 | + * @return float|string The result of the calculation, or an error message string. |
| 25 | + */ |
| 26 | + #[McpTool(name: 'calculate')] |
| 27 | + public function calculate(float $a, float $b, string $operation): float|string |
| 28 | + { |
| 29 | + // Use STDERR for logs |
| 30 | + fwrite(STDERR, "Calculate tool called: a=$a, b=$b, op=$operation\n"); |
| 31 | + |
| 32 | + $op = strtolower($operation); |
| 33 | + $result = null; |
| 34 | + |
| 35 | + switch ($op) { |
| 36 | + case 'add': |
| 37 | + $result = $a + $b; |
| 38 | + break; |
| 39 | + case 'subtract': |
| 40 | + $result = $a - $b; |
| 41 | + break; |
| 42 | + case 'multiply': |
| 43 | + $result = $a * $b; |
| 44 | + break; |
| 45 | + case 'divide': |
| 46 | + if ($b == 0) { |
| 47 | + return 'Error: Division by zero.'; |
| 48 | + } |
| 49 | + $result = $a / $b; |
| 50 | + break; |
| 51 | + default: |
| 52 | + return "Error: Unknown operation '{$operation}'. Supported: add, subtract, multiply, divide."; |
| 53 | + } |
| 54 | + |
| 55 | + if (! $this->config['allow_negative'] && $result < 0) { |
| 56 | + return 'Error: Negative results are disabled.'; |
| 57 | + } |
| 58 | + |
| 59 | + return round($result, $this->config['precision']); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Provides the current calculator configuration. |
| 64 | + * Can be read by clients to understand precision etc. |
| 65 | + * |
| 66 | + * @return array The configuration array. |
| 67 | + */ |
| 68 | + #[McpResource( |
| 69 | + uri: 'config://calculator/settings', |
| 70 | + name: 'calculator_config', |
| 71 | + description: 'Current settings for the calculator tool (precision, allow_negative).', |
| 72 | + mimeType: 'application/json' // Return as JSON |
| 73 | + )] |
| 74 | + public function getConfiguration(): array |
| 75 | + { |
| 76 | + fwrite(STDERR, "Resource config://calculator/settings read.\n"); |
| 77 | + |
| 78 | + return $this->config; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Updates a specific configuration setting. |
| 83 | + * Note: This requires more robust validation in a real app. |
| 84 | + * |
| 85 | + * @param string $setting The setting key ('precision' or 'allow_negative'). |
| 86 | + * @param mixed $value The new value (int for precision, bool for allow_negative). |
| 87 | + * @return array Success message or error. |
| 88 | + */ |
| 89 | + #[McpTool(name: 'update_setting')] |
| 90 | + public function updateSetting(string $setting, mixed $value): array |
| 91 | + { |
| 92 | + fwrite(STDERR, "Update Setting tool called: setting=$setting, value=".var_export($value, true)."\n"); |
| 93 | + if (! array_key_exists($setting, $this->config)) { |
| 94 | + return ['success' => false, 'error' => "Unknown setting '{$setting}'."]; |
| 95 | + } |
| 96 | + |
| 97 | + if ($setting === 'precision') { |
| 98 | + if (! is_int($value) || $value < 0 || $value > 10) { |
| 99 | + return ['success' => false, 'error' => 'Invalid precision value. Must be integer between 0 and 10.']; |
| 100 | + } |
| 101 | + $this->config['precision'] = $value; |
| 102 | + |
| 103 | + // In real app, notify subscribers of config://calculator/settings change |
| 104 | + // $registry->notifyResourceChanged('config://calculator/settings'); |
| 105 | + return ['success' => true, 'message' => "Precision updated to {$value}."]; |
| 106 | + } |
| 107 | + |
| 108 | + if ($setting === 'allow_negative') { |
| 109 | + if (! is_bool($value)) { |
| 110 | + // Attempt basic cast for flexibility |
| 111 | + if (in_array(strtolower((string) $value), ['true', '1', 'yes', 'on'])) { |
| 112 | + $value = true; |
| 113 | + } elseif (in_array(strtolower((string) $value), ['false', '0', 'no', 'off'])) { |
| 114 | + $value = false; |
| 115 | + } else { |
| 116 | + return ['success' => false, 'error' => 'Invalid allow_negative value. Must be boolean (true/false).']; |
| 117 | + } |
| 118 | + } |
| 119 | + $this->config['allow_negative'] = $value; |
| 120 | + |
| 121 | + // $registry->notifyResourceChanged('config://calculator/settings'); |
| 122 | + return ['success' => true, 'message' => 'Allow negative results set to '.($value ? 'true' : 'false').'.']; |
| 123 | + } |
| 124 | + |
| 125 | + return ['success' => false, 'error' => 'Internal error handling setting.']; // Should not happen |
| 126 | + } |
| 127 | +} |
0 commit comments