In terms of espionage and such, yes, absolutely. Jonathan Pollard comes to mind, to give you an example: he passed along thousands of classified documents to Israel.
However, I get the impression you're thinking of something more intense, like a foreign covert operation that occurred military. In this case, I can also give you an example! The one that I'm thinking of is the Black Tom explosion, in New Jersey.
The alleged crime was committed by German agents, who the US claimed had fired Black Tom Terminal in New York Harbor in 1916. This was in order to prevent the ammunitions there, presumably, from reaching the Entente Powers. The Mixed Claims Commission, established to settle WWI claims against the German government, cleared the Germans of any wrongdoing in 1930. However, the claimants were determined to reopen the case. They were rebuffed in 1932 by Supreme Court Justice Roberts (Owen, not the current John G.), who said that their grounds (new evidence) wouldn't change the decision.
In 1933 he changed his mind when allegations of fraud and evidence suppression came up, and the case was reopened. On June 15, 1939, Roberts declared that the Germans were indeed responsible. Captain Franz von Rintelen, a former German secret agent, would later admit responsibility in the operation after he foreswore allegiance to the Nazi government, and pledged to help the Allies in the war.
Sources:
International Arbitration of the Black Tom and Kingsland Cases The Yale Law Journal, Vol. 49, No. 2 (Dec., 1939), pp. 346-351
Special Cable to THE NEW,YORK TIMES. (1940, Jan 03). PLOTTED BLACK TOM, SAYS VON RINTELEN. New York Times
In 1942 the German government launched Operation Pastorius during which several German agents were landed on American soil (in New York and Florida) with the purpose of sabotaging of military, economic, and civilian targets including bridges, railroad stations, power plants, and munitions depots. Two agents, however, betrayed the plot and the remaining saboteurs were arrested prior to any attacks being successfully completed. The group was tried in a military tribunal and sentenced to death, although President Roosevelt commuted the sentence of the two agents who aided in the capture of the others.
Sources:
Ex Parte Quirin, 317 US 1 (1942).
There's a long history during the Cold War (and beyond) of foreign covert operations in the US. Other commenters have already mentioned Jonathan Pollard, who was convicted of spying for Israel in 1987, and the Rosenbergs, who were executed for spying for the Soviet Union in 1953. But there are plenty more — these are just some of the more famous ones:
The Rosenbergs were part of a wider ring of 'atomic spies' operating at Los Alamos (and other American and British nuclear research labs) during the Manhattan Project: see also Klaus Fuchs, David Greenglass and Harry Gold.
Kim Philby, one of the Cambridge Five (Soviet agents who reached senior positions in the British government, diplomatic service and intelligence services), was certainly already spying for the Soviets when he was posted to Washington DC as the SIS liaison to the CIA in 1949-51. Guy Burgess and Donald Maclean, also members of the Five, were also posted to the US while working for the Soviets.
Aldrich Ames was arrested in 1994 for spying for the Russia while serving as a CIA case officer.
Robert Hanssen, an FBI counterintelligence specialist, was convincted in 2001 of spying for the USSR for over two decades.
This Department of Defense report identifies a total of 150 cases of American citizens being involved in espionage against the US from 1947-2001. But there have been others since: here's a piece from last week's New Yorker about a man convicted in 2010 of spying for China while working for Boeing. And a slightly sensationalist CBS News report from 2012 in which a former CIA officer claims there are now more spies working in the US than at the height of the Cold War.
So yes, there has been — and still is — plenty of foreign espionage happening in the US.
To go back really far (and perhaps a touch outside of what you're trying to focus on), General James Wilkinson was twice the commanding general of the United States Army (1796-1798, 1800-1812), the first governor of the Louisiana Territory (1805-1807)... and a paid agent of Spain.
He never really accomplished much (in any of those roles -- "a general who never won a battle or lost a court-martial") apart from extracting vast sums of money from the Spanish. (He did sell out the Lewis and Clark expedition, which the Spanish tried and failed to intercept.)
(Source: An Artist In Treason, by Andro Linklater.)
One obvious foreign covert operation was the Fenian raids on Canada! Organized, formed, armed, in fact entire armies assembled on American soil, then proceeded to march across the Canadian border where they were quickly stopped (having been infiltrated by Canadian agents who had to work hard avoiding both Fenian and American intelligence). I think this would count as foreign given that the Fenians considered themselves to be Irish nationalists.
During WWII, U Boats 202 and 584 landed 4 German soldiers apiece on Long Island and in Florida respectively, to perform acts of sabotage as part of Operation Pastorius. They were eventually captured and tried by military tribunal, whose jurisdiction was upheld in Ex parte Quirin by the Supreme Court.