ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

使用 Java 开发 MCP 服务并发布到 Maven 中央仓库完整指南:TaoToken 统一 Key 接入与 settings.json 配置骨架

使用 Java 开发 MCP 服务并发布到 Maven 中央仓库完整指南:TaoToken 统一 Key 接入与 settings.json 配置骨架 1. 从零跑通 Java MCP 服务为什么需要统一 Key 与 settings.json如果你正在用 Java 写 MCP 服务大概率会遇到两个卡点一是本地调试时 AI 客户端连不上你的 stdio 进程二是发布到 Maven 中央仓库后别人用 JBang 拉起来却报签名或元数据错误。这篇内容聚焦 Java Spring Boot 构建 MCP 服务并发布到 Maven 中央仓库的完整链路重点演示如何通过 TaoToken 统一 Key/API 通道接入 AI 工具给出可复制的 settings.json 配置骨架与 MCP 服务端验证动作帮你一次跑通本地调试到中央仓库发布流程。MCPModel Context Protocol是一个开放协议用于标准化大语言模型与外部数据源和工具之间的交互。它允许开发者将应用程序、数据源或 AI 功能无缝集成到任何使用 MCP 的 LLM 客户端中。MCP 的核心优势包括标准化接口、松耦合架构、易于扩展和跨平台支持。MCP 支持多种通信方式stdio 基于标准输入输出的本地进程通信HTTP/SSE 基于 HTTP 的服务器发送事件WebSocket 双向实时通信。本文以中国天气查询服务为例详细介绍如何使用 Java 开发 MCP 服务并发布到 Maven 中央仓库最终通过 JBang 和 stdio 方式集成到大模型工具中。适合谁看有 Java 基础、想把自己的业务能力封装成 MCP 工具给 AI 调用的后端开发者正在用 Spring Boot 做微服务、想把现有接口快速暴露给大模型的技术团队以及需要把内部工具发布到 Maven 中央仓库供他人复用的开源贡献者。2. TaoToken 前置统一 Key 与 API 通道准备在开始写代码之前先把 AI 工具的接入通道准备好。TaoToken 提供统一的 Key 和 API 通道让你在本地调试 MCP 服务时不用为每个客户端单独配置不同的密钥。你可以先到官网了解整体能力然后进入控制台创建 API Key。具体操作路径访问官网 https://taotoken.net/?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 了解服务概览然后打开控制台 https://taotoken.net/console?utm_sourcetaotoken_aicg_blog_endutm_contentconsoleutm_campaignrewrite 创建你的 API Key。创建完成后在 API Keys 页面 https://taotoken.net/api-keys?utm_sourcetaotoken_aicg_blog_endutm_contentapi-keysutm_campaignrewrite 可以查看和管理所有密钥。拿到 Key 之后你需要把它配置到 AI 客户端的 settings.json 中。这里给出一个可复制的配置骨架适用于大多数支持 MCP 的客户端{ mcpServers: { cn-weather-mcp: { command: jbang, args: [ io.github.your-username:cn-weather-mcp:1.0.0 ], env: { TAOTOKEN_API_KEY: sk-your-taotoken-key, TAOTOKEN_BASE_URL: https://taotoken.net/api } } } }注意 env 字段中的 TAOTOKEN_API_KEY 替换成你在控制台创建的实际 KeyTAOTOKEN_BASE_URL 保持为 https://taotoken.net/api 即可。这样配置的好处是MCP 服务进程启动时就能读到统一的环境变量后续如果要在服务内部调用其他 AI 能力直接复用这个 Key 就行不用在每个工具方法里硬编码。如果你更习惯用模型对话来验证 Key 是否可用可以打开模型对话页面 https://taotoken.net/chat?utm_sourcetaotoken_aicg_blog_endutm_contentchatutm_campaignrewrite 发一条测试消息确认通道正常后再继续下面的开发步骤。3. 可复制配置Spring Boot MCP 服务完整骨架3.1 项目结构与技术选型技术栈选择 Java 17 LTS 版本、Spring Boot 3.5.13、Spring AI MCP、Maven 和 JBang。项目结构如下cn-weather-mcp/ ├── src/main/java/cn/wubo/cn/weather/mcp/ │ ├── WeatherApplication.java # 主启动类 │ └── WeatherService.java # 天气服务实现 ├── src/main/resources/ │ └── application.yml # 配置文件 ├── pom.xml # Maven 配置 └── .github/workflows/ └── publish.yml # CI/CD 发布流程开发环境需要 JDK 17、Maven 3.6、Git 和 GPG。GPG 用于签名发布到中央仓库生成密钥的命令如下gpg --full-generate-key gpg --list-keys gpg --keyserver keyserver.ubuntu.com --send-keys YOUR_KEY_ID3.2 pom.xml 关键配置pom.xml 需要包含 Spring Boot 父工程、Spring AI BOM、MCP Server starter以及源码、Javadoc、GPG 签名和 Central Publishing 四个插件。核心片段如下parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.5.13/version relativePath/ /parent properties java.version17/java.version spring-ai.version1.1.3/spring-ai.version /properties dependencyManagement dependencies dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-bom/artifactId version${spring-ai.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement dependencies dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-mcp-server/artifactId /dependency dependency groupIdorg.springframework/groupId artifactIdspring-web/artifactId /dependency /dependencies发布到中央仓库还需要在 build 插件中配置 maven-source-plugin、maven-javadoc-plugin、maven-gpg-plugin 和 central-publishing-maven-plugin。其中 central-publishing-maven-plugin 的 publishingServerId 设为 centralautoPublish 设为 truewaitUntil 设为 published。3.3 application.yml 配置spring: main: web-application-type: none banner-mode: off ai: mcp: server: name: cn-weather-mcp version: 1.0.0 logging: file: name: ./mcp/cn-weather-mcp.logweb-application-type 设为 none 表示这是非 Web 应用banner-mode 关闭启动横幅日志输出到指定文件方便排查。3.4 服务实现与工具注解WeatherService 是 MCP 服务的核心包含所有工具方法的实现。关键注解有三个Service 标记为 Spring 服务组件Tool 标记该方法为 MCP 工具ToolParam 标注工具方法的参数及其描述。Service public class WeatherService { private static final String BASE_URL http://t.weather.itboy.net/api/weather/city/; private final RestClient restClient; private final ObjectMapper objectMapper; public WeatherService() { this.restClient RestClient.builder().baseUrl(BASE_URL).build(); this.objectMapper new ObjectMapper(); } Tool(description Get current weather for a Chinese city. Input is city code) public String getCurrentWeather( ToolParam(description City code (e.g., 101010100 for Beijing)) String cityCode) { ResponseEntitybyte[] responseEntity restClient.get() .uri({cityCode}, cityCode) .retrieve() .toEntity(byte[].class); byte[] body responseEntity.getBody(); if (body null) { throw new RuntimeException(Empty response from weather API); } String response new String(body, StandardCharsets.UTF_8); WeatherResponse weatherResponse; try { weatherResponse objectMapper.readValue(response, WeatherResponse.class); } catch (JsonProcessingException e) { throw new RuntimeException(Failed to parse weather data: e.getMessage()); } if (200 ! weatherResponse.status()) { throw new RuntimeException(Weather API returned error: weatherResponse.message()); } WeatherDataWrapper data weatherResponse.data(); CityInfo cityInfo weatherResponse.cityInfo(); return String.format(城市%s 温度%s°C 湿度%s 空气质量%s, cityInfo.city(), data.temperature(), data.humidity(), data.quality()); } }3.5 启动类与 ToolCallbackProviderSpringBootApplication public class WeatherApplication { public static void main(String[] args) { SpringApplication.run(WeatherApplication.class, args); } Bean public ToolCallbackProvider weatherTools(WeatherService weatherService) { return MethodToolCallbackProvider.builder() .toolObjects(weatherService) .build(); } }ToolCallbackProvider 提供工具回调的 BeanMethodToolCallbackProvider 是基于方法注解的工具提供者toolObjects(weatherService) 注册包含 Tool 注解的服务对象。4. 验证请求本地调试与成功结果确认4.1 本地编译与运行mvn clean package mvn spring-boot:run运行后观察日志文件 ./mcp/cn-weather-mcp.log如果看到 MCP Server 启动成功的记录说明服务端已经就绪。4.2 通过 JBang 验证 stdio 通信JBang 是一个允许你无需安装 JDK 或配置项目即可运行 Java 代码的工具。安装命令# Windows PowerShell iex { $(iwr https://ps.jbang.dev) } app setup # Linux / macOS curl -Ls https://sh.jbang.dev | bash -s - app setup验证安装jbang --version直接用 JBang 运行你的 MCP 服务jbang io.github.your-username:cn-weather-mcp:1.0.0如果服务正常启动并等待 stdio 输入说明 Maven 坐标解析和进程启动都没问题。4.3 在 AI 客户端中验证工具调用把第 2 节的 settings.json 配置放到客户端的配置目录重启客户端。然后在对话中测试用户北京今天天气怎么样 助手[调用 MCP 工具 searchCityCode(北京)] [获取城市代码 101010100] [调用 MCP 工具 getCurrentWeather(101010100)] 北京今天的天气情况如下 - 温度25°C - 湿度60% - 空气质量良 (PM2.5: 35)看到工具被正确调用并返回结构化结果说明从本地调试到 AI 客户端集成的链路已经跑通。4.4 发布到 Maven 中央仓库在 Sonatype Central 创建账户并完成验证后配置本地 ~/.m2/settings.xmlsettings servers server idcentral/id usernameyour-username/username passwordyour-token/password /server /servers /settingsGitHub Actions 发布流程的核心步骤包括检出代码、设置 Java 17、创建 settings.xml、导入 GPG 密钥、从 tag 提取版本号、设置版本、执行 mvn clean deploy。创建 Release 后自动触发通常 15-30 分钟后同步到 Maven Central。5. 本篇常见错排查5.1 GPG 签名失败No secret key错误信息gpg: signing failed: No secret key说明本地没有可用的私钥。先确认密钥存在gpg --list-secret-keys如果没有输出重新生成gpg --full-generate-key生成后记得把公钥上传到密钥服务器否则中央仓库校验会失败。5.2 Maven 部署被拒绝缺少元数据或签名中央仓库要求 pom.xml 必须包含 developers、licenses、scm 三段元信息同时必须生成 source 和 javadoc jar并且所有构件都有 GPG 签名。检查清单pom.xml 中这三段是否完整maven-source-plugin 和 maven-javadoc-plugin 是否绑定到正确 phasemaven-gpg-plugin 是否配置了 --pinentry-mode loopback。5.3 JBang 无法下载 JARFailed to resolve artifact发布后需要等待 15-30 分钟同步到 Maven Central。如果确认已同步仍报错检查 Maven 坐标是否正确然后清除 JBang 缓存jbang cache clear5.4 MCP 工具无法被识别如果 AI 客户端看不到你的工具按顺序检查方法是否有 Tool 注解方法参数是否有 ToolParam 注解ToolCallbackProvider Bean 是否正确注册application.yml 中 web-application-type 是否为 none。这四个点任意一个缺失都会导致工具注册失败。5.5 settings.json 中环境变量未生效确认 env 字段的 Key 名称与代码中读取的一致。如果服务内部通过 System.getenv(TAOTOKEN_API_KEY) 读取settings.json 里就必须用完全相同的名称。另外注意 JSON 格式不能有尾逗号否则客户端解析会静默失败。6. 接入文档与后续动作本地调试通过后建议把接入文档 https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_contentdocutm_campaignrewrite 过一遍确认 API 通道的参数和返回格式与你的服务实现一致。如果你打算长期用 MCP 做编码辅助或 Agent 开发可以了解 Coding Plan https://taotoken.net/coding-plan?utm_sourcetaotoken_aicg_blog_endutm_contentcoding-planutm_campaignrewrite 的额度方案避免频繁创建临时 Key。对于 Claude Code 用户Anthropic 兼容接入的配置可以参考 https://taotoken.net/claude-code-anthropic?utm_sourcetaotoken_aicg_blog_endutm_contentclaude-code-anthropicutm_campaignrewrite 中的说明把 MCP 服务和统一 Key 一起配好。最后提醒一个实操细节发布到中央仓库后每次更新版本都要同步更新 pom.xml 中的 version 和 Git tag两者不一致会导致 GitHub Actions 提取版本号失败。我试过在 tag 里写 v1.0.0 但 pom 里还是 0.0.1-SNAPSHOT结果 deploy 上去的构件版本和预期完全对不上排查了半天才发现是版本号没对齐。
返回列表