I am trying to build a chatbot using my own MCP server with streamlit. But when executing the code, I am getting an exception "Event loop is closed".
For the first message I am getting the response as expected, but on the second call it is throwing the exception.
Code:
user_text = st.chat_input("Type a message…")
if user_text:
with st.chat_message("user"):
st.markdown(user_text)
st.session_state.history.append(HumanMessage(content=user_text))
# First pass: let the model decide whether to call tools
first = asyncio.run(st.session_state.llm_with_tools.ainvoke(st.session_state.history) #This line is throwing the exception
tool_calls = getattr(first, "tool_calls", None)