Preheat your oven to 350°F (175°C).
Cook the spaghetti in a large pot of salted boiling water according to the package instructions until al dente. Drain and set aside.
In a skillet, melt the butter over medium heat. Add the onions and garlic, sautéing until the onions are translucent (about 3-4 minutes).
Add the sliced mushrooms and cook until softened, about 5 minutes.
Stir in the flour and cook for 1 minute, stirring constantly.
Gradually whisk in the chicken broth and milk, bringing the mixture to a simmer. Cook until the sauce thickens, about 5 minutes.
Season the sauce with salt, black pepper, and thyme. Stir in the shredded chicken, peas, and half of the Parmesan cheese until well combined.
In a large mixing bowl, combine the cooked spaghetti with the chicken mixture, ensuring the pasta is evenly coated.
Pour the mixture into a greased 3-quart baking dish. Sprinkle the top with the remaining Parmesan and the mozzarella cheese.
Bake in the preheated oven for 20-25 minutes or until the cheese is bubbly and golden.
For a crispy topping, broil for an additional 2-3 minutes at the end of baking.