🚀 Gin-for-MCP
基于 Gin 框架的 MCP (Model Context Protocol) 服务示例
](https://go.dev/)   
📖 项目简介
这是一个使用 Gin 框架构建的 HTTP 服务,集成了 Protocol Buffers 和 MCP (Model Context Protocol),并通过 Swagger 自动生成 API 文档。项目展示了如何快速搭建一个支持 MCP 协议的 RESTful API 服务。
✨ 主要特性
- 🎯 Gin 框架 - 高性能的 Go Web 框架
- 📝 Protocol Buffers - 高效的序列化协议
- 🔄 自动代码生成 - 从 Proto 文件生成 Gin HTTP 处理器
- 📚 Swagger 文档 - 自动生成 OpenAPI 规范文档
- 🔌 MCP 集成 - 支持 Model Context Protocol 服务暴露
- ⚡ SSE 支持 - Server-Sent Events 实时通信
🛠️ 技术栈
- Go - 编程语言
- Gin - Web 框架
- Protocol Buffers - 接口定义语言
- gRPC-Gateway - Protobuf 到 HTTP 的转换
- swagger-mcp - MCP 协议支持
📦 前置要求
在开始之前,请确保已安装以下工具:
安装 swagger-mcp
go install github.com/danishjsheikh/swagger-mcp@latest或参考 swagger-mcp 官方文档 进行安装。
🚀 快速开始
1️⃣ 克隆项目
git clone
cd gin-for-mcp2️⃣ 安装依赖
go mod download3️⃣ 生成 API 代码和 Swagger 文档
make api此命令会:
- 从 Proto 文件生成 Go 代码
- 生成 Gin HTTP 处理器
- 生成 OpenAPI/Swagger 文档 (
mcp.swagger.json)
4️⃣ 运行服务
go run .服务将在 http://localhost:8080 启动。
5️⃣ 启动 MCP 服务(可选)
在新的终端窗口中运行:
swagger-mcp --specUrl file://./mcp.swagger.json --sse -sseAddr=:28080MCP 服务将在 http://localhost:28080 启动,支持 SSE 通信。
📝 API 端点
| 方法 | 端点 | 描述 |
|---|---|---|
GET | /test/v1/name | 获取当前存储的名称 |
POST | /test/v1/name | 设置新的名称 |
示例请求
获取名称
curl http://localhost:8080/test/v1/name设置名称
curl -X POST http://localhost:8080/test/v1/name \
-H "Content-Type: application/json" \
-d '{"name": "新名称"}'📂 项目结构
gin-for-mcp/
├── api/
│ └── test/
│ └── v1/
│ ├── test.proto # Protocol Buffers 定义
│ ├── test.pb.go # 生成的 Proto Go 代码
│ └── test_gin.pb.go # 生成的 Gin HTTP 处理器
├── main.go # 主程序入口
├── go.mod # Go 模块依赖
├── go.sum # 依赖校验文件
├── Makefile # 构建脚本
├── mcp.swagger.json # Swagger API 文档
└── README.md # 项目文档🔧 开发指南
修改 API
- 编辑
api/test/v1/test.proto文件 - 运行
make api重新生成代码 - 在
main.go中实现新的服务方法 - 重启服务
添加新服务
- 在
api/目录下创建新的 proto 文件 - 更新
Makefile以包含新的 proto 文件 - 实现服务接口
- 在
main.go中注册新服务
📖 相关资源
📄 许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。
Made with ❤️ using Gin and Go
🌍 English Version
📖 Introduction
A Gin-based HTTP service integrating Protocol Buffers and MCP (Model Context Protocol) with automatic Swagger API documentation generation. This project demonstrates how to quickly build a RESTful API service supporting the MCP protocol.
✨ Key Features
- 🎯 Gin Framework - High-performance Go web framework
- 📝 Protocol Buffers - Efficient serialization protocol
- 🔄 Code Generation - Auto-generate Gin HTTP handlers from Proto files
- 📚 Swagger Docs - Automatic OpenAPI specification generation
- 🔌 MCP Integration - Model Context Protocol service exposure
- ⚡ SSE Support - Server-Sent Events for real-time communication
🚀 Quick Start
- Generate API code and Swagger docs:
make api- Run the server:
go run .Server starts at http://localhost:8080
- Start MCP service (optional):
swagger-mcp --specUrl file://./mcp.swagger.json --sse -sseAddr=:28080MCP service starts at http://localhost:28080
- More details: swagger-mcp
📝 API Endpoints
| Method | Endpoint | Description |
|---|---|---|
GET | /test/v1/name | Get the current stored name |
POST | /test/v1/name | Set a new name |
📂 Project Structure
gin-for-mcp/
├── api/test/v1/ # Proto definitions and generated code
├── main.go # Main application entry
├── Makefile # Build automation
├── mcp.swagger.json # Swagger API documentation
└── README.md # Project documentation