You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+5-21Lines changed: 5 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -423,15 +423,13 @@ For web-based servers, the inspector must be started and configured manually:
423
423
npx @modelcontextprotocol/inspector
424
424
```
425
425
426
-
## Advanced
426
+
## Streaming Tool Responses
427
427
428
-
### Streaming Responses
428
+
For tools that send multiple updates or stream large amounts of data, you can return a generator from the `handle()` method. For web-based servers, this automatically opens an SSE stream and sends an event for each message the generator yields.
429
429
430
-
For tools that need to send multiple updates to the client before completing, or that produce a large amount of data, you can return a generator from the `handle` method. For web-based servers, this will automatically open an SSE stream to the client.
430
+
Within your generator, you can yield any number of `Laravel\Mcp\Tools\ToolNotification` instances to send intermediate updates to the client. When you're done, yield a single `Laravel\Mcp\Tools\ToolResult`to complete the execution.
431
431
432
-
Within your generator, you can `yield` instances of `Laravel\Mcp\Tools\ToolNotification` for intermediate updates and finally `yield` a single `Laravel\Mcp\Tools\ToolResult` for the main result of the tool execution.
433
-
434
-
This is particularly useful for long-running tasks or when you want to provide real-time feedback to the client, such as streaming tokens in a chat application.
432
+
This is particularly useful for long-running tasks or when you want to provide real-time feedback to the client, such as streaming tokens in a chat application:
435
433
436
434
```php
437
435
<?php
@@ -440,31 +438,17 @@ namespace App\Mcp\Tools;
440
438
441
439
use Generator;
442
440
use Laravel\Mcp\Tools\Tool;
443
-
use Laravel\Mcp\Tools\ToolInputSchema;
444
441
use Laravel\Mcp\Tools\ToolNotification;
445
442
use Laravel\Mcp\Tools\ToolResult;
446
443
447
444
class ChatStreamingTool extends Tool
448
445
{
449
-
public function description(): string
450
-
{
451
-
return 'A tool that streams a chat response.';
452
-
}
453
-
454
-
public function schema(ToolInputSchema $schema): ToolInputSchema
455
-
{
456
-
return $schema->string('message')->description('The message to stream back.');
457
-
}
458
-
459
446
public function handle(array $arguments): Generator
460
447
{
461
-
$message = $arguments['message'] ?? "Here's a message from the chat bot.";
462
-
$tokens = explode(' ', $message);
448
+
$tokens = explode(' ', $arguments['message']);
463
449
464
450
foreach ($tokens as $token) {
465
451
yield new ToolNotification('chat/token', ['token' => $token . ' ']);
0 commit comments