Skip to content

Commit 58df1a2

Browse files
committed
Make middleware factory a private constant
1 parent 0eadbef commit 58df1a2

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

contrib/mark3labs/mcp-go/option.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ func WithTracing(options *TracingConfig) server.ServerOption {
3434

3535
server.WithHooks(hooks)(s)
3636

37-
server.WithToolHandlerMiddleware(NewToolHandlerMiddleware())(s)
37+
server.WithToolHandlerMiddleware(toolHandlerMiddleware)(s)
3838
}
3939
}

contrib/mark3labs/mcp-go/tracing.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

contrib/mark3labs/mcp-go/tracing_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
"github.com/stretchr/testify/require"
2222
)
2323

24-
func TestNewToolHandlerMiddleware(t *testing.T) {
24+
func TestToolHandlerMiddleware(t *testing.T) {
2525
mt := mocktracer.Start()
2626
defer mt.Stop()
2727

28-
middleware := NewToolHandlerMiddleware()
28+
middleware := toolHandlerMiddleware
2929
assert.NotNil(t, middleware)
3030
}
3131

0 commit comments

Comments
 (0)