MCP Prompt Cleaner

MCP Prompt Cleaner

Enhances and cleans raw prompts using AI to make them more clear, actionable, and effective. Provides quality assessment, suggestions, and supports both general and code-specific optimization modes.

Category
访问服务器

README

This project was created as an enchancement to Prompt Cleaner that was written in Typescript. As well as an enhancement. This is my 'rosetta' stone project. That I can easily follow to have a deeper understanding on Python. Oh obvious this was Coded with the help of Cursor

MCP Prompt Cleaner

Prompt Cleaner Banner

A Model Context Protocol (MCP) server that uses AI to enhance and clean raw prompts, making them more clear, actionable, and effective.

Features

  • AI-Powered Enhancement: Uses large language models to improve prompt clarity and specificity
  • Concise System Prompt: Uses a structured, efficient prompt format for consistent results
  • Context-Aware Processing: Accepts additional context to guide the enhancement process
  • Mode-Specific Optimization: Supports both "general" and "code" modes for different use cases
  • Quality Assessment: Provides quality scores and detailed feedback on enhanced prompts
  • Two-Level Retry Strategy: HTTP-level retries for network issues, content-level retries for AI output quality
  • Exponential Backoff: Robust error handling with jitter to prevent thundering herd
  • MCP Integration: Full MCP protocol compliance with stdio transport
  • Production Ready: Comprehensive test coverage, clean code, and robust error handling

Installation

Using uv (recommended)

uv sync

Using pip

pip install -e .

Note: This project uses pyproject.toml for dependency management.

Configuration

Local LLM (LMStudio) - Default Setup

The server is configured by default to work with local LLMs like LMStudio. No API key is required:

# 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

Cloud LLM (OpenAI, Anthropic, etc.)

For cloud-based LLMs, create a .env file in the project root:

# 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

Note: .env file support is provided by pydantic-settings - no additional dependencies required.

LMStudio Setup

  1. Download and install LMStudio
  2. Start LMStudio and load a model
  3. Start the local server (usually on http://localhost:1234)
  4. The MCP server will automatically connect to your local LLM

Running the Server

To run the MCP server:

python main.py

Tool Usage

The server provides a clean_prompt tool that accepts:

  • raw_prompt (required): The user's raw, unpolished prompt
  • context (optional): Additional context about the task
  • mode (optional): Processing mode - "general" or "code" (default: "general")
  • temperature (optional): AI sampling temperature 0.0-1.0 (default: 0.2)

Example Tool Call

The tool is called directly with parameters:

# Direct function call
result = await clean_prompt_tool(
    raw_prompt="help me write code",
    context="web development with Python",
    mode="code",
    temperature=0.1
)

Or via MCP protocol:

{
  "method": "tools/call",
  "params": {
    "name": "clean_prompt",
    "arguments": {
      "raw_prompt": "help me write code",
      "context": "web development with Python",
      "mode": "code",
      "temperature": 0.1
    }
  }
}

Example Response

{
  "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 Client Configuration

Claude Desktop

For Local LLM (LMStudio) - No API Key Required

{
  "mcpServers": {
    "mcp-prompt-cleaner": {
      "command": "python",
      "args": ["main.py"]
    }
  }
}

For Cloud LLM (OpenAI, etc.) - API Key Required

{
  "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"
      }
    }
  }
}

Other MCP Clients

The server uses stdio transport and can be configured with any MCP-compatible client by pointing to the main.py file.

Development

Running Tests

uv run pytest

Test Coverage

The project includes comprehensive tests for:

  • JSON extraction from mixed content
  • LLM client with retry logic
  • Prompt cleaning functionality
  • MCP protocol integration

Project Structure

├── 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

Requirements

  • Python 3.11+
  • MCP Python SDK
  • httpx for HTTP client
  • pydantic for data validation
  • pytest for testing

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选