Ugin MCP服务器
模型上下文协议(MCP)服务器 无 web框架。该服务器为AI助手(Claude、Gemini等)提供工具,帮助开发人员构建项目、生成代码,并在使用Ugin构建web应用程序时遵循最佳实践。
🌟 双模式支持
- MCP/stdio模式:适用于Claude Desktop和MCP兼容客户端
- HTTP REST API模式:适用于Google Gemini、web界面和任何HTTP客户端
特性
可用工具
- 脚手架工程 -使用适当的结构和样板代码创建一个新的Ugin项目
- create_route -为新的HTTP路由处理程序生成代码
- 创建中间件 -生成自定义中间件代码
- 创建模型 -使用可选的JSON/DB标签和CRUD方法生成模型结构
- generate_crud_handler -为资源生成完整的CRUD处理程序(列表、获取、创建、更新、删除)
- create_config -生成配置文件模板(环境、数据库、服务器)
- get_example -获取常见用例的示例代码
- get_best_pactics -获取各种开发主题的最佳实践
安装
先决条件
- 达到1.23或更高
- MCP兼容客户端(如Claude Desktop)
从源代码构建
git clone https://github.com/yakuter/ugin-mcp.git
cd ugin-mcp
go mod tidy
go build -o ugin-mcp用法
运行服务器
模式1:MCP/stdio(适用于克劳德桌面)
# Default mode - for MCP clients like Claude Desktop
./ugin-mcp
# Or explicitly specify stdio mode
./ugin-mcp --mode=stdio模式2:HTTP REST API(适用于Gemini和其他应用程序)
# HTTP mode on port 8080
./ugin-mcp --mode=http --port=8080
# Or use a different port
./ugin-mcp --mode=http --port=3000测试HTTP服务器:
curl http://localhost:8080/healthClaude桌面配置
将此添加到您的Claude Desktop配置文件中:
MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"ugin": {
"command": "/path/to/ugin-mcp",
"args": []
}
}
}替换 /path/to/ugin-mcp 使用构建二进制文件的实际路径。
Google Gemini的配置
- 启动HTTP服务器:
./ugin-mcp --mode=http --port=8080- 与Gemini函数调用一起使用:
看 双子座_USAGE.md 详细说明包括:
- Python集成示例 - Gemini的函数声明 - Web界面示例 - 完整的代码示例
快速示例:
import requests
# Call any tool via HTTP
response = requests.post('http://localhost:8080/api/generate_crud_handler',
json={
"resource_name": "User",
"route_prefix": "/api/users"
})
result = response.json()
print(result['data'])工具文档
脚手架工程
使用目录结构、配置文件和启动代码创建一个全新的Ugin项目。
输入:
{
"project_name": "my-api",
"module_path": "github.com/user/my-api",
"directory": "./projects"
}输出:
- 结构完整的项目目录
go.mod和main.go- 目录结构(处理程序/、中间件/、模型/、配置/、实用程序/)
- README.md和.gitignore
create_route
为新的HTTP路由生成处理程序代码。
输入:
{
"method": "POST",
"path": "/api/users",
"handler_name": "CreateUser",
"description": "Creates a new user"
}输出:
- 处理程序功能代码
- 路线注册码
创建中间件
生成自定义中间件代码。
输入:
{
"name": "RateLimiter",
"description": "Rate limiting middleware to prevent abuse"
}输出:
- 完整的中间件功能及使用示例
创建模型
生成具有可选标记和方法的模型结构代码。
输入:
{
"model_name": "User",
"fields": {
"id": "int",
"email": "string",
"created_at": "time.Time"
},
"with_json": true,
"with_db": true,
"with_methods": true
}输出:
- 带有请求标签的模型结构
- CRUD方法存根(如果需要)
get_example
检索常见模式的完整示例代码。
可用示例:
basic-server-基本Ugin服务器设置crud-api-完成CRUD API实施middleware-自定义中间件示例auth-具有注册/登录功能的身份验证系统websocket-WebSocket实现与聊天示例
输入:
{
"example_type": "crud-api"
}generate_crud_handler
为资源生成完整的CRUD(创建、读取、更新、删除)处理程序。
输入:
{
"resource_name": "Product",
"route_prefix": "/api/products",
"with_db": true
}输出:
- 包含所有CRUD操作的完整处理程序代码
- 路线注册码
- 模型示例结构
create_config
生成用于各种目的的配置文件模板。
可用配置类型:
env-环境变量模板(.env.example.)database-数据库配置代码server-服务器配置代码
输入:
{
"config_type": "database"
}输出:
- 配置代码
- 推荐文件名
get_best_pactics
提供各种开发主题的最佳实践。
可用主题:
structure-项目结构和组织error-handling-错误处理模式security-安全最佳实践validation-输入验证logging-记录最佳实践performance-性能优化testing-测试策略
输入:
{
"topic": "security"
}工作流示例
以下是AI助手如何使用这些工具来帮助开发人员:
- 创建新项目:
User: "Create a new API project called user-service"
AI uses: scaffold_project- 添加新端点:
User: "Add a POST endpoint for creating users"
AI uses: create_route- 创建模型:
User: "Create a User model with id, email, and password fields"
AI uses: create_model- 添加中间件:
User: "Add authentication middleware"
AI uses: create_middleware or get_example with "auth"- 获取指导:
User: "What are the security best practices?"
AI uses: get_best_practices with topic "security"发展
项目结构
ugin-mcp/
├── main.go # MCP server implementation
├── go.mod # Go module definition
├── README.md # This file
└── examples/ # Example configurations (optional)添加新工具
要向MCP服务器添加新工具,请执行以下操作:
- 定义输入和输出结构
- 使用签名实现工具功能:
func ToolName(ctx context.Context, req *mcp.CallToolRequest, input InputType) (
*mcp.CallToolResult,
OutputType,
error,
)- 在中注册该工具
main():
mcp.AddTool(
server,
&mcp.Tool{
Name: "tool_name",
Description: "Tool description",
},
ToolName,
)HTTP API终结点
在HTTP模式下运行时,以下端点可用:
| 方法 | 端点 | 描述 |
|---|---|---|
| 得到 | /health | 服务器健康检查 |
| 职位 | /api/scaffold_project | 创建新项目 |
| 职位 | /api/create_route | 生成路由处理程序 |
| 职位 | /api/create_middleware | 生成中间件 |
| 职位 | /api/create_model | 生成模型结构 |
| 职位 | /api/generate_crud_handler | 生成CRUD处理程序 |
| 职位 | /api/create_config | 生成配置文件 |
| 职位 | /api/get_example | 获取代码示例 |
| 职位 | /api/get_best_practices | 获取最佳实践 |
所有POST端点都接受并返回JSON。看 双子座_USAGE.md 例如。
关于乌金
无 是一个轻量级、高性能的Go web框架,灵感来自Gin。它提供:
- 快速HTTP路由
- 中间件支持
- JSON验证
- 错误管理
- 组路由
- 还有更多。..
许可证
MIT许可证
贡献
欢迎投稿!请随时提交拉取请求。
相关链接
- Ugin框架
- MCP Go SDK
- 模型上下文协议
- 谷歌双子座
- Gemini使用指南 -如何使用Google Gemini
