feat: 添加 Strapi 配置文件支持
- 新增 config.yaml:集中管理 Strapi API 配置 - 新增 config_loader.py:配置加载模块 - 更新 http_routes.py:从配置文件读取 API 端点 - 支持从 YAML 文件配置 FAQ 分类和语言 - 更新 requirements.txt:添加 pyyaml 依赖 优势: - 配置与代码分离,易于维护 - 添加新分类无需修改代码 - 支持热加载配置 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
126
mcp_servers/strapi_mcp/config_loader.py
Normal file
126
mcp_servers/strapi_mcp/config_loader.py
Normal file
@@ -0,0 +1,126 @@
|
||||
"""
|
||||
Strapi MCP 配置加载器
|
||||
从 YAML 配置文件加载 Strapi API 配置
|
||||
"""
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class LanguageConfig(BaseModel):
|
||||
"""语言配置"""
|
||||
code: str
|
||||
name: str
|
||||
|
||||
|
||||
class CategoryConfig(BaseModel):
|
||||
"""FAQ 分类配置"""
|
||||
endpoint: str
|
||||
description: str
|
||||
keywords: List[str] = []
|
||||
|
||||
|
||||
class StrapiConfig(BaseModel):
|
||||
"""Strapi 配置"""
|
||||
base_url: str
|
||||
api_token: str = ""
|
||||
languages: List[LanguageConfig] = []
|
||||
faq_categories: Dict[str, CategoryConfig] = {}
|
||||
info_sections: Dict[str, Dict] = {}
|
||||
|
||||
|
||||
def load_config(config_path: Optional[str] = None) -> StrapiConfig:
|
||||
"""加载配置文件
|
||||
|
||||
Args:
|
||||
config_path: 配置文件路径,默认为 config.yaml
|
||||
|
||||
Returns:
|
||||
StrapiConfig: 配置对象
|
||||
"""
|
||||
if config_path is None:
|
||||
# 默认从当前目录的 config.yaml 加载
|
||||
config_path = Path(__file__).parent / "config.yaml"
|
||||
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
config_data = yaml.safe_load(f)
|
||||
|
||||
return StrapiConfig(**config_data)
|
||||
|
||||
|
||||
def get_category_endpoint(category: str, config: Optional[StrapiConfig] = None) -> str:
|
||||
"""获取分类对应的 API 端点
|
||||
|
||||
Args:
|
||||
category: 分类名称
|
||||
config: 配置对象
|
||||
|
||||
Returns:
|
||||
str: API 端点
|
||||
"""
|
||||
if config is None:
|
||||
config = load_config()
|
||||
|
||||
if category in config.faq_categories:
|
||||
return config.faq_categories[category].endpoint
|
||||
|
||||
# 如果没有找到,返回默认格式
|
||||
return f"faq-{category}"
|
||||
|
||||
|
||||
def get_supported_languages(config: Optional[StrapiConfig] = None) -> List[str]:
|
||||
"""获取支持的语言代码列表
|
||||
|
||||
Args:
|
||||
config: 配置对象
|
||||
|
||||
Returns:
|
||||
List[str]: 语言代码列表
|
||||
"""
|
||||
if config is None:
|
||||
config = load_config()
|
||||
|
||||
return [lang.code for lang in config.languages]
|
||||
|
||||
|
||||
def get_all_categories(config: Optional[StrapiConfig] = None) -> Dict[str, str]:
|
||||
"""获取所有分类及其描述
|
||||
|
||||
Args:
|
||||
config: 配置对象
|
||||
|
||||
Returns:
|
||||
Dict[str, str]: 分类名称 -> 描述的映射
|
||||
"""
|
||||
if config is None:
|
||||
config = load_config()
|
||||
|
||||
return {
|
||||
name: cat.description
|
||||
for name, cat in config.faq_categories.items()
|
||||
}
|
||||
|
||||
|
||||
# 导出配置单例
|
||||
_global_config: Optional[StrapiConfig] = None
|
||||
|
||||
|
||||
def get_config() -> StrapiConfig:
|
||||
"""获取全局配置单例"""
|
||||
global _global_config
|
||||
if _global_config is None:
|
||||
_global_config = load_config()
|
||||
return _global_config
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 测试配置加载
|
||||
config = load_config()
|
||||
print(f"✅ 配置加载成功")
|
||||
print(f"Base URL: {config.base_url}")
|
||||
print(f"支持语言: {[lang.code for lang in config.languages]}")
|
||||
print(f"FAQ 分类: {list(config.faq_categories.keys())}")
|
||||
print(f"\n分类详情:")
|
||||
for name, cat in config.faq_categories.items():
|
||||
print(f" - {name}: {cat.description} (/{cat.endpoint})")
|
||||
Reference in New Issue
Block a user