Skip to content

[0.x] Adding events to tool calls #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Server/Events/ToolCallFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Laravel\Mcp\Server\Events;

use Throwable;

class ToolCallFailed
{
/**
* Create a new event instance.
*
* @param string $toolName
* @param array $arguments
* @param Throwable $exception
*/
public function __construct(
public string $toolName,
public array $arguments,
public Throwable $exception,
) {}
}
17 changes: 17 additions & 0 deletions src/Server/Events/ToolCallFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Laravel\Mcp\Server\Events;

class ToolCallFinished
{
/**
* Create a new event instance.
*
* @param string $toolName
* @param array $arguments
*/
public function __construct(
public string $toolName,
public array $arguments,
) {}
}
17 changes: 17 additions & 0 deletions src/Server/Events/ToolCallStarting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Laravel\Mcp\Server\Events;

class ToolCallStarting
{
/**
* Create a new event instance.
*
* @param string $toolName
* @param array $arguments
*/
public function __construct(
public string $toolName,
public array $arguments,
) {}
}
12 changes: 12 additions & 0 deletions src/Server/Methods/CallTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

use Generator;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\ItemNotFoundException;
use Illuminate\Validation\ValidationException;
use Laravel\Mcp\Server\Contracts\Methods\Method;
use Laravel\Mcp\Server\Events\ToolCallFailed;
use Laravel\Mcp\Server\Events\ToolCallFinished;
use Laravel\Mcp\Server\Events\ToolCallStarting;
use Laravel\Mcp\Server\ServerContext;
use Laravel\Mcp\Server\Tools\ToolNotification;
use Laravel\Mcp\Server\Tools\ToolResult;
Expand All @@ -35,9 +39,17 @@ public function handle(JsonRpcRequest $request, ServerContext $context)
);
}

$events = app(Dispatcher::class);

try {
$events->dispatch(new ToolCallStarting($request->params['name'], $request->params['arguments']));

$result = $tool->handle($request->params['arguments']);

$events->dispatch(new ToolCallFinished($request->params['name'], $request->params['arguments']));
} catch (ValidationException $e) {
$events->dispatch(new ToolCallFailed($request->params['name'], $request->params['arguments'], $e));

return JsonRpcResponse::create(
$request->id,
ToolResult::error($e->getMessage())
Expand Down
112 changes: 112 additions & 0 deletions tests/Unit/Methods/CallToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

namespace Tests\Unit\Methods;

use Illuminate\Events\Dispatcher;
use Laravel\Mcp\Server\Events\ToolCallFailed;
use Laravel\Mcp\Server\Events\ToolCallFinished;
use Laravel\Mcp\Server\Events\ToolCallStarting;
use Laravel\Mcp\Server\Methods\CallTool;
use Laravel\Mcp\Server\ServerContext;
use Laravel\Mcp\Server\Transport\JsonRpcRequest;
use Laravel\Mcp\Server\Transport\JsonRpcResponse;
use Laravel\Mcp\Tests\Fixtures\ExampleTool;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Mockery as m;

class CallToolTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}

#[Test]
public function it_returns_a_valid_call_tool_response()
{
Expand Down Expand Up @@ -97,4 +107,106 @@ public function it_returns_a_valid_call_tool_response_with_validation_error()
'isError' => true,
], $response->result);
}

#[Test]
public function it_will_call_the_tool_call_starting_and_finished_event()
{
$request = JsonRpcRequest::fromJson(json_encode([
'jsonrpc' => '2.0',
'id' => 1,
'method' => 'tools/call',
'params' => [
'name' => 'example-tool',
'arguments' => ['name' => 'John Doe'],
],
]));

$context = new ServerContext(
supportedProtocolVersions: ['2025-03-26'],
serverCapabilities: [],
serverName: 'Test Server',
serverVersion: '1.0.0',
instructions: 'Test instructions',
maxPaginationLength: 50,
defaultPaginationLength: 10,
tools: [ExampleTool::class],
resources: [],
prompts: [],
);

$dispatcherMock = m::mock(Dispatcher::class);
$dispatcherMock->shouldReceive('dispatch')
->once()
->with(m::on(function ($event) {
return $event instanceof ToolCallStarting
&& $event->toolName === 'example-tool'
&& $event->arguments === ['name' => 'John Doe'];
}));
$dispatcherMock->shouldReceive('dispatch')
->once()
->with(m::on(function ($event) {
return $event instanceof ToolCallFinished
&& $event->toolName === 'example-tool'
&& $event->arguments === ['name' => 'John Doe'];
}));

app()->instance(Dispatcher::class, $dispatcherMock);

$method = new CallTool;

$method->handle($request, $context);

$this->addToAssertionCount(2);
}

#[Test]
public function it_will_call_the_tool_call_starting_and_failed_event()
{
$request = JsonRpcRequest::fromJson(json_encode([
'jsonrpc' => '2.0',
'id' => 1,
'method' => 'tools/call',
'params' => [
'name' => 'example-tool',
'arguments' => [],
],
]));

$context = new ServerContext(
supportedProtocolVersions: ['2025-03-26'],
serverCapabilities: [],
serverName: 'Test Server',
serverVersion: '1.0.0',
instructions: 'Test instructions',
maxPaginationLength: 50,
defaultPaginationLength: 10,
tools: [ExampleTool::class],
resources: [],
prompts: [],
);

$dispatcherMock = m::mock(Dispatcher::class);
$dispatcherMock->shouldReceive('dispatch')
->once()
->with(m::on(function ($event) {
return $event instanceof ToolCallStarting
&& $event->toolName === 'example-tool'
&& $event->arguments === [];
}));
$dispatcherMock->shouldReceive('dispatch')
->once()
->with(m::on(function ($event) {
return $event instanceof ToolCallFailed
&& $event->toolName === 'example-tool'
&& $event->arguments === [];
}));

app()->instance(Dispatcher::class, $dispatcherMock);

$method = new CallTool;

$method->handle($request, $context);

$this->addToAssertionCount(2);
}
}