Soleimani was killed together with the leaders of some Iranian-backed Shia militias, who themselves have been accused of attacking US targets in Iraq, in particular Abu Mahdi al-Muhandis:
He was credited with being a key leader in the Shia militias, the Popular Mobilisation Forces, known as the Hashed (al-Shaabi), employed as shock troops in the bloody fight against Islamic State in Iraq. Although he worked under Faleh al-Fayyadh, Iraq’s national security adviser, Muhandis was widely recognised as the Hashed’s real leader.
And equally, if not more, important was his involvement in founding and leading the Kata’ib Hezbollah militia, part of the Hashed.
Listed by the US as a terrorist group, Kata’ib Hezbollah was regarded by Washington as a hardline pro-Iranian faction blamed for targeting US troops, with Muhandis himself accused of running “weapons smuggling networks and participat[ing] in bombings of western embassies and attempted assassinations in the region”.
Indeed it was an attack launched by Kata’ib Hezbollah killing a US contractor that triggered the sequence of events that led to US airstrikes on its bases in Iraq and Syria, which in turn sparked the violent demonstrations at the US embassy in Baghdad and, now, the assassination of Muhandis and Suleimani.
Furthermore, Reuters has published an article (based on unnamed Iraqi sources) saying that Soleimani was in Iraq at least in part to plan/coordinate attacks on US targets.
In mid-October, Iranian Major-General Qassem Soleimani met with his Iraqi Shi’ite militia allies at a villa on the banks of the Tigris River, looking across at the U.S. embassy complex in Baghdad.
The Revolutionary Guards commander instructed his top ally in Iraq, Abu Mahdi al-Muhandis, and other powerful militia leaders to step up attacks on U.S. targets in the country using sophisticated new weapons provided by Iran, two militia commanders and two security sources briefed on the gathering told Reuters.
At the Baghdad villa, Soleimani told the assembled commanders to form a new militia group of low-profile paramilitaries - unknown to the United States - who could carry out rocket attacks on Americans housed at Iraqi military bases. He ordered Kataib Hezbollah - a force founded by Muhandis and trained in Iran - to direct the new plan, said the militia sources briefed on the meetings.
The Reuters article mentions that official Iranian sources have declined to comment on these points.
An earlier Reuters article from October noted that
Soleimani, whose Quds force coordinates Tehran-backed militias in Iraq, Syria and Lebanon, is a frequent visitor to Iraq.
It also said that Soleimani's role in Iraq was multi-faceted, including the support of Mahdi's government both in terms of coaxing the Iran-leaning factions to support Mahdi and perhaps more directly coordinating actions against anti-government demonstrators.
Populist Shi’ite cleric Moqtada al-Sadr demanded this week that Abdul Mahdi call an early election to quell the biggest mass protests in Iraq since the U.S.-led invasion that toppled Saddam Hussein in 2003. The demonstrations are fueled by anger at corruption and widespread economic hardship.
Sadr had urged his main political rival Hadi al-Amiri, whose alliance of Iran-backed militias is the second-biggest political force in parliament, to help push out Abdul Mahdi.
But in a secret meeting in Baghdad on Wednesday, Qassem Soleimani, head of the Iranian Revolutionary Guards’ elite Quds Force, intervened. Soleimani asked Amiri and his militia leaders to keep supporting Abdul Mahdi, according to five sources with knowledge of the meeting.
Spokesmen for Amiri and Sadr could not be reached for comment. An Iranian security official confirmed Soleimani was at Wednesday’s meeting, saying he was there to “give advice”.
Since Soleimani appears to have been supporting Mahdi's government, his presence in Iraq couldn't have been unwelcome, from the Iraqi/Mahdi government perspective. After the US strike, the Mahdi government has condemned the action, although they appear to be most upset by the killing of Muhandis, or at least that was mentioned more than Soleimani's...
"The assassination of an Iraqi military commander is an aggression on Iraq as a state, government and people," Abdul Mahdi said in a statement.
"Carrying out physical liquidation operations against leading Iraqi figures or from a brotherly country on the Iraqi lands is a flagrant violation of Iraq's sovereignty and a dangerous escalation that triggers a destructive war in Iraq, the region and the world," Abdul Mahdi said.
The latter statement doesn't get into any details on what Soleimani may have been doing.