tools.tpl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package mcpTool
  2. import (
  3. "context"
  4. "github.com/mark3labs/mcp-go/mcp"
  5. )
  6. func init() {
  7. RegisterTool(&{{.Name | title}}{})
  8. }
  9. type {{.Name | title}} struct {
  10. }
  11. // {{.Description}}
  12. func (t *{{.Name | title}}) Handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
  13. // TODO: 实现工具逻辑
  14. // 参数示例:
  15. // {{- range .Params}}
  16. // {{.Name}} := request.GetArguments()["{{.Name}}"]
  17. // {{- end}}
  18. return &mcp.CallToolResult{
  19. Content: []mcp.Content{
  20. {{- range .Response}}
  21. mcp.{{.Type | title}}Content{
  22. Type: "{{.Type}}",
  23. // TODO: 填充{{.Type}}内容
  24. },
  25. {{- end}}
  26. },
  27. }, nil
  28. }
  29. func (t *{{.Name | title}}) New() mcp.Tool {
  30. return mcp.NewTool("{{.Name}}",
  31. mcp.WithDescription("{{.Description}}"),
  32. {{- range .Params}}
  33. mcp.With{{.Type | title}}("{{.Name}}",
  34. {{- if .Required}}mcp.Required(),{{end}}
  35. mcp.Description("{{.Description}}"),
  36. {{- if .Default}}
  37. {{- if eq .Type "string"}}
  38. mcp.DefaultString("{{.Default}}"),
  39. {{- else if eq .Type "number"}}
  40. mcp.DefaultNumber({{.Default}}),
  41. {{- else if eq .Type "boolean"}}
  42. mcp.DefaultBoolean({{if or (eq .Default "true") (eq .Default "True")}}true{{else}}false{{end}}),
  43. {{- else if eq .Type "array"}}
  44. // 注意:数组默认值需要在后端代码中预处理为正确的格式
  45. // mcp.DefaultArray({{.Default}}),
  46. {{- end}}
  47. {{- end}}
  48. ),
  49. {{- end}}
  50. )
  51. }