Skip to content

Commit 414729c

Browse files
author
klapaudius
committed
Add progress notification handling in TestMcpToolCommand
- Updated `displayResult` to show progress notifications and warn on missing notifications for streaming tools. - Integrated `ProgressNotifier` to track and display progress during tool execution. - Enhanced streaming tool handling with clearer user feedback on operation progress.
1 parent fee0636 commit 414729c

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/Command/TestMcpToolCommand.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace KLP\KlpMcpServer\Command;
44

55
use KLP\KlpMcpServer\Exceptions\TestMcpToolCommandException;
6+
use KLP\KlpMcpServer\Services\ProgressService\ProgressNotifier;
67
use KLP\KlpMcpServer\Services\ToolService\BaseToolInterface;
78
use KLP\KlpMcpServer\Services\ToolService\StreamableToolInterface;
89
use Symfony\Component\Console\Attribute\AsCommand;
@@ -26,16 +27,34 @@ public function __construct(private readonly ContainerInterface $container)
2627

2728
private SymfonyStyle $io;
2829

29-
public function displayResult(mixed $result): void
30+
public function displayResult(mixed $result, array $sentNotifications = [], ?StreamableToolInterface $tool = null): void
3031
{
32+
// Display progress notifications if any were sent
33+
if (!empty($sentNotifications)) {
34+
$this->io->newLine();
35+
$this->io->section('Progress Notifications');
36+
$this->io->text(sprintf('Total notifications sent: %d', count($sentNotifications)));
37+
38+
foreach ($sentNotifications as $index => $notification) {
39+
$this->io->text([
40+
sprintf('Notification #%d:', $index + 1),
41+
json_encode($notification, JSON_PRETTY_PRINT),
42+
]);
43+
}
44+
}
45+
3146
$this->io->success('Tool executed successfully!');
47+
48+
// Display the tool result
3249
$resultText = ['Result:'];
33-
if (is_array($result) || is_object($result)) {
34-
$resultText[] = json_encode($result, JSON_PRETTY_PRINT);
35-
} else {
36-
$resultText[] = (string) $result;
37-
}
50+
$resultText[] = json_encode($result->getSanitizedResult(), JSON_PRETTY_PRINT);
3851
$this->io->text($resultText);
52+
53+
// Check if this was a streaming tool that should have sent notifications
54+
if ($tool && $tool->isStreaming() && empty($sentNotifications)) {
55+
$this->io->newLine();
56+
$this->io->warning('No progress notifications were sent by this streaming tool. Consider adding progress notifications to improve user experience during long-running operations.');
57+
}
3958
}
4059

4160
protected function configure(): void
@@ -85,8 +104,20 @@ private function testTool(): int
85104
json_encode($inputData, JSON_PRETTY_PRINT),
86105
]);
87106
try {
107+
// Track progress notifications if this is a streaming tool
108+
$sentNotifications = [];
109+
if ($tool->isStreaming()) {
110+
$progressNotifier = new ProgressNotifier(
111+
'test-progress-token',
112+
function (array $notification) use (&$sentNotifications) {
113+
$sentNotifications[] = $notification;
114+
}
115+
);
116+
$tool->setProgressNotifier($progressNotifier);
117+
}
118+
88119
$result = $tool->execute($inputData);
89-
$this->displayResult($result);
120+
$this->displayResult($result, $sentNotifications, $tool);
90121

91122
return command::SUCCESS;
92123
} catch (\Throwable $e) {

0 commit comments

Comments
 (0)