Kenya has warned the United States that it might close its Indian Ocean ports to American warships if Washington sends arms to Somalia, according to U.S. administration sources.

Kenya became so upset recently with what it perceived to be a dangerous American "tilt" toward its neighbor and enemy that the government at one point discussed the possibility of turning toward the Soviet Union for arms, these sources said.

The depth of Kenyan feeling about U.S. policy toward Somalia was conveyed by Kenya's foreign minister, Munyua Waiyaki, during a meeting with U.S. Ambassador Andrew Young late last month. Young passed through Nairobi on his way to Lagos where he joined President Carter on his official state visit here.

"I was surprised at the extent of the Kenyans' anxiety and reports of hostility toward the United States over our seeming to reward Somalia for invading someone else's border," said Young in an interview after his talks with Waiyaki.

Kenya still regards Somalia as its number one enemy because the Somali government has never clearly renounced its territorial claims to Kenya's northern frontier region, which is populated by Somali-speaking people. Despite its strong pro-Western orientation, Kenya has just renewed its mutual defense pact with Soviet and Cuban-packed Ethiopia.

The pact, first signed in the mid-1960s, provides for one country to go to the assistance of the other should either of them be attacked by Somalia. Ethiopia did not invoke the pact during the Somali takeover of the Ogaden region, but Kenya did on several occasions interdict Somali insurgents crossing the frontier region on their way to Ethiopia.

The Kenyan-Ethiopian pact shows how differently the states in the Horn of Africa view the situation there compared to the United States and other Western powers whose primary concern has been the buildup of Soviet arms and Cuban troops in Ethiopia. Despite this buildup, Kenya has remained on the Ethiopian side and still views the Somali threat as more important.

Young characterized the Kenyan threat to close off Mombasa port to American warships as "unofficial" but said they had toyed with the idea "in order to get us to understand their importance to the United States.

The Carter administration first offered to provide Somalia with "defensive arms" last July but then quickly reversed itself because of the developing Somali-Ethiopian war. Since withdrawing its forces from the Ogaden last month, the Somali government has been expecting U.S. military assistance as a reward.

The Carter administration, however, has tied such aid to a Somali pledge "not to dishonor the international boundaries of either Ethiopa or Kenya." Such a formal pledge has not been forthcoming and would be politically difficult for President Mohammed Siad Barre to make because of his government's longstanding commitment to reunite all Somali-speaking peoples in the region.

The administration has limited itself so far to providing Somalia with economic assistance. On March 20, Richard M. Moose, assistant secretary of state for African affairs, signed a second agreement with the Somali government for $7 million worth of food by September. This brought to $13 million the total U.S. food aid package in the past four months.

The Carter administration seems to be in a dilemma over how to deal with Somalia. In terms of U.S. relations with the Soviet Union, Washington apparently feels obliged to reward Somalia for its break with Cuba and Moscow last fall and anxious to avoid Siad Barre's collapse following its defeat in the Ogaden war.

On the other hand, Kenya, which is becoming a close ally of the United States, is vehemently opposed to any U.S. military assistance to Somalia because it is convinced the arms will eventually be used by the Somalis to invade the northern frontier district.

The United States is already committed to selling Kenya a squadron of F5E jet fighters and a U.S. military team was recently in Nairobi to discuss other military assistance.

U.S. diplomatic sources say the administration has been considering a policy of providing both Kenya and Somalia with only defensive arms, such as antiaircraft weapons and antitank missiles, in a bid to satisfy the demands of both countries while holding down the arms race in the Horn of Africa.

These sources speculate that such limited assistance may not satisfy Somalia, leading it to turn back to the Soviet Union for military aid. In this case, Siad Barre would probably only have to assure Moscow he would not invade Ethiopa again but would leave him free to initiate a campaign against Kenya's northern frontier district.

This, the sources feel, may in the end be more acceptable to the Somalis than publicly committing themselves to the U.S. conditions in return for only defensive weapons.