这是一个使用Gemini AI进行实时供应链优化的自定义实现。该项目展示了受Anthropic内部FastMCP系统启发的低延迟、多工具编排。
FastMCP不是开源的——它是Anthropic的内部实现。这个项目是对FastMCP关键创新的一个最小模拟:
FastMCP不是开源的,所以我构建了一个基于它的低延迟多工具编排堆栈的最小模拟——展示了一个LLM代理如何通过路由工具对实时供应链更新做出可操作建议的响应。
pip install -r requirements.txt
python3 flask_app.py
导航到 http://localhost:5000
为了数据隐私和内部工具使用,你可以用local-llm-api替换Gemini API:
# 克隆并设置本地LLM API
git clone https://github.com/ANSH-RIYAL/local-llm-api.git
cd local-llm-api
./run_server.sh
# 修改fastmcp_server.py以使用本地API而不是Gemini
# 将GEMINI_API_KEY替换为CUSTOM_API_URL = "http://localhost:8050"
product_id(可选){"product_id": "P001"} → 返回仓库A/B/C的库存水平product_id,warehouse,quantity{"product_id": "P001", "warehouse": "warehouse_A", "quantity": -10}product_id,from_warehouse,to_warehouse,quantity{"product_id": "P001", "from_warehouse": "warehouse_B", "to_warehouse": "warehouse_A", "quantity": 20}product_id,warehouse{"product_id": "P001", "warehouse": "warehouse_A"} → “高风险,1-2天”product_id,quantity{"product_id": "P001", "quantity": 50} → “从供应商X订购50单位,单价$5.50/单位”supply_chain_tools.py中添加函数:def new_tool_function(self, param1: str, param2: int) -> Dict[str, Any]:
"""描述此工具的作用"""
# 实现逻辑
return {"success": True, "result": "工具输出"}
fastmcp_server.py中注册工具:Tool(
name="new_tool_function",
description="描述此工具的作用",
inputSchema={
"type": "object",
"properties": {
"param1": {"type": "string", "description": "参数1"},
"param2": {"type": "integer", "description": "参数2"}
},
"required": ["param1", "param2"]
}
)
handle_call_tool中添加处理器:elif name == "new_tool_function":
result = self.tools.new_tool_function(
arguments["param1"],
arguments["param2"]
)
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Flask Web │ │ 自定义 │ │ Gemini AI │
│ 界面 │◄──►│ FastMCP │◄──►│ (或本地 │
│ │ │ 服务器 │ │ LLM API) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ 事件流 │ │ 供应链 │
│ (CSV 数据) │ │ 工具 │
└─────────────────┘ └─────────────────┘
FastMCP/
├── data/
│ ├── inventory.csv # 产品库存数据
│ └── events.csv # 供应链事件流
├── templates/
│ └── index.html # Web界面
├── supply_chain_tools.py # 核心业务逻辑
├── fastmcp_server.py # 自定义FastMCP实现
├── flask_app.py # Web服务器和API
├── test_demo.py # 演示脚本
├── requirements.txt # Python依赖项
└── README.md # 本文件
P001需求激增 - 40单位事件流 → MCP客户端:"P001需求激增,40单位"
MCP客户端 → get_inventory_status:{"product_id": "P001"}
MCP客户端 → predict_stockout:{"product_id": "P001", "warehouse": "warehouse_A"}
MCP客户端 → calculate_transfer:{"product_id": "P001", "from_warehouse": "warehouse_B", "to_warehouse": "warehouse_A", "quantity": 20}
MCP客户端 → recommend_reorder:{"product_id": "P001", "quantity": 50}
MCP客户端 → 用户:"从B转移20单位到A,从供应商X补货50单位"
当前实现:✅ 部分实现
即将添加的内容:
# 添加到supply_chain_tools.py
def calculate_correlation(self, product1: str, product2: str) -> Dict[str, Any]:
"""计算产品之间的需求相关性"""
# 使用pandas相关性实现
def forecast_demand(self, product_id: str, periods: int) -> Dict[str, Any]:
"""使用简单的指数平滑预测需求"""
# 使用statsmodels实现
def recommend_reroute(self, from_supplier: str, to_supplier: str) -> Dict[str, Any]:
"""根据延误/成本推荐供应路线调整"""
# 使用成本分析实现
示例对话:
事件:"供应商X延误,3天"
MCP客户端:"分析P001、P002、P003的影响..."
调用工具:[get_inventory_status, calculate_correlation, forecast_demand, recommend_reroute]
响应:"将P001从供应商X调整到供应商Y。P002和P003显示0.8的相关性 - 据此调整P002订单。"
需要修改:
# 在survey_tools.py中新增工具
def extract_themes(self, responses: List[str]) -> Dict[str, Any]:
"""从调查回复中提取常见主题"""
def compute_frequencies(self, data: pd.DataFrame) -> Dict[str, Any]:
"""计算回复频率和置信区间"""
def generate_summary_report(self, insights: Dict) -> Dict[str, Any]:
"""生成面向客户的总结报告"""
示例对话:
用户:"分析关于产品X的500份调查回复"
MCP客户端:"使用多个代理处理回复..."
调用工具:[extract_themes, compute_frequencies, generate_summary_report]
响应:"主要主题:UI/UX(45%),性能(32%),价格(23%)。满意度78%(±3% CI)。报告已生成。"
需要修改:
# 在clinical_tools.py中新增工具
def check_symptoms(self, symptoms: List[str]) -> Dict[str, Any]:
"""检查症状与医学数据库匹配"""
def classify_risk(self, vitals: Dict) -> Dict[str, Any]:
"""分类患者风险等级"""
def score_triage_priority(self, risk: str, symptoms: List) -> Dict[str, Any]:
"""评分分诊优先级"""
def generate_doctor_note(self, patient_data: Dict) -> Dict[str, Any]:
"""生成医生笔记"""
示例对话:
患者数据:{"症状":["胸痛","呼吸困难"],"生命体征":{"血压":"140/90"}}
MCP客户端:"分析患者数据..."
调用工具:[check_symptoms, classify_risk, score_triage_priority, generate_doctor_note]
响应:"高风险 - 检测到心脏症状。需立即分诊。医生笔记:'患者出现胸痛和血压升高...' "
需要修改:
# 在pricing_tools.py中新增工具
def calculate_optimal_price(self, cost: float, margin: float, demand_factor: float) -> Dict[str, Any]:
"""使用公式计算最优价格"""
def find_competitor_match(self, product_id: str) -> Dict[str, Any]:
"""找到最近的竞争产品"""
def generate_markdown_explanation(self, price_change: Dict) -> Dict[str, Any]:
"""生成价格变化的markdown解释"""
示例对话:
事件:"竞争者价格变化:产品X,$25.99 → $22.99"
MCP客户端:"分析竞争环境..."
调用工具:[find_competitor_match, calculate_optimal_price, generate_markdown_explanation]
响应:"竞争者降价12%。建议行动:将价格降至$23.99。解释:'我们已调整价格以保持竞争力同时维持健康的利润率...' "
supply_chain_tools.py中添加函数fastmcp_server.py中注册工具data/events.csv中添加事件fastmcp_server.py中的事件处理逻辑fastmcp_server.py以使用本地API端点欢迎扩展此项目:
准备好用AI优化您的供应链了吗? 启动服务器,见证奇迹的发生!🚀
此项目展示了如何构建一个类似FastMCP的自定义系统,用于实时、多工具AI编排。