-1

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)

enter image description here

New contributor
Anjali Yadav is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • Presumably related: stackoverflow.com/questions/69035759/… Commented 2 days ago
  • always put error/ouput as text, not image - we can't copy text from image to use it in comment, answer or search in Google. Commented yesterday
  • always put full error message (traceback) because there are other useful information. Commented yesterday

2 Answers 2

-2
if user_text:
    with st.chat_message("user"):
        st.markdown(user_text)
    st.session_state.history.append(HumanMessage(content=user_text))

    # Use synchronous invoke instead
    first = st.session_state.llm_with_tools.invoke(st.session_state.history)
    tool_calls = getattr(first, "tool_calls", None)
New contributor
Rohit Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-2

user_text = st.char_input("Type a message....")

if user_text:

with st.chat_message("user

New contributor
Aditya Dubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.