Hub documentation
使用 SDK 进行构建
单点登录(SSO) 审计日志 存储区域 用于私有数据集的 Data Studio 资源组(访问控制) 高级计算选项 高级安全 令牌管理 服务账号 发布者分析 门控组集合 网络安全 下载分析 速率限制 博客文章
PRO 计划 仓库 入门仓库 仓库设置 存储限制 存储后端(Xet) 本地缓存 Pull Requests 与 Discussions 通知 集合 Webhooks GitHub Actions Notebooks 后续步骤 许可证
模型 数据集 Spaces Spaces 概览 Spaces GPU 升级 Spaces ZeroGPU Spaces 开发模式 Spaces 磁盘使用量与存储 Spaces 自定义域名 Spaces 作为 MCP 服务器 Spaces 作为智能体工具 将 Spaces 用作 API 端点 Gradio Spaces Streamlit Spaces 静态 HTML Spaces Docker Spaces 嵌入您的 Space 使用 Docker 运行 Spaces Spaces 配置参考 使用 HF 登录按钮 精选 Spaces Spaces 更新日志 高级主题
存储桶 new 容器镜像 new 任务 智能体 智能体概述 Hugging Face AI 智能体 CLI Hugging Face MCP 服务器 Hugging Face Agent Skills 使用 HF SDK 构建智能体 使用 llama.cpp 的本地代理 Agent 库 会话跟踪格式
其他 使用 SDK 进行构建
使用 Hugging Face 智能体 SDK 构建基于 MCP 的智能体。huggingface_hub(Python)和 @huggingface/tiny-agents(JavaScript)库提供了将 LLM 连接到 MCP 工具所需的一切。
安装
Python
JavaScript
pip install "huggingface_hub[mcp]"npm install @huggingface/tiny-agents
# or
pnpm add @huggingface/tiny-agents快速入门:运行一个 Agent
最快的入门方式是使用 tiny-agents CLI:
Python
JavaScript
tiny-agents run julien-c/flux-schnell-generator
npx @huggingface/tiny-agents run "julien-c/flux-schnell-generator"这将从 tiny-agents 集合 中加载一个智能体,连接到其 MCP 服务器,并启动交互式聊天。
使用 Agent 类
Agent 类负责管理聊天循环和 MCP 工具执行。它使用 Inference Providers 来运行 LLM。
Python
JavaScript
from huggingface_hub import Agent
import asyncio
agent = Agent(
model="Qwen/Qwen2.5-72B-Instruct",
provider="novita",
servers=[
{
"type": "sse",
"url": "https://huggingface.co/proxy/evalstate-flux1-schnell.hf.space/gradio_api/mcp/sse"
}
]
)
async def main():
async for chunk in agent.run("Generate an image of a sunset"):
if hasattr(chunk, 'choices'):
delta = chunk.choices[0].delta
if delta.content:
print(delta.content, end="")
asyncio.run(main())有关所有选项,请参阅 Agent 参考。
import { Agent } from "@huggingface/tiny-agents";
const agent = new Agent({
model: "Qwen/Qwen2.5-72B-Instruct",
provider: "novita",
apiKey: process.env.HF_TOKEN,
servers: [
{
type: "sse",
url: "https://huggingface.co/proxy/evalstate-flux1-schnell.hf.space/gradio_api/mcp/sse"
}
]
});
await agent.loadTools();
for await (const chunk of agent.run("Generate an image of a sunset")) {
if ("choices" in chunk) {
const delta = chunk.choices[0]?.delta;
if (delta.content) {
console.log(delta.content);
}
}
}有关所有选项,请参阅 tiny-agents 文档。
直接使用 MCPClient
如需更多控制,请使用 MCPClient 直接管理 MCP 服务器和工具调用。
Python
JavaScript
import asyncio
from huggingface_hub import MCPClient
async def main():
async with MCPClient(
model="Qwen/Qwen2.5-72B-Instruct",
provider="novita",
) as client:
# Connect to an MCP server
await client.add_mcp_server(
type="sse",
url="https://huggingface.co/proxy/evalstate-flux1-schnell.hf.space/gradio_api/mcp/sse"
)
# Process a request with tools
messages = [{"role": "user", "content": "Generate an image of a sunset"}]
async for chunk in client.process_single_turn_with_tools(messages):
if hasattr(chunk, 'choices'):
delta = chunk.choices[0].delta
if delta.content:
print(delta.content, end="")
asyncio.run(main())有关所有选项,请参阅 MCPClient 参考。
JavaScript SDK 使用 Agent 类进行 MCP 交互。如需更底层的控制,请参阅 @huggingface/mcp-client 包。
分享您的 Agent
在 Hub 上为 tiny-agents 集合 贡献智能体。包括:
agent.json- 智能体配置(必需)PROMPT.md或AGENTS.md- 系统提示词(可选)EXAMPLES.md- 示例提示词和使用场景(可选)
了解更多
- huggingface_hub MCP 参考 - Python API 参考
- tiny-agents 文档 - JavaScript API 参考
- 推理提供商 - 可用的 LLM 提供商
- tiny-agents 集合 - 浏览社区智能体
- MCP 服务器指南 - 连接到 Hugging Face MCP 服务器