-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
49 lines (41 loc) · 1.01 KB
/
middleware.go
File metadata and controls
49 lines (41 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"context"
"log"
"time"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
// createLoggingMiddleware creates an MCP middleware that logs method calls.
func createLoggingMiddleware() mcp.Middleware {
return func(next mcp.MethodHandler) mcp.MethodHandler {
return func(
ctx context.Context,
method string,
req mcp.Request,
) (mcp.Result, error) {
start := time.Now()
sessionID := req.GetSession().ID()
// Log request details.
log.Printf("[REQUEST] Session: %s | Method: %s",
sessionID,
method)
// Call the actual handler.
result, err := next(ctx, method, req)
// Log response details.
duration := time.Since(start)
if err != nil {
log.Printf("[RESPONSE] Session: %s | Method: %s | Status: ERROR | Duration: %v | Error: %v",
sessionID,
method,
duration,
err)
} else {
log.Printf("[RESPONSE] Session: %s | Method: %s | Status: OK | Duration: %v",
sessionID,
method,
duration)
}
return result, err
}
}
}