This example provides MCP access to MySQL database context through Java SDK servers backed by JDBC. The packaged server starts in STDIO mode by default.
Available implementations:
mcp-server-mysql-annotated-sdk-impl- Built with the annotated MCP Java SDKmcp-server-mysql-official-sdk-impl- Built with the official MCP Java SDK
Both implementations expose the same MCP resources and tools.
Current MCP capabilities:
- Resources: enabled
- Resource subscriptions and change notifications: enabled by server configuration
- Tools: enabled
- Prompts and completions: not enabled yet
Implemented resource:
db://schema(get_database_schema) - Returns database product/version, tables, table comments, column names, and approximate row counts as JSON
Implemented tools:
show_create_table- Returns the MySQLCREATE TABLEstatement for a table in the current database. Required input:table_name
Database configuration for the MCP server process:
JDBC_URLis optional, must start withjdbc:mysql:, and defaults tojdbc:mysql://localhost:3306/bookstoreDB_USERNAMEis optional and defaults tomcpDB_PASSWORDis optional and defaults tomcp
Sample database:
- MySQL runs in Docker with initialization data from
mcp-server-mysql-support/src/test/resources/init.sql - The sample schema models a small online bookstore with authors, books, customers, orders, order items, and reviews.
Start the sample MySQL database from this directory:
docker compose up -dThe container initializes the bookstore database on first startup. To recreate the database from init.sql, remove the container and start it again:
docker compose down -v
docker compose up -dBuild the annotated SDK MCP server jar from this directory:
../mvnw -pl mcp-server-mysql-annotated-sdk-impl -am packageBuild the official SDK MCP server jar from this directory:
../mvnw -pl mcp-server-mysql-official-sdk-impl -am packageOn Windows PowerShell, use ..\mvnw.cmd instead of ../mvnw.
The jars are created at:
mcp-server-mysql-annotated-sdk-impl/target/mcp-server-mysql-annotated-sdk-impl.jarmcp-server-mysql-official-sdk-impl/target/mcp-server-mysql-official-sdk-impl.jar
Use these paths in the examples below:
<module>- absolute path to thismcp-server-mysqldirectory<jar>- absolute path to either implementation jar, for example:<module>/mcp-server-mysql-annotated-sdk-impl/target/mcp-server-mysql-annotated-sdk-impl.jar<module>/mcp-server-mysql-official-sdk-impl/target/mcp-server-mysql-official-sdk-impl.jar
<mysql-url>-jdbc:mysql://127.0.0.1:3306/bookstore?allowPublicKeyRetrieval=true&useSSL=false&nullCatalogMeansCurrent=true
For Windows paths in JSON/TOML, prefer forward slashes, for example C:/Users/me/code/mcp-java-sdk-examples/mcp-server-mysql/....
Start Inspector with the MySQL server as a STDIO server:
npx @modelcontextprotocol/inspector -e JDBC_URL="<mysql-url>" -e DB_USERNAME=mcp -e DB_PASSWORD=mcp -- java -jar <jar>Open the Inspector URL printed in the terminal, then browse the db://schema resource.
Add the server to Claude Desktop MCP settings:
{
"mcpServers": {
"mcp-server-mysql": {
"command": "java",
"args": ["-jar", "<jar>"],
"env": {
"JDBC_URL": "<mysql-url>",
"DB_USERNAME": "mcp",
"DB_PASSWORD": "mcp"
}
}
}
}Restart Claude Desktop, then browse the db://schema resource from the MCP tools/resources panel.
Add the server with the Codex CLI:
codex mcp add mcp-server-mysql --env JDBC_URL="<mysql-url>" --env DB_USERNAME=mcp --env DB_PASSWORD=mcp -- java -jar <jar>Or add it to ~/.codex/config.toml or a trusted project .codex/config.toml:
[mcp_servers.mcp-server-mysql]
command = "java"
args = ["-jar", "<jar>"]
[mcp_servers.mcp-server-mysql.env]
JDBC_URL = "<mysql-url>"
DB_USERNAME = "mcp"
DB_PASSWORD = "mcp"Add this server in Cursor MCP settings:
{
"mcpServers": {
"mcp-server-mysql": {
"command": "java",
"args": ["-jar", "<jar>"],
"env": {
"JDBC_URL": "<mysql-url>",
"DB_USERNAME": "mcp",
"DB_PASSWORD": "mcp"
}
}
}
}Open Cline MCP settings and add:
{
"mcpServers": {
"mcp-server-mysql": {
"command": "java",
"args": ["-jar", "<jar>"],
"env": {
"JDBC_URL": "<mysql-url>",
"DB_USERNAME": "mcp",
"DB_PASSWORD": "mcp"
}
}
}
}Create or update .vscode/mcp.json:
{
"servers": {
"mcp-server-mysql": {
"type": "stdio",
"command": "java",
"args": ["-jar", "<jar>"],
"env": {
"JDBC_URL": "<mysql-url>",
"DB_USERNAME": "mcp",
"DB_PASSWORD": "mcp"
}
}
}
}For databases that require credentials, add them to the MCP server environment:
"env": {
"JDBC_URL": "<mysql-url>",
"DB_USERNAME": "mcp",
"DB_PASSWORD": "mcp"
}