Sponsored by Deepsite.site

MCP MySQL Server

Created By
JamesSmith8887 months ago
A Spring AI-based MCP capable of executing any SQL query. 一个基于 Spring AI 的MCP服务,可执行任意 SQL。
Content

MCP MySQL Server

一个基于 Spring AI 的MCP,可执行任意 SQL。

中文文档 | English Documentation

快速上手

1. MCP JSON 配置

推荐方式 (Maven Wrapper)

{
  "mcpServers": {
    "mcp-mysql-server": {
      "command": "/Users/xin.y/IdeaProjects/mcp-mysql-server/mvnw",
      "args": [
        "-f",
        "/Users/xin.y/IdeaProjects/mcp-mysql-server/pom.xml",
        "spring-boot:run"
      ]
    }
  }
}

JAR 包方式 (可选,不支持扩展)

{
  "mcpServers": {
    "mcp-mysql-server": {
      "command": "java",
      "args": [
        "-jar",
        "/Users/yangxin/IdeaProjects/mcp-mysql-server/target/mcp-mysql-server-0.0.1-SNAPSHOT.jar"
      ]
    }
  }
}

2. 数据源配置(最小配置)

修改 mcp-mysql-server/src/main/resources/datasource.yml 文件,添加你的数据源信息:

datasource:
  datasources:
    # 第一个数据源示例
    your_db1_name:
      url: jdbc:mysql://localhost:3306/db1
      username: root
      password: password
      default: true  # 标记为默认数据源,如果未标记,则默认使用第一个

扩展功能(可选)

参考 mcp-mysql-server/src/main/resources/extension.yml 文件进行配置,以添加或管理你的扩展功能:

extensions:
  - name: yourExtension
    description: "您的扩展描述"
    mainFileName: main.groovy  # 放置在 src/main/resources/groovy/yourExtension/script/ 目录下

环境要求

在运行之前,请确保你的环境满足以下条件:

  • JDK 21 或更高版本
  • Maven
  • MySQL(作为目标数据库)

功能特点

  • 无限制 SQL 执行:支持执行任意 SQL 语句,没有任何限制。
  • 多数据源支持:支持配置和管理多个数据库数据源。
  • 动态数据源切换:能够在运行时动态切换不同的数据源。
  • 用户自定义数据源:允许用户自定义和配置自己的数据源。

扩展功能

MCP MySQL Server 支持通过 Groovy 脚本来扩展其功能。这些扩展可以在运行时通过特定的工具调用来执行。

配置文件

扩展功能通过 src/main/resources/extension.yml 文件进行配置。此文件定义了一个扩展列表,每个扩展可以包含以下参数:

参数类型是否必需/可选描述示例
nameString必需扩展的唯一名称,用于调用该扩展。zstdDecode
descriptionString可选扩展功能的简要描述。"解码业务快照数据"
enabledBoolean可选是否启用该扩展。默认为 true。设置为 false 则禁用。false
promptString可选当使用此扩展时,提供给 AI 模型的建议提示或说明。"decode the snapshot_data..."
scriptString可选内联的 Groovy 脚本代码。如果提供了此参数,则会忽略 mainFileName 及对应的脚本文件。请注意,对于复杂的脚本或需要外部依赖的扩展,推荐使用 mainFileName 指定脚本文件。`def greet(name) { return "Hello, $name!"; }; greet('Java')`
mainFileNameString条件性必需Groovy 脚本文件的名称(例如 main.groovy)。如果未提供 script 参数,则此参数为必需。脚本文件必须位于 src/main/resources/groovy/<name>/script/ 目录下。main.groovy

脚本和依赖项管理:

  • 脚本文件:如果不使用内联 script,则每个扩展的 Groovy 脚本文件应放置在 src/main/resources/groovy/<extension_name>/script/ 目录下。脚本文件的具体名称通过 mainFileName 参数指定。例如,对于名为 zstdDecode 的扩展,其主脚本文件(如 main.groovy)应位于 /src/main/resources/groovy/zstdDecode/script/main.groovy
  • 依赖 JAR 包:如果扩展需要外部 Java 库(JARs),请将这些 JAR 文件放置在 src/main/resources/groovy/<extension_name>/dependency/ 目录下。服务启动时会自动加载这些依赖。例如,zstdDecode 扩展的依赖 zstd-jni-1.5.5-10.jar 应位于 /src/main/resources/groovy/zstdDecode/dependency/zstd-jni-1.5.5-10.jar

重要提示:运行带有扩展的应用

为确保扩展功能(特别是依赖加载)能正确工作,请务必使用 Maven Wrapper 通过 spring-boot:run 启动应用,而不是直接通过 java -jar 执行 JAR 包。这是因为直接运行 JAR 包时,扩展的类路径和依赖加载可能存在问题。

请参考“MCP JSON 配置”部分中的推荐方式,并务必将路径替换为你项目的实际绝对路径。


数据源配置

默认配置

应用默认使用 src/main/resources/datasource.yml 中的数据源配置。

自定义配置

用户可以通过命令行参数指定自定义数据源配置文件,这在不同的部署环境或测试场景下非常有用:

./mvnw spring-boot:run -Dspring-boot.run.arguments=--datasource.config=/path/to/your-datasource.yml

配置文件格式

数据源配置文件采用 YAML 格式,示例如下:

datasource:
  datasources:
    # 第一个数据源示例
    your_db1_name:
      url: jdbc:mysql://localhost:3306/db1
      username: root
      password: password
      default: true  # 标记为默认数据源,如果未标记,则默认使用第一个

    # 第二个数据源示例
    your_db2_name:
      url: jdbc:mysql://localhost:3306/db2
      username: root
      password: password

使用方法

构建项目

在项目根目录执行以下命令,编译并打包项目:

./mvnw clean package

运行项目(推荐方式)

推荐使用 Maven Wrapper 启动应用,以确保所有功能(包括扩展)正常工作:

./mvnw spring-boot:run

运行项目并指定自定义数据源配置

如果你需要使用自定义的数据源配置文件,可以在运行命令中添加参数:

./mvnw spring-boot:run -Dspring-boot.run.arguments=--datasource.config=/path/to/your-datasource.yml

对应的 MCP JSON 配置如下:

{
  "mcpServers": {
    "mcp-mysql-server": {
      "command": "/your-path/mcp-mysql-server/mvnw",
      "args": [
        "-Dspring-boot.run.arguments=--datasource.config=/your-path/your-datasource.yml",
        "-f",
        "/your-path/mcp-mysql-server/pom.xml",
        "spring-boot:run"
      ]
    }
  }
}

使用 JAR 包运行(该方式不支持扩展)

java -jar target/mcp-mysql-server-0.0.1-SNAPSHOT.jar

对应的 MCP JSON 配置如下:

{
  "mcpServers": {
    "mcp-mysql-server": {
      "command": "java",
      "args": [
        "-jar",
        "/your-path/mcp-mysql-server-0.0.1-SNAPSHOT.jar"
      ]
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
ChatWiseThe second fastest AI chatbot™
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
DeepChatYour AI Partner on Desktop
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Serper MCP ServerA Serper MCP Server
Playwright McpPlaywright MCP server
CursorThe AI Code Editor
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Tavily Mcp
WindsurfThe new purpose-built IDE to harness magic
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Amap Maps高德地图官方 MCP Server
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.