Handle exceptions in MCP tools
mainWhen a tool handler throws an exception, the Laravel MCP SDK automatically converts it into a valid JSON-RPC error response. This allows you to use standard PHP exceptions (like InvalidArgumentException or RuntimeException) to communicate errors to the MCP client.
#[McpTool(name: 'get_user')]
public function getUser(int $userId): array
{
$user = User::find($userId);
if (!$user) {
throw new \InvalidArgumentException("User with ID {$userId} not found");
}
return $user->toArray();
}