MCP-AgentDB
概述
该项目提供了一个代理的、MCP工具驱动的系统,用于使用LLM与PostgreSQL数据库进行交互。它利用LlamaIndex、Ollama和自定义工作流来解释用户请求,选择适当的数据库工具,并透明地执行操作。该系统的设计具有可扩展性和易用性。
特性
- 自然语言数据库操作: 使用简明英语查询、插入、更新和管理数据库。
- 工具驱动执行: 所有操作都是通过明确的工具调用执行的,以确保透明度和可审计性。
- PostgreSQL后端: 使用PostgreSQL作为默认数据库(可配置)。
- 代理工作流: 具有内存、事件驱动步骤和LLM驱动推理的模块化工作流。
- Docker支持: 使用Docker Compose轻松设置应用程序和数据库。
建筑
flowchart LR
subgraph UserAgent["FunctionAgent (LlamaIndex)"]
A[FunctionAgent]
end
subgraph MCP["MCP Server"]
B[MCP Server]
end
subgraph Ollama["Ollama Server"]
C[LLM]
end
subgraph DB["PostgreSQL DB"]
D[PostgreSQL DB]
end
A -- "MCP Protocol Requests/Responses" --> B
C -- "Integration" --> A
B -- "APIs" --> D
style A fill:#cef,stroke:#333,stroke-width:2px,stroke-length:4px
style B fill:#eef,stroke:#333,stroke-width:2px,stroke-length:8px
style C fill:#ffe,stroke:#333,stroke-width:2px
style D fill:#fcf,stroke:#333,stroke-width:2px- main.py: 入口点;运行代理工作流循环。
- scripts/workflow.py: 定义
DatabaseWorkflow,协调LLM、工具选择和执行。 - mcp/mcpserver.py: 实现MCP服务器公开数据库工具(CRUD、模式等)。
- config/settings.py: 从环境变量加载Ollama(LLM)和数据库的配置。
- config/prompts.py: 系统提示指导代理的行为。
- Dockerfile和docker-compose.yml: 应用程序和PostgreSQL的容器化设置。
设置
1.Docker Compose(推荐)
确保你已经安装了Docker和Docker Compose。
git clone
docker-compose up --build- 该应用程序将在
Agent_MCP_Server集装箱。 - PostgreSQL运行在
postgres_db容器(默认用户:postgres,密码:postgres,db:testdb). - 您可以在中自定义环境变量
.env.docker.
2.手动设置
- 安装Python 3.12+
- 安装依赖项:
pip install -r requirements.txt # or use pyproject.toml with pip/uv- 确保PostgreSQL正在运行且可访问(请参阅
config/settings.py默认值)。 - 根据需要设置环境变量(请参见
.env.docker例如)。 - 启动MCP服务器和主工作流:
# With Docker
docker-compose exec -it app bash
python main.py
# Without Docker
python main.py # This will spin up the server and initiate the workflow用法
- 启动时,代理将提示:
What would you like to do? - 输入自然语言请求,例如:
- Show me all the tables in the database. - Add a new customer: 'Alice', 'alice@email.com', 25 to the customers table.
- 代理人将:
1. 解释您的请求 1. 选择并调用适当的数据库工具 1. 返回结果
交互示例
What would you like to do? Show me all the tables in the database.
Tool: list_tables()
Output: The available tables are: customers, products, orders目录结构
config/ # Prompts and settings
mcp/ # MCP server and database tool definitions
scripts/ # Workflow and event logic
main.py # Entry point
Dockerfile # App container
docker-compose # Initiates the app, database and MCP server (Make sure ollama is serving)依赖项
- Python 3.12+
- Calma-Index-LLMS-Ollama
- mcp\[cli\]
- mlflow
- 奥拉马
- psycopg2二进制
- python dotenv
- (参见
pyproject.toml完整列表)
配置
- Ollama LLM: 通过环境变量设置(请参见
config/settings.py). - 数据库: 通过环境变量设置(请参见
config/settings.py).
享受实验的乐趣
