Three middle school students in western New York plotted a lethal attack on their school that was thwarted after an online threat by one student led law enforcement authorities to open an investigation, officials said on Friday.

The students, whose names were not released because of their ages, were charged on Thursday with second-degree conspiracy and accused of planning an armed assault on the Carl I. Bergerson Middle School in Albion, N.Y., said Joseph V. Cardone , the Orleans County district attorney. The students were released to their families under the supervision of the county’s Department of Probation, Mr. Cardone said.

The plot emerged after one student threatened another online on Nov. 1, and then suggested that other children were also at risk, Mr. Cardone said. That prompted law enforcement authorities to open an investigation that led them to two other students who were conspiring with the first student, he said.

The inquiry turned up evidence of what Mr. Cardone called a “credible threat.” The evidence included an unspecified number of guns in the students’ homes, Mr. Cardone said. The guns were legally owned by adult family members , he said.