feat(extract): capture MCP tool_response so ctx_search finds the data the MCP returned#330
Open
halindrome wants to merge 15 commits intomksglu:nextfrom
Open
feat(extract): capture MCP tool_response so ctx_search finds the data the MCP returned#330halindrome wants to merge 15 commits intomksglu:nextfrom
halindrome wants to merge 15 commits intomksglu:nextfrom
Conversation
… the MCP returned extractMcp previously emitted only the call shape (tool name + first arg) while the PostToolUse hook passed all four fields. For mcp__jira__jira_get, mcp__grafana__query_loki_logs, mcp__sentry__list_issues, etc. the response body was in tool_response but never stored, so ctx_search never found it. Match the rule_content precedent at extract.ts:85-91 — destructure tool_response and append it to the event data. No truncation: SQLite TEXT is unbounded, FTS5 indexes arbitrary text, and large responses are exactly what the cache most wants to preserve. Backward-compatible: events.data becomes a superset of what it was. Existing mcp event consumers read the same prefix. Closes mksglu#329 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Author
QA Round 1 — local verification by authorPosting the QA pass I ran against this PR before requesting review, so reviewers can see what's already been checked and what's left for them. Contract verification (against #329)
Verification details
FindingsContract findings: none. Non-blocking observations (already surfaced as the three open questions in the PR description — flagging here so they aren't lost):
Note for CI
Verdict: clean. Ready for maintainer review. No blocking findings; three open stance questions to confirm direction. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #329.
Problem
extractMcp(src/session/extract.ts) destructures onlytool_nameandtool_input, so for tools likemcp__jira__jira_get,mcp__grafana__query_loki_logs,mcp__sentry__list_issues, etc. the captured event records the call shape (jira_get: CVX-5909) but never the body the server returned.ctx_searchtherefore can't surface the ticket text, log lines, or issue details — defeating the cache for exactly the external MCPs whose responses are most expensive to re-fetch.The PostToolUse hook (
hooks/posttooluse.mjs:41-43) already passestool_responsetoextractEvents. The data is arriving — it's just being dropped inextractMcp.Fix
Destructure
tool_responseand append it to the eventdata, gated by the samelength > 0guard used by therule_contentprecedent atsrc/session/extract.ts:85-91:```ts
const responseStr = tool_response && tool_response.length > 0
? `\nresponse: ${safeString(tool_response)}`
: "";
```
rule_content.tool_responseis absent the emitteddatais byte-identical to before (`"ctx_stats"`, not `"ctx_stats\nresponse: "`). Existing consumers reading the prefix continue to work.Tests added
Four cases appended to
tests/session/session-extract.test.ts(new `MCP Events` describe block):tool_responsebody — asserts amcp__jira__jira_getevent includes\"MQTT reconnect storm\"from the response, not just\"jira_get\"and the ticket key.datawith no loss.tool_responseregression —data === \"ctx_stats\"(no `\nresponse:` suffix) when the field is omitted.tool_responseregression — same shape when the field is\"\".tests/session/session-extract.test.ts→ 101/101 pass. Full suite: 1540 pass / 18 skipped / 0 failures attributable to this change.tsc --noEmitclean. Bundle (hooks/session-extract.bundle.mjs) regenerated vianpm run build.Open questions for maintainers
These are flagged for direction — happy to adjust the PR either way:
rule_content. If you'd prefer a defensive ceiling for pathological cases (e.g. a 50 MB grafana export that would stall a synchronous PostToolUse hook), a high cap like 256 KB or 1 MB is a one-line addition: `safeString(tool_response).slice(0, MAX)`.rule_content.extractEnvalready redactsexport VAR=***patterns, so there's precedent in the file for active redaction if you want a pass overtool_responsebefore storage (API keys, OAuth/JWT bearers, customer PII in jira tickets, etc.). I haven't added one because the existingrule_contentpath also stores raw — but happy to add a redaction step if you prefer.mcp__and capture is the expected behavior. If you want an env override, the natural shape is:```ts
const CAPTURE_MCP_RESPONSES = process.env.CONTEXT_MODE_CAPTURE_MCP_RESPONSES !== "0";
```
(default on, disable with
=0).Let me know which direction you want and I'll iterate.