Hub 文件

使用 SDK 進行開發

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

使用 SDK 進行開發

利用 Hugging Face 代理 (agentic) SDK 構建支援 MCP 的 Agent。huggingface_hub (Python) 和 @huggingface/tiny-agents (JavaScript) 程式庫提供了將 LLM 連接到 MCP 工具所需的一切。

安裝

Python
JavaScript
pip install "huggingface_hub[mcp]"

快速入門:執行一個 Agent

最快上手的方法是使用 tiny-agents CLI

Python
JavaScript
tiny-agents run julien-c/flux-schnell-generator

這將從 tiny-agents 集合中載入一個 Agent,連接到其 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://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 參考文件以了解所有選項。

直接使用 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://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 參考文件以了解所有選項。

分享您的 Agent

歡迎將 Agent 貢獻至 Hub 上的 tiny-agents 集合。請包含:

  • agent.json - Agent 設定檔(必填)
  • PROMPT.mdAGENTS.md - 系統提示詞(選填)
  • EXAMPLES.md - 範例提示詞與使用案例(選填)

深入了解

在 GitHub 上更新

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