gpt-4o-search-preview-high· OpenAI
LMArena Search Arena 24K
帮我写一个cursor的提示词,用于让cursor生成一个mcp(模型上下文协议) server的实例,使用的语言是Java springai
- User
帮我写一个cursor的提示词,用于让cursor生成一个mcp(模型上下文协议) server的实例,使用的语言是Java springai
- Assistant
要在 Java 中使用 Spring AI 创建一个 MCP(模型上下文协议)服务器实例,您可以按照以下步骤进行:
-
添加 Maven 依赖项:
在您的
pom.xml文件中,添加以下依赖项:<!-- Spring AI MCP Server WebMVC Starter -->
请注意,
1.0.0-M6是一个里程碑版本,您需要在pom.xml中添加 Spring Milestones 仓库:<repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>-
定义 MCP 服务器配置:
创建一个配置类,注册 MCP 服务器的工具、资源和提示词:
@Configuration @EnableWebMvc public class McpServerConfig implements WebMvcConfigurer { @Bean public ToolCallbackProvider toolCallbackProvider() { // 注册工具 return MethodToolCallbackProvider.builder() .toolObjects(new YourToolService()) .build(); } @Bean public List<mcpserverfeatures.syncresourceregistration> resourceRegistrations() { // 注册资源 return List.of(new McpServerFeatures.SyncResourceRegistration( new Resource("resource://example", "Example Resource", "Description", "text/plain", null), request -> new ReadResourceResult("Resource content") )); } @Bean public List<mcpserverfeatures.syncpromptregistration> promptRegistrations() { // 注册提示词 return List.of(new McpServerFeatures.SyncPromptRegistration( new Prompt("greeting", "Greeting prompt", List.of( new PromptArgument("name", "User's name", true) )), request -> new GetPromptResult("Hello, " + request.getArguments().get("name"), List.of()) )); } }在上述代码中,
YourToolService是您定义的工具服务类,包含具体的业务逻辑。 -
配置应用程序属性:
在
application.yaml文件中,配置 MCP 服务器的属性:spring: ai: mcp: server: name: my-mcp-server version: 1.0.0 type: SYNC sse-message-endpoint: /mcp/messages此配置指定了 MCP 服务器的名称、版本、类型(同步)以及 SSE 消息端点。
-
运行 MCP 服务器:
启动您的 Spring Boot 应用程序,MCP 服务器将根据上述配置运行,并提供工具、资源和提示词的功能。
通过上述步骤,您可以在 Java 中使用 Spring AI 创建一个 MCP 服务器实例,提供标准化的 AI 模型集成功能。</mcpserverfeatures.syncpromptregistration></mcpserverfeatures.syncresourceregistration>
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.