Skip to content

Commit 3c5f60f

Browse files
committed
Add JDBC MCP stdio configuration
1 parent e1a45f9 commit 3c5f60f

23 files changed

Lines changed: 350 additions & 88 deletions

File tree

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,3 @@
77

88
# Maven build files
99
target/
10-
11-
# Test files
12-
*.db

README.md

Lines changed: 137 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,153 @@ The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) lets servers
1717

1818
## JDBC Server
1919

20-
The JDBC example currently includes an annotated-sdk implementation backed by SQLite.
20+
The JDBC example includes an annotated-sdk implementation backed by a JDBC connection.
21+
The packaged server starts in STDIO mode by default.
2122

2223
Implemented resource:
2324

24-
- `db://schema` - Returns database product name and version as JSON
25+
- `db://schema` - Returns database product/version plus table and column metadata as JSON
2526

26-
Default database configuration:
27+
JDBC configuration for the MCP server process:
2728

28-
- `JDBC_URL` defaults to `jdbc:sqlite:./test.db`
29+
- `JDBC_URL` is required
2930
- `JDBC_USERNAME` is optional
3031
- `JDBC_PASSWORD` is optional
3132

32-
Run the server:
33+
Bundled sample database:
3334

34-
```powershell
35-
.\mvnw.cmd -pl mcp-server-jdbc/mcp-server-jdbc-annotated-sdk -am package
36-
java -jar mcp-server-jdbc\mcp-server-jdbc-annotated-sdk\target\mcp-server-jdbc-annotated-sdk-1.0.0.jar
35+
- `mcp-server-jdbc/mcp-server-jdbc-common/src/test/resources/sqlite3.db`
36+
37+
Build the MCP server jar first:
38+
39+
```bash
40+
./mvnw -pl mcp-server-jdbc/mcp-server-jdbc-annotated-sdk -am package
41+
```
42+
43+
On Windows PowerShell, use `.\mvnw.cmd` instead of `./mvnw`.
44+
45+
The jar is created at:
46+
47+
- `mcp-server-jdbc/mcp-server-jdbc-annotated-sdk/target/mcp-server-jdbc-annotated-sdk.jar`
48+
49+
Use these paths in the examples below:
50+
51+
- `<repo>` - absolute path to this repository
52+
- `<jar>` - `<repo>/mcp-server-jdbc/mcp-server-jdbc-annotated-sdk/target/mcp-server-jdbc-annotated-sdk.jar`
53+
- `<sqlite3.db>` - `<repo>/mcp-server-jdbc/mcp-server-jdbc-common/src/test/resources/sqlite3.db`
54+
55+
For Windows paths in JSON/TOML, prefer forward slashes, for example `C:/Users/me/code/mcp-java-sdk-examples/...`.
56+
57+
### MCP Inspector
58+
59+
Start Inspector with the JDBC server as a STDIO server:
60+
61+
```bash
62+
npx @modelcontextprotocol/inspector -e JDBC_URL=jdbc:sqlite:<sqlite3.db> -- java -jar <jar>
63+
```
64+
65+
Open the Inspector URL printed in the terminal, then browse the `db://schema` resource.
66+
67+
### Claude Desktop
68+
69+
Add the server to Claude Desktop MCP settings:
70+
71+
```json
72+
{
73+
"mcpServers": {
74+
"jdbc-mcp-server": {
75+
"command": "java",
76+
"args": ["-jar", "<jar>"],
77+
"env": {
78+
"JDBC_URL": "jdbc:sqlite:<sqlite3.db>"
79+
}
80+
}
81+
}
82+
}
83+
```
84+
85+
Restart Claude Desktop, then browse the `db://schema` resource from the MCP tools/resources panel.
86+
87+
### Codex
88+
89+
Add the server with the Codex CLI:
90+
91+
```bash
92+
codex mcp add jdbc-mcp-server --env JDBC_URL=jdbc:sqlite:<sqlite3.db> -- java -jar <jar>
93+
```
94+
95+
Or add it to `~/.codex/config.toml` or a trusted project `.codex/config.toml`:
96+
97+
```toml
98+
[mcp_servers.jdbc-mcp-server]
99+
command = "java"
100+
args = ["-jar", "<jar>"]
101+
102+
[mcp_servers.jdbc-mcp-server.env]
103+
JDBC_URL = "jdbc:sqlite:<sqlite3.db>"
104+
```
105+
106+
### Cursor
107+
108+
Add this server in Cursor MCP settings:
109+
110+
```json
111+
{
112+
"mcpServers": {
113+
"jdbc-mcp-server": {
114+
"command": "java",
115+
"args": ["-jar", "<jar>"],
116+
"env": {
117+
"JDBC_URL": "jdbc:sqlite:<sqlite3.db>"
118+
}
119+
}
120+
}
121+
}
122+
```
123+
124+
### Cline
125+
126+
Open Cline MCP settings and add:
127+
128+
```json
129+
{
130+
"mcpServers": {
131+
"jdbc-mcp-server": {
132+
"command": "java",
133+
"args": ["-jar", "<jar>"],
134+
"env": {
135+
"JDBC_URL": "jdbc:sqlite:<sqlite3.db>"
136+
}
137+
}
138+
}
139+
}
140+
```
141+
142+
### VS Code
143+
144+
Create or update `.vscode/mcp.json`:
145+
146+
```json
147+
{
148+
"servers": {
149+
"jdbc-mcp-server": {
150+
"type": "stdio",
151+
"command": "java",
152+
"args": ["-jar", "<jar>"],
153+
"env": {
154+
"JDBC_URL": "jdbc:sqlite:<sqlite3.db>"
155+
}
156+
}
157+
}
158+
}
37159
```
38160

39-
Run with a custom SQLite database:
161+
For databases that require credentials, add them to the MCP server environment:
40162

41-
```powershell
42-
$env:JDBC_URL = "jdbc:sqlite:C:\path\to\database.db"
43-
java -jar mcp-server-jdbc\mcp-server-jdbc-annotated-sdk\target\mcp-server-jdbc-annotated-sdk-1.0.0.jar
163+
```json
164+
"env": {
165+
"JDBC_URL": "jdbc:postgresql://localhost:5432/example",
166+
"JDBC_USERNAME": "user",
167+
"JDBC_PASSWORD": "password"
168+
}
44169
```

mcp-server-jdbc/mcp-server-jdbc-annotated-sdk/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
</dependencies>
3232

3333
<build>
34+
<finalName>${project.artifactId}</finalName>
3435
<plugins>
3536
<plugin>
3637
<groupId>org.apache.maven.plugins</groupId>
@@ -40,7 +41,7 @@
4041
<createDependencyReducedPom>false</createDependencyReducedPom>
4142
<transformers>
4243
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
43-
<mainClass>com.github.thought2code.mcp.server.jdbc.annotated.JdbcMcpServer</mainClass>
44+
<mainClass>com.github.thought2code.mcp.server.jdbc.annotated.JdbcMcpStdioServer</mainClass>
4445
</transformer>
4546
</transformers>
4647
</configuration>

mcp-server-jdbc/mcp-server-jdbc-annotated-sdk/src/main/java/com/github/thought2code/mcp/server/jdbc/annotated/JdbcMcpServer.java renamed to mcp-server-jdbc/mcp-server-jdbc-annotated-sdk/src/main/java/com/github/thought2code/mcp/server/jdbc/annotated/JdbcMcpStdioServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.github.thought2code.mcp.annotated.annotation.McpServerApplication;
55

66
@McpServerApplication
7-
public class JdbcMcpServer {
7+
public class JdbcMcpStdioServer {
88
public static void main(String[] args) {
9-
McpApplication.run(JdbcMcpServer.class, args);
9+
McpApplication.run(JdbcMcpStdioServer.class, args, "mcp-server-stdio-mode.yml");
1010
}
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.thought2code.mcp.server.jdbc.annotated;
2+
3+
import com.github.thought2code.mcp.annotated.McpApplication;
4+
import com.github.thought2code.mcp.annotated.annotation.McpServerApplication;
5+
6+
@McpServerApplication
7+
public class JdbcMcpStreamableHttpServer {
8+
public static void main(String[] args) {
9+
McpApplication.run(
10+
JdbcMcpStreamableHttpServer.class, args, "mcp-server-streamable-http-mode.yml");
11+
}
12+
}

mcp-server-jdbc/mcp-server-jdbc-annotated-sdk/src/main/java/com/github/thought2code/mcp/server/jdbc/annotated/McpServerResources.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import com.github.thought2code.mcp.annotated.annotation.McpResource;
44
import com.github.thought2code.mcp.annotated.enums.MimeType;
5-
import com.github.thought2code.mcp.server.jdbc.common.DatabaseSchema;
6-
import com.github.thought2code.mcp.server.jdbc.common.datasource.DataSourceFactory;
5+
import com.github.thought2code.mcp.server.jdbc.common.datasource.DriverManagerDataSource;
6+
import com.github.thought2code.mcp.server.jdbc.common.schema.DatabaseSchema;
7+
import com.github.thought2code.mcp.server.jdbc.common.schema.JdbcSchemaReader;
78
import lombok.extern.slf4j.Slf4j;
89

910
@Slf4j
@@ -15,7 +16,7 @@ public class McpServerResources {
1516
mimeType = MimeType.APPLICATION_JSON)
1617
public DatabaseSchema getDatabaseSchema() {
1718
try {
18-
return DataSourceFactory.getDataSource().getDatabaseSchema();
19+
return new JdbcSchemaReader().read(DriverManagerDataSource.fromEnv());
1920
} catch (Exception e) {
2021
log.error("Failed to get database schema", e);
2122
throw new IllegalStateException("Failed to get database schema", e);
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<configuration>
33

4-
<property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
4+
<property name="LOG_DIR" value="${LOG_DIR:-logs}"/>
5+
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_DIR}/jdbc-mcp-server.log}"/>
6+
<property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%-5level] %logger{36} - %msg%n"/>
57

68
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
79
<target>System.err</target>
@@ -11,11 +13,24 @@
1113
</encoder>
1214
</appender>
1315

16+
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
17+
<file>${LOG_FILE}</file>
18+
<encoder>
19+
<pattern>${PATTERN}</pattern>
20+
<charset>UTF-8</charset>
21+
</encoder>
22+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
23+
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
24+
<maxHistory>14</maxHistory>
25+
</rollingPolicy>
26+
</appender>
27+
1428
<logger name="org.eclipse.jetty" level="WARN"/>
1529
<logger name="io.modelcontextprotocol" level="WARN"/>
1630

17-
<root level="WARN">
31+
<root level="DEBUG">
1832
<appender-ref ref="CONSOLE"/>
33+
<appender-ref ref="FILE"/>
1934
</root>
2035

2136
</configuration>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
enabled: true
2+
mode: STDIO
3+
name: jdbc-mcp-server
4+
version: 1.0.0
5+
type: SYNC
6+
instructions: You are a helpful AI assistant that can access a database and perform operations on it
7+
request-timeout: 20000
8+
capabilities:
9+
resource: true
10+
subscribe-resource: true
11+
prompt: false
12+
tool: false
13+
completion: false
14+
change-notification:
15+
resource: true
16+
prompt: false
17+
tool: false

mcp-server-jdbc/mcp-server-jdbc-annotated-sdk/src/main/resources/mcp-server.yml renamed to mcp-server-jdbc/mcp-server-jdbc-annotated-sdk/src/main/resources/mcp-server-streamable-http-mode.yml

File renamed without changes.

mcp-server-jdbc/mcp-server-jdbc-common/src/main/java/com/github/thought2code/mcp/server/jdbc/common/DataSourceConfig.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)