@@ -3,6 +3,7 @@ package mcpgrafana
33import (
44 "context"
55 "encoding/json"
6+ "errors"
67 "fmt"
78 "reflect"
89
@@ -57,25 +58,25 @@ func ConvertTool[T any, R any](name, description string, toolHandler ToolHandler
5758 handlerValue := reflect .ValueOf (toolHandler )
5859 handlerType := handlerValue .Type ()
5960 if handlerType .Kind () != reflect .Func {
60- return zero , nil , fmt . Errorf ("tool handler must be a function" )
61+ return zero , nil , errors . New ("tool handler must be a function" )
6162 }
6263 if handlerType .NumIn () != 2 {
63- return zero , nil , fmt . Errorf ("tool handler must have 2 arguments" )
64+ return zero , nil , errors . New ("tool handler must have 2 arguments" )
6465 }
6566 if handlerType .NumOut () != 2 {
66- return zero , nil , fmt . Errorf ("tool handler must return 2 values" )
67+ return zero , nil , errors . New ("tool handler must return 2 values" )
6768 }
6869 if handlerType .In (0 ) != reflect .TypeOf ((* context .Context )(nil )).Elem () {
69- return zero , nil , fmt . Errorf ("tool handler first argument must be context.Context" )
70+ return zero , nil , errors . New ("tool handler first argument must be context.Context" )
7071 }
7172 // We no longer check the type of the first return value
7273 if handlerType .Out (1 ).Kind () != reflect .Interface {
73- return zero , nil , fmt . Errorf ("tool handler second return value must be error" )
74+ return zero , nil , errors . New ("tool handler second return value must be error" )
7475 }
7576
7677 argType := handlerType .In (1 )
7778 if argType .Kind () != reflect .Struct {
78- return zero , nil , fmt . Errorf ("tool handler second argument must be a struct" )
79+ return zero , nil , errors . New ("tool handler second argument must be a struct" )
7980 }
8081
8182 handler := func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
@@ -87,23 +88,23 @@ func ConvertTool[T any, R any](name, description string, toolHandler ToolHandler
8788
8889 unmarshaledArgs := reflect .New (argType ).Interface ()
8990 if err := json .Unmarshal ([]byte (s ), unmarshaledArgs ); err != nil {
90- return mcp . NewToolResultError ( fmt .Sprintf ("unmarshal args: %s" , err )), nil
91+ return nil , fmt .Errorf ("unmarshal args: %s" , err )
9192 }
9293
9394 // Need to dereference the unmarshaled arguments
9495 of := reflect .ValueOf (unmarshaledArgs )
9596 if of .Kind () != reflect .Ptr || ! of .Elem ().CanInterface () {
96- return mcp . NewToolResultError ("arguments must be a struct" ), nil
97+ return nil , errors . New ("arguments must be a struct" )
9798 }
9899
99100 args := []reflect.Value {reflect .ValueOf (ctx ), of .Elem ()}
100101
101102 output := handlerValue .Call (args )
102103 if len (output ) != 2 {
103- return mcp . NewToolResultError ("tool handler must return 2 values" ), nil
104+ return nil , errors . New ("tool handler must return 2 values" )
104105 }
105106 if ! output [0 ].CanInterface () {
106- return mcp . NewToolResultError ("tool handler first return value must be interfaceable" ), nil
107+ return nil , errors . New ("tool handler first return value must be interfaceable" )
107108 }
108109
109110 // Handle the error return value first
@@ -112,7 +113,7 @@ func ConvertTool[T any, R any](name, description string, toolHandler ToolHandler
112113 if output [1 ].Kind () == reflect .Interface && ! output [1 ].IsNil () {
113114 handlerErr , ok = output [1 ].Interface ().(error )
114115 if ! ok {
115- return mcp . NewToolResultError ("tool handler second return value must be error" ), nil
116+ return nil , errors . New ("tool handler second return value must be error" )
116117 }
117118 }
118119
@@ -122,13 +123,13 @@ func ConvertTool[T any, R any](name, description string, toolHandler ToolHandler
122123 }
123124
124125 // Check if the first return value is nil (only for pointer, interface, map, etc.)
125- isNilable := output [0 ].Kind () == reflect .Ptr ||
126- output [0 ].Kind () == reflect .Interface ||
127- output [0 ].Kind () == reflect .Map ||
128- output [0 ].Kind () == reflect .Slice ||
129- output [0 ].Kind () == reflect .Chan ||
126+ isNilable := output [0 ].Kind () == reflect .Ptr ||
127+ output [0 ].Kind () == reflect .Interface ||
128+ output [0 ].Kind () == reflect .Map ||
129+ output [0 ].Kind () == reflect .Slice ||
130+ output [0 ].Kind () == reflect .Chan ||
130131 output [0 ].Kind () == reflect .Func
131-
132+
132133 if isNilable && output [0 ].IsNil () {
133134 return nil , nil
134135 }
@@ -165,7 +166,7 @@ func ConvertTool[T any, R any](name, description string, toolHandler ToolHandler
165166 // Case 4: Any other type - marshal to JSON
166167 jsonBytes , err := json .Marshal (returnVal )
167168 if err != nil {
168- return mcp . NewToolResultError ( fmt .Sprintf ("failed to marshal return value: %s" , err )), nil
169+ return nil , fmt .Errorf ("failed to marshal return value: %s" , err )
169170 }
170171
171172 return mcp .NewToolResultText (string (jsonBytes )), nil
0 commit comments