WASHINGTON — President Trump declared on Monday that peace talks with the Taliban were “dead,” but signaled that he would still withdraw United States troops from America’s longest war, following the collapse this weekend of a monthslong effort to ease violence in Afghanistan.

Expressing impatience with the continued burden of keeping 14,000 troops in Afghanistan, the president said it was not the American military’s role to secure the world. “Our soldiers are incredible, but they are serving as policemen, to a large extent,” Mr. Trump said. “We would like to get out, but we will get out at the right time.”

A plan to send home as many as 5,400 American forces by early next year — just as Mr. Trump revs up his re-election campaign — was at the heart of the negotiations that also sought to secure a cease-fire in Kabul. The Afghan capital has been rocked by bloody attacks over the last several weeks, and violence is expected to escalate amid the political uncertainty.

Mr. Trump has long promised to end American involvement in the war, and said on Monday that the Afghan government “will have to take responsibility” for its own security “at the earliest possible time.”