入门攻略
MCP提交
探索
Hello MCP Server Current Time
内容详情
替代品
基于spring-ai-starter-mcp-server的自定义mcp server简单示例,获取当前时间
项目概述
本项目是一个基于Spring AI Starter MCP Server的自定义MCP服务器的简单示例。该项目的主要目的是展示如何使用MCP协议来创建一个自定义的时间服务,该服务能够返回当前系统时间。
功能说明
- 基于Spring Boot框架搭建
- 集成MCP通信协议
- 提供获取当前时间的功能
项目结构
src/main/java/
com/example/mcptime/
McpTimeApplication.java
controller/
TimeController.java
config/
McpServerConfig.java
配置文件
# application.properties
server.port=8090
# MCP服务器配置
mcp.server.name=mcp-time-server
mcp.server.ip=127.0.0.1
mcp.server.port=5000
核心代码
TimeController.java
package com.example.mcptime.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class TimeController {
@GetMapping("/current-time")
public String getCurrentTime() {
return new Date().toString();
}
}
McpServerConfig.java
package com.example.mcptime.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "mcp.server")
public class McpServerConfig {
private String name;
private String ip;
private int port;
// Getters and Setters omitted for brevity
}
McpTimeApplication.java
package com.example.mcptime;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.remoting.mcp.McpServer;
@SpringBootApplication
@ComponentScan(basePackages = "com.example.mcptime")
public class McpTimeApplication {
public static void main(String[] args) {
SpringApplication.run(McpTimeApplication.class, args);
}
@Bean
public McpServer mcpServer() {
return new McpServer("tcp://127.0.0.1:5000");
}
}
运行与验证
- 将项目打包并运行:
mvn spring-boot:run
- 访问
/current-time
端点,查看当前时间:
curl http://localhost:8090/current-time
项目总结
通过本示例,展示了如何基于Spring Boot和MCP协议快速搭建一个自定义的时间服务。此服务可以通过简单的HTTP请求获取当前系统时间,适用于需要精确时间戳的各种应用场景。