使用说明
使用Spring AI的Postgresql MCP服务器的Spring引导启动器
Spring Boot应用程序实现了MCP(模型上下文协议)服务器,该服务器提供对 PostgreSQL数据库通过STDIO传输。
组件
工具
- 怎么翻译
- 执行带有分页功能的只读SQL查询
- 输入:
- sql(string):sql查询。必须包含WHERE子句,不得使用通配符(SELECT*)。
- pageNumber(整数,可选,默认值=0):从零开始的页面索引
- pageSize(整数,可选,默认值=10,最大值=50):每页的行数
- 行为:
- 验证查询是否包含WHERE子句
- 禁止SELECT*或别名通配符
- 在带有LIMIT/OFFSET的只读事务中执行查询
- 返回JSON记录数组
资源
- 表架构(
postgres://<host>/<table>/schema)- 从information_schema.columns中自动发现
- 每个资源URI都列出了表的列元数据:
- column_name(字符串)
- data_type(字符串)
- 通过MCP资源端点以JSON形式公开
- 对每个请求进行动态发现;启动时不进行预加载
创建jar文件
mvn clean package
波德曼图片
本项目的Spring MCP服务器有两种类型的Docker镜像
SSE Docker镜像
- 要创建SSE docker镜像,请使用 Docker 文件
podman build -f dockers/sse/Dockerfile -t postgresql-mcp-server:latest .
STDIO Docker镜像
- 要创建STDIO docker镜像,请使用 Docker 文件
podman build -f dockers/stdio/Dockerfile -t postgresql-mcp-server:latest .
- In some cases, below command may not work. In that case, you can use following command to build the image.
- Dockerfile and jar file should be in the same directory.
- change this "COPY target/postgresql-mcp-server.jar app.jar" to "COPY postgresql-mcp-server.jar app.jar" in the Dockerfile.
podman build -t postgresql-mcp-server .
为SSE创建Postgresql MCP容器
- to run container in sse mode, you need to set the environment variable SPRING_AI_MCP_SERVER_STDIO to false.
- SSE works only in async mode. So change SPRING_AI_MCP_SERVER_TYPE to ASYNC.
- To verify that SSE is working, run this command: curl -v -H "Accept: text/event-stream" http://localhost:8080/sse
-- change host to database host, change 5432 to database port
podman run -i --rm -e DATABASE_URL=jdbc:postgresql://host:5432/db -e DATABASE_USERNAME=user -e DATABASE_PASSWORD=pass -e APP_CURRENT_SCHEMA=public mcp-postgres-spring
主机/客户端设置
**Note:** To use podman, the podman command should be replaced by podman in the command property.克劳德桌面
此配置应添加到claude_desktop_config json中
{
"mcpServers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"DATABASE_URL=${input:pg_url}",
"-e",
"DATABASE_USERNAME=${input:pg_user}",
"-e",
"DATABASE_PASSWORD=${input:pg_password}",
"-e",
"APP_CURRENT_SCHEMA=${input:pg_schema:public}",
"mcp-postgres-spring"
]
}
}
}
VS代码:编码助手设置
对于手动安装,请将以下JSON块添加到VS Code中的Preferences(JSON)文件中。你可以这样做 按Ctrl+Shift+P并键入首选项:打开用户设置(JSON)。
您可以选择将其添加到工作区中名为.vcode/mcp.json的文件中。这允许您共享 与他人配置。
请注意,.vcode/mcp.json文件中不需要mcp密钥。
{
"mcp": {
"inputs": [
{
"type": "promptString",
"id": "pg_url",
"description": "PostgreSQL URL (e.g. jdbc:postgresql://host:5432/db)"
},
{
"type": "promptString",
"id": "pg_user",
"description": "Database username"
},
{
"type": "promptString",
"id": "pg_password",
"description": "Database password"
},
{
"type": "promptString",
"id": "pg_schema",
"description": "Database schema (default: public)",
"default": "public"
}
],
"servers": {
"postgres": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"DATABASE_URL=${input:pg_url}",
"-e",
"DATABASE_USERNAME=${input:pg_user}",
"-e",
"DATABASE_PASSWORD=${input:pg_password}",
"mcp-postgres-spring"
]
}
}
}
}
TECH堆栈
- Spring Boot: 3.4.4
- 春季AI:1.0米7
- Spring MCP服务器(Stdio/Webflux)
- 龙目: 1.18.38
- SpotBugs: 4.8.6
- 杰克逊数据绑定
- 代码检查