Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

Introduction to Agents Day 2

代理工具及與模型上下文協定 (MCP) 的互通性

第二天

深入了解工具的世界,了解 AI 代理如何透過利用外部功能和 API 來“採取行動”,並探索發現和使用模型上下文協定 (MCP) 提供的工具的便捷性。

Tool

讓LLM 知道或做到模型本身不能完成的任務

Tools 種類

  1. Function tools: 定義模型函數的文件
  2. Built-in tools: 模型可用的內部組件
  3. Agent tools: 可交移子Agent 結果

建立Tools準則

  1. 明確的文件: 輸入輸出與目的
  2. 指出模型可以做的事
  3. 好agent要能達成抽象複雜任務
  4. 不回覆原始資料,而是整理過的
  5. 要能處理錯誤

MCP Client-Server 互動( 透過JSON RPC)

  1. MCP server: 提供特定Tools
  2. MCP client:包含在server,將指令分派給要執行tool
  3. MCP host: 與使用者互動,決定何時使用何tool
  4. 技術上使用SSE

提示詞設計架構

  1. 角色
  2. 目標
  3. 規範
    • 輸出只有一個Python 程式碼區塊
    • 不要在此程式區塊前後添加文字
    • Python程式要能執行且可輸出結果
    • “禁止自己計算”,只需產出可計算程式

白皮書要點

  • 外部工具功能,允許智能體執行超出其訓練集範圍的操作或檢索即時數據
  • 模型上下文協定 (MCP),架構元件、通訊層、風險和企業級就緒度差距
  • 將自己的 Python 函數轉換為智慧體可以執行的操作
  • 使用 MCP 並實現長時間運行的操作,其中智能體可以在等待人工批准期間暫停工具調用,然後再恢復執行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
%pip install google-adk

from google.adk.agents import LlmAgent
from google.adk.runners import InMemoryRunner
from google.adk.tools import google_search, AgentTool, ToolContext
from google.genai import types

from google.adk.models.google_llm import Gemini
from google.adk.sessions import InMemorySessionService
from google.adk.code_executors import BuiltInCodeExecutor

# 連線設定
def show_python_code_and_result(response):
...
retry_config = types.HttpRetryOptions(...)

# 付款方式的匯率
def get_fee_for_payment_method(method: str) -> dict:
...

# 匯差
def get_exchange_rate(base_currency: str, target_currency: str) -> dict:
...

# 自訂Python Method Tools
currency_agent = LlmAgent(
name="currency_agent",
model=Gemini(model="gemini-2.5-flash-lite", retry_options=retry_config),
instruction="...提示詞...",
tools=[get_fee_for_payment_method, get_exchange_rate],
)

# 使用Agent換匯
currency_runner = InMemoryRunner(agent=currency_agent)
_ = await currency_runner.run_debug(
"I want to convert 500 US Dollars to Euros using my Platinum Credit Card. How much will I receive?"
)

# 也可以用ADK內建的code_executor Tool
calculation_agent = LlmAgent(
...,
code_executor=BuiltInCodeExecutor(),
)

# 將Agent加為另一個tool

enhanced_currency_agent = LlmAgent(
...,
tools=[
get_fee_for_payment_method,
get_exchange_rate,
AgentTool(agent=calculation_agent), # Using another agent as a tool!
],
)

# 撰寫MCP Tools並在Agent使用

mcp_image_server = McpToolset(
connection_params=StdioConnectionParams(
server_params=StdioServerParameters(
command="npx", # Run MCP server via npx
args=[
"-y", # Argument for npx to auto-confirm install
"@modelcontextprotocol/server-everything",
],
tool_filter=["getTinyImage"],
),
timeout=30,
)
)

image_agent = LlmAgent(
...
tools=[mcp_image_server],


如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels