
在使用 AI 编程助手(如 Claude Code 或 Cursor)时,我们常遇到这样的尴尬:发出的指令过于简略,导致 AI 生成的代码缺乏上下文、格式错误或忽略关键约束。虽然我们可以手动编写详细的 Prompt,但这既耗时又打断思路。

PrePrompt 是一个基于 MCP (Model Context Protocol) 的智能中间件,它位于你的键盘和大语言模型之间。它能实时拦截发送给 LLM 的提示词,利用启发式算法进行评分,并对复杂指令进行自动优化重写。
- 简单指令:毫秒级原样通过,无延迟。
- 复杂指令:自动补充上下文、技术约束和输出格式,确保 AI 一次做对。
核心亮点:智能、无感、高效
智能启发式分类器
PrePrompt 不依赖昂贵的 API 调用进行初步判断,而是使用纯启发式算法在 <1ms 内对每个提示词评分:
- 评分维度:模糊动词、多需求密度、轮次深度、缺失的输出格式信号。
- 精准拦截:仅当评分 ≥ 38 时才触发优化。简单的查找问题(如 “what is jwt”)或简短指令(如 “thanks”)直接跳过,确保零噪音。
堆栈记忆(Stack Memory)
PrePrompt 会“学习”你的开发习惯。经过几次会话后,它会自动识别并记忆你常用的技术栈(语言、框架、数据库等),并将这些上下文隐式注入到每次优化中。
$ preprompt-memory
PrePrompt — learned stack memory
──────────────────────────────────────────────────────
language python confidence: 0.92 (seen 47x)
framework fastapi confidence: 0.88 (seen 31x)
database postgresql confidence: 0.74 (seen 12x)
极致低成本
优化任务由最便宜的 Claude Haiku 模型处理。
- 单次成本:约 $0.001。
- 月度预算:按每天 20 个复杂提示词计算,每月仅需 $0.60。
- 简单指令免费:未触发优化的指令不产生任何 API 费用。
实战对比:优化前 vs 优化后
用户输入(优化前):
write a function that handles youtube oauth token refresh and manages expired credentials with error handling
PrePrompt 优化后:
Write a Python function for FastAPI that handles YouTube OAuth 2.0 token refresh. The function should:
(1) detect expired credentials by checking the expiry timestamp,
(2) use the refresh token to obtain a new access token via the YouTube API,
(3) update and persist the new credentials (consider using a database or file storage), and
(4) include comprehensive error handling for invalid refresh tokens, network failures, and API errors with appropriate logging and exception types.
终端反馈:
╔═ PrePrompt +58 ════════════════════════════════════════════╗
║ The rewritten prompt specifies the technical ║
║ implementation details, clarifies the complete workflow, ║
║ and adds concrete error scenarios and storage ║
║ considerations relevant to FastAPI applications. ║
╠════════════════════════════════════════════════════════════╣
║ ORIGINAL write a function that handles youtube oauth t... ║
║ OPTIMIZED Write a Python function for FastAPI that handles ║
║ YouTube OAuth 2.0 token refresh. The function ║
║ should: (1) detect expired credentials by ║
║ checking the expiry timestamp... ║
╚════════════════════════════════════════════════════════════╝
安装与配置
1. 快速安装(推荐)
pip install preprompt
python scripts/setup_global_hook.py
# 重启 Claude Code 或 Cursor 以生效
2. 源码安装
git clone https://github.com/yashdeeptehlan/preprompt
cd preprompt && ./scripts/install.sh
3. 环境变量设置
确保设置 Anthropic API Key:
export ANTHROPIC_API_KEY=your_api_key_here
常用 CLI 命令
PrePrompt 提供了一套丰富的命令行工具,方便管理和调试:
| 命令 | 描述 |
|---|---|
preprompt-install | 一键设置(API 密钥 + 钩子) |
preprompt-history | 查看最近优化的提示词历史 |
preprompt-stats | 查看优化统计(总数、拦截率、平均评分) |
preprompt-memory | 查看已学习的技术栈上下文 |
preprompt-watch | 在第二个终端实时监听拦截事件 |
preprompt-optimize "..." | 手动测试优化效果 |
preprompt-feedback | 对优化结果打分,帮助模型改进 |
技术架构:无锁异步设计
PrePrompt 采用巧妙的异步架构,确保不会阻塞主进程:
- Hook 层:
pre_prompt.py拦截用户输入,运行轻量级分类器。 - Sidecar 文件:若需优化,将原始提示词写入临时 JSON 文件 (
~/.preprompt/pending/<uuid>.json),而非直接操作数据库,避免锁冲突。 - MCP Server:在下次工具调用时,MCP 服务器读取 Sidecar 文件,调用 Haiku API 进行优化,并将结果存入 SQLite 历史记录。
- 返回结果:优化后的提示词被送回 LLM,完成透明替换。
相关导航


MCP课程

MarkItDown MCP

ElevenLabs MCP

12306 MCP Server

面向初学者的MCP课程

mcp2mqtt






