An 11-year-old Washington state boy who authorities say brought a gun and 400 rounds of ammunition to school told police he did it to defend a friend who was being bullied, according to court documents filed on Thursday.

The boy appeared in court at 9 a.m. local time and was appointed public counsel, said Clark County Juvenile Court Administrator Pat Escamilla. His mother and father were present, according to minutes from the appearance.

The boy brought several knives to his Vancouver, Wash., school on Wednesday along with the gun and ammunition, police said. Frontier Middle School, in Vancouver, Wash., was locked down for about two hours after ammunition was found at around 9:20 a.m. local time (12:20 p.m. ET).

NBC News has withheld the boy’s name because of his age. He is next due Clark County Juvenile Court at 9 a.m. local time on Friday.

In a declaration of probable cause filed in juvenile court on Thursday, police said that the boy’s mother called on Wednesday morning to report fears that her son had taken kitchen knives to school. The boy was held at school by the principal and assistant principal, and a pat-down revealed that he was carrying an unloaded .22 caliber semi-automatic handgun in his pants pocket, police allege in the document.

The boy was also found to be carrying two loaded .22 caliber magazines, according to the probable cause document. Officers said that the boy had “claimed in the presence of school officials that a ‘voice in his head’ was telling him to kill” another student after he had called the boy’s friend “gay,” the document states.

In an interview with police, the boy “stated a voice told him killing [another student] was a good idea; however he planned to just shoot [the student] in the arm and then shoot himself in the head,” according to the probable cause document. “After making theses (sic) admissions he asked to speak to his mother and his request was granted.”

Police said the boy was read his Miranda Rights and juvenile warnings and confirmed that he understood them. He was booked into Clark County Juvenile Detention Center on Wednesday, local NBC News affiliate KGW reported. The probable cause document listed possible charges of attempted assault in the first degree and unlawful possession of a firearm.

Both the middle school and the adjacent Pioneer Elementary School were put on lockdown, the Evergreen Public School district announced on its Facebook page, and reopened later in the day.

Police spokeswoman Kim Kapp told the newspaper that investigators with the Major Crimes Unit started conducting interviews at 11:30 a.m. (2:30 p.m. ET) on Wednesday and these went on well into the night.

The arrest “concludes the majority of our investigation,” Kapp told NBC News in an email on Thursday. “Detectives will continue to interview additional people to see if anyone has anything else but no more arrests are anticipated.”

In the probable cause document, police credited the boy’s mother with averting violence, writing, they “believe it’s reasonable to conclude based on the above mentioned information that [the boy] acted with premeditated intent to cause the death of another. He brought a firearm to the school with bullets, but failed to carry the act out due to his mother’s willingness to report him as a danger to the school and because of the swift action taken by school administrators and VPD officers.”

The school district notified parents about the incident through the district website as well as its Facebook page, district spokesman Kris Fay told local newspaper The Columbian. A text message and email alert system is only subscribed to by a small number of parents and often experiences delays, Fay told the paper.