How real cities handled their ports
Ancient cities tended not to be located on the water, but rather a few miles inland with a client village on the water. This way, they had the utility of ocean access, while still being protected. If the village was lost, no big deal.
As cities got larger they tended to connect the sea-side village with the city via walls. This new and larger city was surrounded by walls on all sides, but often had an inner wall between the port and the actual city. This allowed for greater protection against seaside attacks. Not to mention the port itself may be walled off into sections. These sections might mean that there are 2 harbor areas or the open area only holds ships. All houses and warehouses being behind walls in the 2nd harbor area.
Further improvements were the usage of a sea wall, a wall that literally is standing in the water, so besides the entry to the port it is completely protected. Even the entry typically had a chain possibly with logs that was strung across it. These methods protected the main city, and while the port was not totally safe it was reasonably safe, at least against ships.
In your world, biology and the usage of magic skews things a little, so while the above would still be used you potentially need to add a few additional features. The levels of precaution being reliant on how much money and effort you are willing to invest, and whether the burning of a part of the port concerns you that much. Also, whether these merfolk carry weapons and armor or are simply aquatic naked people.
At night a large chain net is spread across the opening of the port. It might be possible for someone to squeeze through but they would not be able to carry much if any equipment.
With glass: Have a few buoys with bells that are towed right inside and outside the entrance to the harbor. Divers with diving helmets or a diving sphere sit at the bottom and ring the bell if they see attackers.
Without glass: Build a few thin towers, basically thick poles, with a wooden tube carrying air to the bottom of the harbor. At the top of these towers are large bells. Divers suck on the tube and watch the entrance. If they see attackers they pull on the rope and ring the bell.
Buoys are more flexible, but ring faintly. Being rung will be louder than waves though. Towers cost more, are inflexible, but will not ring from the tide.
Same as above, but with water breathing. Maybe have "detect life" active.
Whether your solution is mundane or magical, you WILL need more towers and many more guards. Most ports would have towers, but most would focus outwards. You need towers facing inwards. They only needed a few guards as ships were seen from far away and the chain at night kept them out. Now you need many more guards, as people are harder to spot and keep out than ships.
You need towers to face inwards and have archers or mages constantly watching the harbor. If merpeople attack they need to immediately start firing. Depending on how heavily armed the merfolk are, you may need to sacrifice the particular harbor segment.
Throw pots of pitch, oil, or greek fire onto the water and light the surface. The water will catch fire either driving the merfolk onto a narrow dock or piece of land. The small area and many towers will create a killing field. Alternatively, they will have to retreat as the water will heat and cause burns and possible oxygen problems.
Unless the city messes up, is heavily outnumbered, the merfolk are somehow heavily armed (unlikely), or the merfolk can throw around a ton of magic or explosives, the city will be safe. The port and ships may be lost or raided, but the merfolk will pay dearly for it (seriously they will die).
However the cost of employing so many guards will mean that only cities can do this, as villages simply do not have enough people. So maybe back to the split coastal and interior villages.
Much more effective and safer for merfolk to raid small villages who cannot afford to designate standing guards, regardless of detection. Merfolk could also swim up river to unsuspecting villages.