@@ -35,36 +35,34 @@ func appendTracingHooks(hooks *server.Hooks) {
3535 hooks .AddOnError (tracingHooks .onError )
3636}
3737
38- func NewToolHandlerMiddleware () server.ToolHandlerMiddleware {
39- return func (next server.ToolHandlerFunc ) server.ToolHandlerFunc {
40- return func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
41- toolSpan , ctx := llmobs .StartToolSpan (ctx , request .Params .Name , llmobs .WithIntegration (string (instrumentation .PackageMark3LabsMCPGo )))
38+ var toolHandlerMiddleware = func (next server.ToolHandlerFunc ) server.ToolHandlerFunc {
39+ return func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
40+ toolSpan , ctx := llmobs .StartToolSpan (ctx , request .Params .Name , llmobs .WithIntegration (string (instrumentation .PackageMark3LabsMCPGo )))
4241
43- result , err := next (ctx , request )
42+ result , err := next (ctx , request )
4443
45- inputJSON , marshalErr := json .Marshal (request )
44+ inputJSON , marshalErr := json .Marshal (request )
45+ if marshalErr != nil {
46+ instr .Logger ().Warn ("mcp-go: failed to marshal tool request: %v" , marshalErr )
47+ }
48+ var outputText string
49+ if result != nil {
50+ resultJSON , marshalErr := json .Marshal (result )
4651 if marshalErr != nil {
47- instr .Logger ().Warn ("mcp-go: failed to marshal tool request: %v" , marshalErr )
48- }
49- var outputText string
50- if result != nil {
51- resultJSON , marshalErr := json .Marshal (result )
52- if marshalErr != nil {
53- instr .Logger ().Warn ("mcp-go: failed to marshal tool result: %v" , marshalErr )
54- }
55- outputText = string (resultJSON )
52+ instr .Logger ().Warn ("mcp-go: failed to marshal tool result: %v" , marshalErr )
5653 }
54+ outputText = string (resultJSON )
55+ }
5756
58- toolSpan .AnnotateTextIO (string (inputJSON ), outputText )
59-
60- if err != nil {
61- toolSpan .Finish (llmobs .WithError (err ))
62- } else {
63- toolSpan .Finish ()
64- }
57+ toolSpan .AnnotateTextIO (string (inputJSON ), outputText )
6558
66- return result , err
59+ if err != nil {
60+ toolSpan .Finish (llmobs .WithError (err ))
61+ } else {
62+ toolSpan .Finish ()
6763 }
64+
65+ return result , err
6866 }
6967}
7068
0 commit comments