1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] WebSocket connection closes when calling MCP tools in Google Cloud Run

Discussão em 'Python' iniciado por Stack, Setembro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm building a travel planner agent using MCP (Model Context Protocol) servers with Python and Streamlit. The application works perfectly locally but fails when deployed to Google Cloud Run with WebSocket connection issues.

    Expected behavior: MCP tools should connect to external servers (Airbnb MCP, Travel Planner MCP) and return data successfully, just like they do locally.

    Actual behavior: WebSocket connections close immediately in the Cloud Run environment, causing the MCP tool calls to fail.

    Error Messages:
    Console Error:

    WebSocket connection closed unexpectedly
    Connection to MCP server failed: Connection closed


    Cloud Run Logs:

    [ERROR] WebSocket handshake failed
    [ERROR] MCP server connection timeout after 60 seconds


    Environment:


    1. Local: Python 3.11, Windows (working)


    2. Cloud Run: Python 3.11, Container environment (failing)


    3. Libraries: streamlit, asyncio, websockets (specify exact versions)


    4. MCP Servers: @openbnb/mcp-server-airbnb, @gongrzhe/server-travelplanner-mcp

    How can I configure Cloud Run to allow these MCP server connections to work properly?

    Cloud Config

    Service travel-planner in region us-central1

    URL: https://travel-planner-809145899609.us-central1.run.app
    Ingress: all
    Traffic:
    100% LATEST (currently travel-planner-00009-zvv)
    Scaling: Auto (Min: 0)
    Last updated on 2025-09-12T13:28:38.830709Z by xyz@123gmail.com:
    Revision travel-planner-00009-zvv
    Container None
    Image: gcr.io/ai-agent-travel-planner/travel-planner
    Port: 8080
    Memory: 2Gi
    CPU: 1000m
    Secrets:
    GEMINI_API_KEY GEMINI_API_KEY:latest
    GOOGLE_MAPS_API_KEY GOOGLE_MAPS_API_KEY:latest
    Startup Probe:
    TCP every 240s
    Port: 8080
    Initial delay: 0s
    Timeout: 240s
    Failure threshold: 1
    Type: Default
    Service account: 809145899609-compute@developer.gserviceaccount.com
    Concurrency: 80
    Max instances: 20
    Timeout: 300s


    Example

    import os
    import asyncio
    import MultiMCPTools

    async def run_mcp_travel_planner(destination: str, num_days: int,
    preferences: str, budget: int,
    openai_key: str, google_maps_key: str):
    try:
    # Set Google Maps API key environment variable
    os.environ["GOOGLE_MAPS_API_KEY"] = google_maps_key

    # Initialize MCPTools with Airbnb MCP
    mcp_tools = MultiMCPTools(
    [
    "npx -y @openbnb/mcp-server-airbnb --ignore-robots-txt",
    "npx @gongrzhe/server-travelplanner-mcp",
    ],
    env={
    "GOOGLE_MAPS_API_KEY": google_maps_key,
    },
    timeout_seconds=60,
    )

    # This works locally but fails in Cloud Run
    await mcp_tools.connect() # WebSocket connection closes here


    except Exception as e:
    print(f"MCP connection failed: {e}")
    raise

    # Test function
    async def main():
    await run_mcp_travel_planner("Paris", 3, "museums", 1000, "key", "maps_key")

    if __name__ == "__main__":
    asyncio.run(main())

    Continue reading...

Compartilhe esta Página