Skip to content

Commit c18a53b

Browse files
committed
update docs
1 parent 1469076 commit c18a53b

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

README.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,13 @@ For web-based servers, the inspector must be started and configured manually:
423423
npx @modelcontextprotocol/inspector
424424
```
425425

426-
## Advanced
426+
## Streaming Tool Responses
427427

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.
429429

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.
431431

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:
435433

436434
```php
437435
<?php
@@ -440,31 +438,17 @@ namespace App\Mcp\Tools;
440438

441439
use Generator;
442440
use Laravel\Mcp\Tools\Tool;
443-
use Laravel\Mcp\Tools\ToolInputSchema;
444441
use Laravel\Mcp\Tools\ToolNotification;
445442
use Laravel\Mcp\Tools\ToolResult;
446443

447444
class ChatStreamingTool extends Tool
448445
{
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-
459446
public function handle(array $arguments): Generator
460447
{
461-
$message = $arguments['message'] ?? "Here's a message from the chat bot.";
462-
$tokens = explode(' ', $message);
448+
$tokens = explode(' ', $arguments['message']);
463449

464450
foreach ($tokens as $token) {
465451
yield new ToolNotification('chat/token', ['token' => $token . ' ']);
466-
467-
usleep(100000);
468452
}
469453

470454
yield ToolResult::text("Message streamed successfully.");

0 commit comments

Comments
 (0)