-
Notifications
You must be signed in to change notification settings - Fork 24
Fix agent infinite loop by increasing payload threshold to 512KB and adding path prefix configuration #1441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
37306d7
784f98e
275e2ad
e93a262
0203bba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # Fix for GitHub Issue #18295: MCP Tool Calling Loop Issues | ||
|
|
||
| ## Problem Statement | ||
|
|
||
| Agents were getting stuck in infinite loops when calling MCP tools like `github-list_commits`. The agent would repeatedly make the same tool call, receive a response indicating the payload was too large, attempt to read the payload file, fail (FILE_NOT_FOUND), and retry - creating an infinite loop that would only stop when the workflow timed out. | ||
|
|
||
| ## Root Cause Analysis | ||
|
|
||
| The issue was caused by the interaction between three factors: | ||
|
|
||
| 1. **Low Default Threshold**: The default payload size threshold was set to 10KB (10,240 bytes) | ||
| 2. **Typical GitHub API Response Sizes**: GitHub API responses (especially `list_commits` over 3 days) frequently exceed 10KB: | ||
| - Small query (1-5 commits): ~2-5KB | ||
| - Medium query (10-30 commits): **10-50KB** ← Often exceeds threshold | ||
| - Large query (100+ commits): 100KB-1MB | ||
|
|
||
| 3. **Inaccessible Payload Path**: When a response exceeded 10KB: | ||
| - The middleware would save the payload to disk at an **absolute host path**: `/tmp/jq-payloads/{sessionID}/{queryID}/payload.json` | ||
| - The middleware would return metadata with `payloadPath` pointing to this host path | ||
| - The agent would try to read this path, but it **doesn't exist in the agent's container filesystem** | ||
| - The agent would see `FILE_NOT_FOUND` and retry the tool call | ||
| - This created an infinite loop | ||
|
|
||
| ## Example from Issue #18295 | ||
|
|
||
| From the user's logs: | ||
| ``` | ||
| github-list_commits | ||
| └ {"agentInstructions":"The payload was too large for an MCP response. The comp... | ||
|
|
||
| ✗ bash: cat /tmp/gh-aw/mcp-payloads/***/a47e03f1b3561c858a06b84d5e02eb38/payload.json 2>/dev/null || echo "FILE_NOT_FOUND" | ||
| "description": Required | ||
| ``` | ||
|
|
||
| The agent received: | ||
| - `agentInstructions`: "The payload was too large..." | ||
| - `payloadPath`: `/tmp/jq-payloads/{session}/{query}/payload.json` | ||
|
|
||
| Then tried to read the file, got `FILE_NOT_FOUND`, and retried the tool call. | ||
|
|
||
| ## Solution | ||
|
|
||
| **Increase the default payload size threshold from 10KB to 512KB (524,288 bytes)** | ||
|
|
||
| This ensures that: | ||
| 1. Typical GitHub API responses (10-50KB) are returned **inline** without disk storage | ||
| 2. Only truly large responses (>512KB) trigger the payload-to-disk mechanism | ||
| 3. Agents don't encounter the inaccessible file path issue for normal operations | ||
| 4. The threshold can still be overridden via: | ||
| - Environment variable: `MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD=<bytes>` | ||
| - Command-line flag: `--payload-size-threshold <bytes>` | ||
| - Config file: `payload_size_threshold = <bytes>` | ||
|
|
||
| ## Changes Made | ||
|
|
||
| ### Code Changes | ||
|
|
||
| 1. **internal/config/config_payload.go** | ||
| - Changed `DefaultPayloadSizeThreshold` from `10240` to `524288` | ||
| - Updated comment to explain rationale | ||
|
|
||
| 2. **internal/cmd/flags_logging.go** | ||
| - Changed `defaultPayloadSizeThreshold` from `10240` to `524288` | ||
| - Updated comment | ||
|
|
||
| 3. **internal/config/config_core.go** | ||
| - Updated comment from "10KB" to "512KB" | ||
|
|
||
| 4. **internal/cmd/flags_logging_test.go** | ||
| - Updated test assertion from `10240` to `524288` | ||
|
|
||
| ### Documentation Changes | ||
|
|
||
| 1. **README.md** | ||
| - Updated CLI flag default from `10240` to `524288` | ||
| - Updated environment variable table default from `10240` to `524288` | ||
| - Updated configuration alternative default from `10240` to `524288` | ||
|
|
||
| 2. **config.example-payload-threshold.toml** | ||
| - Updated default from `10240` to `524288` | ||
| - Updated examples to use larger, more realistic values: | ||
| - 256KB (more aggressive storage) | ||
| - 512KB (default) | ||
| - 1MB (minimize disk storage) | ||
|
|
||
| ## Testing | ||
|
|
||
| All tests pass with the new default: | ||
| - Unit tests: ✅ PASS (all packages) | ||
| - Integration tests: ✅ PASS | ||
| - Configuration tests: ✅ PASS | ||
|
|
||
| ## Impact | ||
|
|
||
| ### Before Fix (10KB threshold) | ||
| - GitHub `list_commits` responses frequently exceeded threshold | ||
| - Agents got stuck in infinite loops trying to read inaccessible files | ||
| - Workflows would timeout after repeatedly calling the same tool | ||
| - Poor user experience | ||
|
|
||
| ### After Fix (512KB threshold) | ||
| - GitHub `list_commits` responses are returned inline (typical size 10-50KB) | ||
| - Agents receive complete data without file system access issues | ||
| - No more infinite loops for typical use cases | ||
| - Greatly improved user experience | ||
|
|
||
| ### Performance Considerations | ||
|
|
||
| **Memory Impact**: Minimal. Most responses are still well under 512KB. | ||
|
|
||
| **Network Impact**: Reduced. Returning data inline is faster than writing to disk and returning metadata. | ||
|
|
||
| **Disk I/O Impact**: Significantly reduced. Fewer responses trigger disk storage. | ||
|
|
||
| ## Configuration Options | ||
|
|
||
| Users can still customize the threshold for their specific needs: | ||
|
|
||
| ```bash | ||
| # Lower threshold (more aggressive disk storage) | ||
| MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD=262144 ./awmg --config config.toml | ||
|
|
||
| # Higher threshold (minimize disk storage) | ||
| MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD=1048576 ./awmg --config config.toml | ||
|
|
||
| # Or via config file | ||
| [gateway] | ||
| payload_size_threshold = 524288 | ||
| ``` | ||
|
|
||
| ## Related Issue | ||
|
|
||
| GitHub Issue: https://github.com/github/gh-aw/issues/18295 | ||
|
|
||
| ## Backward Compatibility | ||
|
|
||
| ✅ **Fully backward compatible** | ||
|
|
||
| - Existing configurations continue to work | ||
| - Environment variable and CLI flag still functional | ||
| - Users can explicitly set the old 10KB threshold if desired | ||
| - New default is a **quality-of-life improvement** that makes the gateway work better out-of-the-box | ||
|
|
||
| ## Future Considerations | ||
|
|
||
| If payload path accessibility issues persist for responses >512KB: | ||
|
|
||
| 1. Consider adding a mount configuration to make payload paths accessible to agents | ||
| 2. Consider adding a flag to return large payloads inline (disable disk storage) | ||
| 3. Consider implementing payload compression to reduce size before threshold check | ||
| 4. Consider per-tool threshold configuration for tools known to return large responses | ||
|
|
||
| ## Summary | ||
|
|
||
| The fix addresses the root cause of the infinite loop issue by ensuring that typical MCP tool responses are small enough to be returned inline, avoiding the inaccessible file path problem entirely. The threshold remains configurable for advanced use cases, maintaining flexibility while providing sensible defaults. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,20 +11,23 @@ import ( | |
| const ( | ||
| defaultLogDir = "/tmp/gh-aw/mcp-logs" | ||
| defaultPayloadDir = "/tmp/jq-payloads" | ||
| defaultPayloadSizeThreshold = 10240 // 10KB default threshold | ||
| defaultPayloadPathPrefix = "" // Empty by default - use actual filesystem path | ||
| defaultPayloadSizeThreshold = 524288 // 512KB default threshold | ||
| ) | ||
|
|
||
| // Logging flag variables | ||
| var ( | ||
| logDir string | ||
| payloadDir string | ||
| payloadPathPrefix string | ||
| payloadSizeThreshold int | ||
| ) | ||
|
|
||
| func init() { | ||
| RegisterFlag(func(cmd *cobra.Command) { | ||
| cmd.Flags().StringVar(&logDir, "log-dir", getDefaultLogDir(), "Directory for log files (falls back to stdout if directory cannot be created)") | ||
| cmd.Flags().StringVar(&payloadDir, "payload-dir", getDefaultPayloadDir(), "Directory for storing large payload files (segmented by session ID)") | ||
| cmd.Flags().StringVar(&payloadPathPrefix, "payload-path-prefix", getDefaultPayloadPathPrefix(), "Path prefix to use when returning payloadPath to clients (allows remapping host paths to client/agent container paths)") | ||
| cmd.Flags().IntVar(&payloadSizeThreshold, "payload-size-threshold", getDefaultPayloadSizeThreshold(), "Size threshold (in bytes) for storing payloads to disk. Payloads larger than this are stored, smaller ones returned inline") | ||
| }) | ||
| } | ||
|
|
@@ -41,6 +44,12 @@ func getDefaultPayloadDir() string { | |
| return envutil.GetEnvString("MCP_GATEWAY_PAYLOAD_DIR", defaultPayloadDir) | ||
| } | ||
|
|
||
| // getDefaultPayloadPathPrefix returns the default payload path prefix, checking MCP_GATEWAY_PAYLOAD_PATH_PREFIX | ||
| // environment variable first, then falling back to the hardcoded default | ||
| func getDefaultPayloadPathPrefix() string { | ||
| return envutil.GetEnvString("MCP_GATEWAY_PAYLOAD_PATH_PREFIX", defaultPayloadPathPrefix) | ||
| } | ||
|
Comment on lines
+47
to
+51
|
||
|
|
||
| // getDefaultPayloadSizeThreshold returns the default payload size threshold, checking | ||
| // MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD environment variable first, then falling back to the hardcoded default | ||
| func getDefaultPayloadSizeThreshold() int { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,7 +176,8 @@ func applyJqSchema(ctx context.Context, jsonData interface{}) (interface{}, erro | |
|
|
||
| // savePayload saves the payload to disk and returns the file path | ||
| // The file is saved to {baseDir}/{sessionID}/{queryID}/payload.json | ||
| func savePayload(baseDir, sessionID, queryID string, payload []byte) (string, error) { | ||
| // The returned path uses pathPrefix if provided, otherwise returns the actual filesystem path | ||
| func savePayload(baseDir, pathPrefix, sessionID, queryID string, payload []byte) (string, error) { | ||
| // Create directory structure: {baseDir}/{sessionID}/{queryID} | ||
| dir := filepath.Join(baseDir, sessionID, queryID) | ||
|
|
||
|
|
@@ -214,7 +215,19 @@ func savePayload(baseDir, sessionID, queryID string, payload []byte) (string, er | |
| filePath, stat.Size(), stat.Mode()) | ||
| } | ||
|
|
||
| return filePath, nil | ||
| // If pathPrefix is provided, use it to remap the path for the client | ||
| // This allows the gateway to save files at one path (e.g., /tmp/jq-payloads) | ||
| // while returning a different path to clients (e.g., /workspace/payloads) | ||
| returnPath := filePath | ||
| if pathPrefix != "" { | ||
| // Replace baseDir with pathPrefix in the file path | ||
| relPath := filepath.Join(sessionID, queryID, "payload.json") | ||
| returnPath = filepath.Join(pathPrefix, relPath) | ||
| logger.LogInfo("payload", "Remapped payload path for client: filesystem=%s, clientPath=%s, pathPrefix=%s", | ||
| filePath, returnPath, pathPrefix) | ||
| } | ||
|
Comment on lines
+222
to
+228
|
||
|
|
||
| return returnPath, nil | ||
| } | ||
|
|
||
| // WrapToolHandler wraps a tool handler with jqschema middleware | ||
|
|
@@ -224,10 +237,12 @@ func savePayload(baseDir, sessionID, queryID string, payload []byte) (string, er | |
| // 3. If payload size > sizeThreshold: saves to {baseDir}/{sessionID}/{queryID}/payload.json and returns metadata | ||
| // 4. If payload size <= sizeThreshold: returns original response directly (no file storage) | ||
| // 5. For large payloads: returns first PayloadPreviewSize chars of payload + jq inferred schema | ||
| // 6. Uses pathPrefix to remap returned payloadPath for clients (if configured) | ||
| func WrapToolHandler( | ||
| handler func(context.Context, *sdk.CallToolRequest, interface{}) (*sdk.CallToolResult, interface{}, error), | ||
| toolName string, | ||
| baseDir string, | ||
| pathPrefix string, | ||
| sizeThreshold int, | ||
| getSessionID func(context.Context) string, | ||
| ) func(context.Context, *sdk.CallToolRequest, interface{}) (*sdk.CallToolResult, interface{}, error) { | ||
|
|
@@ -299,7 +314,7 @@ func WrapToolHandler( | |
| logMiddleware.Printf("Payload exceeds threshold: tool=%s, queryID=%s, size=%d bytes, threshold=%d bytes, saving to disk", | ||
| toolName, queryID, payloadSize, sizeThreshold) | ||
|
|
||
| filePath, saveErr := savePayload(baseDir, sessionID, queryID, payloadJSON) | ||
| filePath, saveErr := savePayload(baseDir, pathPrefix, sessionID, queryID, payloadJSON) | ||
| if saveErr != nil { | ||
| logMiddleware.Printf("Failed to save payload: tool=%s, queryID=%s, sessionID=%s, error=%v", toolName, queryID, sessionID, saveErr) | ||
| logger.LogError("payload", "Failed to save payload to filesystem: tool=%s, queryID=%s, session=%s, error=%v", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The README section is framed around fields not supported in JSON stdin config (it explicitly calls this out for
payloadSizeThreshold), butpayloadPathPrefixis also not supported in stdin JSON (StdinGatewayConfig has onlypayloadDir). Please explicitly note the stdin limitation here as well to avoid implying it works in JSON stdin configs.This issue also appears on line 304 of the same file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated README to explicitly note that
payloadPathPrefixis not supported in JSON stdin format (line 228). Also updated the CLI help output to include the--payload-path-prefixflag (line 307).Both fields now clearly state they are not supported in JSON stdin and must be configured via CLI flag or environment variable.
Commit: 0203bba