### Setup Python Virtual Environment and Install Dependencies Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md This snippet provides commands to set up a Python virtual environment, activate it, and install necessary dependencies like `google-adk`, `mcp-flight-search`, `google-generativeai`, and `python-dotenv`. It also shows how to set required environment variables for Google API key and SerpAPI key. ```bash # Setup virtual environment (Mac or Unix) python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate # Install dependencies pip install google-adk # Agent Development Kit pip install mcp-flight-search # MCP server pip install google-generativeai python-dotenv # GenAI Python SDK # Note: ADK uses GOOGLE_API_KEY instead of GEMINI_API_KEY export GOOGLE_API_KEY="your-google-api-key" export SERP_API_KEY="your-serpapi-key" ``` -------------------------------- ### Set Up Python Virtual Environment and Dependencies Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md Commands to create and activate a Python virtual environment, then install all required project dependencies listed in `requirements.txt` using pip. Includes notes for Windows users. ```bash # Setup virtual environment (Mac or Unix) python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate # Install all dependencies from requirements.txt pip install -r requirements.txt ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md Steps to copy the example environment file (`.env.example`) to `.env` and then edit it to add actual API keys for services like Google API and SerpAPI, which are necessary for the application to function. ```bash # Copy the example environment file cp .env.example .env # Edit the .env file with your actual API keys # Replace placeholders with your actual keys: # GOOGLE_API_KEY=your-actual-google-api-key # SERP_API_KEY=your-actual-serpapi-key ``` -------------------------------- ### Example User Query for Flight Search Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md This snippet provides an example of a user query string used to demonstrate the flight search assistant's capabilities. It shows the typical input format for a flight search request. ```text "Find flights from Atlanta to Las Vegas 2025-05-05" ``` -------------------------------- ### Start ADK Client Application Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md Command to run the main client script (`client.py`), which initiates the application. This process involves connecting to the MCP Flight Search server, initializing the ADK agent, and processing queries. ```bash python client.py ``` -------------------------------- ### Verify MCP Flight Search Server Installation Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md This command verifies that the `mcp-flight-search` server is correctly installed in the Python environment, displaying its package information. ```bash pip show mcp-flight-search ``` -------------------------------- ### Connect to MCP Flight Search Server and Get Tools Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md This asynchronous Python function `get_tools_async` connects to the `mcp-flight-search` server using `StdioServerParameters`. It retrieves the available tools from the server, which are then used by the ADK agent. It also manages the server's lifecycle with an exit stack. ```python # --- Step 1: Get tools from MCP server --- async def get_tools_async(): """Gets tools from the Flight Search MCP Server.""" print("Attempting to connect to MCP Flight Search server...") server_params = StdioServerParameters( command="mcp-flight-search", args=["--connection_type", "stdio"], env={"SERP_API_KEY": os.getenv("SERP_API_KEY")}, ) tools, exit_stack = await MCPToolset.from_server( connection_params=server_params ) print("MCP Toolset created successfully.") return tools, exit_stack ``` -------------------------------- ### Initialize ADK Runner and Session Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md This Python snippet demonstrates how to initialize an in-memory session service, create a new session, define a user query, format it as `types.Content`, and set up the ADK `Runner` with the agent and session service. It prepares the environment for executing an ADK agent. ```python async def async_main(): # Create services session_service = InMemorySessionService() # Create a session session = session_service.create_session( state={}, app_name='flight_search_app', user_id='user_flights' ) # Define the user prompt query = "Find flights from Atlanta to Las Vegas 2025-05-05" print(f"User Query: '{query}'") # Format input as types.Content content = types.Content(role='user', parts=[types.Part(text=query)]) # Get agent and exit_stack root_agent, exit_stack = await get_agent_async() # Create Runner runner = Runner( app_name='flight_search_app', agent=root_agent, session_service=session_service, ) ``` -------------------------------- ### Clone ADK Python MCP Client Repository Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md Instructions to clone the project repository from GitHub and navigate into the project directory using standard Git and Bash commands. ```bash git clone https://github.com/arjunprabhulal/adk-python-mcp-client.git cd adk-python-mcp-client ``` -------------------------------- ### ADK Python MCP Client Project Directory Structure Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md An overview of the project's file and directory layout, detailing the purpose of key files and folders such as `Images/`, `client.py`, `README.md`, `requirements.txt`, and environment variable files. ```text adk-python-mcp-client/ ├── Images/ │ ├── adk-mcp-client-log.gif # Standard logging demo animation │ ├── adk-mcp-client-debug.gif # Debug mode demo animation │ └── image.png # Architecture diagram ├── client.py # ADK client implementation ├── README.md # Project documentation ├── requirements.txt # Dependencies ├── .env.example # Environment variables template └── .env # Environment variables (not tracked by Git) ``` -------------------------------- ### Create ADK LlmAgent with MCP Server Tools Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md This asynchronous Python function `get_agent_async` creates an `LlmAgent` instance, equipping it with the tools fetched from the MCP server. It configures the agent with a specific Gemini model, a name, and an instruction for flight search tasks, handling one-way trips by using an empty string for the return date. ```python # --- Step 2: Define ADK Agent Creation --- async def get_agent_async(): """Creates an ADK Agent equipped with tools from the MCP Server.""" tools, exit_stack = await get_tools_async() print(f"Fetched {len(tools)} tools from MCP server.") # Create the LlmAgent root_agent = LlmAgent( model=os.getenv("GEMINI_MODEL", "gemini-2.5-pro-preview-03-25"), name='flight_search_assistant', instruction='Help user to search for flights using available tools based on prompt. If return date not specified, use an empty string for one-way trips.', tools=tools, ) return root_agent, exit_stack ``` -------------------------------- ### Execute ADK Agent and Process Events Source: https://github.com/arjunprabhulal/adk-python-mcp-client/blob/main/README.md This Python code shows how to execute the ADK `Runner` asynchronously with a new message. It then iterates through the received events and ensures proper cleanup of resources, such as closing the MCP server connection using an `exit_stack`. ```python print("Running agent...") events_async = runner.run_async( session_id=session.id, user_id=session.user_id, new_message=content ) # Process events async for event in events_async: print(f"Event received: {event}") # Always clean up resources print("Closing MCP server connection...") await exit_stack.aclose() print("Cleanup complete.") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.