MCP服务器示例

一个基于Spring Boot构建的简单MCP服务器示例,用于展示如何通过MCP协议与AI工具交互。

  • by NCAR
  • Java
  • 本地部署
  • 更新于
在 GitHub 查看 NCAR/sage-mcp-server-example

使用说明

sage mcp服务器示例

一个非常简单的例子,一个内置于spring boot中的mcp服务器。

受到这个例子的启发:

将你的AI连接到一切:Spring AI的MCP启动程序

模型上下文协议

生命周期,官方文件。

工具,官方文件。

春天

流式HTTP MCP服务器,Spring AI文档。

Docker镜像

$ docker build -t sage-mcp-server-example:latest --build-arg APP_JAR=target/sage-mcp-server-example-1.0-SNAPSHOT.jar .

卷发请求

MCP初始化

需要建立mcp会话id http标头属性。

curl -v -X POST http://localhost:8080/mcp \
  -H "Accept: application/json, text/event-stream" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
      "protocolVersion": "2025-03-26",
      "capabilities": {
        "tools": {},
        "resources": {},
        "prompts": {}
      },
      "clientInfo": {
        "name": "test-client",
        "version": "1.0.0"
      }
    }
  }'

输出:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /mcp HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.1.2
> Accept: application/json, text/event-stream
> Content-Type: application/json
> Content-Length: 318
>
< HTTP/1.1 200
< Mcp-Session-Id: 97e6ab94-1d1d-4676-83e0-795084695d5c
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Thu, 04 Dec 2025 19:41:28 GMT
<
* Connection #0 to host localhost left intact
{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-03-26","capabilities":{"completions":{},"logging":{},"prompts":{"listChanged":true},"resources":{"subscribe":false,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"sage-mcp-server","version":"1.0.0"}}}

然后,我们需要获取返回的Mcp会话Id,并将其用于接下来的几个请求。

列出MCP工具

curl -X POST http://localhost:8080/mcp \
  -H "Mcp-Session-Id: <id_from_initialize>" \
  -H "Accept: application/json, text/event-stream" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/list",
    "params": {
      },
      "clientInfo": {
        "name": "test-client",
        "version": "1.0.0"
      }
    }
  }'

输出示例:

{
  "jsonrpc":"2.0",
  "id":1,
  "result":{
    "tools":[
      {
        "name":"getCurrentTime",
        "title":"getCurrentTime",
        "description":"Get the time from the server.",
        "inputSchema":{
          "type":"object",
          "properties":{
            
          },
          "required":[
            
          ]
        },
        "annotations":{
          "title":"",
          "readOnlyHint":false,
          "destructiveHint":true,
          "idempotentHint":false,
          "openWorldHint":true
        }
      },
      {
        "name":"getTemperature",
        "title":"getTemperature",
        "description":"Get the temperature (in celsius) for a specific location",
        "inputSchema":{
          "type":"object",
          "properties":{
            "latitude":{
              "type":"number",
              "format":"double",
              "description":"The location latitude"
            },
            "longitude":{
              "type":"number",
              "format":"double",
              "description":"The location longitude"
            }
          },
          "required":[
            "latitude",
            "longitude"
          ]
        },
        "annotations":{
          "title":"",
          "readOnlyHint":false,
          "destructiveHint":true,
          "idempotentHint":false,
          "openWorldHint":true
        }
      }
    ]
  }
}

调用温度MCP工具

curl -X POST http://localhost:8080/mcp \
  -H "Mcp-Session-Id: e4e5f549-97f7-4792-88f9-3dfa7d246257" \
  -H "Accept: application/json, text/event-stream" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
        "name": "getTemperature",
        "arguments": {
          "latitude": 39.9783,
          "longitude": 105.2750
        }
      },
      "clientInfo": {
        "name": "test-client",
        "version": "1.0.0"
      }
    }
  }'

输出示例:

{
  "jsonrpc":"2.0",
  "id":1,
  "result":{
    "content":[
      {
        "type":"text",
        "text":"{\"current\":{\"time\":\"2025-12-04T19:45:00\",\"interval\":900,\"temperature_2m\":-4.6},\"current_units\":{\"time\":\"iso8601\",\"interval\":\"seconds\",\"temperature_2m\":\"°C\"}}"
      }
    ],
    "isError":false
  }
}

调用当前时间MCP工具

curl -X POST http://localhost:8080/mcp \
  -H "Mcp-Session-Id: e4e5f549-97f7-4792-88f9-3dfa7d246257" \
  -H "Accept: application/json, text/event-stream" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
        "name": "getCurrentTime",
        "arguments": {
        }
      },
      "clientInfo": {
        "name": "test-client",
        "version": "1.0.0"
      }
    }
  }'

输出示例:

{
  "jsonrpc":"2.0",
  "id":1,
  "result":{
    "content":[
      {
        "type":"text",
        "text":"\"2025-12-04T12:57:16.190039\""
      }
    ],
    "isError":false
  }
}