MCP服务器模板

一个基于Spring Boot的模板,用于创建使用Spring AI的模型上下文协议(MCP)服务器,提供基本配置、示例工具和测试界面。

  • by tricerafi
  • HTML
  • 本地部署
  • 更新于
在 GitHub 查看 tricerafi/doku-mcp-test

使用说明

MCP服务器模板

一个使用Spring AI创建模型上下文协议(MCP)服务器的Spring Boot模板。

概述

此模板为使用Spring Boot构建MCP服务器提供了一个简洁的起点。它包括:

  • 使用Spring AI进行基本的MCP服务器配置
  • 示例工具实现
  • 用于测试的简单网页聊天界面
  • Maven 构建配置

特点/特性

  • MCP协议支持基于Spring AI的MCP服务器实现构建
  • 示例工具展示MCP工具模式的回声工具
  • 网页界面用于测试您的MCP服务器的简单聊天界面
  • Spring Boot具备自动配置功能的生产就绪框架

快速入门

先决条件

  • Java 21 或更高版本
  • Maven 3.6或更高版本

运行服务器

  1. 克隆此仓库
  2. 运行应用程序:
    mvn spring-boot:run
    
  3. 访问服务器:
    • 聊天界面: http://localhost:8080/
    • MCP服务器端点: http://localhost:8080/mcp

建筑;建造

mvn clean package

配置

服务器已配置在 src/main/resources/application.properties:

# Server Configuration
server.port=8080

# Spring AI MCP Server Properties
spring.ai.mcp.server.name=template-mcp-server
spring.ai.mcp.server.version=1.0.0
spring.ai.mcp.server.type=SYNC
spring.ai.mcp.server.instructions=This is a template MCP server with example tools
spring.ai.mcp.server.base-url=/mcp
spring.ai.mcp.server.capabilities.tool=true
spring.ai.mcp.server.capabilities.resource=true
spring.ai.mcp.server.capabilities.prompt=true
spring.ai.mcp.server.capabilities.completion=true
spring.ai.mcp.server.enabled=true
spring.ai.mcp.server.protocol=STATELESS

添加新工具

要添加新的MCP工具,请在(相应位置)创建方法 ToolService.java 附有……的注释 @Tool:

@Tool(description = "Your tool description")
public String yourTool(String parameter) {
    // Your tool implementation
    return "Tool result";
}

项目结构

src/
├── main/
│   ├── java/com/example/mcpserver/
│   │   ├── McpServerApplication.java      # Main application class
│   │   ├── controller/
│   │   │   └── HomeController.java        # Web controller for chat interface
│   │   └── service/
│   │       └── ToolService.java           # MCP tools implementation
│   └── resources/
│       ├── application.properties         # Application configuration
│       └── static/
│           └── index.html                # Chat interface

发展

测试工具

  1. 启动服务器
  2. 导航至 http://localhost:8080 用于聊天界面
  3. 使用聊天界面测试您的工具
  4. 示例回声工具可以通过输入任意消息来测试
  5. 对于MCP客户端集成,请连接至 http://localhost:8080/mcp

定制化

  1. 更新包名改变 com.example 到您所需的包结构
  2. 修改服务器元数据更新MCP服务器属性 application.properties
  3. 添加你的工具实施新工具于 ToolService.java
  4. 自定义用户界面修改 index.html 以匹配您的品牌形象

许可证

此模板仅作为教育和开发用途提供,不附带任何保证。