MCP 课程文档
设置项目
加入 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` 文件中。
后续步骤
项目结构和环境设置好后,我们就可以
- 创建 MCP 服务器 - 实现核心标签功能
- 构建 Webhook 监听器 - 处理传入的讨论事件
- 集成代理 - 将 MCP 工具与 webhook 处理连接
- 测试和部署 - 验证功能并部署到 Spaces
在下一节中,我们将深入创建 MCP 服务器,它将处理所有 Hugging Face Hub 交互。
请确保您的 `.env` 文件安全,切勿将其提交到版本控制。`.env` 文件应添加到您的 `.gitignore` 文件中,以防止秘密意外泄露。