|
6 | 6 | "flag" |
7 | 7 | "fmt" |
8 | 8 | "log" |
| 9 | + "os" |
9 | 10 | "strconv" |
10 | 11 | "strings" |
11 | 12 | "time" |
@@ -34,31 +35,33 @@ const ( |
34 | 35 | func NewMCPServer() *server.MCPServer { |
35 | 36 | hooks := &server.Hooks{} |
36 | 37 |
|
| 38 | + // All hook output goes to stderr. In stdio mode, stdout is the JSON-RPC |
| 39 | + // transport; writing to stdout from hooks corrupts the protocol stream. |
37 | 40 | hooks.AddBeforeAny(func(ctx context.Context, id any, method mcp.MCPMethod, message any) { |
38 | | - fmt.Printf("beforeAny: %s, %v, %v\n", method, id, message) |
| 41 | + fmt.Fprintf(os.Stderr, "beforeAny: %s, %v, %v\n", method, id, message) |
39 | 42 | }) |
40 | 43 | hooks.AddOnSuccess(func(ctx context.Context, id any, method mcp.MCPMethod, message any, result any) { |
41 | | - fmt.Printf("onSuccess: %s, %v, %v, %v\n", method, id, message, result) |
| 44 | + fmt.Fprintf(os.Stderr, "onSuccess: %s, %v, %v, %v\n", method, id, message, result) |
42 | 45 | }) |
43 | 46 | hooks.AddOnError(func(ctx context.Context, id any, method mcp.MCPMethod, message any, err error) { |
44 | | - fmt.Printf("onError: %s, %v, %v, %v\n", method, id, message, err) |
| 47 | + fmt.Fprintf(os.Stderr, "onError: %s, %v, %v, %v\n", method, id, message, err) |
45 | 48 | }) |
46 | 49 | hooks.AddBeforeInitialize(func(ctx context.Context, id any, message *mcp.InitializeRequest) { |
47 | | - fmt.Printf("beforeInitialize: %v, %v\n", id, message) |
| 50 | + fmt.Fprintf(os.Stderr, "beforeInitialize: %v, %v\n", id, message) |
48 | 51 | }) |
49 | 52 | hooks.AddOnRequestInitialization(func(ctx context.Context, id any, message any) error { |
50 | | - fmt.Printf("AddOnRequestInitialization: %v, %v\n", id, message) |
| 53 | + fmt.Fprintf(os.Stderr, "AddOnRequestInitialization: %v, %v\n", id, message) |
51 | 54 | // authorization verification and other preprocessing tasks are performed. |
52 | 55 | return nil |
53 | 56 | }) |
54 | 57 | hooks.AddAfterInitialize(func(ctx context.Context, id any, message *mcp.InitializeRequest, result *mcp.InitializeResult) { |
55 | | - fmt.Printf("afterInitialize: %v, %v, %v\n", id, message, result) |
| 58 | + fmt.Fprintf(os.Stderr, "afterInitialize: %v, %v, %v\n", id, message, result) |
56 | 59 | }) |
57 | 60 | hooks.AddAfterCallTool(func(ctx context.Context, id any, message *mcp.CallToolRequest, result any) { |
58 | | - fmt.Printf("afterCallTool: %v, %v, %v\n", id, message, result) |
| 61 | + fmt.Fprintf(os.Stderr, "afterCallTool: %v, %v, %v\n", id, message, result) |
59 | 62 | }) |
60 | 63 | hooks.AddBeforeCallTool(func(ctx context.Context, id any, message *mcp.CallToolRequest) { |
61 | | - fmt.Printf("beforeCallTool: %v, %v\n", id, message) |
| 64 | + fmt.Fprintf(os.Stderr, "beforeCallTool: %v, %v\n", id, message) |
62 | 65 | }) |
63 | 66 |
|
64 | 67 | mcpServer := server.NewMCPServer( |
|
0 commit comments