这个项目是为了作为增强(或改进)而创建的 即时清理工具(或“快速清理器”) 这个是用TypeScript编写的,而且还做了一些增强。这是我“罗塞塔”石碑项目,通过它我可以更容易地深入理解Python。哦,当然,这个项目是在Cursor的帮助下编写的
MCP 提示清理器
一个利用人工智能(AI)来增强和净化原始提示的模型上下文协议(MCP)服务器,使其更加清晰、可操作且有效。
特点/功能
- AI赋能的增强利用大型语言模型提高提示的清晰度和具体性
- 简洁的系统提示采用结构化、高效的提示格式以确保结果的一致性
- 上下文感知处理接受额外上下文以指导增强过程
- 模式特定优化支持“通用”和“代码”两种模式,以适应不同的使用场景
- 质量评估为增强后的提示提供质量评分和详细反馈
- 两级重试策略针对网络问题的HTTP级别重试,针对AI输出质量的内容级别重试
- 指数退避具有抖动功能的健壮错误处理机制,以防止雷鸣般涌入的问题
- MCP集成完全符合MCP协议,支持stdio传输
- 已准备好投入生产全面的测试覆盖率、整洁的代码以及稳健的错误处理
安装
使用紫外线(推荐)
uv sync使用 pip
pip install -e .注:本项目使用 pyproject.toml 用于依赖管理。
配置
本地大型语言模型(LMStudio)- 默认设置
服务器默认配置为与本地大型语言模型(如LMStudio)一起工作。无需API密钥:
# Default configuration (no .env file needed)
# LLM_API_ENDPOINT=http://localhost:1234/v1/chat/completions
# LLM_API_KEY=None (not required for local LLMs)
# LLM_MODEL=local-model云端大型语言模型(OpenAI、Anthropic 等)
对于基于云的大型语言模型(LLMs),创建一个 .env 项目根目录下的文件:
# LLM API Configuration
LLM_API_ENDPOINT=https://api.openai.com/v1/chat/completions
LLM_API_KEY=your-api-key-here
LLM_MODEL=gpt-4
LLM_TIMEOUT=60
LLM_MAX_TOKENS=600
# Retry Configuration
CONTENT_MAX_RETRIES=2注: .env 文件支持由……提供 pydantic-settings - 无需额外依赖。
LMStudio 安装设置
- 下载并安装 LMStudio
- 启动LMStudio并加载一个模型
- 启动本地服务器(通常在
http://localhost:1234) - MCP服务器将自动连接到您的本地大型语言模型(LLM)
运行服务器
运行MCP服务器:
python main.py工具使用
服务器提供了一个 clean_prompt 接受以下输入的工具:
raw_prompt(必填):用户的原始、未经润色的提示context(可选):关于任务的额外背景信息mode(可选):处理模式 - “通用”或“代码”(默认:“通用”)temperature(可选):AI采样温度 0.0-1.0(默认值:0.2)
示例工具调用
该工具直接以参数形式调用:
# Direct function call
result = await clean_prompt_tool(
raw_prompt="help me write code",
context="web development with Python",
mode="code",
temperature=0.1
)或者通过MCP协议:
{
"method": "tools/call",
"params": {
"name": "clean_prompt",
"arguments": {
"raw_prompt": "help me write code",
"context": "web development with Python",
"mode": "code",
"temperature": 0.1
}
}
}示例回复
{
"cleaned": "Help me write Python code for web development. I need assistance with [specific task] using [framework/library]. The code should [requirements] and handle [error cases].",
"notes": [
"Added placeholders for specific task and framework",
"Specified requirements and error handling"
],
"open_questions": [
"What specific web development task?",
"Which Python framework?",
"What are the exact requirements?"
],
"risks": ["Without specific details, the code may not meet requirements"],
"unchanged": false,
"quality": {
"score": 4,
"reasons": ["Clear structure", "Identifies missing information", "Actionable guidance"]
}
}MCP客户端配置
Claude Desktop(可译为“Claude桌面版”或保持原样,根据上下文判断是否需要具体化为某类软件或应用的名称)
对于本地大型语言模型(LMStudio)- 不需要API密钥
{
"mcpServers": {
"mcp-prompt-cleaner": {
"command": "python",
"args": ["main.py"]
}
}
}对于云端大语言模型(如OpenAI等)- 需要API密钥
{
"mcpServers": {
"mcp-prompt-cleaner": {
"command": "python",
"args": ["main.py"],
"env": {
"LLM_API_KEY": "your-api-key-here",
"LLM_API_ENDPOINT": "https://api.openai.com/v1/chat/completions",
"LLM_MODEL": "gpt-4"
}
}
}
}其他MCP客户端
服务器使用stdio传输方式,并且可以通过指向任何兼容MCP的客户端进行配置 main.py 文件。
发展
运行测试
uv run pytest测试覆盖率
该项目包括以下方面的全面测试:
- 从混合内容中提取JSON
- 带有重试逻辑的LLM客户端
- 即时清洁功能
- MCP协议集成
项目结构
├── main.py # MCP server with tool registration
├── config.py # Configuration management
├── schemas.py # Pydantic models for validation
├── tools/
│ └── cleaner.py # Main clean_prompt implementation
├── llm/
│ └── client.py # AI API client with retry logic
├── utils/
│ └── json_extractor.py # JSON extraction utilities
├── prompts/
│ └── cleaner.md # AI system prompt
└── tests/ # Comprehensive test suite要求
- Python 3.11+(或可译为“Python 3.11及以上版本”)
- MCP Python SDK(MCP Python软件开发工具包)
- HTTP 客户端的 httpx
- 使用pydantic进行数据验证
- 使用 pytest 进行测试
许可证
麻省理工学院(MIT)
