
当 Spring AI 的 MCP Client 想换一条模型通道在 Spring AI 里搭 MCP Client-Server 架构时很多人会先跑通一个最小闭环用spring-ai-mcp-client-spring-boot-starter做 MCP Host/Client用spring-ai-anthropic-spring-boot-starter接 Claude然后在application.yaml里写spring.ai.anthropic.api-key和claude-3-7-sonnet-20250219。这个结构本身没问题问题往往出在“模型通道”这一层MCP Client 还没开始连 Brave Search、filesystem、author-tools-serverChatClient 的模型调用就先卡住了。本文要解决的正是这个接入配置问题——不改 MCP Server不改 MCP Client只把 Spring AI 的模型调用通道切到 TaoToken官网https://taotoken.net/?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content让聊天机器人先跑起来搜索、读文件、作者查询仍由原 MCP Server 完成。一、原问题与场景MCP 架构没变变的是模型通道原文的架构是标准的 MCP Client-ServerMCP HostSpring Boot 聊天机器人内部持有 ChatClientMCP Client由spring-ai-mcp-client-spring-boot-starter建立与 MCP Server 的一对一连接MCP ServerBrave Search、filesystem、以及自定义的 author-tools-server通过 stdio 或 sse 暴露工具。在application.yaml里原文用spring.ai.anthropic.api-key: ${ANTHROPIC_API_KEY}和model: claude-3-7-sonnet-20250219配置模型。这个写法在 Spring AI 1.0.0-M6 下会自动创建ChatModelBeanChatClient 再通过SyncMcpToolCallbackProvider把 MCP 工具挂上去。痛点在于MCP Client 要连 Brave Search、filesystem、author-tools-server 之前ChatClient 的模型通道必须先能消耗 Token。如果这一步的密钥或 Base URL 没配对MCP Host 根本走不到工具调用那一步Tool注解的方法也不会被触发。这里要区分清楚改的是 Spring AI 模型客户端的兼容 Base URL不是把 Key、MCP Server 或 MCP Client 本身改成 TaoToken。MCP 的 Client-Server 结构保持原样只是模型调用这一跳换了出口。二、TaoToken 前置先拿 Key再改 Base URL在动手改配置之前先到 https://taotoken.net/?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 创建一个 Key。拿到 Key 后记住两个地址官网https://taotoken.net/?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_contentAPI Base URLhttps://taotoken.net/api注意API 地址不要加/v1也不要带 UTM 参数。Spring AI 的 Anthropic 客户端会自己在 Base URL 后面拼路径多写/v1反而会拼出错误路径。如果你后续要排障或查接入细节可以走这两个入口API Keys 管理https://taotoken.net/console/api-keys?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content接入文档https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content这一步只解决“模型通道能消耗 Token”的问题。Brave Search 的BRAVE_API_KEY、filesystem 的目录参数、author-tools-server 的http://localhost:8081都还是按原文配置不需要动。三、可复制配置application.yaml 与 pom.xml先确认pom.xml里的依赖和原文一致dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-anthropic-spring-boot-starter/artifactId version1.0.0-M6/version /dependency dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-mcp-client-spring-boot-starter/artifactId version1.0.0-M6/version /dependency repositories repository idspring-milestones/id nameSpring Milestones/name urlhttps://repo.spring.io/milestone/url snapshots enabledfalse/enabled /snapshots /repository /repositories然后改application.yaml。关键是把 Anthropic 的base-url指向 TaoToken 的 API 地址api-key用你刚创建的 Keyspring: ai: anthropic: api-key: ${TAOTOKEN_API_KEY} base-url: https://taotoken.net/api chat: options: model: claude-3-7-sonnet-20250219 mcp: client: stdio: connections: brave-search: command: npx args: - -y - modelcontextprotocol/server-brave-search env: BRAVE_API_KEY: ${BRAVE_API_KEY} filesystem: command: npx args: - -y - modelcontextprotocol/server-filesystem - ./ sse: connections: author-tools-server: url: http://localhost:8081这里有几个点要确认base-url写https://taotoken.net/api不要写https://taotoken.net/api/v1api-key从环境变量TAOTOKEN_API_KEY读取不要把 Key 硬编码进仓库MCP 的 stdio 和 sse 配置保持原文结构Brave Search、filesystem、author-tools-server 都不需要改model仍写claude-3-7-sonnet-20250219Spring AI 会把这个模型名传给兼容通道。ChatClient 的 Bean 也不用改Bean ChatClient chatClient(ChatModel chatModel, SyncMcpToolCallbackProvider toolCallbackProvider) { return ChatClient .builder(chatModel) .defaultTools(toolCallbackProvider.getToolCallbacks()) .build(); }ChatbotService和 Controller 同样保持原样String chat(String question) { return chatClient .prompt() .user(question) .call() .content(); } PostMapping(/chat) ResponseEntityChatResponse chat(RequestBody ChatRequest chatRequest) { String answer chatbotService.chat(chatRequest.question()); return ResponseEntity.ok(new ChatResponse(answer)); } record ChatRequest(String question) {} record ChatResponse(String answer) {}这样改完之后Spring AI 启动时仍会扫描 MCP 配置创建 MCP Client 连接 Brave Search、filesystem 和 author-tools-server并生成SyncMcpToolCallbackProvider。区别只在于 ChatModel 的请求会发到 TaoToken 的兼容通道而不是 Anthropic 官方通道。四、验证请求与成功结果启动 MCP Serverauthor-tools-server 在 8081再启动 MCP Host。然后用 HTTPie 向/chat发问先验证模型通道是否通http POST :8080/chat questionHow much was Elon Musks initial offer to buy OpenAI in 2025?如果模型通道配通你会看到类似原文的响应{ answer: Elon Musks initial offer to buy OpenAI was $97.4 billion. [Source](https://www.reuters.com/technology/openai-board-rejects-musks-974-billion-offer-2025-02-14/). }这个响应说明两件事ChatClient 经 TaoToken 调到了模型并且 MCP Host 触发了 Brave Search 的Tool。接着验证 filesystemhttp POST :8080/chat questionCreate a text file named mcp-demo.txt with content This is awesome!.预期响应{ answer: The text file named mcp-demo.txt has been successfully created with the content you specified. }最后验证自定义 author-tools-serverhttp POST :8080/chat questionWho wrote the article Testing CORS in Spring Boot? on Baeldung, and how can I contact them?预期响应{ answer: The article Testing CORS in Spring Boot on Baeldung was written by John Doe. You can contact him via email at john.doebaeldung.com. }这三个请求都成功说明模型通道走 TaoToken 后MCP Client-Server 的工具调用链没有断。搜索、读文件、作者查询仍由原 MCP Server 完成TaoToken 只承担模型调用这一跳。五、本篇常见错排查错误 1Base URL 多写了/v1现象启动不报错但/chat返回 404 或路径错误。原因Spring AI 的 Anthropic 客户端会在 Base URL 后自行拼接路径写成https://taotoken.net/api/v1会拼出多余层级。处理改成https://taotoken.net/api不要加/v1也不要带 UTM 参数。错误 2Key 没放进环境变量现象启动时报api-key为空或请求返回 401。原因application.yaml里写的是${TAOTOKEN_API_KEY}但环境变量没设置。处理在启动前 export或在 IDE 的 Run Configuration 里加环境变量。Key 到 https://taotoken.net/console/api-keys?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 创建和管理。错误 3MCP Client 连不上 Brave Search现象/chat能返回模型回答但搜索类问题没有来源链接。原因BRAVE_API_KEY没配或npx下载modelcontextprotocol/server-brave-search失败。处理确认BRAVE_API_KEY环境变量存在网络能访问 npm registry。这一步和 TaoToken 无关是 MCP Server 自身的配置。错误 4author-tools-server 的 sse 连接失败现象作者查询问题返回模型编造的答案而不是john.doebaeldung.com。原因author-tools-server 没启动或端口不是 8081。处理确认 MCP Server 在http://localhost:8081运行application.yaml里的sse.connections.author-tools-server.url和实际端口一致。错误 5把 MCP Server 或 MCP Client 也改成 TaoToken现象配置越改越乱MCP 工具全部失效。原因误以为 TaoToken 要替换整个 MCP 架构。处理TaoToken 只改 Spring AI 模型客户端的 Base URL 和 Key。MCP Server、MCP Client、Tool方法、stdio/sse 连接都保持原样。六、语义一致 CTA如果你正在按原文搭 Spring AI 的 MCP Client-Server 架构卡在模型通道这一步可以先到 https://taotoken.net/?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content 创建 Key然后把spring.ai.anthropic.base-url改成https://taotoken.net/apiapi-key用新 Key。MCP 的 Brave Search、filesystem、author-tools-server 配置不动ChatClient 和Tool也不动。配通之后用 HTTPie 向/chat发三个问题分别验证搜索、文件系统、作者查询。如果三个都返回预期结果说明模型通道和 MCP 工具链都正常。排障和接入细节可以查API Keyshttps://taotoken.net/console/api-keys?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content接入文档https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content如果你后续要长期跑编码类 Agent或者把 MCP Host 用在日常开发流程里可以再看 Coding Planhttps://taotoken.net/coding-plan?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content验证单个模型是否通可以直接用模型对话https://taotoken.net/models?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_contentMCP 的 Client-Server 架构本身不复杂复杂的是能力提供方。TaoToken 在这里只解决模型通道的接入配置问题让 ChatClient 先能消耗 Token剩下的搜索、读文件、作者查询仍交给原 MCP Server 完成。