|
2 | 2 |
|
3 | 3 | namespace KLP\KlpMcpServer\Command; |
4 | 4 |
|
| 5 | +use KLP\KlpMcpServer\Exceptions\MakeMcpToolCommandException; |
5 | 6 | use Symfony\Component\Console\Attribute\AsCommand; |
6 | 7 | use Symfony\Component\Console\Command\Command; |
7 | 8 | use Symfony\Component\Console\Input\InputArgument; |
@@ -212,50 +213,44 @@ protected function detectYamlIndentation(string $content): int |
212 | 213 | */ |
213 | 214 | protected function registerToolInConfig(string $toolClassName): bool |
214 | 215 | { |
215 | | - $configPath = $this->kernel->getProjectDir().'/config/packages/klp_mcp_server.yaml'; |
216 | | - |
217 | | - if (! $this->files->exists($configPath)) { |
218 | | - $this->io->error("Config file not found: {$configPath}"); |
219 | | - |
220 | | - return false; |
221 | | - } |
222 | | - |
223 | | - $content = $this->files->readFile($configPath); |
224 | | - |
225 | | - // Find the tools array in the config file |
226 | | - if (! preg_match('/(tools:\s*(\s*-\s*[[:alnum:]\\\\]*|\[\]))/s', $content, $matches)) { |
227 | | - $this->io->error('Could not locate tools array in config file.'); |
| 216 | + try { |
| 217 | + $configPath = $this->kernel->getProjectDir().'/config/packages/klp_mcp_server.yaml'; |
228 | 218 |
|
229 | | - return false; |
230 | | - } |
| 219 | + if (! $this->files->exists($configPath)) { |
| 220 | + throw new MakeMcpToolCommandException("Config file not found: {$configPath}"); |
| 221 | + } |
231 | 222 |
|
232 | | - $toolsArrayContent = $matches[1]; |
| 223 | + $content = $this->files->readFile($configPath); |
233 | 224 |
|
234 | | - // Detect the indentation level used in the YAML file |
235 | | - $indentation = $this->detectYamlIndentation($content); |
236 | | - $indentStr = str_repeat(' ', $indentation); |
237 | | - $fullEntry = "\n{$indentStr}- {$toolClassName}"; |
| 225 | + // Find the tools array in the config file |
| 226 | + if (! preg_match('/(tools:\s*(\s*-\s*[[:alnum:]\\\\]*|\[\]))/s', $content, $matches)) { |
| 227 | + throw new MakeMcpToolCommandException('Could not locate tools array in config file.'); |
| 228 | + } |
238 | 229 |
|
239 | | - // Check if the tool is already registered |
240 | | - if (str_contains($toolsArrayContent, $toolClassName)) { |
241 | | - $this->io->info('Tool is already registered in config file.'); |
| 230 | + $toolsArrayContent = $matches[1]; |
242 | 231 |
|
243 | | - return true; |
244 | | - } |
| 232 | + // Detect the indentation level used in the YAML file |
| 233 | + $indentation = $this->detectYamlIndentation($content); |
| 234 | + $indentStr = str_repeat(' ', $indentation); |
| 235 | + $fullEntry = "\n{$indentStr}- {$toolClassName}"; |
245 | 236 |
|
246 | | - // Add the new tool to the tools array |
247 | | - $newToolsArrayContent = $toolsArrayContent.$fullEntry; |
248 | | - $newContent = str_replace($toolsArrayContent, $newToolsArrayContent, $content); |
249 | | - $newContent = str_replace('tools: []', 'tools:', $newContent); |
| 237 | + // Check if the tool is already registered |
| 238 | + if (str_contains($toolsArrayContent, $toolClassName)) { |
| 239 | + $this->io->info('Tool is already registered in config file.'); |
| 240 | + } else { |
| 241 | + // Add the new tool to the tools array |
| 242 | + $newToolsArrayContent = $toolsArrayContent . $fullEntry; |
| 243 | + $newContent = str_replace($toolsArrayContent, $newToolsArrayContent, $content); |
| 244 | + $newContent = str_replace('tools: []', 'tools:', $newContent); |
250 | 245 |
|
251 | | - // Write the updated content back to the config file |
252 | | - try { |
253 | | - $this->files->dumpFile($configPath, $newContent); |
254 | | - $this->io->info('Tool registered successfully in config/packages/klp_mcp_server.yml'); |
| 246 | + // Write the updated content back to the config file |
| 247 | + $this->files->dumpFile($configPath, $newContent); |
| 248 | + $this->io->info('Tool registered successfully in config/packages/klp_mcp_server.yml'); |
| 249 | + } |
255 | 250 |
|
256 | 251 | return true; |
257 | | - } catch (\Throwable) { |
258 | | - $this->io->error('Failed to update config file. Please manually register the tool.'); |
| 252 | + } catch (\Throwable $e) { |
| 253 | + $this->io->error("Failed to update config file: {$e->getMessage()}"); |
259 | 254 |
|
260 | 255 | return false; |
261 | 256 | } |
|
0 commit comments