代理开发工具包集成

展示Google代理开发工具包(ADK)与模型上下文协议(MCP)的集成,通过实际求职场景演示功能工具、代理工具和MCP工具的使用。

  • by zey-2
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 zey-2/ADK_MCP

使用说明

ADK MCP演示库

概述

该存储库使用真实的求职场景演示了谷歌的代理开发工具包(ADK)与模型上下文协议(MCP)的集成。它通过示例展示了所有三种ADK工具类型——功能工具、代理工具和MCP工具。

工具类型

  • 功能工具:与真实API集成的自定义Python函数(FindSGJobs.com)
  • 代理工具:将复杂任务委托给其他代理的专业代理
  • MCP工具:通过标准化MCP协议进行外部服务集成

目录

安装说明

先决条件

  • Python 3.13+
  • Google API密钥(Gemini API访问)

三步设置

  1. 安装依赖项:

    conda env create -f environment.yml
    conda activate google-adk
    
  2. 配置API密钥:

    # Create a .env file in the root directory
    echo "GOOGLE_API_KEY=your_google_api_key_here" > .env
    # Then edit .env and replace with your actual API key
    
  3. 运行演示:

    # Demo 1: Function Tools
    python 1_function_tools_demo.py
    
    # Demo 2: Agent Tools
    python 2_agent_tools_demo.py
    
    # Demo 3: MCP Integration
    python 3_demo_mcp_with_adk.py
    

文件结构

ADK_MCP/
├── 1_function_tools_demo.py       # Function tools demo
├── 2_agent_tools_demo.py          # Agent tools demo
├── 3_demo_mcp_with_adk.py         # MCP + ADK integration demo
├── findsgjobs_mcp_server.py       # MCP server implementation
├── function_tool.py               # Shared API functions
├── environment.yml                # Conda dependencies
├── .env                           # API keys (create this)
└── README.md                      # This file

每个演示的代理工具概述

工具类型

功能工具

成为代理工具的自定义Python函数

何时使用

  • 您有自定义业务逻辑
  • 需要调用外部API
  • 要求确定性、快速执行
  • 希望完全控制实施

示例实现

def search_jobs(keywords: str, page: int = 1) -> dict:
    """
    Search FindSGJobs API for job listings.

    Args:
        keywords: Search terms (e.g., "software engineer")
        page: Page number for pagination

    Returns:
        Structured dict with status and job data
    """
    try:
        response = requests.get(
            "https://www.findsgjobs.com/apis/job/search",
            params={"keywords": keywords, "page": page},
            timeout=10
        )
        response.raise_for_status()

        data = response.json()
        jobs = parse_job_results(data)

        return {
            "status": "success",
            "total_jobs": data.get("total", 0),
            "jobs": jobs
        }
    except Exception as e:
        return {
            "status": "error",
            "error_message": str(e)
        }

代理工具

使用专用代理作为其他代理的工具

何时使用

  • 需要专业知识
  • 需要复杂的分析或推理
  • 需要代码生成和执行
  • 多步骤问题解决

示例实现

# Create specialist agent
data_analyst = LlmAgent(
    name="DataAnalystAgent",
    model=Gemini(model="gemini-2.5-flash-lite"),
    instruction="""You are a data analyst specializing in job market analysis.
    When given job data, generate Python code to analyze trends and provide insights.
    Always print your results to stdout.""",
    code_executor=BuiltInCodeExecutor(),
)

# Use as tool in main agent
job_assistant = LlmAgent(
    name="JobSearchAssistant",
    instruction="Use DataAnalystAgent for complex market analysis",
    tools=[AgentTool(agent=data_analyst)],
)

MCP工具

连接到外部模型上下文协议服务器

何时使用

  • 想要使用社区构建的集成
  • 需要标准化的外部服务访问
  • 不希望编写自定义集成代码
  • 想要生态系统兼容性

示例实现

# Connect to MCP server
mcp_toolset = McpToolset(
    connection_params=StdioConnectionParams(
        server_params=StdioServerParameters(
            command="python",
            args=["findsgjobs_mcp_server.py"],
        ),
        timeout=30,
    )
)

# Use in agent
agent = LlmAgent(
    tools=[mcp_toolset]
)

许可证

以学习为目的的教育示范。