Skip to content

fix(agent): align generate_wearing_sweater tool call with MCP schema#2

Open
GRayMillerDes wants to merge 4 commits into
cuppibla:mainfrom
GRayMillerDes:fix/adk-agent-tool-schema
Open

fix(agent): align generate_wearing_sweater tool call with MCP schema#2
GRayMillerDes wants to merge 4 commits into
cuppibla:mainfrom
GRayMillerDes:fix/adk-agent-tool-schema

Conversation

@GRayMillerDes

@GRayMillerDes GRayMillerDes commented Mar 28, 2026

Copy link
Copy Markdown

🐛 Overview

This Pull Request addresses four critical blockers found in the 01-starter codebases across both 03-Connect-ADK-MCP-UI and 04-Adding-Memory-Bank. These issues completely broke the image generation tool pipeline, multi-turn LLM sessions, and the Vertex AI Memory Bank deployment scripts.

🛠️ Fixes Applied

1. FastMCP Schema Validation Crash (agent.py)

  • Symptom: Generating a character wearing a sweater via image upload consistently failed with a 500 error on the very first turn.
  • Root Cause: The agent_instruction prompt instructed the LLM to pass pattern_description into the generate_wearing_sweater() tool. However, the MCP server exclusively accepts an image_path argument, triggering a strict Pydantic validation error downstream.
  • Resolution: Removed the invalid parameter from the prompt and enforced explicit Sequential Tool Execution (generating the pattern first, then generating the selfie). (Applied to both chapters 03 and 04).

2. TrafficType Enum Deepcopy Crash (main.py)

  • Symptom: Engaging in a multi-turn conversation instantly crashed the application with AttributeError: ON_DEMAND_PRIORITY.
  • Root Cause: ADK's InMemorySessionService attempts to use Python's native copy.deepcopy() to isolate session history. However, the latest google-genai SDK embeds custom Enum objects like TrafficType.ON_DEMAND_PRIORITY inside event metadata, which lack robust pickling support and cause deepcopy to aggressively fail when reconstructing the Enum attribute.
  • Resolution: Injected a global monkey patch at the top of main.py overriding copy.deepcopy to gracefully skip deepcopying TrafficType objects without mutating the original structure. (Applied to both chapters 03 and 04).

3. Missing Vertex AI Types & Engine Initialization (deploy_agent.py)

  • Symptom: Executing ./use_memory_bank.sh instantly failed with NameError: name 'GenerateMemoriesExample' is not defined.
  • Root Cause: The starter code for deploy_agent.py was fundamentally missing all Vertex AI SDK class definitions (MemoryBankConfig, GenerateMemoriesExample) and CustomizationConfig, leaving the shell script to die blindly.
  • Resolution: Completed the missing code by importing the correct types from vertexai.types and explicitly initializing the Agent Engine deployment block to properly align with 02-solution.

4. Memory Bank Retrieval Tool Missing (agent.py)

  • Symptom: Even after successfully deploying the Agent Engine and setting USE_MEMORY_BANK=true, the agent would still reply "I don't have a memory of your past preferences" upon restart.
  • Root Cause: The 01-starter codebase correctly initialized the after_agent_callback to save memories but lacked the PreloadMemoryTool in the agent_tools list to retrieve them. This meant the agent had "writing rights" but No "reading rights" to the Memory Bank.
  • Resolution: Conditionally injected PreloadMemoryTool() into the agent's toolset when USE_MEMORY_BANK is enabled. Also ensured AGENT_ENGINE_ID is correctly synchronized from the root .env to the local backend environment.

There was a strict schema validation mismatch crashing the agent framework when an image was uploaded.

Root cause:
The system prompt in \`backend/agent.py\` previously instructed the LLM to pass \`pattern_description\` as a kwarg to \[generate_wearing_sweater()\](cci:1://file:///Users/graymiller/Downloads/GitHub/holiday_workshop/03-Connect-ADK-MCP-UI/01-starter/backend/mcp_server.py:142:0-178:48). However, the corresponding tool defined in \`backend/mcp_server.py\` solely accepts an \`image_path\` argument.

Resolution:
- Removed the invalid \`pattern_description\` argument from the prompt examples and instructions.
- Enforced a sequential tool execution pipeline within the agent instructions, ensuring the model explicitly calls \`generate_sweater_pattern\` to create the static pattern image before subsequently calling \`generate_wearing_sweater\`.
…ma in chapter 4

Root cause:
The starter code in chapter 04 (04-Adding-Memory-Bank) had the same outdated schema definition bug, instructing the LLM to pass \`pattern_description\` to \[generate_wearing_sweater()\](cci:1://file:///Users/graymiller/Downloads/GitHub/holiday_workshop/04-Adding-Memory-Bank/01-starter/backend/mcp_server.py:142:0-178:48), which predictably crashed the agent when users requested to generate a sweater.

Resolution:
- Removed the invalid parameter from instructions in chapter 4.
- Handled sequential tool execution to explicitly require calling \`generate_sweater_pattern\` before generating the selfie.
Root Cause:
The starter code for deploy_agent.py was missing its mandatory Vertex AI Memory Bank types (e.g. GenerateMemoriesExample, CustomizationConfig) and the actual Agent Engine registration logic, causing it to crash with NameError.

Resolution:
Complete the starter deploy_agent.py by integrating the proper Vertex AI SDK class typings and the MemoryTopic customization configuration necessary for the deployment script (use_memory_bank.sh) to succeed and return an AGENT_ENGINE_ID.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant