The deposed shah of Iran left Mexico for medical treatment in New York last October only after President Jose Lopez Portillo assured the shah in writing that he would be welcomed back to that country and granted asylum.

"Your home is always Mexico," Lopez Portillo told shah Mohammad Rez Pahlavi in a message reported to the White House in a secret State Department cable at the time.

"This is your country. You are always welcome. We are distressed and disturbed by your health. A king should have premier medical treatment.

"You should go to the United States and we await your return. You can live anywhere in Mexico, be it Cuernavaca, Acapulco or Mexico City. We will give you security and asylum."

A Carter administration official said this week that U.S. officials were aware of Lopez Portillo's assurances and therefore were stunned when the Mexico president, citing his nation's "vital interests," decided on Nov. 29 not to permit the shah to return to Mexico.

The message from Lopez Portillo was handled to the shah by an intermediary on Oct. 20 two days before the shah left Mexico for New York where he underwent treatment for cancer and gall stones.

"That assurance was reconfirmed verbally to the State Department by Mexico authorities right up until the day Lopez Portillo said the shah would not be welcome in Mexico," a Carter administration official said.

The decision by Lopez Portillo not to readmit the shah caused consternation among U.S. officials and prompted a frantic search for a new home for him that did not end until last Friday when Panama agreed to let him go there.

The incident, coming at a time when 50 American hostages were being held in the U.S. Embassy in Tehran, added tension to traditionally uneasy U.S.-Mexico relations.

For example, a Carter administration official, expressing contempt for Lopez Portillo, told the Los Angeles Times: "We understand that on several occasions Lopez Portillo visited the shah when he was in Cuernavaca and recommended several private entrepreneurs to him. Lopez Portillo attempted to get the shah to invest some of his money in the enterprises."

The same official said Lopez Portillo's message encouraging the shah to go to the United States was delivered to the deposed Iranian leader "at a time when he was deciding whether or not he wished to come to the United States."

"U.S. officials knew of that message," the official said, "and that was a factor in our decision to have him come here for treatment."

The shah lived in a rented home in Cuernavaca from June 10 to Oct. 22.

The Carter administration's decision to admit the shah was controversial. Although most critics have withheld comment because of the sensitivity of the hostages situations, Sen. Edward M. Kennedy (D-Mass.) kicked up a storm by challenging the decision.

Adminstration officials are quietly spreading the word that because of Kennedy's criticism, at least one nation -- Argentina -- refused to grant the shah asylum. Asked yesterday about the letter from Lopez Portillo, a spokesman for the Mexican president said emphatically, "All this is absolutely false. There was no letter. We communicated through diplomatic channels. There is a campaign to discredit us."

[Asked about Lopez Portillo's alleged visits to see the shah in Cuernavaca, and about the president's recommendations of Mexican entrepreneurs, the spokesman said angrily: "Give me a break. That is a lie. How would the president do something like that? They (Washington) are trying to provoke us."]

An administration official said that on Dec. 4, Raul H. Castro, the U.S. ambassador to Argentina, sent a cable to the State Department saying the Argentinian foreign minister listed three reasons for not granting asylum to the shah:

Concern for their own nationals and property interests.

"Recrudescence" (recurrence) of terrorism.

Argentina could not become associated with an individual who justly or unjustly would be the subject of attacks by prominent American politicians.