An FBI agent on Wednesday testified that a Fairfax County man being held on a weapons charge had talked about a desire to join the Islamic State or attack a military recruiting center.

Yusuf Wehelie, 25, has been under federal surveillance since at least December, according to documents filed in federal court in Alexandria. He was arrested last week after attempting to fly to Minneapolis.

“We didn’t know where he was going,” FBI agent Richard Gaylord testified.

Defense attorney Cadence Mertz said that, in fact, Wehelie was going to stay with an aunt for a basketball tournament.

That disconnect is at the heart of the case against Wehelie, who gained national attention when he was detained along with his brother in Egypt six years ago. He is not charged with any crime related to terrorism. But prosecutors argue that he is a potential terrorist because of comments he allegedly made to undercover agents.

“In light of what’s occurred in Orlando and San Bernardino, this is the kind of talk . . . that we have to take seriously,” Assistant U.S. Attorney Brandon L. Van Grack said in court Wednesday, referring to recent attacks in those cities. “He actually discussed a plan that he had thought through” to go abroad or attack the military here.

According to Gaylord, Wehelie said “he would love to jihad” and praised the Islamic State for mass murder. Watching a video in which a member of the militant group breaks someone’s neck, Gaylord said, Wehelie laughed. Gaylord said that Wehelie told undercover agents that he would like to join the group abroad or, if he could not leave, try to attack a military recruitment station in the United States with explosives.

After attacks like those in San Bernardino, Calif., and Orlando, Van Grack said, people ask what signs or indicators there were, and “there are the types of indicators and signs.”

However, no weapons were found at Wehelie’s home, and there is no evidence that he has owned any. The weapons charge came about because an undercover FBI agent paid Wehelie to transport four machine guns from Maryland to Virginia, according to court documents and testimony. There is no evidence that he discussed terrorism with the agent in connection with those guns or until after that job was over. Wehelie cannot own a gun because of a 2011 burglary conviction.

The agent met Wehelie during a cigarette-trafficking operation last winter, according to Gaylord, and then began calling and texting him frequently. In January, he bought Wehelie a Samsung Galaxy phone. According to Gaylord, the agent asked Wehelie whether he would be comfortable carrying a weapon to transport large amounts of cash. Wehelie then allegedly asked the agent for help buying a revolver that he would share with his cousin. About a month later, the agent paid Wehelie $300 to move the machine guns, which had been rendered inoperable before the trip. At some point after February, Wehelie stopped answering the agent’s calls.

“Despite the prosecution’s attempt to paint him with all the furor and fervor of the past few weeks, he is not charged with any charges related to terrorism,” Mertz said in court.

She argued that Wehelie was not dangerous, just adrift.

“He’s been smoking too much pot, and he’s had a really difficult time trying to get a job because of his felony conviction,” she said. She said he needed treatment for substance abuse and mental-health issues.

Judge Ivan D. Davis ruled that Wehelie be held without bail, saying his “unknown mental-health status” made him more dangerous.

Wehelie’s family gained prominence in 2010, when he and older brother Yahye were stopped in Cairo on their way back from a trip to Yemen.

Both brothers were born in the United States to Somali immigrants. Yahye Wehelie told reporters in 2010 that he had gone to Yemen to learn Arabic and find a wife. His younger brother came over a year later for the wedding and stayed for several months before both decided to return to the United States. They were stopped at the airport.

Yusuf Wehelie, then 19, said he was questioned by FBI agents and then spent three days being beaten and interrogated by Egyptian police before he was allowed to fly home.

“I was placed in a corridor with other prisoners and shackled to the wall,” he said in a statement shortly after his return. “I could barely move and injured my shoulder because of the confinement. Whenever I tried to sleep, I was kicked by a guard.”

His older brother was told he was on the no-fly list and was stranded in Egypt for six weeks under FBI scrutiny. He said agents asked about any connections to Islamist radicals, including Sharif Mobley, a New Jersey native who was arrested and charged with murder shortly after meeting Yahye Wehelie on a street in Yemen.

The Council on American-Islamic Relations used the case of the Wehelie brothers to underscore concerns about U.S. authorities questioning American citizens abroad without counsel present.

Davis also ruled Wednesday that Haris Qamar, arrested last week and charged with attempting to provide material support for the Islamic State, be held without bail.

Qamar tweeted and spoke in graphic terms of his desire to commit bloody acts on the militant group’s behalf, according to court documents. But defense attorney Ken Troccoli emphasized that “there’s a difference between speech and conduct.”

Qamar did take photos around the D.C. area at the behest of an FBI informant who said they would be used in an Islamic State propaganda video, according to court documents. He also told the informant that he had driven by the homes of several U.S. military personnel whose addresses had been posted on an Islamic State “kill list.”

And he bought a plane ticket to Turkey two years ago with the intention of fighting abroad, according to the affidavit. However, he never showed up for the flight: His parents, learning of his intentions, took his passport.

Adam Goldman contributed to this report.