暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

echo.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package tools
  2. import (
  3. "encoding/json"
  4. "git.x2erp.com/qdy/go-svc-mcp/internal/mcp"
  5. )
  6. func init() {
  7. mcp.Register("echo", "回显输入的消息,演示自动注册和依赖注入",
  8. map[string]interface{}{
  9. "type": "object",
  10. "properties": map[string]interface{}{
  11. "message": map[string]interface{}{
  12. "type": "string",
  13. "description": "要回显的消息",
  14. },
  15. "repeat": map[string]interface{}{
  16. "type": "integer",
  17. "description": "重复次数",
  18. "default": 1,
  19. "minimum": 1,
  20. "maximum": 10,
  21. },
  22. },
  23. "required": []string{"message"},
  24. },
  25. func(input json.RawMessage, deps *mcp.ToolDependencies) (interface{}, error) {
  26. var params struct {
  27. Message string `json:"message"`
  28. Repeat int `json:"repeat"`
  29. }
  30. if len(input) > 0 {
  31. if err := json.Unmarshal(input, &params); err != nil {
  32. return nil, err
  33. }
  34. }
  35. if params.Repeat == 0 {
  36. params.Repeat = 1
  37. }
  38. // 使用依赖项(数据库工厂和上下文)记录日志或执行其他操作
  39. if deps.DBFactory != nil {
  40. // 可以记录到数据库或执行查询
  41. // 示例:记录工具调用
  42. }
  43. // 构建回显结果
  44. result := ""
  45. for i := 0; i < params.Repeat; i++ {
  46. if i > 0 {
  47. result += " "
  48. }
  49. result += params.Message
  50. }
  51. return map[string]interface{}{
  52. "tenant_id": deps.ReqCtx.TenantID,
  53. "original": params.Message,
  54. "echo": result,
  55. "repeat": params.Repeat,
  56. "has_db": deps.DBFactory != nil,
  57. }, nil
  58. },
  59. )
  60. }