|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OPGG\LaravelMcpServer\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | +use Illuminate\Support\Str; |
| 8 | + |
| 9 | +class MakeMcpNotificationCommand extends Command |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The name and signature of the console command. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $signature = 'make:mcp-notification {name : The name of the MCP notification handler} {--method= : The notification method to handle}'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The console command description. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $description = 'Create a new MCP notification handler class'; |
| 24 | + |
| 25 | + /** |
| 26 | + * The filesystem instance. |
| 27 | + * |
| 28 | + * @var \Illuminate\Filesystem\Filesystem |
| 29 | + */ |
| 30 | + protected $files; |
| 31 | + |
| 32 | + /** |
| 33 | + * Create a new command instance. |
| 34 | + * |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public function __construct(Filesystem $files) |
| 38 | + { |
| 39 | + parent::__construct(); |
| 40 | + |
| 41 | + $this->files = $files; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Execute the console command. |
| 46 | + * |
| 47 | + * @return int |
| 48 | + */ |
| 49 | + public function handle() |
| 50 | + { |
| 51 | + $className = $this->getClassName(); |
| 52 | + $path = $this->getPath($className); |
| 53 | + $method = $this->getNotificationMethod(); |
| 54 | + |
| 55 | + // Check if file already exists |
| 56 | + if ($this->files->exists($path)) { |
| 57 | + $this->error("❌ MCP notification handler {$className} already exists!"); |
| 58 | + |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + // Create directories if they don't exist |
| 63 | + $this->makeDirectory($path); |
| 64 | + |
| 65 | + // Generate the file using stub |
| 66 | + $this->files->put($path, $this->buildClass($className, $method)); |
| 67 | + |
| 68 | + $this->info("✅ Created: {$path}"); |
| 69 | + |
| 70 | + $fullClassName = "\\App\\MCP\\Notifications\\{$className}"; |
| 71 | + |
| 72 | + // Ask if they want to automatically register the notification handler |
| 73 | + if ($this->confirm('🤖 Would you like to automatically register this notification handler in your MCP server?', true)) { |
| 74 | + $this->info('☑️ Add this to your MCPServer registration:'); |
| 75 | + $this->comment(' // In your service provider or server setup'); |
| 76 | + $this->comment(" \$server->registerNotificationHandler(new {$fullClassName}());"); |
| 77 | + } else { |
| 78 | + $this->info("☑️ Don't forget to register your notification handler:"); |
| 79 | + $this->comment(' // In your service provider or server setup'); |
| 80 | + $this->comment(" \$server->registerNotificationHandler(new {$fullClassName}());"); |
| 81 | + } |
| 82 | + |
| 83 | + // Display usage instructions |
| 84 | + $this->newLine(); |
| 85 | + $this->info('📋 Your notification handler overview:'); |
| 86 | + $this->comment(" • Method: {$method}"); |
| 87 | + $this->comment(' • Returns: HTTP 202 (no response body)'); |
| 88 | + $this->comment(' • Purpose: Fire-and-forget event processing'); |
| 89 | + |
| 90 | + $this->newLine(); |
| 91 | + $this->info('📡 Clients can send this notification via JSON-RPC:'); |
| 92 | + $this->comment(' {'); |
| 93 | + $this->comment(' "jsonrpc": "2.0",'); |
| 94 | + $this->comment(" \"method\": \"{$method}\","); |
| 95 | + $this->comment(' "params": {'); |
| 96 | + $this->comment(' "key": "value",'); |
| 97 | + $this->comment(' "data": { ... }'); |
| 98 | + $this->comment(' }'); |
| 99 | + $this->comment(' }'); |
| 100 | + |
| 101 | + $this->newLine(); |
| 102 | + $this->info('💡 Common notification use cases:'); |
| 103 | + $this->comment(' • Progress updates for long-running tasks'); |
| 104 | + $this->comment(' • Event logging and activity tracking'); |
| 105 | + $this->comment(' • Real-time notifications and broadcasts'); |
| 106 | + $this->comment(' • Background job triggering'); |
| 107 | + $this->comment(' • Request cancellation handling'); |
| 108 | + |
| 109 | + $this->newLine(); |
| 110 | + $this->info('🧪 Test your notification handler:'); |
| 111 | + $this->comment(' curl -X POST http://localhost:8000/mcp \\'); |
| 112 | + $this->comment(' -H "Content-Type: application/json" \\'); |
| 113 | + $this->comment(' -H "Accept: application/json, text/event-stream" \\'); |
| 114 | + $this->comment(" -d '{\"jsonrpc\":\"2.0\",\"method\":\"{$method}\",\"params\":{}}'"); |
| 115 | + $this->comment(' # Should return: HTTP 202 with empty body'); |
| 116 | + |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get the class name from the command argument. |
| 122 | + * |
| 123 | + * @return string |
| 124 | + */ |
| 125 | + protected function getClassName() |
| 126 | + { |
| 127 | + $name = $this->argument('name'); |
| 128 | + |
| 129 | + // Clean up the input: remove multiple spaces, hyphens, underscores |
| 130 | + // and handle mixed case input |
| 131 | + $name = preg_replace('/[\s\-_]+/', ' ', trim($name)); |
| 132 | + |
| 133 | + // Convert to StudlyCase |
| 134 | + $name = Str::studly($name); |
| 135 | + |
| 136 | + // Ensure the class name ends with "Handler" if not already |
| 137 | + if (! Str::endsWith($name, 'Handler')) { |
| 138 | + $name .= 'Handler'; |
| 139 | + } |
| 140 | + |
| 141 | + return $name; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Get the notification method from option or ask user. |
| 146 | + * |
| 147 | + * @return string |
| 148 | + */ |
| 149 | + protected function getNotificationMethod() |
| 150 | + { |
| 151 | + $method = $this->option('method'); |
| 152 | + |
| 153 | + if (! $method) { |
| 154 | + $method = $this->ask('What notification method should this handler process? (e.g., notifications/progress)'); |
| 155 | + } |
| 156 | + |
| 157 | + // Ensure it starts with 'notifications/' if not already |
| 158 | + if (! Str::startsWith($method, 'notifications/')) { |
| 159 | + $method = 'notifications/'.ltrim($method, '/'); |
| 160 | + } |
| 161 | + |
| 162 | + return $method; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Get the destination file path. |
| 167 | + * |
| 168 | + * @return string |
| 169 | + */ |
| 170 | + protected function getPath(string $className) |
| 171 | + { |
| 172 | + // Create the file in the app/MCP/Notifications directory |
| 173 | + return app_path("MCP/Notifications/{$className}.php"); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Build the directory for the class if necessary. |
| 178 | + * |
| 179 | + * @param string $path |
| 180 | + * @return string |
| 181 | + */ |
| 182 | + protected function makeDirectory($path) |
| 183 | + { |
| 184 | + $directory = dirname($path); |
| 185 | + |
| 186 | + if (! $this->files->isDirectory($directory)) { |
| 187 | + $this->files->makeDirectory($directory, 0755, true, true); |
| 188 | + } |
| 189 | + |
| 190 | + return $directory; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Build the class with the given name. |
| 195 | + * |
| 196 | + * @return string |
| 197 | + */ |
| 198 | + protected function buildClass(string $className, string $method) |
| 199 | + { |
| 200 | + $stub = $this->files->get($this->getStubPath()); |
| 201 | + |
| 202 | + return $this->replaceStubPlaceholders($stub, $className, $method); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Get the stub file path. |
| 207 | + * |
| 208 | + * @return string |
| 209 | + */ |
| 210 | + protected function getStubPath() |
| 211 | + { |
| 212 | + return __DIR__.'/../../stubs/notification.stub'; |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Replace the stub placeholders with actual values. |
| 217 | + * |
| 218 | + * @return string |
| 219 | + */ |
| 220 | + protected function replaceStubPlaceholders(string $stub, string $className, string $method) |
| 221 | + { |
| 222 | + return str_replace( |
| 223 | + ['{{ class }}', '{{ namespace }}', '{{ method }}'], |
| 224 | + [$className, 'App\\MCP\\Notifications', $method], |
| 225 | + $stub |
| 226 | + ); |
| 227 | + } |
| 228 | +} |
0 commit comments