Skip to content

Commit faa48c1

Browse files
author
klapaudius
committed
Refactor MakeMcpToolCommand error handling and add exception
Introduced MakeMcpToolCommandException to improve error handling in MakeMcpToolCommand. Replaced direct error messages with exceptions for better encapsulation and readability. Removed unused parameters in KlpMcpServerExtension and updated tool registration logic to enhance reliability.
1 parent ce01870 commit faa48c1

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

src/Command/MakeMcpToolCommand.php

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace KLP\KlpMcpServer\Command;
44

5+
use KLP\KlpMcpServer\Exceptions\MakeMcpToolCommandException;
56
use Symfony\Component\Console\Attribute\AsCommand;
67
use Symfony\Component\Console\Command\Command;
78
use Symfony\Component\Console\Input\InputArgument;
@@ -212,50 +213,44 @@ protected function detectYamlIndentation(string $content): int
212213
*/
213214
protected function registerToolInConfig(string $toolClassName): bool
214215
{
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';
228218

229-
return false;
230-
}
219+
if (! $this->files->exists($configPath)) {
220+
throw new MakeMcpToolCommandException("Config file not found: {$configPath}");
221+
}
231222

232-
$toolsArrayContent = $matches[1];
223+
$content = $this->files->readFile($configPath);
233224

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+
}
238229

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];
242231

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}";
245236

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);
250245

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+
}
255250

256251
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()}");
259254

260255
return false;
261256
}

src/DependencyInjection/KlpMcpServerExtension.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,5 @@ public function load(array $configs, ContainerBuilder $container): void
2727
$container->setParameter('klp_mcp_server.adapters.redis.host', $config['adapters']['redis']['host'] ?? 'default');
2828
$container->setParameter('klp_mcp_server.adapters.redis.ttl', $config['adapters']['redis']['ttl'] ?? 100);
2929
$container->setParameter('klp_mcp_server.tools', $config['tools']);
30-
// $container->setParameter('klp_mcp_server.prompts', $config['prompts']);
31-
// $container->setParameter('klp_mcp_server.resources', $config['resources']);
3230
}
3331
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace KLP\KlpMcpServer\Exceptions;
4+
5+
class MakeMcpToolCommandException extends \Exception {}

0 commit comments

Comments
 (0)