I have successfully converted the project to a Spring AI MCP Server with PostgreSQL integration.
pom.xml: Updated to use Spring Boot 3.3.0 and Spring AI1.0.0-SNAPSHOT(experimental support for MCP).PostgresMcpServerApplication.java: Created the main Spring Boot application class.DatabaseService.java: Implemented the database service and annotated methods with@Toolto expose them as MCP tools.application.properties: Configured database connection settings with environment variable overrides.- Deleted: Removed the legacy
PostgresMcpServer.java.
- Java 17+
- Maven
- PostgreSQL Database
You can run the server using Maven. Make sure to set the environment variables for your database connection.
$env:DB_URL="jdbc:postgresql://localhost:5433/your_db"
$env:DB_USER="your_user"
$env:DB_PASSWORD="your_password"
mvn spring-boot:runIssue: The server exits immediately when connected to Claude Desktop.
Cause: Port conflict (port 8080 already in use) or missing server.port=0 configuration.
Fix:
- Ensure no other process is using port 8080.
- Add
-Dserver.port=0to the Claude Desktop configuration to use a random port. - Ensure
logging.level.root=INFO(notVERBOSE) inapplication.properties.
Issue: The server connects but shows no tools.
Cause: Missing spring-ai-core dependency and incorrect tool registration.
Fix:
- Added
spring-ai-coredependency topom.xml. - Created
McpConfig.javato explicitly register tools usingToolCallbacks.from(databaseService).
- Build:
mvn clean package -DskipTests - Run:
java -Dspring.main.banner-mode=off -Dserver.port=0 -jar target/postgres-mcp-server-0.0.1-SNAPSHOT.jar - Check Logs: Verify
mcp-server.logshowsRegistered tools: 2.
The server exposes the following tools:
listTables: Lists all public tables in the database.executeQuery: Executes a read-only SQL query (SELECT only).
These tools are automatically registered by Spring AI's MCP auto-configuration.
This guide provides instructions on how to test your Spring AI MCP Server.
Run the included JUnit tests to verify the application context and bean configuration.
mvn testThe MCP Inspector is the best way to interactively test your server.
- Node.js installed (
npxcommand available)
-
Start the Server: Make sure your database is running and environment variables are set.
$env:DB_URL="jdbc:postgresql://localhost:5433/postgres" $env:DB_USER="admin" $env:DB_PASSWORD="password" mvn spring-boot:run
The server will start on port 8080.
-
Run Inspector: Open a new terminal and run:
npx @modelcontextprotocol/inspector
-
Connect:
- In the Inspector UI (usually opens in browser), select SSE (Server-Sent Events) as the transport.
- Enter the URL:
http://localhost:8080/sse(orhttp://localhost:8080/mcp/ssedepending on auto-configuration, check logs). - Click Connect.
-
Interact:
- You should see
listTablesandexecuteQueryin the Tools list. - Try running
listTables.
- You should see
To use this server with Claude Desktop, you need to run it in a way that it communicates via Standard Input/Output (Stdio).
- Build the JAR:
mvn clean package -DskipTests
- Locate the JAR in
target/postgres-mcp-server-0.0.1-SNAPSHOT.jar.
Edit your %APPDATA%\Claude\claude_desktop_config.json:
{
"mcpServers": {
"postgres": {
"command": "java",
"args": [
"-Dspring.main.banner-mode=off",
"-Dlogging.pattern.console=",
"-Dserver.port=0",
"-jar",
"D:/DevOps/GenAI/postgres-mcp-server/target/postgres-mcp-server-0.0.1-SNAPSHOT.jar"
],
"env": {
"DB_URL": "jdbc:postgresql://localhost:5433/postgres",
"DB_USER": "admin",
"DB_PASSWORD": "password"
}
}
}
}Note: We disable the web application and console logging to ensure clean JSON communication on stdout.
You can use curl to verify the server is listening.
curl -v http://localhost:8080/sseYou should see a connection established.