-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude_code_http_server.py
More file actions
executable file
·142 lines (112 loc) · 3.91 KB
/
claude_code_http_server.py
File metadata and controls
executable file
·142 lines (112 loc) · 3.91 KB
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
#!/home/panda/Documents/PythonScripts/OutcomeBackcasting/backcast_venv/bin/python3
"""
HTTP Server for Claude Code Integration
Allows mobile devices to send prompts to Claude Code and receive responses via HTTP.
"""
from flask import Flask, request, jsonify
from flask_cors import CORS
import subprocess
import json
import os
app = Flask(__name__)
CORS(app)
# Store conversation history
conversation_history = []
@app.route('/api/health', methods=['GET'])
def health_check():
"""Health check endpoint"""
return jsonify({
'status': 'healthy',
'service': 'Claude Code HTTP Gateway',
'version': '1.0'
})
@app.route('/api/prompt', methods=['POST'])
def send_prompt():
"""Send a prompt to Claude Code and get response"""
data = request.json
if 'prompt' not in data:
return jsonify({'status': 'error', 'message': 'Missing prompt field'}), 400
prompt = data['prompt']
# Add to conversation history
conversation_history.append({
'role': 'user',
'content': prompt
})
try:
# Use the Anthropic API directly
import anthropic
# Get API key from environment
api_key = os.environ.get('ANTHROPIC_API_KEY')
if not api_key:
return jsonify({
'status': 'error',
'message': 'ANTHROPIC_API_KEY not set in environment'
}), 500
client = anthropic.Anthropic(api_key=api_key)
# Create message with conversation history
response = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=8096,
messages=conversation_history
)
# Extract response text
response_text = response.content[0].text
# Add assistant response to history
conversation_history.append({
'role': 'assistant',
'content': response_text
})
return jsonify({
'status': 'success',
'response': response_text,
'conversation_length': len(conversation_history)
})
except Exception as e:
return jsonify({
'status': 'error',
'message': f'Error calling Claude API: {str(e)}'
}), 500
@app.route('/api/conversation', methods=['GET'])
def get_conversation():
"""Get conversation history"""
return jsonify({
'status': 'success',
'conversation': conversation_history,
'length': len(conversation_history)
})
@app.route('/api/conversation/clear', methods=['POST'])
def clear_conversation():
"""Clear conversation history"""
global conversation_history
conversation_history = []
return jsonify({
'status': 'success',
'message': 'Conversation cleared'
})
@app.route('/api/conversation/export', methods=['GET'])
def export_conversation():
"""Export conversation as markdown"""
markdown = "# Claude Conversation\n\n"
for msg in conversation_history:
role = msg['role'].title()
content = msg['content']
markdown += f"## {role}\n\n{content}\n\n---\n\n"
return jsonify({
'status': 'success',
'markdown': markdown
})
if __name__ == '__main__':
# Check for API key
if not os.environ.get('ANTHROPIC_API_KEY'):
print("ERROR: ANTHROPIC_API_KEY environment variable not set!")
print("Please set it in your ~/.bashrc or run:")
print("export ANTHROPIC_API_KEY='your-key-here'")
exit(1)
# Run on all interfaces (0.0.0.0) so it's accessible from mobile
print("=" * 60)
print("Claude Code HTTP Gateway")
print("=" * 60)
print("Server starting on http://0.0.0.0:8080")
print("Access from mobile at: http://10.139.148.158:8080")
print("=" * 60)
app.run(host='0.0.0.0', port=8080, debug=True)