MCP 課程文件

設定專案

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

設定專案

在本節中,我們將為拉取請求代理設定開發環境。

我們將使用現代 Python 工具和 `uv` 進行依賴管理,並建立必要的配置檔案。如果您不熟悉 `uv`,可以在此處瞭解更多資訊。

專案結構

讓我們首先建立專案目錄並瞭解檔案結構

git clone https://huggingface.co/spaces/mcp-course/tag-this-repo

我們的最終專案結構將如下所示

hf-pr-agent/
├── mcp_server.py              # Core MCP server with tagging tools
├── app.py                     # FastAPI webhook listener and agent
├── requirements.txt           # Python dependencies
├── pyproject.toml             # Project configuration
├── env.example                # Environment variables template
├── cleanup.py                 # Development utility

依賴和配置

讓我們來看看我們專案的依賴項和配置。

1. Python 專案配置

我們將使用 `uv` 來建立 `pyproject.toml` 檔案來定義我們的專案

如果您尚未安裝 `uv`,可以按照此處的說明進行操作。

[project]
name = "mcp-course-unit3-example"
version = "0.1.0"
description = "FastAPI and Gradio app for Hugging Face Hub discussion webhooks"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
    "fastapi>=0.104.0",
    "uvicorn[standard]>=0.24.0",
    "gradio>=4.0.0",
    "huggingface-hub[mcp]>=0.32.0",
    "pydantic>=2.0.0",
    "python-multipart>=0.0.6",
    "requests>=2.31.0",
    "python-dotenv>=1.0.0",
    "fastmcp>=2.0.0",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["src"]

為了與各種部署平臺相容,`requirements.txt` 中也重複了相同的內容

要建立虛擬環境,請執行

uv venv
source .venv/bin/activate # or .venv/Scripts/activate on Windows

要安裝依賴項,請執行

uv sync

2. 環境配置

建立 `env.example` 以記錄所需的環境變數

# Hugging Face API Token (required)
# Get from: https://huggingface.co/settings/tokens
HF_TOKEN=hf_your_token_here

# Webhook Secret (required for production)
# Use a strong, random string
WEBHOOK_SECRET=your-webhook-secret-here

# Model for the agent (optional)
HF_MODEL=owner/model

# Provider for MCP agent (optional)
HF_PROVIDER=huggingface

您需要從此處獲取您的 Hugging Face API 令牌。

您還需要生成一個 webhook 金鑰。您可以透過執行以下命令來完成此操作

python -c "import secrets; print(secrets.token_hex(32))"

然後,您需要根據 `env.example` 檔案將 webhook 金鑰新增到您的 `.env` 檔案中。

後續步驟

專案結構和環境設定好後,我們就可以

  1. 建立 MCP 伺服器 - 實現核心標籤功能
  2. 構建 Webhook 監聽器 - 處理傳入的討論事件
  3. 整合代理 - 將 MCP 工具與 webhook 處理連線
  4. 測試和部署 - 驗證功能並部署到 Spaces

在下一節中,我們將深入建立 MCP 伺服器,它將處理所有 Hugging Face Hub 互動。

請確保您的 `.env` 檔案安全,切勿將其提交到版本控制。`.env` 檔案應新增到您的 `.gitignore` 檔案中,以防止秘密意外洩露。

< > 在 GitHub 上更新

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