ADK 的 Hugging Face MCP 工具¶
Supported in ADKPythonTypeScript
可以利用 Hugging Face MCP 服务器 将你的 ADK 智能体连接到 Hugging Face Hub 和成千上万个 Gradio AI 应用程序。
使用场景¶
- 发现 AI/ML 资产:根据任务、库或关键词在 Hub 中搜索和过滤模型、数据集和论文。
- 构建多步工作流:将工具链接在一起,例如用一个工具转录音频,然后用另一个工具总结生成的文本。
- 查找 AI 应用程序:搜索可以执行特定任务(如背景去除或文本转语音)的 Gradio Space。
前置条件¶
与智能体一起使用¶
from google.adk.agents import Agent
from google.adk.tools.mcp_tool import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
from mcp import StdioServerParameters
HUGGING_FACE_TOKEN = "YOUR_HUGGING_FACE_TOKEN"
root_agent = Agent(
model="gemini-2.5-pro",
name="hugging_face_agent",
instruction="Help users get information from Hugging Face",
tools=[
McpToolset(
connection_params=StdioConnectionParams(
server_params = StdioServerParameters(
command="npx",
args=[
"-y",
"@llmindset/hf-mcp-server",
],
env={
"HF_TOKEN": HUGGING_FACE_TOKEN,
}
),
timeout=30,
),
)
],
)
from google.adk.agents import Agent
from google.adk.tools.mcp_tool import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPServerParams
HUGGING_FACE_TOKEN = "YOUR_HUGGING_FACE_TOKEN"
root_agent = Agent(
model="gemini-2.5-pro",
name="hugging_face_agent",
instruction="Help users get information from Hugging Face",
tools=[
McpToolset(
connection_params=StreamableHTTPServerParams(
url="https://huggingface.co/mcp",
headers={
"Authorization": f"Bearer {HUGGING_FACE_TOKEN}",
},
),
)
],
)
import { LlmAgent, MCPToolset } from "@google/adk";
const HUGGING_FACE_TOKEN = "YOUR_HUGGING_FACE_TOKEN";
const rootAgent = new LlmAgent({
model: "gemini-2.5-pro",
name: "hugging_face_agent",
instruction: "Help users get information from Hugging Face",
tools: [
new MCPToolset({
type: "StdioConnectionParams",
serverParams: {
command: "npx",
args: ["-y", "@llmindset/hf-mcp-server"],
env: {
HF_TOKEN: HUGGING_FACE_TOKEN,
},
},
}),
],
});
export { rootAgent };
import { LlmAgent, MCPToolset } from "@google/adk";
const HUGGING_FACE_TOKEN = "YOUR_HUGGING_FACE_TOKEN";
const rootAgent = new LlmAgent({
model: "gemini-2.5-pro",
name: "hugging_face_agent",
instruction: "Help users get information from Hugging Face",
tools: [
new MCPToolset({
type: "StreamableHTTPConnectionParams",
url: "https://huggingface.co/mcp",
header: {
Authorization: `Bearer ${HUGGING_FACE_TOKEN}`,
},
}),
],
});
export { rootAgent };
可用工具¶
| 工具 | 描述 |
|---|---|
| Spaces Semantic Search | 通过自然语言查询查找最佳 AI 应用 |
| Papers Semantic Search | 通过自然语言查询查找 ML 研究论文 |
| Model Search | 搜索 ML 模型,支持按任务、库等过滤 |
| Dataset Search | 搜索数据集,支持按作者、标签等过滤 |
| Documentation Semantic Search | 搜索 Hugging Face 文档库 |
| Hub Repository Details | 获取有关模型、数据集和 Space 的详细信息 |
配置¶
要配置 Hugging Face Hub MCP 服务器中可用的工具,请访问你的 Hugging Face 账户中的 MCP 设置页面。
要配置本地 MCP 服务器,可以使用以下环境变量:
TRANSPORT:使用的传输类型(stdio、sse、streamableHttp或streamableHttpJson)DEFAULT_HF_TOKEN:⚠️ 请求将使用在 Authorization: Bearer 头部收到的HF_TOKEN进行服务。如果没有发送头部,则使用DEFAULT_HF_TOKEN。仅在开发/测试环境或本地 STDIO 部署中设置此项。⚠️- 如果使用 stdio 传输运行,且未设置
DEFAULT_HF_TOKEN,则使用HF_TOKEN。 HF_API_TIMEOUT:Hugging Face API 请求的超时时间(以毫秒为单位)(默认:12500ms / 12.5 秒)USER_CONFIG_API:用于用户设置的 URL(默认为本地前端)MCP_STRICT_COMPLIANCE:设置为 True 以在 JSON 模式下拒绝 GET 405(默认提供欢迎页面)。AUTHENTICATE_TOOL:是否包含 Authenticate 工具以在调用时发出 OAuth 质询SEARCH_ENABLES_FETCH:当设置为 true 时,只要启用了hf_doc_search,就会自动启用hf_doc_fetch工具