Here are some protective maneuvers that will block a lot of invasion attempts that are based on historic designs.
Layered walls. Many fortresses (which are a level above castle) would use concentric rings of walls. Depending on the era and the region, the outer wall was often made of rubble or piled stones, which would absorb cannonballs and other siege engine munition with less damage, were harder to blow holes in, and made a sappers life that much more difficult. Rubble also nerfs a lot of spells (disintigrate a section and the rest just falls into the hole, stoneshape is useless, etc).
The inner walls were often taller than the outer walls to ensure the defenders could continue to lay the smack down on invaders who breach the outer defenses.
Many fortresses have roads in between the walls, 20-40' wide, where cavalry, infantry or artillery (e.g. bombards) could be moved quickly. Narrower is better since it prevents invaders from having too much room to dodge the boiling oil & other crap the defenders would drop on them.
Moats were a similar defense, sappers who didn't go deep enough were drowned. Given the size of a fortress, a moat isn't adviseable unless it is built on an existing lake. You can bring back the lake by flooding one of the rings between walls, using magic to ensure a nice waterproof seal. Anyone breaches that wall and is hit with a flood. I tend to augment those water barriers IMC with amphibious predators, like alligators or sometimes just pirhana. I break up the water barriers up into separate tanks so that one breach doesn't drain the whole thing.
Trained animals. True, standard falcons wouldn't do drek against most foes but in theory you can train a giant falcon to hunt just like you could a regular one. Nothing like unleashing a mew full of giant birds of prey onto your aerial enemies. I also recommend giant bats to hunt the invisible, flying foes.
Same goes for defeating sappers; get a couple of animals with tremorsense. You could probably train Ankhegs to at least react to digging and could possible bribe xorn to be your lookouts.
Siege engines in fixed fortifications have more range than mobile ones simply because they can be built BIG and in a world of mechanical devices, size is important. Trebuchets are the big daddy of siege engines, using counterweights the size of small houses to lob refrigerator-sized munitions hundreds of yards. Though the use of frangible munitions, like sewage, was preferred to spread the damage around.
Which brings me to a point: Sewage is ammunition in a siege. The defender, via trebuchet, should have more range and be able to infect, infest, and generally infuriate the invaders.
Poisoning the water supply is probably more of a defender trick than that of an attacker. Any good fortress is on an independent, or incredibly massive, water supply. Wells, lakes, and massive cisterns are the order of the day. The defender's water supply will thus be protected from attack (through multiple guarded locations) or shared with the enemy (in the case of a lake). The attacker's water supply will probably be the primary target for all the trebuchet-fired sewage and corpses.
One trick for some defenders was to blow their own dams, thus denying their enemy a ready water supply while simultaneously ruining the crops. Oh sure, it ruins the crops but the defenders had already written them off as a loss so might as well deny them to your attacker.
Teleporting & gating is really a rather minor threat to most cities. The commanders simply stay in motion, cycling between locations within the city, and maintain a solid chain of command. Assuming the defenders have access to any reasonable amount of magic (simple divinations like "will our walls be breached today?" Will enemies get within our walls?") they can deal with the attacks.
Really, teleportation & gating is on par with a successful sapper making an access tunnel into the city. The good thing is that most of those spells put the caster at risk, either be coming along or creating a 2-way doorway.