|
| 1 | +package mcpgrafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "reflect" |
| 8 | + |
| 9 | + "github.com/invopop/jsonschema" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +func MustTool(name, description string, toolHandler any) (mcp.Tool, server.ToolHandlerFunc) { |
| 15 | + tool, handler, err := ConvertTool(name, description, toolHandler) |
| 16 | + if err != nil { |
| 17 | + panic(err) |
| 18 | + } |
| 19 | + return tool, handler |
| 20 | +} |
| 21 | + |
| 22 | +func ConvertTool(name, description string, toolHandler any) (mcp.Tool, server.ToolHandlerFunc, error) { |
| 23 | + zero := mcp.Tool{} |
| 24 | + handlerValue := reflect.ValueOf(toolHandler) |
| 25 | + handlerType := handlerValue.Type() |
| 26 | + if handlerType.Kind() != reflect.Func { |
| 27 | + return zero, nil, fmt.Errorf("tool handler must be a function") |
| 28 | + } |
| 29 | + if handlerType.NumIn() != 2 { |
| 30 | + return zero, nil, fmt.Errorf("tool handler must have 2 arguments") |
| 31 | + } |
| 32 | + if handlerType.NumOut() != 2 { |
| 33 | + return zero, nil, fmt.Errorf("tool handler must return 2 values") |
| 34 | + } |
| 35 | + if handlerType.In(0) != reflect.TypeOf((*context.Context)(nil)).Elem() { |
| 36 | + return zero, nil, fmt.Errorf("tool handler first argument must be context.Context") |
| 37 | + } |
| 38 | + if handlerType.Out(0) != reflect.TypeOf(&mcp.CallToolResult{}) { |
| 39 | + return zero, nil, fmt.Errorf("tool handler first return value must be mcp.CallToolResult") |
| 40 | + } |
| 41 | + if handlerType.Out(1).Kind() != reflect.Interface { |
| 42 | + return zero, nil, fmt.Errorf("tool handler second return value must be error") |
| 43 | + } |
| 44 | + |
| 45 | + argType := handlerType.In(1) |
| 46 | + if argType.Kind() != reflect.Struct { |
| 47 | + return zero, nil, fmt.Errorf("tool handler second argument must be a struct") |
| 48 | + } |
| 49 | + |
| 50 | + handler := func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 51 | + |
| 52 | + s, err := json.Marshal(request.Params.Arguments) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("marshal args: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + unmarshaledArgs := reflect.New(argType).Interface() |
| 58 | + if err := json.Unmarshal([]byte(s), unmarshaledArgs); err != nil { |
| 59 | + return mcp.NewToolResultError(fmt.Sprintf("unmarshal args: %s", err)), nil |
| 60 | + } |
| 61 | + |
| 62 | + // Need to dereference the unmarshaled arguments |
| 63 | + of := reflect.ValueOf(unmarshaledArgs) |
| 64 | + if of.Kind() != reflect.Ptr || !of.Elem().CanInterface() { |
| 65 | + return mcp.NewToolResultError("arguments must be a struct"), nil |
| 66 | + } |
| 67 | + |
| 68 | + args := []reflect.Value{reflect.ValueOf(ctx), of.Elem()} |
| 69 | + |
| 70 | + output := handlerValue.Call(args) |
| 71 | + if len(output) != 2 { |
| 72 | + return mcp.NewToolResultError("tool handler must return 2 values"), nil |
| 73 | + } |
| 74 | + if !output[0].CanInterface() { |
| 75 | + return mcp.NewToolResultError("tool handler first return value must be mcp.CallToolResult"), nil |
| 76 | + } |
| 77 | + var result *mcp.CallToolResult |
| 78 | + var ok bool |
| 79 | + if !output[0].IsNil() { |
| 80 | + result, ok = output[0].Interface().(*mcp.CallToolResult) |
| 81 | + if !ok { |
| 82 | + return mcp.NewToolResultError("tool handler first return value must be mcp.CallToolResult"), nil |
| 83 | + } |
| 84 | + } |
| 85 | + var handlerErr error |
| 86 | + if !output[1].IsNil() { |
| 87 | + handlerErr, ok = output[1].Interface().(error) |
| 88 | + if !ok { |
| 89 | + return mcp.NewToolResultError("tool handler second return value must be error"), nil |
| 90 | + } |
| 91 | + } |
| 92 | + return result, handlerErr |
| 93 | + } |
| 94 | + |
| 95 | + jsonSchema := createJsonSchemaFromHandler(toolHandler) |
| 96 | + properties := make(map[string]any, jsonSchema.Properties.Len()) |
| 97 | + for pair := jsonSchema.Properties.Oldest(); pair != nil; pair = pair.Next() { |
| 98 | + properties[pair.Key] = pair.Value |
| 99 | + } |
| 100 | + inputSchema := mcp.ToolInputSchema{ |
| 101 | + Type: jsonSchema.Type, |
| 102 | + Properties: properties, |
| 103 | + Required: jsonSchema.Required, |
| 104 | + } |
| 105 | + |
| 106 | + return mcp.Tool{ |
| 107 | + Name: name, |
| 108 | + Description: description, |
| 109 | + InputSchema: inputSchema, |
| 110 | + }, handler, nil |
| 111 | +} |
| 112 | + |
| 113 | +// Creates a full JSON schema from a user provided handler by introspecting the arguments |
| 114 | +func createJsonSchemaFromHandler(handler any) *jsonschema.Schema { |
| 115 | + handlerValue := reflect.ValueOf(handler) |
| 116 | + handlerType := handlerValue.Type() |
| 117 | + argumentType := handlerType.In(1) |
| 118 | + inputSchema := jsonSchemaReflector.ReflectFromType(argumentType) |
| 119 | + fmt.Println(inputSchema.Properties) |
| 120 | + return inputSchema |
| 121 | +} |
| 122 | + |
| 123 | +var ( |
| 124 | + jsonSchemaReflector = jsonschema.Reflector{ |
| 125 | + BaseSchemaID: "", |
| 126 | + Anonymous: true, |
| 127 | + AssignAnchor: false, |
| 128 | + AllowAdditionalProperties: true, |
| 129 | + RequiredFromJSONSchemaTags: true, |
| 130 | + DoNotReference: true, |
| 131 | + ExpandedStruct: true, |
| 132 | + FieldNameTag: "", |
| 133 | + IgnoredTypes: nil, |
| 134 | + Lookup: nil, |
| 135 | + Mapper: nil, |
| 136 | + Namer: nil, |
| 137 | + KeyNamer: nil, |
| 138 | + AdditionalFields: nil, |
| 139 | + CommentMap: nil, |
| 140 | + } |
| 141 | +) |
0 commit comments