Hugging Face 的 MCP 工具,用于 ADK(Automatic Document Creation)¶
在 ADK 中得到支持。PythonTypeScript
The Hugging Face MCP Server 该工具可用于将您的 ADK(Android Development Kit)代理连接到 Hugging Face Hub,以及数千个基于 Gradio 的 AI 应用程序。
使用场景¶
- 发现 AI/ML 资源:您可以根据任务、使用的库或关键词,在 Hub 中搜索和筛选模型、数据集以及相关论文。
- 构建多步骤工作流将各种工具串联起来使用,例如:先用一个工具将音频内容转录成文本,再用另一个工具对转录后的文本进行总结。
- 查找 AI 应用程序:搜索能够执行特定任务的 Gradio 工具(Gradio Spaces)——例如去除背景图像或实现文本转语音(text-to-speech)的功能。
先决条件¶
与代理程序(agent)一起使用。¶
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-flash-latest",
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 StreamableHTTPConnectionParams
HUGGING_FACE_TOKEN = "YOUR_HUGGING_FACE_TOKEN"
root_agent = Agent(
model="gemini-flash-latest",
name="hugging_face_agent",
instruction="Help users get information from Hugging Face",
tools=[
McpToolset(
connection_params=StreamableHTTPConnectionParams(
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-flash-latest",
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-flash-latest",
name: "hugging_face_agent",
instruction: "Help users get information from Hugging Face",
tools: [
new MCPToolset({
type: "StreamableHTTPConnectionParams",
url: "https://huggingface.co/mcp",
transportOptions: {
requestInit: {
headers: {
Authorization: `Bearer ${HUGGING_FACE_TOKEN}`,
},
},
},
}),
],
});
export { rootAgent };
可用的工具¶
| 工具 | 描述 |
|---|---|
| Spaces语义搜索 | 通过自然语言查询来找到最佳的人工智能应用程序。 |
| 论文语义搜索(Papers Semantic Search) | 通过自然语言查询来查找机器学习研究论文 |
| 模型搜索 | 使用过滤器(如任务类型、使用的库等)来搜索机器学习模型…… |
| 数据集搜索(Dataset Search) | 使用作者、标签等条件来搜索数据集…… |
| 文档语义搜索 | 在 Hugging Face 的文档库中搜索相关内容。 |
| 中心仓库详细信息 | 获取关于模型(Models)、数据集(Datasets)和空间(Spaces)的详细信息。 |
配置¶
要配置 Hugging Face Hub MCP 服务器中可使用的工具,请访问: MCP 设置页面 在你的 Hugging Face 账户中。
要配置本地的 MCP 服务器,您可以使用以下环境变量:
TRANSPORT:要使用的传输类型(stdio,sse,streamableHttp,或streamableHttpJson)DEFAULT_HF_TOKEN: ⚠️ 请求的处理方式是……HF_TOKEN该令牌是通过 `Authorization: Bearer` 标头接收到的;如果没有发送该标头,则会使用默认的 `DEFAULT_HF_TOKEN`。请仅在开发/测试环境或本地 STDIO 部署中设置此参数。⚠️- 如果使用 `stdio` 作为传输方式(即通过标准输入/输出进行数据传输),
HF_TOKEN如果满足某个条件,则使用该方法/功能。DEFAULT_HF_TOKEN该属性(`is`)未被设置(即其值为 `null` 或 `undefined`)。 HF_API_TIMEOUT:Hugging Face API 请求的超时时间(以毫秒为单位) (默认值:12500 毫秒,即 12.5 秒)USER_CONFIG_API:用于用户设置的 URL(默认为本地前端)。MCP_STRICT_COMPLIANCE当使用 JSON 模式发送 GET 请求时,如果请求被拒绝(即返回 405 错误),该参数应被设置为 `True`(默认值为 `False`);此时系统会显示一个欢迎页面。AUTHENTICATE_TOOL:是否需要包含一个 “Authenticate” 工具,以便在调用该工具时生成 OAuth 挑战码。SEARCH_ENABLES_FETCH当该参数被设置为 `true` 时,只要 `hf_doc_search` 被启用,`hf_doc_fetch` 工具也会自动被启用。