@@ -39,36 +39,34 @@ func appendTracingHooks(hooks *server.Hooks) {
3939 hooks .AddOnError (tracingHooks .onError )
4040}
4141
42- func NewToolHandlerMiddleware () server.ToolHandlerMiddleware {
43- return func (next server.ToolHandlerFunc ) server.ToolHandlerFunc {
44- return func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
45- toolSpan , ctx := llmobs .StartToolSpan (ctx , request .Params .Name , llmobs .WithIntegration (string (instrumentation .PackageMark3LabsMCPGo )))
42+ var toolHandlerMiddleware = func (next server.ToolHandlerFunc ) server.ToolHandlerFunc {
43+ return func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
44+ toolSpan , ctx := llmobs .StartToolSpan (ctx , request .Params .Name , llmobs .WithIntegration (string (instrumentation .PackageMark3LabsMCPGo )))
4645
47- result , err := next (ctx , request )
46+ result , err := next (ctx , request )
4847
49- inputJSON , marshalErr := json .Marshal (request )
48+ inputJSON , marshalErr := json .Marshal (request )
49+ if marshalErr != nil {
50+ instr .Logger ().Warn ("mcp-go: failed to marshal tool request: %v" , marshalErr )
51+ }
52+ var outputText string
53+ if result != nil {
54+ resultJSON , marshalErr := json .Marshal (result )
5055 if marshalErr != nil {
51- instr .Logger ().Warn ("mcp-go: failed to marshal tool request: %v" , marshalErr )
52- }
53- var outputText string
54- if result != nil {
55- resultJSON , marshalErr := json .Marshal (result )
56- if marshalErr != nil {
57- instr .Logger ().Warn ("mcp-go: failed to marshal tool result: %v" , marshalErr )
58- }
59- outputText = string (resultJSON )
56+ instr .Logger ().Warn ("mcp-go: failed to marshal tool result: %v" , marshalErr )
6057 }
58+ outputText = string (resultJSON )
59+ }
6160
62- toolSpan .AnnotateTextIO (string (inputJSON ), outputText )
63-
64- if err != nil {
65- toolSpan .Finish (llmobs .WithError (err ))
66- } else {
67- toolSpan .Finish ()
68- }
61+ toolSpan .AnnotateTextIO (string (inputJSON ), outputText )
6962
70- return result , err
63+ if err != nil {
64+ toolSpan .Finish (llmobs .WithError (err ))
65+ } else {
66+ toolSpan .Finish ()
7167 }
68+
69+ return result , err
7270 }
7371}
7472
0 commit comments