# How the Claude Desktop Hook Automatically Captures Conversation Context for MCP Memory Service

> Learn how the Claude Desktop hook automatically captures conversation context by using a Node.js script to parse transcripts and store structured memories in the MCP Memory Service.

- Repository: [Henry/mcp-memory-service](https://github.com/doobidoo/mcp-memory-service)
- Tags: how-to-guide
- Published: 2026-02-28

---

**The Claude Desktop hook automatically captures conversation context by executing a post-tool-use Node.js script that parses the latest transcript exchange, detects valuable content patterns, and stores structured memories via HTTP POST to the MCP Memory Service.**

The **doobidoo/mcp-memory-service** repository implements an intelligent auto-capture mechanism that integrates seamlessly with Claude Desktop. This system operates as a **post-tool-use hook**, monitoring conversation transcripts to persist valuable context without manual intervention. By leveraging regex pattern matching and configurable rules, the hook ensures that only meaningful interactions—such as architectural decisions, error resolutions, and implementation details—are committed to the knowledge graph.

## Hook Invocation and Input Handling

When Claude Desktop completes a tool execution (such as `Edit`, `Write`, or `Bash`), it triggers the Node.js script located at [`claude-hooks/core/auto-capture-hook.js`](https://github.com/doobidoo/mcp-memory-service/blob/main/claude-hooks/core/auto-capture-hook.js). The hook receives a JSON blob via **stdin** containing the path to the conversation transcript and the current working directory.

```js
const stdinData = await readStdin();          // read JSON from stdin
const input = JSON.parse(stdinData);          // { transcript_path, cwd, … }

```

The script immediately extracts the `transcript_path` and `cwd` fields to locate the conversation history and determine the project context. This input structure enables the hook to operate statelessly, processing each tool use independently without maintaining persistent connections.

## Transcript Parsing and User Overrides

The hook loads the transcript file—a JSON array of messages—and extracts the most recent user and assistant messages using `parseTranscript()`. This function isolates the final exchange that occurred immediately before the tool execution.

```js
const transcript = await parseTranscript(transcriptPath);
const overrides = hasUserOverride(transcript.userMessage);

```

Users can control capture behavior through **inline overrides** embedded in their messages. The `hasUserOverride()` function in [`claude-hooks/utilities/auto-capture-patterns.js`](https://github.com/doobidoo/mcp-memory-service/blob/main/claude-hooks/utilities/auto-capture-patterns.js) scans for two specific directives:

- `#remember` — Forces immediate capture regardless of pattern matching
- `#skip` — Prevents the current exchange from being stored

These overrides provide manual control when the automatic pattern detection might otherwise miss critical context or capture unnecessary noise.

## Pattern Detection and Tagging

If no override is detected, the combined conversation text (`User: …\n\nAssistant: …`) is passed to `detectPatterns()` from [`claude-hooks/utilities/auto-capture-patterns.js`](https://github.com/doobidoo/mcp-memory-service/blob/main/claude-hooks/utilities/auto-capture-patterns.js). This module executes a prioritized list of regular expression patterns against the content to determine if the exchange holds long-term value.

The detection engine evaluates six distinct pattern categories:

- **Decision** — Architectural choices or design commitments
- **Error** — Bugs, exceptions, or problem resolutions  
- **Learning** — New insights or conceptual understanding
- **Implementation** — Code changes or technical execution details
- **Important** — High-priority information or critical context
- **Code** — Specific programming logic or syntax discussions

```js
for (const [name, pat] of sortedPatterns) {
  if (pat.minLength && content.length < pat.minLength) continue;
  if (pat.regex.test(contentLower)) {
    return {
      isValuable: true,
      memoryType: pat.memoryType,
      matchedPattern: name,
      confidence: pat.confidence,
      description: pat.description
    };
  }
}

```

When a pattern matches, the function returns a detection object containing `isValuable: true`, the `memoryType` (e.g., `Decision`, `Error`, `Learning`, `Context`), the `matchedPattern` name, and a confidence score. Tags are then generated from the detection result and the project name derived from the `cwd`.

## Storing the Memory via HTTP API

Once content is validated as valuable, the `storeMemory()` function constructs an HTTP POST request to the MCP Memory Service endpoint running at `127.0.0.1:8000`. The request targets `/api/memories` with a JSON payload containing the (optionally truncated) content, detected memory type, generated tags, and metadata.

```js
const result = await storeMemory(
    config,
    truncatedContent,
    detection.memoryType,
    tags
);

```

The HTTP request includes specific headers and a structured body:

```json
{
  "method": "POST",
  "hostname": "127.0.0.1",
  "port": 8000,
  "path": "/api/memories",
  "headers": {
    "Content-Type": "application/json",
    "Content-Length": "<payload length>"
  },
  "body": JSON.stringify({
    "content": "<combined user/assistant text>",
    "memory_type": "Decision",
    "tags": ["auto-captured", "smart-ingest", "decision", "my-app"],
    "metadata": {
      "source": "auto-capture",
      "hook": "PostToolUse",
      "captured_at": "2026-02-28T15:04:12.345Z"
    }
  })
}

```

The hook exits with status code **0** regardless of whether the capture succeeded, was skipped, or failed, ensuring that Claude Desktop's workflow continues uninterrupted.

## Summary

The Claude Desktop integration with MCP Memory Service provides a robust, automated approach to knowledge capture:

- **Stateless execution** via stdin JSON input containing transcript paths and working directories
- **User override support** through `#remember` and `#skip` directives in conversation text
- **Multi-pattern detection** using prioritized regex matching for decisions, errors, learnings, and implementations
- **HTTP API storage** to localhost:8000 with structured metadata identifying the source as `auto-capture`
- **Graceful degradation** ensuring Claude Desktop operations continue even if memory storage fails

## Frequently Asked Questions

### How can I force or prevent the Claude Desktop hook from capturing a specific conversation?

You can insert explicit override commands directly into your message text. Type `#remember` anywhere in your message to force immediate capture regardless of pattern matching, or type `#skip` to prevent the current exchange from being stored in the MCP Memory Service. These directives are detected by the `hasUserOverride()` function in [`claude-hooks/utilities/auto-capture-patterns.js`](https://github.com/doobidoo/mcp-memory-service/blob/main/claude-hooks/utilities/auto-capture-patterns.js).

### What conversation patterns trigger automatic memory capture?

The hook evaluates content against six prioritized regex patterns defined in [`auto-capture-patterns.js`](https://github.com/doobidoo/mcp-memory-service/blob/main/auto-capture-patterns.js): Decision (architectural choices), Error (bug fixes), Learning (new insights), Implementation (code changes), Important (critical context), and Code (technical logic). Each pattern has configurable minimum length requirements and confidence scores to reduce false positives.

### Where does the Claude Desktop hook store captured memories?

The hook transmits structured data via HTTP POST to `127.0.0.1:8000/api/memories`. The payload includes the conversation text, detected memory type (e.g., `Decision`, `Error`), auto-generated tags, and metadata indicating the source as `auto-capture` and the trigger as `PostToolUse`. The receiving endpoint is implemented in the MCP Memory Service's web API layer.

### Which file controls the auto-capture configuration settings?

Default behavior is governed by [`claude-hooks/config.json`](https://github.com/doobidoo/mcp-memory-service/blob/main/claude-hooks/config.json), which specifies the HTTP endpoint, enabled pattern lists, minimum content length thresholds, and debug mode flags. Users can customize capture sensitivity, disable specific pattern types, or modify the target port by adjusting this configuration file before Claude Desktop initializes the hook.