WASHINGTON — Defense Secretary James Mattis learned in May from a colleague that President Donald Trump had made the decision to withdraw the U.S. from the Iran nuclear deal, and scrambled to get his boss on the phone before a formal announcement was made. It wouldn't be the last time he was caught off guard by a presidential announcement.

A month later, Mattis was informed that Trump had ordered a pause in U.S. military exercises with South Korea only after the president had already promised the concession to North Korean leader Kim Jong Un.

Last week, Trump again blindsided and overruled his defense secretary by publicly directing the Pentagon to create a sixth military branch overseeing operations in space.

The way these recent presidential decisions on major national security issues have played out, as detailed by current and former White House and defense officials, underscores a significant change in Mattis's role in recent months. The president is relying less and less on the advice of one of the longest-serving members of his cabinet, the officials said.

"They don't really see eye to eye," said a former senior White House official who has closely observed the relationship.

It's a stark contrast to Trump's early enthusiasm for the retired four-star Marine general he proudly referred to as "Mad Dog." And while the two men had disagreements from the start — on the use of enhanced interrogation techniques on terrorism suspects, for instance — Trump still kept Mattis in the loop on major decisions and heeded his counsel.

"He's never been one of the go-tos in the gang that's very close to the president," a senior White House official said. "But the president has a lot of respect for him."

In recent months, however, the president has cooled on Mattis, in part because he's come to believe his defense secretary looks down on him and slow-walks his policy directives, according to current and former administration officials.

The dynamic was exacerbated with Trump's announcement in March that he had chosen John Bolton as national security adviser, a move Mattis opposed, and Mike Pompeo's confirmation as secretary of state soon after.

The president is now more inclined to rely on his own instincts or the advice of Pompeo and Bolton, three people familiar with the matter said.

One defense official said there is no indication Trump is unhappy with Mattis, just that he is not in the inner decision-making circle anymore. The official said Mattis does not contradict the president publicly or in the media and does not draw the president's ire.

Asked for comment on NBC's reporting, chief Pentagon spokesperson Dana White said, "This is pure silliness."

On behalf of the White House, National Security Council spokesman Garrett Marquis said, "For an unnamed expert to claim a department is not in the loop is ludicrous."

National security adviser John Bolton, left, and Secretary of State Mike Pompeo at the White House this month. Andrew Harnik / AP file

Mattis has influenced some key decisions. He was among the advisers last summer who convinced Trump to keep U.S. troops in Afghanistan. He kept Trump from ditching the Iran nuclear deal for more than a year after taking office. This past April, when Trump wanted to withdraw U.S. troops from Syria, Mattis convinced him to agree — reluctantly — to keep them there to finish the fight against ISIS.

But a second former senior White House official said Mattis doesn't spend much time with the president, and is really only with him for meetings. "[Trump] respected 'Mad Dog Mattis' and thought he was tough," the official said. "But they were never especially close."

Early in the administration it was common for the men to speak on the phone several times per day, but that also has changed, according to a senior defense official.

And a broader shift in the relationship between the president and his defense secretary has occurred during the past six months. It began in earnest when Trump decided in December to move the U.S. embassy in Israel from Tel Aviv to Jerusalem, White House and defense officials said. Mattis opposed the move, saying it would heighten security concerns in the region.

The list of policy decisions Trump has made against Mattis's advice or without his knowledge has since grown. It includes not just withdrawing from the Iran nuclear deal and ordering the Pentagon to create a so-called space force, but also barring transgender people from serving openly in the military and a host of moves the president has made as part of negotiations with North Korea, say multiple current and former officials.

Mattis was against Trump deploying the National Guard to the border. When Trump raised the issue with him, Mattis told the president he didn't think it was a good idea but that such a move wasn't under his control as defense secretary, according to one current and one former official.

"[Mattis] didn't feel like the mission was well defined," said a senior White House official.

Trump signed a proclamation in April authorizing the deployment of state National Guard forces to protect the border.

In May, according to two officials familiar with the matter, Mattis learned Trump had made a decision on the Iran deal only after it was finalized. Bolton had made the case for dropping the deal directly to the president in the Oval Office rather than in a meeting with the national security team, as is typical for such decisions. The defense secretary, who was at the Pentagon, called the president's personal secretary, Madeleine Westerhout, saying he needed to speak with him before he made a formal announcement. One source said Mattis was able to reach Trump, while another said he reached either Trump or a White House aide.

Even though the president made the decision without Mattis, one defense official said Mattis was not surprised by the outcome. "He was expecting the decision," said the official. "He just didn't know it was final until he asked."

Mattis had pushed back against Trump's likely decision to abandon the Iran deal for months, but after Secretary of State Rex Tillerson and National Security Adviser H.R. McMaster left the administration earlier this year, he stopped objecting so vocally, according to an official familiar with the meeting.

Mattis had benefited from serving with Tillerson and McMaster, because Trump didn't like either of them, officials said. Whenever Tillerson and Mattis both opposed one of Trump's ideas, as they often did, Trump would focus his anger on Tillerson, officials said, adding that the president did the same with McMaster.

Now Mattis is working alongside a secretary of state Trump very much likes and a national security adviser who is more closely aligned with his own views, such as his position on Iran.

In perhaps the most consequential national security initiative Trump has undertaken — direct talks with North Korea's Kim Jong Un — Mattis appears to be on the sidelines. He's had little role before, during and after Trump's Singapore summit with Kim, despite the major U.S. military presence in South Korea, officials said.

A defense official said that ahead of the summit Trump and Mattis spoke about an array of topics that could come up, and canceling the joint military exercises with South Korea was not among them. Mattis was not expecting Trump's announcement on June 12 that the exercises would be canceled, the official said, and as a result had not notified the two commands most directly involved: U.S. Forces Korea and the South Korean military.

In fact, Mattis found out from one of his assistant secretaries, Randy Schriver, early Tuesday morning after the summit had concluded, according to a former senior White House official.

Pentagon spokesperson Dana White, however, insisted "there were no surprises."

A senior White House official did not dispute that Mattis found out after the fact, but said, "He knew that it could have been offered. The president was in the room with the leader of North Korea and made the decision."

The joint exercises with South Korea had been seen by top military officials like Mattis as critical to readiness and interoperability in the region. Yet the Pentagon released a statement this week suspending the exercises and referring to them as "war games," a phrase that the U.S. military has traditionally avoided in describing the exercises but is used by North Korea and China.

President Trump participates in a wreath-laying ceremony at the Tomb of the Unknown Solider at Arlington National Cemetery on May 28 with Defense Secretary Jim Mattis and Maj. Gen. Michael L. Howard. Evan Vucci / AP

Trump's June 18 directive to create what he described as a "space force" branch of the military directly contradicted his defense secretary's advice. In October, Mattis wrote a letter to Sen. John McCain, R.-Ariz., chairman of the Senate Armed Services Committee, saying "I oppose the creation of a new military service and additional organizational layers at a time when we are focused on reducing overhead and integrating joint warfighting efforts."

Trump didn't even mention Mattis during his announcement of the space force on June 18. Instead he singled out Marine Corps Gen. Joseph Dunford, chairman of the Joint Chiefs of Staff, who was present at the event, saying "if you would carry that assignment out, I would be very greatly honored."

Afterward, Mattis personally added a line in the statement from the Pentagon that pointedly noted the creation of such a force "has implications for intelligence operations for the Air Force, Army, Marines and Navy," according to a defense official.

The current and former officials said Trump has become tired of what he views as Mattis ignoring or slow-rolling his policy decisions.

President Trump is introduced by Defense Secretary Mattis during the commissioning ceremony of the aircraft carrier USS Gerald R. Ford at Naval Station Norfolk in Norfolk, Virginia, on Jul. 22, 2017. Jonathan Ernst / Reuters

Trump blamed Mattis for the bumpy rollout of his changes to the military's transgender policy, said two former administration officials.

A senior defense official said Mattis slow-rolled Trump on the transgender issue early in the administration, though another official said he was simply overseeing a deliberate process that took time.

In spring 2017 Trump asked Mattis to change the Obama-era policy that allowed transgender individuals to serve openly in the military. Trump asked Mattis about it for "a few weeks or maybe even a couple months," but Mattis did not respond with action.

Rep. Mark Meadows, R-N.C., chair of the Freedom Caucus, and White House aide Marc Short were among those who told the president Mattis was dragging his feet, said one former official. Eventually Trump tweeted about the change in transgender policy in July, forcing Mattis's hand.

The friction escalated in January when Trump ordered Mattis to end the practice of allowing the family members of U.S. troops stationed in South Korea to accompany them during their deployments. But Mattis, with the assistance of chief of staff John Kelly, put off implementing the directive, say one defense official and one former administration official, angering Trump.

Trump repeatedly said he wanted to sign an order changing the policy on military dependents in South Korea, but Mattis and other officials, including Kelly, tried to stall him, according to three former officials. "It was kind of like a game of tag. There were plenty of other people, in addition to Mattis, who slow-walked that," the former official said. The order was never implemented.

"He knows that he told them to do it and they didn't do it," another former senior White House official said.

This official said taken individually each of these things was a "low-grade annoyance" for Trump. "But cumulatively they've begun to add up," the official said.