[Python] Running MCP Server from py web view app on a diffreent thread after packaging

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have Py Web View app and I need to run an MCP server in the backround.
    I am using MultiProcessing.
    In a python environment, everything works fine.
    I want it to package it as an exe, and I am using pyinstaller for that matter.
    The packaging process completes without error.
    My issue is that when I run the exe, the process is respawned infinitely. Meaning that the main window keeps being opened in new instances of the app. How can I stop it from respawning infinitely?

    My MCP File:

    mcp = FastMCP("resume_fetcher")


    @mcp.tool()
    async def get_files():
    """

    """
    return 'using tool'


    # Run the server with streamable-http transport
    class MCPRunner:
    def run_mcp(self):

    # Set server configuration through the settings property
    mcp.settings.mount_path = "/mcp"
    mcp.settings.port = 8765
    mcp.settings.host = "127.0.0.1"

    # Run the server with streamable-http transport
    logging.debug(f"Starting MCP server at http://{mcp.settings.host}:{mcp.settings.port}{mcp.settings.mount_path}")
    mcp.run(transport="streamable-http")


    my py web view app:

    class CommandsAutomatorApi:
    def init_mcp(self, mcp_runner):
    self.mcp_process = multiprocessing.Process(target=mcp_runner.run_mcp)
    self.mcp_process.start()

    def __del__(self):
    """Cleanup method to terminate the MCP process when the agent is destroyed"""
    if hasattr(self, 'mcp_process') and self.mcp_process.is_alive():
    self.mcp_process.terminate()
    self.mcp_process.join()
    def main():
    api = CommandsAutomatorApi()
    mcp_runner = MCPRunner()
    api.init_mcp(mcp_runner)
    window = webview.create_window(
    'Commands Automator',
    'ui/commands_automator.html',
    js_api=api,
    width=1000,
    height=800
    )
    webview.start(icon='ui/resources/Commands_Automator.ico')

    if __name__ == '__main__':
    main()

    Continue reading...

Compartilhe esta Página