Could be quite a few things causing it to surge at idle, however I would suspect fuel pressure, it would be good to put a gauge on it and see what's going on at idle. Next, check the coolant temp as reported by a scanner. Check for vacuum leaks as well.
These engines and trucks are getting old, it's a good idea to 'refresh' the grounds, the one at the back of the cylinder head on the passenger side that goes to the frame can rot off on rust belt trucks, as well as the one from the frame to the cab itself. Also a good idea to remove and clean the one that goes to the engine block at the front lower passenger side, and the ones that go to the thermostat housing.
Another thing to check is the coolant full? If the coolant has trapped air or is low, the sensor reading the coolant temp will give a false reading to the computer... and as we know: Garbage In, Garbage Out ( GIGO )
After those, if it's high mileage, you may be in need of a new TPS, they wear out and can cause issues as well.
Good idea to pull the plugs and check the gaps and condition of them if you don't know the history of the truck, it's not a hard job. If any are oil fouled or the gap is excessive, put in a fresh set of Iridiums.
Cap and rotor could also be suspect, pop off the cap and have a look at the contacts, if they look like they have a build up and the rotor looks in the least bit worn, toss on a new set as well, they aren't terribly expensive.