tool_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package kit
  2. import (
  3. "context"
  4. "encoding/json"
  5. "testing"
  6. "github.com/mark3labs/mcp-go/mcp"
  7. )
  8. type patchArgs struct {
  9. Kind string `json:"kind" jsonschema:"类型"`
  10. IDs []int `json:"ids" jsonschema:"编号"`
  11. }
  12. func TestDefProperties(t *testing.T) {
  13. d := Def[patchArgs]{
  14. Name: "patch_test", Title: "补充 schema", Description: "测试", ReadOnly: true,
  15. Handler: func(context.Context, patchArgs) (*mcp.CallToolResult, error) { return nil, nil },
  16. Properties: map[string]map[string]any{
  17. "kind": {"enum": []string{"pid", "vid"}},
  18. "ids": {"type": "array", "minItems": 1, "items": map[string]any{"type": "integer", "minimum": 0}},
  19. },
  20. }
  21. var schema struct {
  22. Properties map[string]map[string]any `json:"properties"`
  23. Required []string `json:"required"`
  24. }
  25. if err := json.Unmarshal(d.Tool().RawInputSchema, &schema); err != nil {
  26. t.Fatal(err)
  27. }
  28. ids := schema.Properties["ids"]
  29. if ids["type"] != "array" {
  30. t.Errorf("ids.type 应为 array(不再可空),实际 %v", ids["type"])
  31. }
  32. if ids["description"] != "编号" {
  33. t.Errorf("补充约束不应覆盖生成的描述,实际 %v", ids["description"])
  34. }
  35. if items, _ := ids["items"].(map[string]any); items["minimum"] != float64(0) {
  36. t.Errorf("ids.items.minimum 未生效:%v", ids["items"])
  37. }
  38. if enum, _ := schema.Properties["kind"]["enum"].([]any); len(enum) != 2 {
  39. t.Errorf("kind.enum 未生效:%v", schema.Properties["kind"])
  40. }
  41. if len(schema.Required) != 2 {
  42. t.Errorf("required 应保持不变,实际 %v", schema.Required)
  43. }
  44. }
  45. func TestDefPropertiesUnknownField(t *testing.T) {
  46. defer func() {
  47. if recover() == nil {
  48. t.Error("补充不存在的字段应 panic")
  49. }
  50. }()
  51. Def[patchArgs]{Name: "patch_bad", Properties: map[string]map[string]any{"nope": {"type": "string"}}}.Tool()
  52. }