hello-mcp-server-current-time

hello-mcp-server-current-time

好的,这是一个基于 `spring-ai-starter-mcp-server` 的自定义 MCP Server 简单示例,用于获取当前时间。 我将提供代码示例,并解释关键部分。 **1. 项目结构 (假设 Maven 项目)** ``` my-mcp-server/ ├── pom.xml └── src/ └── main/ ├── java/ │ └── com/example/ │ └── mcp/ │ ├── config/ │ │ └── McpServerConfig.java // MCP Server 配置 │ └── controller/ │ └── TimeController.java // 处理时间请求的 Controller └── resources/ └── application.properties // 配置文件 ``` **2. `pom.xml` (Maven 依赖)** ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <!-- 使用最新的 Spring Boot 版本 --> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>my-mcp-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>my-mcp-server</name> <description>Demo project for Spring Boot MCP Server</description> <properties> <java.version>17</java.version> <spring-ai.version>1.0.0-M2</spring-ai.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-core</artifactId> <version>${spring-ai.version}</version> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-spring-boot-starter-mcp-server</artifactId> <version>${spring-ai.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` **关键依赖:** * `spring-boot-starter-web`: 提供 Spring Web MVC 功能,用于创建 RESTful API。 * `spring-ai-core`: Spring AI 核心库。 * `spring-ai-spring-boot-starter-mcp-server`: Spring AI MCP Server 启动器。 **3. `application.properties` (配置文件)** ```properties # 端口号 server.port=8080 # MCP Server 配置 (可选,使用默认值即可) spring.ai.mcp.server.enabled=true spring.ai.mcp.server.path=/mcp ``` **4. `McpServerConfig.java` (MCP Server 配置)** ```java package com.example.mcp.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Bean; import org.springframework.ai.autoconfigure.AiServiceProperties; import org.springframework.ai.autoconfigure.McpServerProperties; import org.springframework.ai.mcp.server.AiServiceHandlerFunction; import org.springframework.ai.mcp.server.McpServerEndpoint; import org.springframework.ai.mcp.server.McpServerFunctionRegistry; import org.springframework.ai.mcp.server.support.DefaultMcpServerFunctionRegistry; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.ApplicationContext; import org.springframework.web.servlet.function.RouterFunction; import org.springframework.web.servlet.function.ServerResponse; import java.util.List; import static org.springframework.web.servlet.function.RouterFunctions.route; import static org.springframework.web.servlet.function.RequestPredicates.POST; @Configuration public class McpServerConfig { @Bean @ConditionalOnMissingBean public McpServerFunctionRegistry mcpServerFunctionRegistry() { return new DefaultMcpServerFunctionRegistry(); } @Bean public McpServerEndpoint mcpServerEndpoint(McpServerProperties mcpServerProperties, McpServerFunctionRegistry mcpServerFunctionRegistry, ApplicationContext applicationContext, List<AiServiceHandlerFunction> aiServiceHandlerFunctions) { return new McpServerEndpoint(mcpServerProperties, mcpServerFunctionRegistry, applicationContext, aiServiceHandlerFunctions); } @Bean public RouterFunction<ServerResponse> timeRouterFunction(TimeController timeController, McpServerProperties mcpServerProperties) { return route(POST(mcpServerProperties.getPath() + "/time"), timeController::getTime); } } ``` **5. `TimeController.java` (处理时间请求的 Controller)** ```java package com.example.mcp.controller; import org.springframework.stereotype.Component; import org.springframework.web.servlet.function.ServerRequest; import org.springframework.web.servlet.function.ServerResponse; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import static org.springframework.web.servlet.function.ServerResponse.ok; @Component public class TimeController { public ServerResponse getTime(ServerRequest request) { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = now.format(formatter); return ok().body(formattedDateTime); } } ``` **解释:** * `@Component`: 将 `TimeController` 标记为一个 Spring 组件,使其可以被自动注入。 * `getTime(ServerRequest request)`: 处理 `/mcp/time` POST 请求的方法。 * `LocalDateTime.now()`: 获取当前时间。 * `DateTimeFormatter`: 格式化时间为 `yyyy-MM-dd HH:mm:ss` 格式。 * `ok().body(formattedDateTime)`: 返回 HTTP 200 OK 状态码,并将格式化后的时间作为响应体。 **6. 启动应用程序** 运行 Spring Boot 应用程序。 **7. 测试** 使用 `curl` 或 Postman 等工具发送 POST 请求到 `/mcp/time`: ```bash curl -X POST http://localhost:8080/mcp/time ``` **预期响应:** ``` 2023-11-21 10:30:00 (实际时间会不同) ``` **总结:** 这个示例展示了如何使用 `spring-ai-starter-mcp-server` 创建一个简单的 MCP Server,并添加自定义的端点来处理特定的请求。 关键步骤包括: 1. 添加必要的 Maven 依赖。 2. 配置 `application.properties`。 3. 创建一个 Controller 来处理请求。 4. 创建一个配置类,将Controller注册到RouterFunction中。 5. 启动应用程序并测试端点。 **重要提示:** * 确保你已经安装了 Java 17 或更高版本。 * 根据你的实际需求调整时间格式。 * 这个示例非常简单,你可以根据需要添加更复杂的功能,例如身份验证、授权、数据验证等。 * 请根据实际情况调整 Spring AI 的版本号。 * 如果遇到问题,请检查日志文件以获取更多信息。 这个例子应该能帮助你开始构建自己的自定义 MCP Server。 如果你有任何问题,请随时提出。

devotion-coding

研究与数据
访问服务器

README

hello-mcp-server-current-time

基于 spring-ai-starter-mcp-server 的自定义 mcp server 简单示例,获取当前时间。

推荐服务器

Crypto Price & Market Analysis MCP Server

Crypto Price & Market Analysis MCP Server

一个模型上下文协议 (MCP) 服务器,它使用 CoinCap API 提供全面的加密货币分析。该服务器通过一个易于使用的界面提供实时价格数据、市场分析和历史趋势。 (Alternative, slightly more formal and technical translation): 一个模型上下文协议 (MCP) 服务器,利用 CoinCap API 提供全面的加密货币分析服务。该服务器通过用户友好的界面,提供实时价格数据、市场分析以及历史趋势数据。

精选
TypeScript
MCP PubMed Search

MCP PubMed Search

用于搜索 PubMed 的服务器(PubMed 是一个免费的在线数据库,用户可以在其中搜索生物医学和生命科学文献)。 我是在 MCP 发布当天创建的,但当时正在度假。 我看到有人在您的数据库中发布了类似的服务器,但还是决定发布我的服务器。

精选
Python
mixpanel

mixpanel

连接到您的 Mixpanel 数据。 从 Mixpanel 分析查询事件、留存和漏斗数据。

精选
TypeScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。

精选
Python
Nefino MCP Server

Nefino MCP Server

为大型语言模型提供访问德国可再生能源项目新闻和信息的能力,允许按地点、主题(太阳能、风能、氢能)和日期范围进行筛选。

官方
Python
Vectorize

Vectorize

将 MCP 服务器向量化以实现高级检索、私有深度研究、Anything-to-Markdown 文件提取和文本分块。

官方
JavaScript
Mathematica Documentation MCP server

Mathematica Documentation MCP server

一个服务器,通过 FastMCP 提供对 Mathematica 文档的访问,使用户能够从 Wolfram Mathematica 检索函数文档和列出软件包符号。

本地
Python
kb-mcp-server

kb-mcp-server

一个 MCP 服务器,旨在实现便携性、本地化、简易性和便利性,以支持对 txtai “all in one” 嵌入数据库进行基于语义/图的检索。任何 tar.gz 格式的 txtai 嵌入数据库都可以被加载。

本地
Python
Research MCP Server

Research MCP Server

这个服务器用作 MCP 服务器,与 Notion 交互以检索和创建调查数据,并与 Claude Desktop Client 集成以进行和审查调查。

本地
Python
Cryo MCP Server

Cryo MCP Server

一个API服务器,实现了模型补全协议(MCP),用于Cryo区块链数据提取,允许用户通过任何兼容MCP的客户端查询以太坊区块链数据。

本地
Python