SpringAI配置相关MCP有这几种方式:SSE,Stdio,StreamableHTTP等方式

其中Stdio是本地进程间调用,需要本地有服务

Stdio

Stdio在SpringAI配置需要在mcp-servers.json中进行相关配置

{
  "mcpServers": {
    "baidu-maps": {
      "command": "cmd",
      "args": [
        "/c",
        "npx",
        "-y",
        "@baidumap/mcp-server-baidu-map"
      ],
      "env": {
        "BAIDU_MAP_API_KEY": "xxxx"
      }
    }
  }
}

cmd /c是指启动后关闭cmd窗口并常驻后台

并且这种配置方式要求本地要有nodejs等相关环境

下面这种方式有时候启动失败,因此用cmd /c的方式

{
  "mcpServers": {
    "baidu-maps": {
      "command": "npx",
      "args": [
        "-y",
        "@baidumap/mcp-server-baidu-map"
      ],
      "env": {
        "BAIDU_MAP_API_KEY": "xxxx"
      }
    }
  }
}

SSE和StreamableHTTP

这两个主要是远程调用,在SpringAI中不能用上面stdio的那种方式,而是需要在application.yml中进行配置

SSE配置

spring:
  ai:
    mcp:
      client:
        sse:
          connections:
            open-webSearch:
              url: https://mcp.api-inference.modelscope.net/
              sse-endpoint: 8a9d148xx/sse

StreamableHTTP配置

spring:
  ai:
    mcp:
      client:
        streamable-http:
          connections:
            open-webSearch:
              url: https://mcp.api-inference.modelscope.net/
              endpoint: 8a9d148xx/mcp

这里面有几个注意点:url是基础url,虽然地址是https://mcp.api-inference.modelscope.net/8a9df1ffb3e148/mcp,但是url不能这样写,要拆成https://mcp.api-inference.modelscope.net/和后面的端点

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐