产品目录API

提供基于FastAPI的产品目录API,并通过FastMCP暴露为AI工具,适用于AI代理如Claude Desktop。

  • by EyaBenSlamaaa
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 EyaBenSlamaaa/Lab2---Building-an-AI-Ready-Product-Catalog-with-FastAPI-and-MCP

使用说明

产品目录API + MCP服务器

描述

这个项目提供了一个 产品目录API 使用FastAPI,并通过FastMCP将其作为AI工具暴露出来。
它可以与人工智能代理一起使用,如 Claude Desktop(克劳德桌面版)。

  • FastAPI 端点用于列出所有产品并按ID获取产品。
  • FastMCP 将这些端点作为可由AI调用的工具进行暴露。

项目结构

产品目录实验室/ │ ├─ main.py # FastAPI 应用程序 ├─ mcp_server.py # 暴露工具的MCP服务器 ├─ venv/ # Python虚拟环境 └─ README.md(文件夹或文件名,可译为“README.md 文件/目录”)

YAML(YAML Ain't Markup Language,YAML不是标记语言) 复制代码


安装与设置

1️⃣ 克隆仓库并进入项目 git clone(克隆) 进入 product-catalog-lab 目录

2️⃣ 创建并激活虚拟环境 `python -m venv venv` 翻译成中文是:使用 Python 的内置模块 `venv` 创建一个名为 `venv` 的虚拟环境

Windows

venv\Scripts\activate(在命令行中)用于激活虚拟环境

3️⃣ 安装依赖项 使用 pip 安装 fastapi[all]、uvicorn 和 fastmcp

4️⃣ 第一部分:FastAPI 产品目录 启动服务器 使用 uvicorn 运行 main:app,主机设为 localhost,端口设为 8000,并启用重载功能

Tester les endpoints

Lister tous les produits

curl http://localhost:8000/products

通过ID获取产品

curl http://localhost:8000/products/1

Swagger UI

http://localhost:8000/docs

OpenAPI schema(开放应用程序编程接口模式)

curl http://localhost:8000/openapi.json > openapi.json

5️⃣ 第二部分:MCP服务器(FastMCP) 启动MCP服务器

在一个新航站楼内:

venv\Scripts\activate
python mcp_server.py

暴露的工具:list_products 和 get_product。

配置 Claude Desktop

Ouvrir Claude Desktop → Paramètres → Développeur → Éditer config

添加MCP配置:

{
  "mcpServers": {
  "product-catalog": {
  "command": "C:\\Users\\eyabe\\product-catalog-lab\\venv\\Scripts\\python.exe",
  "args": ["C:\\Users\\eyabe\\product-catalog-lab\\mcp_server.py"]
 }}}

重新启动Claude Desktop。

在聊天中启用工具

 Recherche et outils → Sélectionnez "product-catalog-mcp"

测试查询

Lister tous les produits

Récupérer un produit par ID (ex. 2)