-
Notifications
You must be signed in to change notification settings - Fork 16
Fix large payload middleware: populate CallToolResult.Content field and add extensive logging #766
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 all commits
5633484
3109482
ce2c181
845515c
27dbad2
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -113,16 +113,41 @@ func applyJqSchema(ctx context.Context, jsonData interface{}) (string, error) { | |||||||||||||||
| func savePayload(baseDir, sessionID, queryID string, payload []byte) (string, error) { | ||||||||||||||||
| // Create directory structure: {baseDir}/{sessionID}/{queryID} | ||||||||||||||||
| dir := filepath.Join(baseDir, sessionID, queryID) | ||||||||||||||||
|
|
||||||||||||||||
| logger.LogDebug("payload", "Creating payload directory: baseDir=%s, session=%s, query=%s, fullPath=%s", | ||||||||||||||||
| baseDir, sessionID, queryID, dir) | ||||||||||||||||
|
|
||||||||||||||||
| if err := os.MkdirAll(dir, 0700); err != nil { | ||||||||||||||||
| logger.LogError("payload", "Failed to create payload directory: path=%s, error=%v", dir, err) | ||||||||||||||||
| return "", fmt.Errorf("failed to create payload directory: %w", err) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| logger.LogDebug("payload", "Successfully created payload directory: path=%s, permissions=0700", dir) | ||||||||||||||||
|
|
||||||||||||||||
| // Save payload to file with restrictive permissions (owner read/write only) | ||||||||||||||||
| filePath := filepath.Join(dir, "payload.json") | ||||||||||||||||
| payloadSize := len(payload) | ||||||||||||||||
|
|
||||||||||||||||
| logger.LogInfo("payload", "Writing large payload to filesystem: path=%s, size=%d bytes (%.2f KB, %.2f MB)", | ||||||||||||||||
| filePath, payloadSize, float64(payloadSize)/1024, float64(payloadSize)/(1024*1024)) | ||||||||||||||||
|
|
||||||||||||||||
| if err := os.WriteFile(filePath, payload, 0600); err != nil { | ||||||||||||||||
| logger.LogError("payload", "Failed to write payload file: path=%s, size=%d bytes, error=%v", | ||||||||||||||||
| filePath, payloadSize, err) | ||||||||||||||||
| return "", fmt.Errorf("failed to write payload file: %w", err) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| logger.LogInfo("payload", "Successfully saved large payload to filesystem: path=%s, size=%d bytes, permissions=0600", | ||||||||||||||||
| filePath, payloadSize) | ||||||||||||||||
|
|
||||||||||||||||
| // Verify file was written correctly | ||||||||||||||||
| if stat, err := os.Stat(filePath); err != nil { | ||||||||||||||||
| logger.LogWarn("payload", "Could not verify payload file after write: path=%s, error=%v", filePath, err) | ||||||||||||||||
| } else { | ||||||||||||||||
| logger.LogDebug("payload", "Payload file verified: path=%s, size=%d bytes, mode=%s", | ||||||||||||||||
| filePath, stat.Size(), stat.Mode()) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
Comment on lines
+143
to
+150
|
||||||||||||||||
| // Verify file was written correctly | |
| if stat, err := os.Stat(filePath); err != nil { | |
| logger.LogWarn("payload", "Could not verify payload file after write: path=%s, error=%v", filePath, err) | |
| } else { | |
| logger.LogDebug("payload", "Payload file verified: path=%s, size=%d bytes, mode=%s", | |
| filePath, stat.Size(), stat.Mode()) | |
| } |
Copilot
AI
Feb 6, 2026
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.
If savePayload fails, filePath will be empty but the middleware still builds/returns client metadata and logs a payloadPath. That can mislead clients into trying to read a non-existent file. Consider only returning/advertising payloadPath when saveErr == nil (or explicitly include storage failure info in the metadata).
See below for a potential fix:
"preview": preview,
"schema": schemaJSON,
"originalSize": len(payloadJSON),
"truncated": truncated,
}
// Only advertise payloadPath when we actually have a stored payload
if filePath != "" {
rewrittenResponse["payloadPath"] = filePath
}
logMiddleware.Printf("Rewritten response: tool=%s, queryID=%s, sessionID=%s, originalSize=%d, truncated=%v",
toolName, queryID, sessionID, len(payloadJSON), truncated)
if filePath != "" {
logger.LogInfo("payload", "Created metadata response for client: tool=%s, queryID=%s, session=%s, payloadPath=%s, originalSize=%d bytes, truncated=%v",
toolName, queryID, sessionID, filePath, len(payloadJSON), truncated)
} else {
logger.LogInfo("payload", "Created metadata response for client without payloadPath: tool=%s, queryID=%s, session=%s, originalSize=%d bytes, truncated=%v",
toolName, queryID, sessionID, len(payloadJSON), truncated)
}
Copilot
AI
Feb 6, 2026
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.
This log line hardcodes an agent/container path (/workspace/mcp-payloads/...) that depends on workflow-specific mounts and may be incorrect in most deployments. Consider removing the hardcoded path, or deriving/logging it from configuration so operational logs don’t mislead users about where the payload is accessible.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -64,7 +64,30 @@ func TestMiddlewareIntegration(t *testing.T) { | |||||||||||
| require.NotNil(t, result, "Result should not be nil") | ||||||||||||
| assert.False(t, result.IsError, "Result should not indicate error") | ||||||||||||
|
|
||||||||||||
| // Verify response structure | ||||||||||||
| // Verify the result Content field contains the transformed response | ||||||||||||
| require.NotEmpty(t, result.Content, "Result should have Content") | ||||||||||||
| textContent, ok := result.Content[0].(*sdk.TextContent) | ||||||||||||
| require.True(t, ok, "Content should be TextContent") | ||||||||||||
| require.NotEmpty(t, textContent.Text, "TextContent should have text") | ||||||||||||
|
|
||||||||||||
| // Parse the JSON from Content | ||||||||||||
| var contentMap map[string]interface{} | ||||||||||||
| err = json.Unmarshal([]byte(textContent.Text), &contentMap) | ||||||||||||
| require.NoError(t, err, "Content should be valid JSON") | ||||||||||||
|
|
||||||||||||
| // Verify all required fields exist in Content | ||||||||||||
| assert.Contains(t, contentMap, "queryID", "Content should contain queryID") | ||||||||||||
| assert.Contains(t, contentMap, "payloadPath", "Content should contain payloadPath") | ||||||||||||
| assert.Contains(t, contentMap, "preview", "Content should contain preview") | ||||||||||||
| assert.Contains(t, contentMap, "schema", "Content should contain schema") | ||||||||||||
| assert.Contains(t, contentMap, "originalSize", "Content should contain originalSize") | ||||||||||||
| assert.Contains(t, contentMap, "truncated", "Content should contain truncated") | ||||||||||||
|
|
||||||||||||
| // Verify queryID format in Content | ||||||||||||
| queryIDFromContent := contentMap["queryID"].(string) | ||||||||||||
|
||||||||||||
| queryIDFromContent := contentMap["queryID"].(string) | |
| queryIDValue, ok := contentMap["queryID"] | |
| require.True(t, ok, "Content queryID should be present") | |
| queryIDFromContent, ok := queryIDValue.(string) | |
| require.True(t, ok, "Content queryID should be a string") |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -140,26 +140,40 @@ func TestWrapToolHandler(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.NotNil(t, result, "Result should not be nil") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.False(t, result.IsError, "Result should not be an error") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify rewritten response structure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataMap, ok := data.(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.True(t, ok, "Data should be a map") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, dataMap, "queryID", "Response should contain queryID") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, dataMap, "payloadPath", "Response should contain payloadPath") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, dataMap, "preview", "Response should contain preview") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, dataMap, "schema", "Response should contain schema") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, dataMap, "originalSize", "Response should contain originalSize") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, dataMap, "truncated", "Response should contain truncated") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify the result Content field contains the transformed response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.NotEmpty(t, result.Content, "Result should have Content") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| textContent, ok := result.Content[0].(*sdk.TextContent) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.True(t, ok, "Content should be TextContent") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.NotEmpty(t, textContent.Text, "TextContent should have text") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Parse the JSON from Content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var contentMap map[string]interface{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err = json.Unmarshal([]byte(textContent.Text), &contentMap) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.NoError(t, err, "Content should be valid JSON") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify transformed response in Content field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, contentMap, "queryID", "Content should contain queryID") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, contentMap, "payloadPath", "Content should contain payloadPath") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, contentMap, "preview", "Content should contain preview") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, contentMap, "schema", "Content should contain schema") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, contentMap, "originalSize", "Content should contain originalSize") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, contentMap, "truncated", "Content should contain truncated") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify queryID is a valid hex string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queryID, ok := dataMap["queryID"].(string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queryID, ok := contentMap["queryID"].(string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.True(t, ok, "queryID should be a string") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.NotEmpty(t, queryID, "queryID should not be empty") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify schema is present | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| schema := dataMap["schema"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| schema := contentMap["schema"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.NotNil(t, schema, "Schema should not be nil") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Also verify rewritten response in data return value (for internal use) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataMap, ok := data.(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.True(t, ok, "Data should be a map") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, dataMap, "queryID", "Data should contain queryID") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.Contains(t, dataMap, "payloadPath", "Data should contain payloadPath") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Clean up test directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer os.RemoveAll(filepath.Join("/tmp", "gh-awmg")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -216,14 +230,26 @@ func TestWrapToolHandler_LongPayload(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.NoError(t, err, "Should not return error") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.NotNil(t, result, "Result should not be nil") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataMap, ok := data.(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.True(t, ok, "Data should be a map") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify Content field contains the transformed response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.NotEmpty(t, result.Content, "Result should have Content") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| textContent, ok := result.Content[0].(*sdk.TextContent) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.True(t, ok, "Content should be TextContent") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Parse the JSON from Content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var contentMap map[string]interface{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err = json.Unmarshal([]byte(textContent.Text), &contentMap) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.NoError(t, err, "Content should be valid JSON") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify truncation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.True(t, dataMap["truncated"].(bool), "Should indicate truncation") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preview := dataMap["preview"].(string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify truncation in Content field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.True(t, contentMap["truncated"].(bool), "Should indicate truncation in Content") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| preview := contentMap["preview"].(string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.LessOrEqual(t, len(preview), 503, "Preview should be truncated to ~500 chars + '...'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.True(t, strings.HasSuffix(preview, "..."), "Preview should end with '...'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Also verify in data return value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dataMap, ok := data.(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.True(t, ok, "Data should be a map") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.True(t, dataMap["truncated"].(bool), "Should indicate truncation in data") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+244
to
+252
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert.True(t, contentMap["truncated"].(bool), "Should indicate truncation in Content") | |
| preview := contentMap["preview"].(string) | |
| assert.LessOrEqual(t, len(preview), 503, "Preview should be truncated to ~500 chars + '...'") | |
| assert.True(t, strings.HasSuffix(preview, "..."), "Preview should end with '...'") | |
| // Also verify in data return value | |
| dataMap, ok := data.(map[string]interface{}) | |
| require.True(t, ok, "Data should be a map") | |
| assert.True(t, dataMap["truncated"].(bool), "Should indicate truncation in data") | |
| truncatedVal, ok := contentMap["truncated"] | |
| require.True(t, ok, "Content JSON should contain 'truncated' field") | |
| truncated, ok := truncatedVal.(bool) | |
| require.True(t, ok, "'truncated' field in Content JSON should be a bool") | |
| assert.True(t, truncated, "Should indicate truncation in Content") | |
| previewVal, ok := contentMap["preview"] | |
| require.True(t, ok, "Content JSON should contain 'preview' field") | |
| preview, ok := previewVal.(string) | |
| require.True(t, ok, "'preview' field in Content JSON should be a string") | |
| assert.LessOrEqual(t, len(preview), 503, "Preview should be truncated to ~500 chars + '...'") | |
| assert.True(t, strings.HasSuffix(preview, "..."), "Preview should end with '...'") | |
| // Also verify in data return value | |
| dataMap, ok := data.(map[string]interface{}) | |
| require.True(t, ok, "Data should be a map") | |
| dataTruncatedVal, ok := dataMap["truncated"] | |
| require.True(t, ok, "Data JSON should contain 'truncated' field") | |
| dataTruncated, ok := dataTruncatedVal.(bool) | |
| require.True(t, ok, "'truncated' field in data JSON should be a bool") | |
| assert.True(t, dataTruncated, "Should indicate truncation in data") |
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.
These LogInfo messages are emitted for every wrapped tool call (the middleware saves all payloads), and the underlying file logger fsyncs on each log write. This combination can significantly increase I/O and latency in production. Consider downgrading most of these messages to debug (or sampling), and avoid wording like "large payload" unless there is an actual size threshold.