[Python] Problems with streamlit using MCP client

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    Having issues with Streamlit, it does not seem compatible with an MCP client. Getting NotImplemented error.

    This is the relevant streamlit code:

    available_scripts = ["mcp_server1.py", "mcp_server2.py"]
    selected_script = st.selectbox("MCP server script", available_scripts)
    if st.button("Enable MCP server"):
    mcp_client = MCPClient()
    asyncio.run(mcp_client.connect_to_server(selected_script))


    Now my MCPClient looks like this (based on MCP quickstart):

    class MCPClient:
    def __init__(self):
    self.session: ClientSession | None = None
    self.exit_stack = AsyncExitStack()

    async def connect_to_server(self, server_script_path: str):
    is_python = server_script_path.endswith(".py")
    is_js = server_script_path.endswith(".js")
    if not (is_python or is_js):
    raise ValueError("Server script must be a .py or .js file")
    command = "python" if is_python else "node"
    server_params = StdioServerParameters(
    command=command,
    args=[server_script_path],
    env=None
    )

    stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
    self.stdio, self.write = stdio_transport
    self.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))

    await self.session.initialize()

    ...


    If I do this, I get a strange NotImplemented error. I will paste the error at the end, it looks like a streamlit thing.

    What I discovered after debugging a bit is that the cause is await self.exit_stack.enter_async_context(...). If I remove everything after that and replace it with await asyncio.sleep(3), then it waits 3 seconds and does not fail.

    I have looked around, but have not found a solution. I have seen somewhere to try:

    import nest_asyncio
    nest_asyncio.apply()


    But either I am not putting it in the right place or this is not the problem, because I have seen no changes.

    Please help! Any idea why it is failing?

    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 121, in exec_func_with_error_handling
    result = func()
    ^^^^^^
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 595, in code_to_exec
    self._session_state.on_script_will_rerun(
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\streamlit\runtime\state\safe_session_state.py", line 68, in on_script_will_rerun
    self._state.on_script_will_rerun(latest_widget_states)
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\streamlit\runtime\state\session_state.py", line 558, in on_script_will_rerun
    self._call_callbacks()
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\streamlit\runtime\state\session_state.py", line 571, in _call_callbacks
    self._new_widget_state.call_callback(wid)
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\streamlit\runtime\state\session_state.py", line 272, in call_callback
    callback(*args, **kwargs)
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\app.py", line 146, in toggle_mcp
    asyncio.run(mcp_client.test(st.session_state["mcp_script"]))
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\nest_asyncio.py", line 30, in run
    return loop.run_until_complete(task)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\nest_asyncio.py", line 98, in run_until_complete
    return f.result()
    ^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\asyncio\futures.py", line 203, in result
    raise self._exception.with_traceback(self._exception_tb)
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\asyncio\tasks.py", line 277, in __step
    result = coro.send(None)
    ^^^^^^^^^^^^^^^
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\mcp_client.py", line 52, in test
    stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 650, in enter_async_context
    result = await _enter(cm)
    ^^^^^^^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\contextlib.py", line 210, in __aenter__
    return await anext(self.gen)
    ^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\mcp\client\stdio\__init__.py", line 115, in stdio_client
    process = await _create_platform_compatible_process(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\mcp\client\stdio\__init__.py", line 230, in _create_platform_compatible_process
    process = await create_windows_process(command, args, env, errlog, cwd)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\mcp\client\stdio\win32.py", line 85, in create_windows_process
    process = await anyio.open_process(
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\anyio\_core\_subprocesses.py", line 190, in open_process
    return await get_async_backend().open_process(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\danid\Documents\Personal\Projects\llm_chat_room\.venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 2561, in open_process
    process = await asyncio.create_subprocess_exec(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\asyncio\subprocess.py", line 223, in create_subprocess_exec
    transport, protocol = await loop.subprocess_exec(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 1708, in subprocess_exec
    transport = await self._make_subprocess_transport(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 503, in _make_subprocess_transport
    raise NotImplementedError

    Continue reading...

Compartilhe esta Página