MI5 is alert to the risk that terrorists will re-establish training camps in Afghanistan and seek a “propaganda advantage” as coalition forces leave the country, the director-general of the UK’s domestic intelligence service has said.

Delivering his annual threat assessment on Wednesday, Ken McCallum said MI5 was shifting to a new and “challenging” mode of counter-terrorism in Afghanistan, in which the UK, US and other allies “have neither the advantages nor the risks” of troops on the ground.

Last week, UK prime minister Boris Johnson announced that the majority of British forces have now left Afghanistan, ahead of September’s departure deadline for the withdrawal of all US military personnel. Joe Biden, US president, chose the twenty year anniversary of the 9/11 terrorist attacks — which prompted the Afghan deployment — to end what he called “America’s longest war”.

However, as Nato allies withdraw, there are already signs that Taliban forces are making rapid territorial gains and may seek to overturn the government in Kabul.

Commenting on the current instability, McCallum said while the military campaign had successfully dismantled al-Qaeda’s infrastructure, terrorists have a record of exploiting “ungoverned spaces”.

“In the case of Afghanistan . . . you might imagine that if pockets of ungoverned space open up, some terrorist groups might seek, for example, to re-establish some training facilities there as we’ve seen in the past,” he told reporters.

The director-general, who was speaking at MI5’s headquarters in London, added that the departure of Nato forces might inspire terrorists on British soil.

“It must surely be likely that extremist groups of various sorts, including UK-based groupings that have no meaningful connections themselves to Afghanistan, will seek to portray this to their own adherents and potentially to those they’re seeking to interest or recruit or radicalise as a victory for extremist Islam,” he said.

“Extremists will seek to take a propaganda advantage from the situation in Afghanistan”.

McCallum used his speech to warn of the growing risk from hostile states such as Russia, China and Iran, urging Britons to adopt the same vigilance about espionage as they do for terrorism.

Revealing that MI5 has doubled its resources to deal with state-based threats — which range from Russia’s use of a nerve agent in Salisbury three years ago, to intellectual property theft and online disinformation — he said that increasingly, members of the public are falling victim to state activity.

“Given half a chance, hostile actors will short-circuit years of patient British research or investment,” he said. “This is happening at scale, and it affects us all: UK jobs, UK public services, UK futures.”

MI5 resources focused on combating hostile state activity rose from three per cent just over a decade ago to 14.5 per cent by 2014. Recent figures are undisclosed for security reasons, but officials emphasise that the vast majority of intelligence resources are still dedicated to counter-terrorism work.

On the risk posed by rightwing extremists, McCallum described the “high prevalence” of teenagers being drawn to this activity. A third of all late stage attack plots prevented by British intelligence agencies in the last four years were from rightwing terrorists, with one case involving a child aged just 13, he said.

“In that particular case, that individual who . . . was operating largely online, had stimulated quite a bit of a risk in a number of places before anyone realised that they were, in fact, 13 years old,” the director-general said.