package tools import ( "encoding/json" "git.x2erp.com/qdy/go-svc-mcp/internal/mcp" ) func init() { mcp.Register("echo", "回显输入的消息,演示自动注册和依赖注入", map[string]interface{}{ "type": "object", "properties": map[string]interface{}{ "message": map[string]interface{}{ "type": "string", "description": "要回显的消息", }, "repeat": map[string]interface{}{ "type": "integer", "description": "重复次数", "default": 1, "minimum": 1, "maximum": 10, }, }, "required": []string{"message"}, }, func(input json.RawMessage, deps *mcp.ToolDependencies) (interface{}, error) { var params struct { Message string `json:"message"` Repeat int `json:"repeat"` } if len(input) > 0 { if err := json.Unmarshal(input, ¶ms); err != nil { return nil, err } } if params.Repeat == 0 { params.Repeat = 1 } // 使用依赖项(数据库工厂和上下文)记录日志或执行其他操作 if deps.DBFactory != nil { // 可以记录到数据库或执行查询 // 示例:记录工具调用 } // 构建回显结果 result := "" for i := 0; i < params.Repeat; i++ { if i > 0 { result += " " } result += params.Message } return map[string]interface{}{ "tenant_id": deps.ReqCtx.TenantID, "original": params.Message, "echo": result, "repeat": params.Repeat, "has_db": deps.DBFactory != nil, }, nil }, ) }