WASHINGTON— President Biden said that the U.S. is close to brokering a deal that would lead to a cease-fire in Gaza , ending a war that local health authorities say has killed more than 40,000 in the enclave, freeing hostages held by Hamas, and, U.S. officials hope, head off a potential looming Iranian attack on Israel.

The question now is whether Israel and Hamas really want the agreement .

As a broker of the negotiations, the U.S. has often tried to minimize differences between Israel and Hamas, only to see talks collapse when the time comes to finalize an agreement.

The Biden administration has stepped up diplomatic efforts this month amid rising political pressures at home. Thousands of protesters are expected to rally outside the Democratic National Convention in Chicago beginning on Monday to demand a cease-fire and an end to U.S. military support for Israel. More protests are expected to sprout up on college campuses around the country in September, when students return for fall classes.

The U.S. is also trying to contain the threat from Iran and its ally Hezbollah, which are expected to launch an attack on Israel in retaliation for the killings of top militant leaders. That assault, if it happens, could spark yet another military response from Israel that U.S. officials fear could ignite a wider war .

U.S. officials began sounding upbeat about the prospects for a peace deal in recent weeks, although few other observers have seen much evidence for optimism.

Speaking to reporters in the Oval Office on Friday, Biden said he had been busy working on forging a cease-fire effort and “we are closer to a deal than we have ever been.” He declined to give details saying “I don’t want to jinx anything.”

“We’re not there yet,” he continued, “but we’re much, much closer than it was three days ago.”

Later on Friday night, Biden continued to express hope that a deal was within reach.

“It’s still in play. I’m optimistic,” he told reporters, declining to say exactly why. “It’s far from over. Just a couple more issues. I think we’ve got a shot.”

A senior U.S. administration official on Friday said negotiations had reached an “endgame,” adding that the last two days had proven “the most constructive 48 hours that we’ve had in this process in many months.”

Negotiators are hoping to hammer out remaining sticking points ahead of more talks next week in Cairo.

But Hamas official Sami Abu Zuhri told the Journal that the U.S. was exaggerating progress.

“The American administration is attempting to create a false sense of positivity, without any genuine intent to stop the war, merely aiming to buy time,” he said.

Hamas didn’t attend this week’s talks, requiring mediators to relay messages and proposals to the group, further delaying a final agreement.

Many of the details of the agreement have vexed diplomats for months, such as the list of hostages and Palestinian prisoners to exchange. Other points, like the screening by Israelis of Palestinians moving from Gaza’s south to the north, also require further discussion.

Hamas militants on Oct. 7 abducted more than 240 civilians and soldiers. Hamas released more than 100 hostages during a temporary cease-fire that ended on Dec. 1.

Israel says there are now 111 hostages abducted that day remaining in Gaza, which includes people whom Israel has concluded are no longer alive.

The senior U.S. official issued an unusually harsh broadside against the Israelis, insisting that failing to seize the opportunity put hostages at risk. “If you continue to negotiate for months and months and try to get a perfect deal or every last drop of blood from the stone, you risk having no hostages left to save,” the official said.

The administration’s optimism belies the fact that there is still no deal even after months of painstaking negotiations. That led the U.S. to join Qatar and Egypt in announcing a “bridging proposal” designed to cover all outstanding issues.

Bruce Hoffman, a specialist in counterterrorism at Georgetown University, said there has been little change in the public stances of either Israeli Prime Minister Benjamin Netanyahu or Hamas leaders since peace efforts began, and that any lasting peace deal is unlikely at this juncture.

“The likelihood of their success has always been slim, it certainly is slimmer now,” he said.

The talks come as the U.S. and its partners are trying to head off an Iranian attack on Israel in retaliation for the killing of a Hamas leader and Hezbollah commander in July. Qatari Prime Minister Mohammed bin Abdulrahman al-Thani spoke with Iran’s top diplomat Thursday and Friday, urging Tehran in their conversations not to attack Israel while cease-fire talks were under way, a Middle Eastern diplomat said.

Al-Thani, who is also Qatar’s foreign minister, further insisted that any escalation would only complicate tense negotiations that were yielding some progress.

It is possible those warnings will convince Tehran to delay its expected retaliation, which the White House has repeatedly said could come at any moment with little notice. The senior administration official assured the U.S. and its allies were prepared for all contingencies, warning Iran it would face “cataclysmic” consequences if it went forward with an assault.

For the past week, the U.S. diplomatic effort in the region has been spearheaded by some top-level diplomats, including CIA Director Bill Burns and the White House’s Middle East coordinator Brett McGurk .

On Saturday, Secretary of State Antony Blinken is expected to join the effort by flying to Tel Aviv, his ninth trip to the region. Publicly, he has mainly blamed Hamas leader Yahya Sinwar for blocking the deal, charging that Sinwar sees political benefit from the carnage in the Gaza Strip.

But Blinken’s trip is expected to focus more directly on pressuring Netanyahu, who Biden in June accused of prolonging the war for his own political advantage. Later that month, Netanyahu aired a video message in English claiming the U.S. was withholding weapons from Israel, a charge the U.S. denied.

Write to Alan Cullison at alan.cullison@wsj.com and Alexander Ward at alex.ward@wsj.com