Go MCP Server 项目初始化:工具注册架构 + 编译运行通过
- Tool 接口 + Registry 注册中心,工具模块化插拔 - system 工具:health / info - database 工具:db_query / db_tables / db_table_info / db_explain(四层 SQL 安全) - docker 工具:docker_ps / docker_logs / docker_inspect(白名单 + 大小限制) - middleware:auth(JWT+APIKey)/ ratelimit(令牌桶)/ audit(slog 结构化) - stdio / SSE 双传输模式,Viper 多环境配置 - Dockerfile 多阶段构建(golang:alpine → scratch),~15MB 镜像 - docker-compose.yml 一键部署 + 安全加固(read_only / no-new-privileges / cap_drop) - go build ./... / go vet ./... / go test ./... 全部通过 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
)
|
||||
|
||||
// getArgs 把 req.Params.Arguments 转换为 map[string]any。
|
||||
func getArgs(req mcp.CallToolRequest) map[string]any {
|
||||
if args, ok := req.Params.Arguments.(map[string]any); ok {
|
||||
return args
|
||||
}
|
||||
return map[string]any{}
|
||||
}
|
||||
|
||||
// Tool 是所有 MCP 工具必须实现的接口。
|
||||
// 每个工具模块实现此接口,然后注册到 Registry。
|
||||
type Tool interface {
|
||||
Name() string
|
||||
|
||||
// Description 返回工具的一句话描述
|
||||
Description() string
|
||||
|
||||
// Register 向 MCP Server 注册自己的工具处理器
|
||||
Register(mcpServer *server.MCPServer) error
|
||||
|
||||
// Initialize 建立连接、初始化资源
|
||||
Initialize(ctx context.Context) error
|
||||
|
||||
// Shutdown 释放资源、关闭连接
|
||||
Shutdown(ctx context.Context) error
|
||||
|
||||
// HealthCheck 返回当前工具的连接状态,nil 表示健康
|
||||
HealthCheck(ctx context.Context) error
|
||||
}
|
||||
Reference in New Issue
Block a user