本项目基于Gemini AI,对FastMCP(模型上下文协议)进行了自定义实现,可用于实时供应链优化。该项目受Anthropic内部FastMCP系统的启发,展示了低延迟的多工具编排能力。
pip install -r requirements.txt
python3 flask_app.py
访问 http://localhost:5000
若出于数据隐私和内部工具使用的考虑,你可以使用 local-llm-api 将Gemini API替换为你自己的本地大语言模型:
# 克隆并设置本地大语言模型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"
FastMCP并非开源项目,它是Anthropic的内部实现。本项目是对FastMCP关键创新点的最小模拟:
由于FastMCP未开源,我构建了一个低延迟多工具编排栈的最小模拟,展示了大语言模型代理如何通过路由工具对实时供应链更新做出可操作的建议。
本项目的使用流程如下:
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 │ │ Custom │ │ Gemini AI │
│ Interface │◄──►│ FastMCP │◄──►│ (or Local │
│ │ │ Server │ │ LLM API) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Event Stream │ │ Supply Chain │
│ (CSV Data) │ │ Tools │
└─────────────────┘ └─────────────────┘
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 # 本文件
DEMAND_SPIKE for P001 - 40 units
Event Stream → MCP Client: "DEMAND_SPIKE: P001, 40 units"
MCP Client → get_inventory_status: {"product_id": "P001"}
MCP Client → predict_stockout: {"product_id": "P001", "warehouse": "warehouse_A"}
MCP Client → calculate_transfer: {"product_id": "P001", "from_warehouse": "warehouse_B", "to_warehouse": "warehouse_A", "quantity": 20}
MCP Client → recommend_reorder: {"product_id": "P001", "quantity": 50}
MCP Client → User: "Transfer 20 units from B to A, reorder 50 units from Supplier X"
当前实现情况:✅ 部分实现
近期可添加的功能:
# 添加到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]:
"""根据延迟/成本建议供应重新路由"""
# 实现成本分析
示例对话:
Event: "SUPPLIER_DELAY: Supplier X, 3 days"
MCP Client: "Analyzing impact on P001, P002, P003..."
Tools Called: [get_inventory_status, calculate_correlation, forecast_demand, recommend_reroute]
Response: "Reroute P001 from Supplier X to Supplier Y. P002 and P003 show 0.8 correlation - adjust P002 orders accordingly."
需要修改的内容:
# 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]:
"""生成面向客户的摘要报告"""
示例对话:
User: "Analyze 500 survey responses about Product X"
MCP Client: "Processing responses with multiple agents..."
Tools Called: [extract_themes, compute_frequencies, generate_summary_report]
Response: "Top themes: UI/UX (45%), Performance (32%), Price (23%). 78% satisfaction rate (±3% CI). Report generated."
需要修改的内容:
# 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]:
"""生成医生笔记"""
示例对话:
Patient Data: {"symptoms": ["chest pain", "shortness of breath"], "vitals": {"bp": "140/90"}}
MCP Client: "Analyzing patient data..."
Tools Called: [check_symptoms, classify_risk, score_triage_priority, generate_doctor_note]
Response: "HIGH RISK - Cardiac symptoms detected. Immediate triage required. Doctor note: 'Patient presents with chest pain and elevated BP...'"
需要修改的内容:
# 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解释"""
示例对话:
Event: "COMPETITOR_PRICE_CHANGE: Product X, $25.99 → $22.99"
MCP Client: "Analyzing competitive landscape..."
Tools Called: [find_competitor_match, calculate_optimal_price, generate_markdown_explanation]
Response: "Competitor reduced price by 12%. Recommended action: Reduce price to $23.99. Explanation: 'We've adjusted our pricing to remain competitive while maintaining healthy margins...'"
supply_chain_tools.py
中添加函数fastmcp_server.py
中注册工具data/events.csv
中添加事件fastmcp_server.py
中更新事件处理逻辑fastmcp_server.py
以使用本地API端点本项目文档未提及许可证相关信息。
欢迎通过以下方式扩展本项目:
准备好使用AI优化你的供应链了吗? 启动服务器,见证奇迹发生!🚀
本项目展示了如何构建一个类似FastMCP的系统,用于实时多工具AI编排。