Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(claude): capture live MCP content-block responses

Kyle Elliott9c41c7a8a6d7
diff --git a/cli/beacon-hooks/cmd/post_tool.go b/cli/beacon-hooks/cmd/post_tool.go
index 780d95fd..79ae3654 100644
--- a/cli/beacon-hooks/cmd/post_tool.go
+++ b/cli/beacon-hooks/cmd/post_tool.go
@@ -202,7 +202,7 @@ func parseClaudeCopilotInput(input map[string]interface{}, logger *logging.Logge
sessionID, _ = input["session_id"].(string)
toolName, _ = input["tool_name"].(string)
toolInput, _ = input["tool_input"].(map[string]interface{})
- toolResponse, _ = input["tool_response"].(map[string]interface{})
+ toolResponse = resolveToolResponse(input)
}
if !isFileEditTool(platformFlag, toolName) {
@@ -396,6 +396,12 @@ func resolveToolResponse(input map[string]interface{}) map[string]interface{} {
}
}
}
+ // MCP tool responses may arrive as content-block lists. Keep the full block
+ // payload under the canonical content key; text consumers select text blocks
+ // without flattening media payloads into output.
+ if content, ok := input["tool_response"].([]interface{}); ok {
+ return map[string]interface{}{"content": content}
+ }
// If tool_response is a plain string, wrap it for downstream compatibility
if respStr, ok := input["tool_response"].(string); ok && respStr != "" {
return map[string]interface{}{"result": respStr}
diff --git a/cli/beacon-hooks/cmd/post_tool_test.go b/cli/beacon-hooks/cmd/post_tool_test.go
index a9d1d3f6..25c902ca 100644
--- a/cli/beacon-hooks/cmd/post_tool_test.go
+++ b/cli/beacon-hooks/cmd/post_tool_test.go
@@ -2,7 +2,10 @@ package cmd
import (
"path/filepath"
+ "reflect"
"testing"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
func TestIsFileEditTool(t *testing.T) {
@@ -691,6 +694,83 @@ func TestRunPostToolEmitsClaudeToolEvents(t *testing.T) {
}
}
+func TestClaudePostToolPreservesMCPContentBlocksAndExtractsTextOnly(t *testing.T) {
+ setupHookConfigDirs(t)
+ platformFlag = "claude"
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+
+ content := []interface{}{
+ map[string]interface{}{"type": "text", "text": "first result"},
+ map[string]interface{}{"type": "image", "mimeType": "image/png", "data": "base64-private-payload"},
+ map[string]interface{}{"type": "text", "text": "second result"},
+ }
+ runHookWithInput(t, runPostTool, map[string]interface{}{
+ "session_id": "claude-session", "hook_event_name": "PostToolUse",
+ "tool_name": "mcp__docs__search", "tool_use_id": "call-mcp-1",
+ "tool_input": map[string]interface{}{"query": "beacon"},
+ "tool_response": content,
+ })
+
+ events := endpointEvents(t, logPath)
+ if len(events) != 1 {
+ t.Fatalf("event count = %d, want 1", len(events))
+ }
+ event := events[0]
+ genAI := event["gen_ai"].(map[string]interface{})
+ call := genAI["tool"].(map[string]interface{})["call"].(map[string]interface{})
+ result := call["result"].(map[string]interface{})
+ blocks := result["content"].([]interface{})
+ if len(blocks) != 3 || blocks[1].(map[string]interface{})["data"] != "base64-private-payload" {
+ t.Fatalf("MCP result blocks = %#v, want all original content blocks", blocks)
+ }
+ if text := asymptoteobserve.ToolResultPlainText(result); text != "first result\nsecond result" {
+ t.Fatalf("MCP result text = %q, want both text blocks without encoded media", text)
+ }
+}
+func TestClaudePostToolFailureKeepsTypedMCPResult(t *testing.T) {
+ setupHookConfigDirs(t)
+ platformFlag = "claude"
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+ content := []interface{}{map[string]interface{}{"type": "text", "text": "permission denied"}}
+
+ runHookWithInput(t, runPostTool, map[string]interface{}{
+ "session_id": "claude-session", "hook_event_name": "PostToolUseFailure",
+ "tool_name": "mcp__docs__search", "tool_use_id": "call-mcp-failed",
+ "tool_input": map[string]interface{}{"query": "beacon"},
+ "tool_response": content, "error": "MCP call failed",
+ })
+
+ event := endpointEvents(t, logPath)[0]
+ if action := event["event"].(map[string]interface{})["action"]; action != "tool.failed" {
+ t.Fatalf("failed MCP action = %v, want tool.failed", action)
+ }
+ result := event["gen_ai"].(map[string]interface{})["tool"].(map[string]interface{})["call"].(map[string]interface{})["result"].(map[string]interface{})
+ if !reflect.DeepEqual(result["content"], content) {
+ t.Fatalf("failed MCP result = %#v, want original content blocks %#v", result, content)
+ }
+}
+
+func TestResolveClaudeToolResponseContentBlockEdgeCases(t *testing.T) {
+ platformFlag = "claude"
+ for _, tc := range []struct {
+ name string
+ raw interface{}
+ want map[string]interface{}
+ }{
+ {"empty block list", []interface{}{}, map[string]interface{}{"content": []interface{}{}}},
+ {"error object remains intact", map[string]interface{}{"isError": true, "content": []interface{}{map[string]interface{}{"type": "text", "text": "failed"}}}, map[string]interface{}{"isError": true, "content": []interface{}{map[string]interface{}{"type": "text", "text": "failed"}}}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ got := resolveToolResponse(map[string]interface{}{"tool_response": tc.raw})
+ if !reflect.DeepEqual(got, tc.want) {
+ t.Fatalf("resolved response = %#v, want %#v", got, tc.want)
+ }
+ })
+ }
+}
+
func TestResolveToolInput(t *testing.T) {
tests := []struct {
name string
diff --git a/docs/runtimes/claude-code.mdx b/docs/runtimes/claude-code.mdx
index 22c7a65e..f81f45fd 100644
--- a/docs/runtimes/claude-code.mdx
+++ b/docs/runtimes/claude-code.mdx
@@ -85,6 +85,11 @@ Beacon detects Claude Code through the `claude` executable and Claude settings p
| MDM deployment | Supported through the system-mode package or MDM-managed settings |
Claude Code hooks cover `SessionStart`, `UserPromptSubmit`, `PreToolUse`, `PostToolUse`, `PostToolUseFailure`, `Stop`, `SubagentStart`, `SubagentStop`, `PermissionRequest`, and `SessionEnd`. Pre-tool matching includes common Claude tools such as `Bash`, `Edit`, `Write`, `MultiEdit`, `Read`, `Glob`, `Grep`, `WebFetch`, `WebSearch`, `Agent`, and `mcp__.*`.
+Claude's live `PostToolUse` hook retains MCP tool responses in their content-block
+form, including mixed text and media blocks. Text summaries and content-retention
+markers use text blocks only, so binary media data is not rendered as base64
+output. Failure hook events remain classified as `tool.failed`.
+
## Normalized OTLP events
diff --git a/pkg/asymptoteobserve/toolresult.go b/pkg/asymptoteobserve/toolresult.go
index eeda810a..05c4217f 100644
--- a/pkg/asymptoteobserve/toolresult.go
+++ b/pkg/asymptoteobserve/toolresult.go
@@ -42,8 +42,9 @@ func IngestedContentToolName(name string) bool {
}
// ToolResultPlainText renders a tool result as text: a string as is; for an object or list (a
-// hook's tool_response, an MCP content-block list), every string it contains, maps in sorted key
-// order, one per line, trimmed. Numbers, booleans and nulls are sizes, counts and flags rather
+// hook's tool_response, an MCP content-block list), every ordinary string it contains and text
+// from MCP text blocks, with maps in sorted key order, one per line. MCP media blocks do not
+// contribute their binary data. Numbers, booleans and nulls are sizes, counts and flags rather
// than content and contribute nothing. Deterministic for the same result on every run.
//
// It does no redaction or truncation; callers apply the limit that governs where the text goes.
@@ -60,6 +61,20 @@ func collectToolResultStrings(v interface{}, out *[]string) {
*out = append(*out, typed)
}
case map[string]interface{}:
+ // MCP image/audio blocks carry encoded bytes rather than model-visible text.
+ // Other response objects can also have a type discriminator, so do not
+ // discard arbitrary typed objects or textual embedded resources.
+ switch typed["type"] {
+ case "image", "audio":
+ if _, encoded := typed["data"]; encoded {
+ return
+ }
+ case "text":
+ if text, ok := typed["text"].(string); ok {
+ collectToolResultStrings(text, out)
+ return
+ }
+ }
keys := make([]string, 0, len(typed))
for key := range typed {
keys = append(keys, key)
diff --git a/pkg/asymptoteobserve/toolresult_test.go b/pkg/asymptoteobserve/toolresult_test.go
index ce02c3fe..ddbf361e 100644
--- a/pkg/asymptoteobserve/toolresult_test.go
+++ b/pkg/asymptoteobserve/toolresult_test.go
@@ -31,7 +31,12 @@ func TestToolResultPlainText(t *testing.T) {
{"string", " hello ", "hello"},
{"nil", nil, ""},
{"claude read", map[string]interface{}{"type": "text", "file": map[string]interface{}{"filePath": "/a", "content": "body", "numLines": 1.0}}, "body\n/a\ntext"},
- {"mcp blocks", map[string]interface{}{"content": []interface{}{map[string]interface{}{"type": "text", "text": "one"}, map[string]interface{}{"type": "text", "text": "two"}}}, "one\ntext\ntwo\ntext"},
+ {"mcp blocks", map[string]interface{}{"content": []interface{}{map[string]interface{}{"type": "text", "text": "one"}, map[string]interface{}{"type": "image", "data": "base64-secret", "mimeType": "image/png"}, map[string]interface{}{"type": "text", "text": "two"}}}, "one\ntwo"},
+ {"empty MCP blocks", map[string]interface{}{"content": []interface{}{}}, ""},
+ {"MCP media only", map[string]interface{}{"content": []interface{}{map[string]interface{}{"type": "image", "data": "base64-secret"}}}, ""},
+ {"MCP textual resource", map[string]interface{}{"content": []interface{}{map[string]interface{}{"type": "resource", "resource": map[string]interface{}{"text": "embedded instructions", "uri": "notes://item"}}}}, "embedded instructions\nnotes://item\nresource"},
+ {"typed response object", map[string]interface{}{"type": "success", "result": "response body"}, "response body\nsuccess"},
+ {"plain list", []interface{}{"first", "second"}, "first\nsecond"},
{"scalars only", map[string]interface{}{"code": 200.0, "ok": true}, ""},
{"typed", []map[string]string{{"text": "typed"}}, "typed"},
}