Heat the oven to 375°F. Blanch the beans in boiling water until bright green and just tender, 4 to 5 minutes. Drain, cool under cold water and pat dry.
Cook mushrooms in butter until their liquid evaporates, then add garlic and flour. Whisk in stock and milk and simmer until thick. Season with salt and pepper.
Fold beans into the sauce, transfer to a greased 2-quart dish and bake for 15 to 20 minutes. Scatter the onions on top and bake 5 more minutes.