Ms Cox, 41, is Labour MP for Batley and Spen. She won the seat in the 2015 general election.
Reports at the scene said the attacker - believed to be a man - remained at large.
Ms Cox was conducting a surgery at the library for constituents.
She is believed to have been airlifted to Leeds General Infirmary.
It is understood that other MPs in the area are considering closing their offices for the rest of the day as a precautionary measure.
There was speculation among some fellow MPs that Ms Cox may have been targeted over her failure to vote against air strikes in Syria.
A source close to Ms Cox said: “It’s just a massive shock. She has been a huge force for good for her constituency since she was elected.”
A police helicopter is hovering above the area.
Labour Chief Whip Rosie Winterton, the MP for Doncaster Central, is reported to have told colleagues that the incident is “serious”.
Witnesses said an onlooker tackled a man to the floor and tried to take a gun from him. Moments later, a second shot was heard.
An eyewitness said: “The whole scene has been taped off. There are armed police everywhere.”
One eye-witness told the Yorkshire Post: “I was just on my way to the Tesco in the middle of Birstall at about quarter past one when I realised a helicopter was in the air but didn’t think any more of it.
“Then as I got nearer to the library I heard a girl on her phone talking about a man with a gun and I saw the police tape and ambulances.
“It must have only just happened because there was at least one person still lying in the road being treated by paramedics.
“There was a big police presence and a few of them were armed, which was a bit unnerving to see so close to home.
“At about half one I saw a man being carried on a stretcher into an ambulance.
“Another person is supposed to have been injured but I didn’t see any sign of them.
“The last I saw was just before two o’clock, the whole of the road outside the library was still taped off and a lot of streets round about.
“It’s market day in Birstall so the area will have been busy when this happened. It’s just a shocking situation.”
Another eyewitness saw a man being put into an ambulance, shortly before 1.30pm.
There were at least four ambulances parked nearby, and a heavy police presence including armed officers.
Shocked locals stood on street corners while others chose to remain in their homes.
A member of her Ms Cox’s said: “I’ve just arrived at the scene but I’ve not been told anything more.
“She was doing a surgery. I got a call from a colleague who was with her and I came straight over. There was an ambulance there. She has been injured but I don’t know how or what.”
Cambridge-educated Ms Cox, who was born in West Yorkshire, was an Oxfam aid worker before entering parliament last year.
Despite being a relative newcomer to Westminster, Labour colleagues have repeatedly tipped her to rise to the top of the party.
Daniel Blyth, 38, a company director from Birstall, said: “I saw seven police cars racing up Leeds Road towards Morley. They were unmarked BMWs.
“There are two helicopters flying overhead.
“It all seems very dramatic. I have heard that two people have been shot but I’m not who are why. It’s all very unusual for Birstall.”
Tearful Hichem Ben-Abdallah, owner of the nearby Azzuro restaurant, rushed to the scene after the first shot.
She said: “They’ve taken the victim to hospital - she’s in a bad state.
“I had customers. I was just serving them drinks, then all of a sudden we heard screaming.
“I saw a river of people rushing down the hill. They were screaming - and pulling something behind a car.
“A lady was down on the floor. The man pulled a gun - it was a makeshift gun, not like something you see on television.
“After the second shot I turned and ran. He walked away very calmly, down the steps. Nobody stopped him - he had a gun.
“It’s like I’m dreaming. Even the litter bins have been cordoned off - maybe he dropped something in there.
Terri Flynn-Edwards, owner of Divine Hair Salon, Birstall, which is just yards from the library, said: “I have heard from several customers coming in that an MP has been shot outside the library. I am shocked, this is a quiet village.”
Councillor Robert Light (Con, Birstall & Birkenshaw) said: “I’m shocked to hear that something like this has happened.”