File size: 4,682 Bytes
9c7d451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a41fa
9c7d451
 
 
e4a41fa
9c7d451
 
 
 
e4a41fa
9c7d451
 
 
 
 
 
 
 
 
 
e4a41fa
9c7d451
 
 
 
 
 
 
 
 
 
 
e4a41fa
9c7d451
 
 
 
 
 
 
e4a41fa
9c7d451
 
 
 
e4a41fa
9c7d451
 
 
 
 
 
 
 
 
e4a41fa
9c7d451
 
 
 
 
 
 
 
 
 
 
e4a41fa
9c7d451
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a41fa
9c7d451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Show BEFORE/AFTER code changes for MCP integration.

This demonstrates the minimal code change needed to add Headroom
compression to MCP tool outputs in your host application.

Run with:
    PYTHONPATH=. python -m examples.mcp_demo.show_before_after
"""


def main():
    print("\n" + "=" * 70)
    print("HEADROOM MCP INTEGRATION - DEVELOPER EXPERIENCE")
    print("=" * 70)

    # =========================================================================
    # Option 1: Standalone Function (Simplest)
    # =========================================================================
    print("\n" + "─" * 70)
    print("OPTION 1: Standalone Function (2 lines to add)")
    print("─" * 70)

    print("\nBEFORE (in your MCP host application):")
    print("-" * 40)
    before_standalone = """
# Your MCP host application
result = await mcp_client.call_tool("search_logs", {"service": "api"})
messages.append({"role": "tool", "content": result})
"""
    print(before_standalone)

    print("\nAFTER (with Headroom compression):")
    print("-" * 40)
    after_standalone = """
from headroom.integrations.mcp import compress_tool_result  # ADD THIS

# Your MCP host application
result = await mcp_client.call_tool("search_logs", {"service": "api"})
compressed = compress_tool_result(                          # ADD THIS
    content=result,                                         # ADD THIS
    tool_name="search_logs",                                # ADD THIS
    user_query="find errors in api",                        # ADD THIS
)                                                           # ADD THIS
messages.append({"role": "tool", "content": compressed})
"""
    print(after_standalone)

    # =========================================================================
    # Option 2: Client Wrapper (Zero-Touch After Setup)
    # =========================================================================
    print("\n" + "─" * 70)
    print("OPTION 2: Client Wrapper (wrap once, forget)")
    print("─" * 70)

    print("\nBEFORE:")
    print("-" * 40)
    before_wrapper = """
from mcp import Client

# Create MCP client
client = Client(transport)

# Use client normally
result = await client.call_tool("search_logs", {"service": "api"})
"""
    print(before_wrapper)

    print("\nAFTER:")
    print("-" * 40)
    after_wrapper = """
from mcp import Client
from headroom.integrations.mcp import HeadroomMCPClientWrapper  # ADD THIS

# Create MCP client
base_client = Client(transport)
client = HeadroomMCPClientWrapper(base_client)  # WRAP IT (1 line)

# Use client normally - compression is automatic!
result = await client.call_tool("search_logs", {"service": "api"})
"""
    print(after_wrapper)

    # =========================================================================
    # Option 3: With Metrics
    # =========================================================================
    print("\n" + "─" * 70)
    print("OPTION 3: With Metrics (track savings)")
    print("─" * 70)

    print("\nCode with metrics tracking:")
    print("-" * 40)
    with_metrics = """
from headroom.integrations.mcp import compress_tool_result_with_metrics

result = await mcp_client.call_tool("search_logs", {"service": "api"})
compression = compress_tool_result_with_metrics(
    content=result,
    tool_name="search_logs",
    user_query="find errors",
)

print(f"Tokens saved: {compression.tokens_saved}")
print(f"Compression: {compression.compression_ratio:.1%}")
print(f"Errors preserved: {compression.errors_preserved}")

messages.append({"role": "tool", "content": compression.compressed_content})
"""
    print(with_metrics)

    # =========================================================================
    # Summary
    # =========================================================================
    print("\n" + "=" * 70)
    print("SUMMARY: What Developers Need to Do")
    print("=" * 70)

    print("""
1. SIMPLEST (Standalone Function):
   - Add 1 import
   - Wrap tool result with compress_tool_result()
   - 2 lines of code change

2. EASIEST (Client Wrapper):
   - Add 1 import
   - Wrap your MCP client once
   - All subsequent tool calls automatically compressed

3. OBSERVABILITY (With Metrics):
   - Use compress_tool_result_with_metrics()
   - Get full metrics: tokens_saved, compression_ratio, errors_preserved
   - Track savings over time

Key Benefits:
- 70-85% token reduction on large tool outputs
- 100% ERROR preservation (log entries, exceptions, failures)
- Zero config needed (smart defaults for common MCP servers)
- Full schema preservation (JSON structure intact)
""")

    print("=" * 70)


if __name__ == "__main__":
    main()