Commit a95d236
fix(mcp-proxy): add missing return after error handling in logger middleware (#2566)
* fix(mcp-proxy): add missing return after error handling in logger middleware
Why this change was needed:
When GetMCPServerByName returns an error, the code was calling
c.Abort() but missing a return statement. This caused the function
to continue executing, potentially leading to nil pointer dereference
when accessing the mcp variable in subsequent SetMCPServerID,
SetMCPServerName, and SetGatewayID calls.
What changed:
- Added return statement after c.Abort() in error handling path
- Prevents continuation of execution after sending error response
Problem solved:
Eliminates potential nil pointer panic when MCP server lookup fails.
The middleware now properly terminates execution after returning the
error response to the client.
Co-authored-by: Han-Ya-Jun <1581532052@qq.com>
Co-authored-by: claude <noreply@anthropic.com>
* fix(mcp-proxy): add body size truncation to prevent excessive logging
Why this change was needed:
The logger middleware had inconsistent truncation behavior for response
bodies. Error responses (hasError=true) were logged without truncation,
potentially causing:
1. Excessive log file growth when errors contain large payloads
2. Memory pressure during log processing
3. Service instability due to unbounded log writes
4. Possible service restart every 6 minutes due to resource exhaustion
What changed:
- Added MaxLogBodySize constant (2KB) for consistent truncation
- Removed conditional logging logic (hasError check)
- Always truncate response_body regardless of HTTP status
- Updated request body and params to use the same constant
- Simplified code by removing unnecessary branching
Problem solved:
1. All logged bodies are now consistently capped at 2KB
2. Prevents memory and disk issues from large error responses
3. Reduces log processing overhead
4. Should resolve the 6-minute restart cycle if caused by logging
Technical details:
- Request body: 2KB max (was 1KB)
- Request params: 2KB max (was 1KB)
- Response body: 2KB max (was unlimited for errors, 1KB for success)
Co-authored-by: Han-Ya-Jun <1581532052@qq.com>
Co-authored-by: claude <noreply@anthropic.com>
* fix(mcp-proxy): add truncation to audit logs for tool requests/responses
Why this change was needed:
Audit logs in proxy.go recorded full tool request and response payloads
without any size limits. When MCP tools return large responses (e.g.,
database queries, API responses with arrays), this caused:
1. Unbounded audit log growth
2. Memory pressure during log processing
3. Potential service instability and restarts
4. Disk I/O bottlenecks from massive log writes
What changed:
- Added MaxAuditLogSize constant (16KB) for audit log truncation
- Created truncateForAudit() helper function with type-safe handling
- Applied truncation to 3 audit log points:
* Tool request logging (request.RawArguments)
* Error logging (unmarshal failures)
* Tool response logging (submit result)
- Supports multiple types: string, []byte, json.RawMessage, any
Technical details:
truncateForAudit() handles:
- Direct string/byte truncation with "...[truncated]" suffix
- JSON marshaling for complex types
- Safe type assertion for zap.String() calls
- 16KB limit provides good balance of detail and performance
Problem solved:
- Audit logs are now bounded at 16KB per field
- Prevents memory exhaustion from large tool responses
- Maintains sufficient audit detail for debugging
- Complements the earlier 2KB limit in API logger middleware
Size rationale:
- 16KB captures most typical MCP tool responses
- Larger than API logs (2KB) to preserve debugging context
- Still prevents unbounded growth
- Balances observability vs. resource usage
Co-authored-by: Han-Ya-Jun <1581532052@qq.com>
Co-authored-by: claude <noreply@anthropic.com>
* refactor(mcp-proxy): address code review feedback on logging
Fixes issues raised in PR review comments:
1. Changed truncateForAudit() return type from interface{} to string
- Eliminates brittle type assertions
- Simplifies usage at call sites
- All branches already returned strings internally
2. Limited response body buffer size in bodyLogWriter
- Previously buffered unlimited response data in memory
- Now caps buffer at MaxLogBodySize (2KB) for logging
- Prevents memory exhaustion on large responses
- Still forwards full response to client unchanged
3. Removed redundant log.Printf() call
- Duplicate logging already handled by auditLog.Info()
- Reduces log noise and processing overhead
4. Simplified audit log field types
- Changed from zap.Any() to zap.String() for truncated values
- More explicit and type-safe
- Better performance (no reflection)
Technical details:
- bodyLogWriter.Write() now limits buffering while still writing full response
- truncateForAudit() signature change: interface{} -> string
- Removed type assertion: truncateForAudit(...).(string) -> truncateForAudit(...)
- Updated all 3 audit log call sites to use zap.String()
These changes maintain the same security/stability fixes while improving
code quality and addressing review feedback.
Co-authored-by: Han-Ya-Jun <1581532052@qq.com>
Co-authored-by: claude <noreply@anthropic.com>
---------
Co-authored-by: handryhan <handryhan@tencent.com>
Co-authored-by: claude <noreply@anthropic.com>1 parent ffa10f0 commit a95d236
2 files changed
Lines changed: 74 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
48 | 85 | | |
49 | 86 | | |
50 | 87 | | |
| |||
326 | 363 | | |
327 | 364 | | |
328 | 365 | | |
329 | | - | |
| 366 | + | |
330 | 367 | | |
331 | 368 | | |
332 | 369 | | |
333 | | - | |
334 | | - | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
335 | 373 | | |
336 | 374 | | |
337 | 375 | | |
| |||
461 | 499 | | |
462 | 500 | | |
463 | 501 | | |
464 | | - | |
465 | | - | |
| 502 | + | |
466 | 503 | | |
467 | 504 | | |
468 | 505 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
37 | 42 | | |
38 | 43 | | |
39 | | - | |
| 44 | + | |
| 45 | + | |
40 | 46 | | |
41 | 47 | | |
42 | 48 | | |
43 | | - | |
44 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
45 | 60 | | |
46 | 61 | | |
47 | 62 | | |
| |||
57 | 72 | | |
58 | 73 | | |
59 | 74 | | |
60 | | - | |
| 75 | + | |
61 | 76 | | |
62 | 77 | | |
63 | 78 | | |
64 | 79 | | |
65 | 80 | | |
66 | | - | |
| 81 | + | |
67 | 82 | | |
68 | 83 | | |
69 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
70 | 89 | | |
71 | 90 | | |
72 | 91 | | |
| |||
77 | 96 | | |
78 | 97 | | |
79 | 98 | | |
| 99 | + | |
80 | 100 | | |
81 | 101 | | |
82 | 102 | | |
| |||
94 | 114 | | |
95 | 115 | | |
96 | 116 | | |
97 | | - | |
98 | | - | |
99 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
100 | 122 | | |
101 | 123 | | |
102 | 124 | | |
| |||
110 | 132 | | |
111 | 133 | | |
112 | 134 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 135 | + | |
119 | 136 | | |
120 | 137 | | |
121 | 138 | | |
| |||
0 commit comments