I’m wondering if anyone has an example they could share of handling an AI Agent triggered MCP Elicitation callback. I realize this might not be the best fit for Streamlit given its linear execution model, but I’m hoping someone has found a solution.
I’d treat the elicitation callback as an out-of-band request rather than trying to render UI from inside it. The pattern I’d prototype is a background MCP session that writes the pending request into per-session state, then lets a normal Streamlit rerun render the form and pass the result back through a synchronization primitive. I don’t have a verified end-to-end example to claim this works with the current SDK callback contract, so that return contract and the cross-thread behavior are the first things I’d test.
You can handle asynchronous MCP elicitation/create callbacks cleanly inside Streamlit’s linear top-to-bottom execution model by bridging the background MCP client loop to the Streamlit UI thread with a queue.Queue + threading.Event stored in st.session_state and rendering the elicitation form inside an @st.fragment.
- When the MCP server sends an
elicitation/createJSON-RPC request mid-tool-call, the async MCP handler pushes the requested JSON schema intosession_state.elicitation_req_queueand awaits anasyncio.Future/threading.Event. - On the main thread, a polling
@st.fragment(run_every="0.5s")detects the pending elicitation, renders the dynamic input form (st.form), and when the user clicks Submit, pushes the user’s payload intoelicitation_resp_queueand unblocks the background MCP coroutine without restarting the agent run:
import queue
import threading
import streamlit as st
if "mcp_bridge" not in st.session_state:
st.session_state.mcp_bridge = {
"pending_prompt": None,
"response_value": None,
"event": threading.Event(),
}
def mcp_elicitation_callback(prompt_text: str) -> str:
"""Called from the background MCP client thread when the server requests user input."""
bridge = st.session_state.mcp_bridge
bridge["event"].clear()
bridge["pending_prompt"] = prompt_text
bridge["event"].wait(timeout=300) # Pause background tool call until user submits form
return bridge["response_value"] or ""
@st.fragment(run_every="0.5s")
def render_mcp_elicitation_modal():
bridge = st.session_state.mcp_bridge
if bridge["pending_prompt"]:
with st.form("mcp_elicitation_form"):
st.warning(f"Agent requires confirmation: {bridge['pending_prompt']}")
ans = st.text_input("Your response")
if st.form_submit_button("Submit to MCP Server"):
bridge["response_value"] = ans
bridge["pending_prompt"] = None
bridge["event"].set()
st.rerun(scope="fragment")
Using st.rerun(scope="fragment") keeps the rest of your Streamlit page intact while resuming the paused MCP tool execution.