SpringAI开发MCP

官方文档

https://mcp-docs.cn/examples

依赖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<!-- 测试发现1.0.0-M版本的依赖有问题,base-url配置后无法读取 -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!-- 版本与 spring-ai 保持一致 -->
        <dependency>
            <groupId>com.github.victools</groupId>
            <artifactId>jsonschema-generator</artifactId>
            <version>4.31.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.victools</groupId>
            <artifactId>jsonschema-module-jackson</artifactId>
            <version>4.31.1</version>
        </dependency>

配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
spring:
  #  main:
  # 关闭web服务(关键配置)
  #    web-application-type: none
  ai:
    mcp:
      server:
        #        stdio: true
        name: CodeGen-WebMvc-McpSever
        version: 1.0.0
        # 只决定 「工具方法本身」 在 Spring 容器里被调用时是同步还是异步执行,跟stdio、sse完全无关。
        type: ASYNC
        enable: true
        instructions: '生成前端代码的mcp-server'
        # 由客户端访问,如cursor、trae,因此mcp.json填的是这个
        sse-endpoint: /sse
        # 业务调用
        sse-message-endpoint: /mcp
        base-url: /api/v1
        tool-change-notification: true
        capabilities:
          tool: true
          resource: true
          prompt: true
          completion: true

MCP.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
	"mcpServers": {
		"mcp-jar-stdio": {
			"command": "java",
			"args": [
				"-Dspring.ai.mcp.server.stdio=true",
				"-jar",
				"D:/code/mcpServer.jar"
			],
			"env": {
				"new_env": "FAT",
				"idc": "AliCloudLocal"
			}
		}
	}
}

不配置 servlet.context-path,则似乎因为 mcp 协议版本问题连不上 mcp server

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计