# MCP 模型上下文协议

# 简介

MCP 定义了一套标准化的接口、数据格式和通信机制,专门用于在分布式部署的模型(或模型组件、AI Agent)之间交换、同步和维护与模型执行相关的上下文信息

# 开始

MCP 模型上下文协议,其实就是给大模型安装了一双手,所有 MCP 服务按照这双手的规范,让这双手能调用自己的服务。

例子:

  1. 按照规范写了一个服务是可以调用本地文件,修改或者新建文件,那么模型就会通过这双手,去调用这个服务,而不需要我们自己去改

  2. 发邮箱

  3. 浏览器页面操作

  4. 查资料

  5. 生成图文内容

  6. 等等

推荐一个精选 mcp 服务的网站 awesome-mcp-servers

上面这些都可以写成 mcp 服务,然后我们告诉模型要做什么,模型自己通过这双手去调用,那么要怎么调用呢?也就是怎么拥有这双手?

现在市面上有很多工具有,比如: cursorchatboxCherry Studio

这里以 Cherry Studio 做列子配置

# 开发自己的 MCP 服务

# stdio 类型

此类型是标准输入输出,拿 java 来举例子,相当于将打包好的 jar 包下载下来,在本地运行,调用对应的方法。

# 创建 java springboot 项目

pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.3</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-server</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

创建一个 Service:

package com.tz.mcp.service;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
/**
 * <p> Project: hello_mcp - HelloService </p>
 *
 * @author Tz
 * @version 1.0.0
 * @date 2025/07/08 19:53
 * @since 1.0.0
 */
@Service
public class HelloService {
    @Tool(description = "获取姓名")
    public String getName() {
        return "张三";
    }
    @Tool(description = "根据姓名获取固定电话")
    public String getPhoneByName(
            @ToolParam(description = "姓名") String name
    ) {
        if (name == null) {
            return null;
        } else if (name.equals("张三")) {
            return "你好: " + name + " 你的号码是:13xxxxxxx";
        } else {
            return "没有查询到用户,默认号码:xxxxx";
        }
    }
}

main:

package com.tz.mcp;
import com.tz.mcp.service.HelloService;
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;
/**
 * <p> Project </p>
 *
 * @author Tz
 * @version 1.0.0
 * @date 2025/07/08 19:29
 * @since 1.0.0
 */
@SpringBootApplication
public class MCPApplication {
    public static void main(String[] args) {
        SpringApplication.run(MCPApplication.class, args);
    }
    @Bean
    public ToolCallbackProvider weatherTools(HelloService helloService) {
        return  MethodToolCallbackProvider.builder().toolObjects(helloService).build();
    }
}

写好后打包好即可!

# sse 类型

也就是网络请求的 mcp, 下面开始

# 创建 java springboot 项目

pom.xml

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
</dependency>

创建 service

package com.tz.mcp.service;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
/**
 * <p> Project: hello_mcp - ToolServer </p>
 *
 * @author Tz
 * @version 1.0.0
 * @date 2025/07/09 19:53
 * @since 1.0.0
 */
@Service
public class ToolServer {
    @Tool(description = "获取名字")
    public String getName() {
        return "张三";
    }
    @Tool(description = "根据名字获取手机号码")
    public String getPhoneByName(String name) {
        return name + "132xxxxxx";
    }
}

创建配置类 McpServerConfig 注册

package com.tz.mcp.config;
import com.tz.mcp.service.ToolServer;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * <p> Project: hello_mcp - McpConfig </p>
 *
 * @author Tz
 * @version 1.0.0
 * @date 2025/07/09 11:39
 * @since 1.0.0
 */
@Configuration
@EnableWebMvc
public class McpServerConfig implements WebMvcConfigurer {
    @Bean
    public ToolCallbackProvider openLibraryTools(ToolServer toolServer) {
        return MethodToolCallbackProvider.builder().toolObjects(toolServer).build();
    }
}

yml 文件:

server:
  port: 8088
spring:
  application:
    name: springboot-tz-mcp-workflow-server
  ai:
    mcp:
      server:
        name: tz-mcp-workflow-server
        version: 1.0.0
        type: SYNC
        sse-message-endpoint: /mcp/messages

需要把项目启动起来!!!

#cherry studio 中配置

# 运行效果