Cars MCP Server
Okay, here's a basic example of how you might set up a simple Minecraft Protocol (MCP) server using Spring AI. This is a high-level outline and requires you to fill in the details based on your specific needs and the MCP library you choose. This example focuses on the Spring AI integration for handling commands or interactions. **Important Considerations:** * **MCP Library:** There isn't a single "standard" MCP library for Java. You'll need to choose one. Popular options include: * **MinecraftForge:** A very common modding platform. If you're building a mod, this is likely your choice. * **SpongeAPI:** Another modding platform, known for its plugin API. * **Custom Implementation:** You *could* implement the MCP protocol yourself, but this is a significant undertaking. I strongly recommend using an existing library. * **Spring Boot:** This example assumes you're using Spring Boot for easy setup and dependency management. * **Spring AI:** This example uses Spring AI to process player input and generate responses. **Project Setup (Maven or Gradle):** Add the following dependencies to your `pom.xml` (Maven) or `build.gradle` (Gradle): **Maven (`pom.xml`):** ```xml <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>0.8.0</version> <!-- Or the latest version --> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai</artifactId> <version>0.8.0</version> <!-- Or the latest version --> </dependency> <!-- Your chosen MCP library dependency goes here. Example using a hypothetical MCP library: --> <!-- <dependency> <groupId>com.example</groupId> <artifactId>mcp-library</artifactId> <version>1.0.0</version> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> ``` **Gradle (`build.gradle`):** ```gradle dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.ai:spring-ai-core:0.8.0' // Or the latest version implementation 'org.springframework.ai:spring-ai-openai:0.8.0' // Or the latest version // Your chosen MCP library dependency goes here. Example using a hypothetical MCP library: // implementation 'com.example:mcp-library:1.0.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' } ``` **1. Spring Boot Application Class:** ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class McpServerApplication { public static void main(String[] args) { SpringApplication.run(McpServerApplication.class, args); } } ``` **2. MCP Server Component (Example):** ```java import org.springframework.ai.client.AiClient; import org.springframework.ai.prompt.PromptTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.util.HashMap; import java.util.Map; @Component public class McpServer { // Replace with your actual MCP server implementation private boolean isRunning = false; @Autowired private AiClient aiClient; @Value("${spring.ai.openai.api-key}") private String openAiApiKey; @Value("${mcp.server.port}") private int serverPort; @Value("${mcp.ai.prompt}") private String aiPrompt; @PostConstruct public void startServer() { System.out.println("Starting MCP Server on port: " + serverPort); System.out.println("Using OpenAI API Key: " + openAiApiKey); // Initialize your MCP server here (using your chosen library) // Example (replace with actual code): // this.mcpServer = new MyMcpserver(serverPort); // this.mcpServer.start(); isRunning = true; System.out.println("MCP Server started."); } @PreDestroy public void stopServer() { if (isRunning) { System.out.println("Stopping MCP Server"); // Stop your MCP server here (using your chosen library) // Example (replace with actual code): // this.mcpServer.stop(); isRunning = false; System.out.println("MCP Server stopped."); } } // Example method to handle player input and use Spring AI public String handlePlayerCommand(String playerName, String command) { System.out.println("Received command from " + playerName + ": " + command); // Use Spring AI to generate a response PromptTemplate promptTemplate = new PromptTemplate(aiPrompt); Map<String, Object> model = new HashMap<>(); model.put("playerName", playerName); model.put("command", command); String response = aiClient.generate(promptTemplate.create(model)).getGeneration().getText(); System.out.println("AI Response: " + response); return response; // Or send the response back to the player in-game } } ``` **3. Configuration (`application.properties` or `application.yml`):** ```properties spring.ai.openai.api-key=YOUR_OPENAI_API_KEY # Replace with your actual OpenAI API key mcp.server.port=25565 # Or your desired port mcp.ai.prompt=Player {playerName} issued command: {command}. Respond in a helpful and Minecraft-themed way. ``` **Explanation:** * **Dependencies:** The `spring-boot-starter-web` dependency is included for basic web functionality (though you might not need it directly for the MCP server itself, it's often useful for management endpoints). `spring-ai-core` and `spring-ai-openai` are the core Spring AI dependencies. You'll need to add the dependency for your chosen MCP library. * **`McpServerApplication`:** A standard Spring Boot application entry point. * **`McpServer` Component:** * `@Component`: Marks this class as a Spring-managed component. * `@Autowired AiClient`: Injects the Spring AI client. * `@Value`: Injects values from your `application.properties` or `application.yml` file. **Important:** Replace `YOUR_OPENAI_API_KEY` with your actual OpenAI API key. * `@PostConstruct`: The `startServer()` method is called after the Spring context is initialized. This is where you would start your MCP server. **You'll need to replace the placeholder comments with the actual code to initialize and start your chosen MCP library.** * `@PreDestroy`: The `stopServer()` method is called when the Spring context is shutting down. This is where you would stop your MCP server. **You'll need to replace the placeholder comments with the actual code to stop your chosen MCP library.** * `handlePlayerCommand()`: This is a *very* simplified example of how you might handle player input. It takes the player's name and command as input, uses Spring AI to generate a response, and then returns the response. **You'll need to adapt this to your specific MCP library and how it handles player input.** * **Spring AI Integration:** * `PromptTemplate`: Defines the prompt that will be sent to the AI model. The prompt includes placeholders for the player's name and command. * `aiClient.generate()`: Sends the prompt to the AI model and returns a response. * The response is then printed to the console and returned. * **`application.properties`:** Contains the configuration for your application, including the OpenAI API key, the server port, and the AI prompt. **Remember to replace `YOUR_OPENAI_API_KEY` with your actual key.** **How to Use It (Conceptual):** 1. **Choose an MCP Library:** Select the MCP library that best suits your needs (MinecraftForge, SpongeAPI, or a custom implementation). 2. **Implement MCP Server Logic:** Replace the placeholder comments in the `McpServer` class with the actual code to initialize, start, and stop your MCP server using your chosen library. This will involve handling network connections, player authentication, world loading, etc. 3. **Handle Player Input:** Modify the `handlePlayerCommand()` method to receive player input from your MCP server. This will likely involve listening for specific events or packets from the MCP library. 4. **Send Responses to Players:** Modify the `handlePlayerCommand()` method to send the AI-generated response back to the player in the game. This will involve using the appropriate methods from your MCP library to send messages to players. 5. **Configure Spring AI:** Make sure you have a valid OpenAI API key and that you've configured it in your `application.properties` file. You can also experiment with different AI models and prompt templates to get the desired behavior. **Example Scenario:** 1. A player types `/ask what is the best way to find diamonds?` in the game. 2. Your MCP server receives this command. 3. The `handlePlayerCommand()` method is called with `playerName` set to the player's name and `command` set to "what is the best way to find diamonds?". 4. The `PromptTemplate` is used to create a prompt like: "Player Steve issued command: what is the best way to find diamonds?. Respond in a helpful and Minecraft-themed way." 5. The prompt is sent to the OpenAI API. 6. The OpenAI API generates a response, such as: "Ahoy, matey! To find diamonds, ye should dig down to level -58 and look for them near lava pools. Be careful, though, or ye might get burned!" 7. The response is sent back to the player in the game. **Important Notes:** * **Error Handling:** This is a very basic example and doesn't include any error handling. You'll need to add error handling to your code to make it more robust. * **Security:** Be very careful about security when building an MCP server. Make sure you properly authenticate players and protect against exploits. * **Asynchronous Operations:** MCP servers are typically multi-threaded. Make sure you handle player input and AI responses asynchronously to avoid blocking the main server thread. Consider using Spring's `@Async` annotation or other concurrency mechanisms. * **Rate Limiting:** Be mindful of the OpenAI API's rate limits. You may need to implement rate limiting in your code to avoid being throttled. * **Prompt Engineering:** The quality of the AI's responses depends heavily on the prompt you provide. Experiment with different prompts to get the best results. * **Cost:** Using OpenAI's API incurs costs. Be aware of the pricing and monitor your usage. **Chinese Translation of Key Terms:** * **MCP (Minecraft Protocol):** Minecraft 协议 (Minecraft Xiéyì) * **Spring AI:** Spring 人工智能 (Spring Réngōng Zhìnéng) * **Server:** 服务器 (Fúwùqì) * **Player:** 玩家 (Wánjiā) * **Command:** 命令 (Mìnglìng) * **Prompt:** 提示 (Tíshì) * **API Key:** API 密钥 (API Mìyuè) * **Dependency:** 依赖 (Yīlài) * **Configuration:** 配置 (Pèizhì) * **Response:** 回应 (Huíyìng) / 响应 (Xiǎngyìng) This example provides a starting point for building an MCP server with Spring AI. You'll need to adapt it to your specific needs and the MCP library you choose. Remember to consult the documentation for your chosen MCP library and the Spring AI documentation for more information. Good luck!
amarpreetbhatia
README
Cars MCP 服务器
这是一个使用 Spring AI 构建的 MCP (模型上下文协议) 服务器。该项目演示了如何使用 Spring AI 的 ToolCallback
API 来公开用于与汽车列表交互的工具,该列表代表 Amarpreet 的愿望清单。
演示
项目结构
该项目组织如下:
-
src/main/java/com/amarpreet/carsmcpserver/
CarsmcpserverApplication.java
: Spring Boot 应用程序的主要入口点。它初始化应用程序并使用CarService
类注册ToolCallback
beans。Car.java
: 一个简单的record
类,表示一辆汽车,包含两个字段:modelName
和buildYear
。CarService.java
: 一个服务类,提供与汽车列表交互的方法。这些方法使用ToolCallback
API 公开为工具。
-
src/main/resources/application.properties
: 包含 Spring Boot 应用程序的配置属性。 -
src/test/java/com/amarpreet/carsmcpserver/
CarsmcpserverApplicationTests.java
: 包含一个基本测试,以确保应用程序上下文正确加载。
ToolCallback API
ToolCallback
API 用于将 CarService
类中的方法公开为工具。这些工具可以通过编程方式或通过 AI 驱动的界面调用。CarsmcpserverApplication
类使用 ToolCallbacks.from()
方法注册这些工具。
工具注册
在 CarsmcpserverApplication.java
中:
@Bean
public List<ToolCallback> amarpreetTools(CarService carService) {
return List.of(ToolCallbacks.from(carService));
}
这将注册 CarService
类中所有使用 @Tool
注释的方法作为 ToolCallback
实例。
CarService
CarService
类是此应用程序的核心。它管理一个汽车列表并提供以下方法:
CarService 中的方法
getCars()
- 工具名称:
get_cars
- 描述: 检索 Amarpreet 愿望清单中的汽车列表。
- 返回类型:
List<Car>
getCar(String name)
- 工具名称:
get_car_by_name
- 描述: 按型号名称查找汽车。
- 参数:
name
- 要搜索的型号名称。
- 返回类型:
Car
(如果未找到,则为null
)
getCarByYear(Integer year)
- 工具名称:
get_car_by_years
- 描述: 按生产年份查找汽车。
- 参数:
year
- 要搜索的生产年份。
- 返回类型:
Car
(如果未找到,则为null
)
初始化
CarService
类在使用 @PostConstruct
注释的应用程序启动期间初始化汽车列表:
public void init() {
log.info("Initializing cars MCP server...");
cars.addAll(List.of(
new Car("BMW Neue Klasse", 2025),
new Car("Ferrari EV", 2025),
new Car("Mercedes AMG 4dr EV", 2025),
new Car("Porsche 718 Boxster", 2025)
));
}
如何运行
使用 Maven 构建项目:
mvn clean install
运行应用程序:
java -jar target/carsmcpserver-0.0.1-SNAPSHOT.jar
配置
可以使用位于 src/main/resources
中的 application.properties
文件配置应用程序。关键属性包括:
spring.application.name
: 应用程序的名称。spring.main.web-application-type
: 对于非 Web 应用程序,设置为none
。spring.ai.mcp.server
: MCP 服务器的名称。spring.ai.mcp.version
: MCP 服务器的版本。
推荐服务器
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
MCP Package Docs Server
促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。
Claude Code MCP
一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。
@kazuph/mcp-taskmanager
用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。
mermaid-mcp-server
一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。
Jira-Context-MCP
MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

Linear MCP Server
一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。
Curri MCP Server
通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。