当前位置: 首页 > news >正文

spring-ai-alibaba第六章阿里dashscope集成mcp百度翻译tools

1、百度翻译mcp服务端 pom文件

    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

2、百度翻译mcp服务端yml配置文件

#
# Copyright 2025-2026 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# spring.main.web-application-type=none

# NOTE: You must disable the banner and the console logging 
# to allow the STDIO transport to work !!!
spring:
  main:
    banner-mode: off
  ai:
    mcp:
      server:
        name: my-translate-server
        version: 0.0.1

    alibaba:
      toolcalling:
        baidutranslate:
          enabled: true
          app-id: xxxxxxxx
          secret-key: oooooooo

server:
  port: 38080
# logging.pattern.console=

3、百度翻译mcp服务端 百度appid合key配置文件类

package org.springframework.ai.mcp.sample.server.conf;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties(prefix = "spring.ai.alibaba.toolcalling.baidutranslate")
@Configuration
public class BaidutranslateProperties {

    private String appId;

    private String secretKey;

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

}

4、百度翻译 mcp 服务端 百度翻译service类

package org.springframework.ai.mcp.sample.server;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.mcp.sample.server.conf.BaidutranslateProperties;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author yingzi
 * @date 2025/3/27:11:13
 */
@Service
public class BaidutranslateService {

    private static final Logger logger = LoggerFactory.getLogger(BaidutranslateService.class);

    private static final String TRANSLATE_HOST_URL = "https://fanyi-api.baidu.com/api/trans/vip/translate";

    private static final Random random = new Random();
    private final WebClient webClient;
    private final String appId;
    private final String secretKey;

    public BaidutranslateService(BaidutranslateProperties properties) {
        assert StringUtils.hasText(properties.getAppId());
        this.appId = properties.getAppId();
        assert StringUtils.hasText(properties.getSecretKey());
        this.secretKey = properties.getSecretKey();

        this.webClient = WebClient.builder()
                .defaultHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded")
                .build();
    }

    @Tool(description = "Baidu translation function for general text translation")
    public Map<String, String> baiduTranslateMethod(@ToolParam(description = "Content that needs to be translated") String q,
                                                    @ToolParam(description = "Source language that needs to be translated") String from,
                                                    @ToolParam(description = "Target language to translate into") String to) {
        if (!StringUtils.hasText(q) || !StringUtils.hasText(from)
                || !StringUtils.hasText(to)) {
            return null;
        }
        String salt = String.valueOf(random.nextInt(100000));
        String sign = DigestUtils.md5DigestAsHex((appId + q + salt + secretKey).getBytes());
        String url = UriComponentsBuilder.fromHttpUrl(TRANSLATE_HOST_URL).toUriString();
        try {
            MultiValueMap<String, String> body = constructRequestBody(q, from, to, salt, sign);
            Mono<String> responseMono = webClient.post().uri(url).bodyValue(body).retrieve().bodyToMono(String.class);

            String responseData = responseMono.block();
            assert responseData != null;
            logger.info("Translation request: {}, response: {}", q, responseData);

            return parseResponse(responseData);

        }
        catch (Exception e) {
            logger.error("Failed to invoke translate API due to: {}", e.getMessage());
            return null;
        }
    }

    private MultiValueMap<String, String> constructRequestBody(String q, String from, String to, String salt, String sign) {
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("q", q);
        body.add("from", from);
        body.add("to", to);
        body.add("appid", appId);
        body.add("salt", salt);
        body.add("sign", sign);
        return body;
    }

    private Map<String, String> parseResponse(String responseData) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            Map<String, String> translations = new HashMap<>();
            TranslationResponse responseList = mapper.readValue(responseData, TranslationResponse.class);
            String to = responseList.to;
            List<TranslationResult> translationsList = responseList.trans_result;
            if (translationsList != null) {
                for (TranslationResult translation : translationsList) {
                    String translatedText = translation.dst;
                    translations.put(to, translatedText);
                    logger.info("Translated text to {}: {}", to, translatedText);
                }
            }
            return translations;
        }
        catch (Exception e) {
            try {
                Map<String, String> responseList = mapper.readValue(responseData,
                        mapper.getTypeFactory().constructMapType(Map.class, String.class, String.class));
                logger.info(
                        "Translation exception, please inquire Baidu translation api documentation to info error_code:{}",
                        responseList);
                return responseList;
            }
            catch (Exception ex) {
                logger.error("Failed to parse json due to: {}", ex.getMessage());
                return null;
            }
        }
    }

    public record TranslationResult(String src, String dst) {
    }

    public record TranslationResponse(String from, String to, List<TranslationResult> trans_result) {
    }
}

5、百度翻译 mcp服务端启动类

/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author brianxiadong
 */

package org.springframework.ai.mcp.sample.server;


import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class McpBaiduServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(McpBaiduServerApplication.class, args);
    }


    @Bean
    public ToolCallbackProvider baiduTranslateFunction(BaidutranslateService baidutranslateService) {
        return MethodToolCallbackProvider.builder().toolObjects(baidutranslateService).build();
    }


}

 

6、spring-ai 大模型应用程序 mcp客户端, 集成了mcp天气查询工具和 mcp百度翻译工具

pom文件

	<dependencies>

		<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-mcp-client-webflux-spring-boot-starter</artifactId>
			<version>${spring-ai.version}</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba.cloud.ai</groupId>
			<artifactId>spring-ai-alibaba-starter</artifactId>
			<version>${spring-ai-alibaba.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


	</dependencies>

yml配置文件

 

server:
  port: 58888
  servlet:
    encoding:
      charset: UTF-8
      enabled: true
      force: true

spring:
  application:
    name: mcp
  ai:
    dashscope:
      api-key: sk-xoxoxoxoxoxox
    mcp:
      client:
        sse:
          connections:
            server1:
              url: http://localhost:58080
            server2:
              url: http://localhost:38080
  mandatory-file-encoding: UTF-8

# 调试日志
logging:
  level:
    io:
      modelcontextprotocol:
        client: DEBUG
        spec: DEBUG

 

百度翻译controller

package org.springframework.ai.mcp.samples.client.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/translate")
public class McpTranslateController {

    private final ChatClient dashScopeChatClient;
    private final ToolCallbackProvider tools;


    public McpTranslateController(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools) {
        this.tools = tools;
        this.dashScopeChatClient = chatClientBuilder
                .build();
    }


    /**
     * 无工具版
     */
    @GetMapping("/chat")
    public String simpleChat(@RequestParam(value = "query", defaultValue = "帮我把以下内容翻译成英文:寻寻觅觅") String query) {
        return dashScopeChatClient.prompt(query).call().content();
    }

    /**
     * 调用工具版 - function
     */
    @GetMapping("/chat-mcp")
    public String chatTranslateFunction(@RequestParam(value = "query", defaultValue = "帮我把以下内容翻译成英文:寻寻觅觅") String query) {
        return dashScopeChatClient.prompt(query).tools(tools).call().content();
    }


}

 

天气查询 controller

package org.springframework.ai.mcp.samples.client.controller;



import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/weather")
public class McpWeatherController {

    private final ChatClient dashScopeChatClient;
    private final ToolCallbackProvider tools;

    public McpWeatherController(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools) {
//        使用方式只需注入ToolCallbackProvider和ChatClient.Builder
        this.tools = tools;
        this.dashScopeChatClient = chatClientBuilder
                .build();
    }


    /**
     * 调用工具版 - function
     */
    @GetMapping("/chat-mcp")
    public String chatMcp(@RequestParam(value = "query", defaultValue = "请告诉我北京1天以后的天气",required = false) String query) {
        System.out.println("\n>>> QUESTION: " + query);
        String msg = dashScopeChatClient.prompt(query).tools(tools).call().content();
        System.out.println("\n>>> ASSISTANT: " + msg);
        return msg;
    }

    @GetMapping("/chat")
    public String chat(@RequestParam(value = "query", defaultValue = "请告诉我北京1天以后的天气",required = false) String query) {
        System.out.println("\n>>> QUESTION: " + query);
        String msg = dashScopeChatClient.prompt(query).call().content();
        System.out.println("\n>>> ASSISTANT: " + msg);
        return msg;
    }


}

 

启动类

/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author brianxiadong
 */
package org.springframework.ai.mcp.samples.client;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication(exclude = {
        org.springframework.ai.autoconfigure.mcp.client.SseHttpClientTransportAutoConfiguration.class
})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }


}

 

 

http://www.dtcms.com/a/107795.html

相关文章:

  • Java 大视界 -- Java 大数据在智慧文旅虚拟场景构建与沉浸式体验增强中的技术支撑(168)
  • STM32F103_LL库+寄存器学习笔记14 - CAN发送完成中断
  • 【小兔鲜】day02 Pinia、项目起步、Layout
  • 2023年CIE SCI1区TOP:序列融合麻雀搜索算法ISSA,深度解析+性能实测
  • N元语言模型的时间和空间复杂度计算
  • 【网络协议】三次握手与四次挥手
  • 【区块链 + 可信存证】创世云区块链存证平台 | FISCO BCOS 应用案例
  • Redis 数据结构的底层实现—字符串、哈希表、列表、集合
  • 【银河麒麟系统常识】命令:reboot(立即重启操作系统)
  • SQL server 2022和SSMS的使用案例1
  • linux,物理机、虚拟机,同时内外网实现方案;物理机与虚拟机互通网络;
  • 机器学习 分类算法
  • 苍穹外卖day12
  • 网络安全的重要性与防护措施
  • 一、STM32简介
  • OpenCV 图形API(10)用于执行标量除以矩阵的逐元素操作函数divRC()
  • 低代码框架
  • 网络协议之系列
  • 商标知识产权管理系统平台有哪些功能?
  • 第三方平台步步紧逼之下,独立站商城系统应如何做
  • MoLe-VLA:通过混合层实现的动态跳层视觉-语言-动作模型实现高效机器人操作
  • 牛脸分割数据集labelme格式1324张1类别有增强图片
  • macvlan 和 ipvlan 实现原理及设计案例详解
  • 华为Eudemon8000E Series CFCard概述
  • MySQL 事务与锁机制详解
  • pytorch 离线安装
  • win11 设置 wsl2 ubuntu
  • 4 月 6 日 共赴香港|聚焦 Polkadot 为 Web3 云开拓的创新实践路径!
  • IGMP(Internet Group Management Protocol)与组播技术深度解析
  • 巧记英语四级单词 Unit1-2【晓艳老师版】