
一个适合初学者的MCP服务器模板,配有PostgreSQL连接器,代码简洁易懂。
概览
这是一个为那些想自己构建MCP服务器的人准备的模板项目。我设计得非常简单易懂且易于适应——代码简单明了,附带MCP文档,让你能快速上手。 简而言之;简而言之——这是一种为人工智能编写插件的方式 模型上下文协议(MCP)是大型语言模型与外部工具和数据交互的一种标准方式。简而言之: - 允许LLM执行命令(如运行数据库查询)——是可附加到对话中的数据(如将文件附加到提示词上)——是生成一致LLM指令的模板 1. - execute_query - 对数据库运行SQL查询 - test_connection - 验证数据库连接正常 2. - db://tables - 模式中所有表的列表 - db://tables/{table_name} - 特定表的模式信息 - db://schema - 数据库中所有表的完整模式信息 3.
概述(AI 翻译)
这是一个为那些想自己构建MCP服务器的人准备的模板项目。我设计得非常简单易懂且易于适应——代码简单明了,附带MCP文档,让你能快速上手。
什么是MCP?
总结;简而言之——这是一种为人工智能编写插件的方法
模型上下文协议(MCP)是大型语言模型与外部工具和数据交互的一种标准方式。简而言之:
- 工具允许LLM执行命令(如运行数据库查询)
- 资源是你可以附加到对话中的数据(比如将文件附加到提示词上)
- 提示词是生成一致LLM指令的模板
特色
该 PostgreSQL MCP 服务器实现了:
- 工具
execute_query- 对你的数据库运行SQL查询test_connection- 验证数据库连接是否正常
- 资源
db://tables- 模式中所有表的列表db://tables/{table_name}- 特定表的模式信息db://schema- 数据库中所有表的完整模式信息
- 提示词
- 查询生成模板
- 分析查询构建器
- 基于本仓库中的模板
先修条件
- Python 3.8+
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
Simple PostgreSQL MCP Server
This is a template project for those looking to build their own MCP servers. I designed it to be dead simple to understand and adapt - the code is straightforward with MCP docs attached so you can quickly get up to speed.
What is MCP?
TL;DR - It’s a way to write plugins for AI
Model Context Protocol (MCP) is a standard way for LLMs to interact with external tools and data. In a nutshell:
- Tools allow the LLM to execute commands (like running a database query)
- Resources are data you can attach to conversations (like attaching a file to a prompt)
- Prompts are templates that generate consistent LLM instructions
Features
This PostgreSQL MCP server implements:
-
Tools
execute_query- Run SQL queries against your databasetest_connection- Verify the database connection is working
-
Resources
db://tables- List of all tables in the schemadb://tables/{table_name}- Schema information for a specific tabledb://schema- Complete schema information for all tables in the database
-
Prompts
- Query generation templates
- Analytical query builders
- Based on the templates in this repo
Prerequisites
- Python 3.8+
- uv - Modern Python package manager and installer
- npx (included with Node.js)
- PostgreSQL database you can connect to
Quick Setup
-
Create a virtual environment and install dependencies:
# Create a virtual environment with uv uv venv # Activate the virtual environment source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install dependencies uv pip install -r requirements.txt -
Run the server with the MCP Inspector:
# Replace with YOUR actual database credentials npx @modelcontextprotocol/inspector uv --directory . run postgres -e DSN=postgresql://username:password@hostname:port/database -e SCHEMA=publicNote: If this is your first time running npx, you’ll be prompted to approve the installation. Type ‘y’ to proceed.
After running this command, you’ll see the MCP Inspector interface launched in your browser. You should see a message like:
MCP Inspector is up and running at http://localhost:5173If the browser doesn’t open automatically, copy and paste the URL into your browser. You should see something like this:
-
Using the Inspector:
- Click the “Connect” button in the interface (unless there’s an error message in the console on the bottom left)
- Explore the “Tools”, “Resources”, and “Prompts” tabs to see available functionality
- Try clicking on listed commands or typing resource names to retrieve resources and prompts
- The interface allows you to test queries and see how the MCP server responds
-
Take a look at the official docs
安装
npx @modelcontextprotocol/inspector uv --directory . run postgres -e DSN=postgresql://username:password@hostname:port/database -e SCHEMA=public配置
{
"mcpServers": {
"postgres": {
"command": "/path/to/uv",
"args": [
"--directory",
"/path/to/simple-psql-mcp",
"run",
"postgres"
],
"env": {
"DSN": "postgresql://username:password@localhost:5432/my-db",
"SCHEMA": "public"
}
}
}
}