feat(logging): enhance logging for metadata extraction and MCP tool handling
This commit is contained in:
@@ -3,7 +3,9 @@ package mcpserver
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/google/uuid"
|
||||
@@ -19,11 +21,39 @@ var toolSchemaOptions = &jsonschema.ForOptions{
|
||||
},
|
||||
}
|
||||
|
||||
func addTool[In any, Out any](server *mcp.Server, tool *mcp.Tool, handler func(context.Context, *mcp.CallToolRequest, In) (*mcp.CallToolResult, Out, error)) {
|
||||
func addTool[In any, Out any](server *mcp.Server, logger *slog.Logger, tool *mcp.Tool, handler func(context.Context, *mcp.CallToolRequest, In) (*mcp.CallToolResult, Out, error)) {
|
||||
if err := setToolSchemas[In, Out](tool); err != nil {
|
||||
panic(fmt.Sprintf("configure MCP tool %q schemas: %v", tool.Name, err))
|
||||
}
|
||||
mcp.AddTool(server, tool, handler)
|
||||
mcp.AddTool(server, tool, logToolCall(logger, tool.Name, handler))
|
||||
}
|
||||
|
||||
func logToolCall[In any, Out any](logger *slog.Logger, toolName string, handler func(context.Context, *mcp.CallToolRequest, In) (*mcp.CallToolResult, Out, error)) func(context.Context, *mcp.CallToolRequest, In) (*mcp.CallToolResult, Out, error) {
|
||||
if logger == nil {
|
||||
return handler
|
||||
}
|
||||
|
||||
return func(ctx context.Context, req *mcp.CallToolRequest, in In) (*mcp.CallToolResult, Out, error) {
|
||||
start := time.Now()
|
||||
attrs := []any{slog.String("tool", toolName)}
|
||||
if req != nil && req.Params != nil {
|
||||
attrs = append(attrs, slog.Any("arguments", req.Params.Arguments))
|
||||
}
|
||||
|
||||
logger.Info("mcp tool started", attrs...)
|
||||
result, out, err := handler(ctx, req, in)
|
||||
|
||||
completionAttrs := append([]any{}, attrs...)
|
||||
completionAttrs = append(completionAttrs, slog.Duration("duration", time.Since(start)))
|
||||
if err != nil {
|
||||
completionAttrs = append(completionAttrs, slog.String("error", err.Error()))
|
||||
logger.Error("mcp tool completed", completionAttrs...)
|
||||
return result, out, err
|
||||
}
|
||||
|
||||
logger.Info("mcp tool completed", completionAttrs...)
|
||||
return result, out, nil
|
||||
}
|
||||
}
|
||||
|
||||
func setToolSchemas[In any, Out any](tool *mcp.Tool) error {
|
||||
|
||||
Reference in New Issue
Block a user