LibreChat自省MCP服务器

一个MCP(模型上下文协议)服务器,使LibreChat中的AI助手能够读取和修改存储在MongoDB中的自定义指令,从而创建一个自我反思的AI工作流。

  • by John-Vance
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 John-Vance/librechat-introspection-mcp

使用说明

🔮 LibreChat内省MCP服务器

MCP(模型上下文协议)服务器,使LibreChat中的AI助手能够 阅读和修改自己的自定义说明 存储在MongoDB中。这创建了一个自我反思的人工智能工作流程,助手可以根据用户交互和反馈来发展其行为。

✨ 特性

  • 📖 阅读说明 -检索任何预设的当前自定义说明
  • ✏️ 更新说明 -完全替换自定义说明
  • ➕ 附加说明 -在不丢失现有指南的情况下添加新指南
  • 🔒 安全 -仅修改 promptPrefix 指定预设的字段
  • 🚀 轻松设置 -使用FastMCP的简单Python服务器

🎯 用例

  • 自我提升的人工智能助手 适应你的工作风格
  • 动态提示工程 人工智能在那里完善自己的指令
  • 个性化 -让AI学习并记住你的偏好
  • 实验 -轻松测试不同的指令集

📋 先决条件

  • Librechat 使用MongoDB运行
  • Python 3.10或更高版本
  • 网络访问您的LibreChat MongoDB实例
  • 配置了自定义指令的LibreChat预设

🚀 快速开始

1.克隆存储库

git clone https://github.com/John-Vance/librechat-introspection-mcp.git
cd librechat-introspection-mcp

2.设置Python环境

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -r requirements.txt

3.配置环境变量

创建 .env 文件或导出以下环境变量:

# Required: MongoDB connection string
export MONGO_URI="mongodb://your-librechat-host:27017"

# Optional: Database name (default: LibreChat)
export MONGO_DATABASE="LibreChat"

# Optional: Preset name to manage (default: Folly)
export PRESET_TITLE="YourPresetName"

# Optional: Server host and port
export MCP_HOST="0.0.0.0"
export MCP_PORT="8085"

4.暴露MongoDB(如果使用Docker)

如果你的LibreChat MongoDB在Docker中运行,你需要暴露端口27017。将此添加到您的 docker-compose.yml:

services:
  chat-mongodb:
    # ... existing config ...
    ports:
      - "27017:27017"

然后重新启动: docker compose up -d

5.运行服务器

python server.py

您应该看到:

INFO:__main__:Starting Introspection MCP Server on 0.0.0.0:8085
INFO:__main__:MongoDB URI: mongodb://your-librechat-host:27017
INFO:__main__:Database: LibreChat
INFO:__main__:Target Preset: YourPresetName

⚙️ LibreChat配置

将自省服务器添加到您的 librechat.yaml:

mcpServers:
  introspection:
    type: "sse"
    url: "http://your-mcp-server-host:8085/sse"
    startup: true
    requiresOAuth: false

更新后 librechat.yaml,重新启动librechat:

# If using Docker
docker compose restart

# If using systemd
sudo systemctl restart librechat

🛠️ 可用工具

get_instructions

检索已配置预设的当前自定义说明。

AI提示示例:

“显示您当前的自定义说明。”

set_instructions

用新文本完全替换自定义说明。

AI提示示例:

“更新你的指示,更注重简洁的回答。”

append_instructions

在现有指令的末尾添加文本,而不替换它们。

AI提示示例:

“添加一个提醒,在解释编程概念时始终包含代码示例。”

🐧 作为服务运行(Linux)

对于生产使用,请将服务器作为systemd服务运行:

1.复制服务文件

sudo cp mcp-introspection.service /etc/systemd/system/

2.编辑服务文件

更新路径和环境变量以匹配您的设置:

sudo nano /etc/systemd/system/mcp-introspection.service

3.启用和启动

sudo systemctl daemon-reload
sudo systemctl enable mcp-introspection
sudo systemctl start mcp-introspection

4.检查状态

sudo systemctl status mcp-introspection
sudo journalctl -u mcp-introspection -f

🔧 故障排除

“未找到预设”错误

确保 PRESET_TITLE 与您的LibreChat预设的确切名称相匹配。在LibreChat的UI中检查您的预设或直接查询MongoDB:

mongosh "mongodb://localhost:27017/LibreChat" --eval "db.presets.find({}, {title: 1})"

连接被拒绝

  1. 验证MongoDB是否可以从MCP服务器主机访问
  2. 检查防火墙规则是否允许端口27017上的流量
  3. 如果使用Docker,请确保暴露MongoDB端口

LibreChat看不到MCP服务器

  1. 验证服务器是否正在运行: curl http://your-host:8085/sse
  2. 检查 librechat.yaml 语法正确
  3. 配置更改后重新启动LibreChat

📁 项目结构

librechat-introspection-mcp/
├── server.py                 # Main MCP server
├── requirements.txt          # Python dependencies
├── mcp-introspection.service # Systemd service file
├── .gitignore
├── LICENSE
└── README.md

🤝 贡献

欢迎投稿!请随时提交拉取请求。

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢

📬 联系

由...创建 @约翰·万斯 -请随时联系我们!


“最好的人工智能助手是那些和你一起学习和成长的人。” 🌱