Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

Hub 上的智慧體

此頁面彙集了 Hugging Face 為智慧體工作流程提供的所有庫和工具。

  • HF MCP 伺服器: 將您的 MCP 相容 AI 助手直接連線到 Hugging Face Hub。
  • tiny-agents: 一個輕量級的 MCP 驅動智慧體工具包,提供 JS (@huggingface/tiny-agents) 和 Python (huggingface_hub) 版本。
  • Gradio MCP 伺服器: 輕鬆地從 Gradio 應用和 Spaces 建立 MCP 伺服器。
  • smolagents: 一個 Python 庫,使您能夠用幾行程式碼執行強大的智慧體。

HF MCP 伺服器

官方的 Hugging Face MCP (模型上下文協議) 伺服器 實現了 Hugging Face Hub 與任何 MCP 相容 AI 助手(包括 VSCode、Cursor 和 Claude Desktop)之間的無縫整合。

透過 HF MCP 伺服器,您可以直接連線到 Hub 的生態系統,從而增強 AI 助手的效能。它包含:

  • 一套精選的內建工具,如 Spaces 和論文語義搜尋、模型和資料集探索等。
  • MCP 相容的 Gradio 應用:連線到 Hugging Face 社群構建的任何MCP 相容 Gradio 應用

入門

請訪問huggingface.co/settings/mcp來配置您的 MCP 客戶端並開始使用。

此功能處於實驗階段 ⚗️ 並將持續發展。

smolagents

smolagents 是一個輕量級庫,用於以少量程式碼行涵蓋所有智慧體用例,從程式碼編寫智慧體到計算機使用。它與模型無關,支援使用 Hugging Face Transformers 提供的本地模型,以及透過推理提供商提供的模型和專有模型提供商。

它提供了一種獨特的智慧體:CodeAgent,它是一個用 Python 程式碼編寫其動作的智慧體。它還支援大多數其他智慧體框架那樣以 JSON blob 編寫動作的標準智慧體,稱為ToolCallingAgent。要了解有關在程式碼與 JSON 中編寫動作的更多資訊,請檢視我們在DeepLearning.AI 上的新短期課程

如果您想避免自己定義智慧體,啟動智慧體最簡單的方法是透過 CLI,使用 smolagent 命令。

smolagent "Plan a trip to Tokyo, Kyoto and Osaka between Mar 28 and Apr 7." \
--model-type "InferenceClientModel" \
--model-id "Qwen/Qwen2.5-Coder-32B-Instruct" \
--imports "pandas numpy" \
--tools "web_search"

智慧體可以作為 Spaces 推送到 Hugging Face Hub。在此檢視人們構建的所有酷炫智慧體。

smolagents 還支援 MCP 伺服器作為工具,如下所示:

# pip install --upgrade smolagents mcp
from smolagents import MCPClient, CodeAgent
from mcp import StdioServerParameters
import os

server_parameters = StdioServerParameters(
    command="uvx",  # Using uvx ensures dependencies are available
    args=["--quiet", "pubmedmcp@0.1.3"],
    env={"UV_PYTHON": "3.12", **os.environ},
)

with MCPClient(server_parameters) as tools:
    agent = CodeAgent(tools=tools, model=model, add_base_tools=True)
    agent.run("Please find the latest research on COVID-19 treatment.")

文件中瞭解更多資訊。

tiny-agents (JS 和 Python)

tiny-agents 是一個輕量級工具包,用於在 Hugging Face Inference Client + 模型上下文協議 (MCP) 的基礎上執行和構建 MCP 驅動的智慧體。它以 JS 包 @huggingface/tiny-agentshuggingface_hub Python 包的形式提供。

@huggingface/tiny-agents (JS)

@huggingface/tiny-agents 包提供了一個簡單直接的 CLI 和一個簡單的程式設計 API,用於在 JS 中執行和構建 MCP 驅動的智慧體。

入門指南

首先,您需要安裝該軟體包

npm install @huggingface/tiny-agents
# or
pnpm add @huggingface/tiny-agents

然後,您可以執行您的智慧體

npx @huggingface/tiny-agents [command] "agent/id"

Usage:
  tiny-agents [flags]
  tiny-agents run   "agent/id"
  tiny-agents serve "agent/id"

Available Commands:
  run         Run the Agent in command-line
  serve       Run the Agent as an OpenAI-compatible HTTP server

您可以直接從tiny-agents資料集載入智慧體,或者指定您自己的本地智慧體配置的路徑。

高階用法 除了 CLI,您還可以使用 Agent 類進行更精細的控制。對於更低級別的互動,請使用 @huggingface/mcp-client 包中的 MCPClient 直接連線到 MCP 伺服器並管理工具呼叫。

huggingface.js 文件中瞭解有關 tiny-agents 的更多資訊。

huggingface_hub (Python)

huggingface_hub 庫是在 Python 中執行 MCP 驅動智慧體最簡單的方法。它包括一個高階 tiny-agents CLI 以及透過 AgentMCPClient 類進行程式設計訪問——所有這些都旨在與 Hugging Face Inference Providers、本地 LLM 或任何與 OpenAI API 規範相容的推理端點一起使用。

快速入門

安裝支援 MCP 的最新版本

pip install "huggingface_hub[mcp]>=0.32.2"

然後,您可以執行您的智慧體

> tiny-agents run --help
                                                                                                                                                                                     
 Usage: tiny-agents run [OPTIONS] [PATH] COMMAND [ARGS]...                                                                                                                           
                                                                                                                                                                                     
 Run the Agent in the CLI                                                                                                                                                            
                                                                                                                                                                                     
                                                                                                                                                                                     
╭─ Arguments ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│   path      [PATH]  Path to a local folder containing an agent.json file or a built-in agent stored in the 'tiny-agents/tiny-agents' Hugging Face dataset                         │
│                     (https://huggingface.co/datasets/tiny-agents/tiny-agents)                                                                                                     │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --help          Show this message and exit.                                                                                                                                       │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

CLI 會拉取配置,連線到其 MCP 伺服器,列印可用工具,然後等待您的提示。

高階用法

為了進行更精細的控制,直接使用 MCPClient。這個低階介面擴充套件了 AsyncInferenceClient,允許 LLM 透過模型上下文協議 (MCP) 呼叫工具。它支援本地 (stdio) 和遠端 (http/sse) MCP 伺服器,處理工具註冊和執行,並將結果即時流回模型。

huggingface_hub MCP 文件中瞭解更多資訊:huggingface_hub MCP 文件

自定義智慧體

要建立您自己的智慧體,只需建立一個資料夾(例如,my-agent/),並在 agent.json 檔案中定義您的智慧體的配置。以下示例展示了一個網路瀏覽智慧體,該智慧體配置為透過 Nebius 推理提供商使用 Qwen/Qwen2.5-72B-Instruct 模型,並且它配備了一個 playwright MCP 伺服器,使其可以使用網路瀏覽器。

{
	"model": "Qwen/Qwen2.5-72B-Instruct",
	"provider": "nebius",
	"servers": [
		{
			"type": "stdio",
			"command": "npx",
			"args": ["@playwright/mcp@latest"]
		}
	]
}

要使用本地 LLM(例如 llama.cppLM Studio),只需提供一個 endpointUrl

{
	"model": "Qwen/Qwen3-32B",
	"endpointUrl": "https://:1234/v1",
	"servers": [
		{
			"type": "stdio",
			"command": "npx",
			"args": ["@playwright/mcp@latest"]
		}
	]
}

(可選)新增 PROMPT.md 以自定義系統提示。

不要猶豫,透過在tiny-agents Hugging Face 資料集中開啟拉取請求,將您的智慧體貢獻給社群。

Gradio MCP 伺服器/工具

您只需幾行 Python 程式碼即可使用 Gradio 構建 MCP 伺服器。如果您有一個現有的 Gradio 應用或 Space,想要用作 MCP 伺服器/工具,只需更改一行程式碼。

要使 Gradio 應用程式成為 MCP 伺服器,只需在啟動您的演示時傳入 mcp_server=True,如下所示。

# pip install gradio

import gradio as gr

def generate_image(prompt: str):
   """
   Generate an image based on a text prompt
   
   Args:
       prompt: a text string describing the image to generate
   """
   pass

demo = gr.Interface(
    fn=generate_image,
    inputs="text",
    outputs="image",
    title="Image Generator"
)

demo.launch(mcp_server=True)

MCP 伺服器將在您的應用程式所在的 http://your-space-id.hf.space/gradio_api/mcp/sse 地址可用。它將為您的 Gradio 應用程式中的每個函式提供一個對應的工具,工具描述將自動從函式的文件字串生成。

最後,將其新增到您選擇的 MCP 客戶端(例如 Cursor)的設定中。

{
  "mcpServers": {
    "gradio": {
      "url": "http://your-server:port/gradio_api/mcp/sse"
    }
  }
}

這非常強大,因為它允許 LLM 將任何 Gradio 應用程式用作工具。您可以在Spaces上找到數千個這樣的應用程式。在此瞭解更多資訊。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.