Claude Code Integration

Learn how to integrate TraceMem with Claude Code using Anthropic's MCP connector, enabling Claude to call TraceMem MCP tools directly from the Messages API without running a separate MCP client.

Overview

Anthropic's MCP connector enables calling remote MCP servers directly from the Messages API without running a separate MCP client. This is ideal for teams building their own agent service on Anthropic:

  • They call Claude via the Messages API
  • Claude can call TraceMem MCP tools via the connector
  • No extra infrastructure besides providing the TraceMem server and authentication

This integration uses a beta header/versioned feature and provides seamless access to TraceMem's decision tracking capabilities directly within Claude Code workflows.

Prerequisites

Before setting up the integration, ensure you have:

  1. A TraceMem account - Sign up at app.tracemem.com
  2. An Agent API key - Generate one from your TraceMem project settings
  3. Anthropic API access - Access to Anthropic's Messages API with MCP connector support
  4. Beta feature access - The MCP connector requires beta header/versioned feature access

MCP Connector Configuration

The MCP connector allows Claude to call remote MCP servers directly. Configure TraceMem as a remote MCP server:

Configuration Structure

json
{
  "mcp_servers": {
    "tracemem": {
      "url": "https://mcp.tracemem.com",
      "command": null
    }
  }
}

Authentication

TraceMem requires authentication via the Authorization header with the format Agent <your-api-key>. This is handled automatically by the MCP connector when configured properly.

Messages API Integration

Reference Implementation

Here's a complete example of using the Messages API with the MCP connector to enable TraceMem integration:

python
import anthropic
import json

# Initialize the Anthropic client
client = anthropic.Anthropic(
    api_key="your-anthropic-api-key"
)

# Configure MCP connector with TraceMem
mcp_config = {
    "mcp_servers": {
        "tracemem": {
            "url": "https://mcp.tracemem.com",
            "command": None
        }
    }
}

# Make a request with MCP connector enabled
message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    system="You are a helpful assistant with access to TraceMem for decision tracking.",
    messages=[
        {
            "role": "user",
            "content": "Create a decision envelope for refactoring the user authentication system."
        }
    ],
    # Enable MCP connector with beta header
    extra_headers={
        "anthropic-beta": "mcp-connector-2024-10-22"
    },
    # Configure MCP servers
    tools=mcp_config.get("mcp_servers", {}),
    # Authentication for TraceMem MCP server
    tool_choice="auto"
)

Required Headers

The MCP connector requires specific headers:

python
headers = {
    "anthropic-beta": "mcp-connector-2024-10-22",  # Beta feature header
    "x-api-key": "your-anthropic-api-key",
    # TraceMem authentication is handled via MCP server configuration
}

Tool Selection

When using the MCP connector, Claude automatically discovers available tools from the configured MCP servers. You can:

  1. Let Claude choose tools automatically - Use tool_choice="auto" (recommended)
  2. Force tool usage - Use tool_choice={"type": "tool", "name": "decision_create"} for specific tools
  3. Disable tools - Use tool_choice="none" to disable tool usage

Authentication Configuration

TraceMem authentication is configured at the MCP server level. The MCP connector handles authentication headers automatically:

python
# The MCP connector will automatically include:
# Authorization: Agent <your-traceme-api-key>
# 
# Configure this when setting up the MCP server connection

SKILL.md for TraceMem Lifecycle

To enforce the TraceMem lifecycle in Claude Code, create a SKILL.md file that guides Claude on when and how to use TraceMem tools. This ensures consistent decision tracking throughout your development workflow.

Example SKILL.md

Create a SKILL.md file in your project root:

markdown
# TraceMem Decision Tracking Skill

## Overview

This skill enables Claude Code to track architecture decisions, code changes, and design choices using TraceMem's decision envelope system.

## When to Use TraceMem

Always create a decision envelope when:
- Making architectural decisions
- Refactoring code
- Implementing new features
- Making database schema changes
- Deploying changes
- Modifying configuration
- Changing security settings

## TraceMem Lifecycle

### 1. Create Decision Envelope

Before starting work, create a decision envelope:

    Use the decision_create tool with:
    - intent: A structured intent like "code.refactor.execute" or "architecture.auth.system.design"
    - automation_mode: "propose" (for review) or "auto" (for automatic approval)
    - metadata: Relevant context about the decision

### 2. Add Context Throughout Work

As you make progress, add context to the decision:

    Use decision_add_context to document:
    - Design choices and rationale
    - Implementation details
    - Trade-offs considered
    - Dependencies identified

### 3. Evaluate Policies (if applicable)

If your decision involves data access or policy evaluation:

    Use decision_evaluate to check:
    - Policy compliance
    - Data access permissions
    - Approval requirements

### 4. Request Approval (if needed)

If policy evaluation returns "requires_exception":

    Use decision_request_approval to:
    - Request human approval
    - Provide clear context for approvers
    - Wait for approval before proceeding

### 5. Read/Write Data (if needed)

If your decision involves data access:

    Use decision_read to:
    - Query data products
    - Understand current state
    - Gather context for decisions

    Use decision_write to:
    - Make data changes
    - Update records
    - Create new entries

### 6. Close Decision

When work is complete, close the decision:

    Use decision_close with:
    - outcome: "commit" (success), "abort" (cancelled), or "reject" (rejected)
    - summary: Brief summary of what was accomplished

## Intent Naming Conventions

Use structured intents following this pattern:

- `code.change.apply` - Code changes
- `code.refactor.execute` - Code refactoring
- `architecture.<domain>.<component>.<action>` - Architecture decisions
- `deploy.release.execute` - Deployments
- `data.change.apply` - Database changes
- `secrets.change.propose` - Secret modifications

## Example Workflow

1. **Start**: "I'm going to refactor the authentication system"
   - Create decision with intent: "code.refactor.execute"
   
2. **Progress**: "I'm extracting the auth logic into a separate service"
   - Add context: "Extracting authentication logic to improve testability"
   
3. **Complete**: "Refactoring complete, all tests passing"
   - Close decision with outcome: "commit"

## Important Notes

- Always create a decision envelope before starting significant work
- Add context throughout the work, not just at the end
- Use appropriate intents to categorize decisions
- Close decisions explicitly - don't leave them open
- If approval is required, wait for it before proceeding
- Use decision_read to understand current state before making changes

Using SKILL.md in Claude Code

Place the SKILL.md file in your project root. Claude Code will automatically reference it when working on your codebase, ensuring consistent TraceMem usage.

You can also explicitly reference it:

text
"Before starting this refactoring, review SKILL.md for TraceMem decision tracking guidelines."

Complete Example

Here's a complete example of integrating TraceMem with Claude Code:

python
import anthropic
import json
import os

# Configuration
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
TRACEMEM_API_KEY = os.getenv("TRACEMEM_API_KEY")

client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)

# MCP configuration with TraceMem
mcp_servers = {
    "tracemem": {
        "url": "https://mcp.tracemem.com",
        "command": None,
        # Authentication is configured via MCP connector
        # The connector will use: Authorization: Agent <TRACEMEM_API_KEY>
    }
}

# Example: Ask Claude to create a decision and refactor code
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=4096,
    system="""
    You are a helpful coding assistant with access to TraceMem for decision tracking.
    
    When making significant changes:
    1. Create a decision envelope using decision_create
    2. Add context as you work using decision_add_context
    3. Close the decision when complete using decision_close
    
    Review SKILL.md for detailed TraceMem lifecycle guidelines.
    """,
    messages=[
        {
            "role": "user",
            "content": """
            I want to refactor the user authentication system to use OAuth2.
            Please:
            1. Create a TraceMem decision envelope
            2. Document the approach
            3. Implement the refactoring
            4. Close the decision when done
            """
        }
    ],
    extra_headers={
        "anthropic-beta": "mcp-connector-2024-10-22"
    },
    tools=mcp_servers,
    tool_choice="auto"
)

# Process the response
print(response.content)

Tool Discovery

Claude automatically discovers available TraceMem tools through the MCP connector. Available tools include:

  • Decision Management: decision_create, decision_get, decision_close, decision_add_context
  • Data Access: decision_read, decision_write
  • Policy Evaluation: decision_evaluate
  • Approvals: decision_request_approval
  • Product Discovery: products_list, product_get
  • Capabilities: capabilities_get

Security Best Practices

  1. Protect API Keys

    • Store API keys in environment variables
    • Never commit keys to version control
    • Use secure secret management
  2. Use Environment Variables

    bash
    export ANTHROPIC_API_KEY="your-anthropic-key"
    export TRACEMEM_API_KEY="your-tracemem-key"
    
  3. Review Decision Access

    • Ensure only authorized team members have access
    • Review decision traces regularly
    • Monitor API key usage
  4. Minimize Data Transfer

    • Only include necessary context in decisions
    • Use TraceMem's automatic secret redaction
    • Avoid including sensitive data in metadata

Troubleshooting

MCP Connector Not Available

If you receive errors about MCP connector:

  1. Verify Beta Access: Ensure you have access to the beta feature
  2. Check Header: Verify the anthropic-beta header is correct
  3. Review Documentation: Check Anthropic's latest MCP connector documentation

Authentication Errors

If you receive authentication errors from TraceMem:

  1. Verify API Key: Ensure your TraceMem API key is correct
  2. Check Format: Verify the key format is valid
  3. Review Permissions: Ensure the API key has necessary permissions

Tools Not Available

If TraceMem tools don't appear:

  1. Verify Configuration: Check MCP server configuration
  2. Test Connection: Verify you can reach https://mcp.tracemem.com
  3. Check Logs: Review API response for error messages

Connection Issues

If you're unable to connect:

  1. Network Check: Verify network connectivity
  2. Firewall: Check if firewall is blocking connections
  3. Endpoint: Verify the TraceMem endpoint URL is correct

TraceMem is trace-native infrastructure for AI agents