-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
331 lines (285 loc) · 12.8 KB
/
app.py
File metadata and controls
331 lines (285 loc) · 12.8 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from flask import Flask, request, jsonify, render_template_string
import subprocess
import json
import os
import re
from dotenv import load_dotenv
from groq import Groq
load_dotenv()
app = Flask(__name__)
with open("tools.json") as f:
tools_schema = json.load(f)
process = subprocess.Popen(
["python", "server.py"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True
)
conversation_history = []
def call_tool(tool_name, args):
request_data = {"tool": tool_name, "arguments": args}
process.stdin.write(json.dumps(request_data) + "\n")
process.stdin.flush()
return json.loads(process.stdout.readline())
def run_agent(user_message):
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
conversation_history.append({"role": "user", "content": user_message})
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "system",
"content": f"""You are an assistant that manages GitHub repos.
You have access to these tools: {json.dumps(tools_schema)}
IMPORTANT RULES:
- Use the EXACT repo name the user typed — never guess, correct, or substitute it.
- If ONE action needed: {{"tool": "tool_name", "arguments": {{}}}}
- If MULTIPLE actions needed: [{{"tool": "t1", "arguments": {{}}}}, {{"tool": "t2", "arguments": {{}}}}]
- NEVER put each JSON on a separate line — always use a single array.
- When editing a file, pass ONLY the new content the user specified — never add extra characters.
- NO extra text — ONLY JSON. Only plain text if no tool needed."""
},
*conversation_history
]
)
reply = response.choices[0].message.content.strip()
conversation_history.append({"role": "assistant", "content": reply})
# parse single or multi tool
try:
parsed = json.loads(reply)
if isinstance(parsed, list):
results = []
for action in parsed:
result = call_tool(action["tool"], action["arguments"])
results.append({"tool": action["tool"], "result": result})
conversation_history.append({"role": "user", "content": f"Tool results: {json.dumps(results)[:600]}"})
return format_results(results)
if isinstance(parsed, dict) and "tool" in parsed:
result = call_tool(parsed["tool"], parsed["arguments"])
conversation_history.append({"role": "user", "content": f"Tool result: {json.dumps(result)[:500]}"})
return format_result(parsed["tool"], result)
except:
pass
# fallback: extract JSON
match = re.search(r'\[.*\]|\{.*"tool".*\}', reply, re.DOTALL)
if match:
try:
parsed = json.loads(match.group())
if isinstance(parsed, list):
results = []
for action in parsed:
result = call_tool(action["tool"], action["arguments"])
results.append({"tool": action["tool"], "result": result})
return format_results(results)
result = call_tool(parsed["tool"], parsed["arguments"])
return format_result(parsed["tool"], result)
except:
pass
# fallback: newline separated
lines = [l.strip() for l in reply.strip().split('\n') if l.strip().startswith('{')]
if len(lines) > 1:
results = []
for line in lines:
try:
action = json.loads(line)
if "tool" in action:
result = call_tool(action["tool"], action["arguments"])
results.append({"tool": action["tool"], "result": result})
except:
continue
if results:
return format_results(results)
return reply
def format_result(tool, result):
if isinstance(result, dict):
if result.get("exists") is True:
return f"✅ Repo **{result['name']}** exists — {result.get('url', '')}"
if result.get("exists") is False:
return f"❌ Repo **{result['name']}** not found"
if "decoded_content" in result:
return f"📄 **{result.get('name', 'file')}**:\n```\n{result['decoded_content']}\n```"
if result.get("renamed") is True:
if "url" in result:
return f"✅ Repo renamed from **{result['from']}** to **{result['to']}** — {result['url']}"
return f"✅ File renamed from **{result['from']}** to **{result['to']}**"
if result.get("deleted") is True: # ✅ moved here inside dict block
if "file" in result:
return f"🗑️ File **{result['file']}** deleted from **{result['repo']}**"
return f"🗑️ Repo **{result['repo']}** deleted permanently"
if result.get("created") is True and "branch" in result: # ✅ moved here
return f"🌿 Branch **{result['branch']}** created in **{result['repo']}** from **{result['from']}**"
if "content" in result and "commit" in result:
name = result["content"]["name"]
url = result["content"]["html_url"]
sha = result["commit"]["sha"][:7]
return f"✅ **{name}** pushed successfully! [view on GitHub]({url}) — commit `{sha}`"
if "html_url" in result:
return f"✅ Done! [{result['name']}]({result['html_url']})"
if "error" in result:
return f"❌ Error: {result['error']}"
if isinstance(result, list):
lines = "\n".join([f"- {f['name']} ({f['size']} bytes)" for f in result])
return f"📁 Files:\n{lines}"
return str(result)
def format_results(results):
lines = []
for r in results:
lines.append(format_result(r["tool"], r["result"]))
return "\n".join(lines)
HTML = """
<!DOCTYPE html>
<html>
<head>
<title>GitHub MCP Agent</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: system-ui, sans-serif; background: #0d1117; color: #e6edf3; height: 100vh; display: flex; flex-direction: column; }
header { padding: 16px 24px; background: #161b22; border-bottom: 1px solid #30363d; display: flex; align-items: center; gap: 10px; }
header h1 { font-size: 16px; font-weight: 600; }
.badge { background: #238636; color: #fff; font-size: 11px; padding: 2px 8px; border-radius: 10px; }
#chat { flex: 1; overflow-y: auto; padding: 24px; display: flex; flex-direction: column; gap: 16px; }
.msg { max-width: 75%; padding: 12px 16px; border-radius: 12px; font-size: 14px; line-height: 1.6; white-space: pre-wrap; word-break: break-word; }
.user { align-self: flex-end; background: #1f6feb; color: #fff; border-radius: 12px 12px 2px 12px; }
.agent { align-self: flex-start; background: #161b22; border: 1px solid #30363d; border-radius: 12px 12px 12px 2px; }
.agent a { color: #58a6ff; }
footer { padding: 16px 24px; background: #161b22; border-top: 1px solid #30363d; display: flex; gap: 10px; }
#input { flex: 1; background: #0d1117; border: 1px solid #30363d; border-radius: 8px; padding: 10px 14px; color: #e6edf3; font-size: 14px; outline: none; }
#input:focus { border-color: #58a6ff; }
#send { background: #238636; color: #fff; border: none; border-radius: 8px; padding: 10px 20px; font-size: 14px; cursor: pointer; }
#send:hover { background: #2ea043; }
.typing { align-self: flex-start; color: #8b949e; font-size: 13px; }
.quick { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px 24px 12px; }
.quick button { background: #161b22; border: 1px solid #30363d; color: #8b949e; border-radius: 16px; padding: 4px 12px; font-size: 12px; cursor: pointer; }
.quick button:hover { border-color: #58a6ff; color: #58a6ff; }
#upload-panel { display: none; }
#upload-panel.open { display: flex !important; }
</style>
</head>
<body>
<header>
<span style="font-size:20px">⚡</span>
<h1>GitHub MCP Agent</h1>
<span class="badge">live</span>
</header>
<div id="chat">
<div class="msg agent">Hey! I'm your GitHub agent. Ask me anything — create repos, push files, list your projects...</div>
</div>
<div class="quick">
<button onclick="send('list all my repos')">list repos</button>
<button onclick="send('create a repo called test-web')">create repo</button>
<button onclick="send('check if test-repo exists')">check repo</button>
<button onclick="send('list files in github-mcp')">list files</button>
<button onclick="toggleUpload()">upload file</button>
</div>
<div id="upload-panel" style="padding: 10px 24px; background:#161b22; border-top: 1px solid #30363d; border-bottom: 1px solid #30363d; flex-direction: row; gap: 8px; align-items: center; flex-wrap: wrap;">
<input type="text" id="upload-repo" placeholder="repo name e.g. github-mcp"
style="background:#0d1117; border:1px solid #30363d; border-radius:8px; padding:8px 12px; color:#e6edf3; font-size:13px; width:200px; outline:none;"/>
<input type="file" id="upload-file" multiple
style="color:#8b949e; font-size:13px;"/>
<button onclick="uploadFiles()"
style="background:#238636; color:#fff; border:none; border-radius:8px; padding:8px 16px; font-size:13px; cursor:pointer;">
Push to GitHub
</button>
<button onclick="toggleUpload()"
style="background:transparent; color:#8b949e; border:1px solid #30363d; border-radius:8px; padding:8px 12px; font-size:13px; cursor:pointer;">
cancel
</button>
</div>
<footer>
<input id="input" placeholder="Ask me anything about your GitHub repos..." onkeydown="if(event.key==='Enter') send()"/>
<button id="send" onclick="send()">Send</button>
</footer>
<script>
const chat = document.getElementById('chat');
const input = document.getElementById('input');
function addMsg(text, role) {
const div = document.createElement('div');
div.className = 'msg ' + role;
div.innerHTML = text.replace(/\*\*(.*?)\*\*/g, '<b>$1</b>')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank">$1</a>')
.replace(/```([\s\S]*?)```/g, '<code style="background:#0d1117;padding:4px 8px;border-radius:4px;display:block;margin-top:6px">$1</code>');
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
}
function addTyping() {
const div = document.createElement('div');
div.className = 'typing';
div.id = 'typing';
div.textContent = 'Agent is thinking...';
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
}
function toggleUpload() {
const panel = document.getElementById('upload-panel');
panel.classList.toggle('open');
}
async function send(text) {
const msg = text || input.value.trim();
if (!msg) return;
input.value = '';
addMsg(msg, 'user');
addTyping();
const res = await fetch('/chat', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({message: msg})
});
const data = await res.json();
document.getElementById('typing')?.remove();
addMsg(data.reply, 'agent');
}
async function uploadFiles() {
const repo = document.getElementById('upload-repo').value.trim();
const files = document.getElementById('upload-file').files;
if (!repo) { addMsg('❌ Please enter a repo name', 'agent'); return; }
if (files.length === 0) { addMsg('❌ Please select files', 'agent'); return; }
toggleUpload();
addMsg(`Uploading ${files.length} file(s) to ${repo}...`, 'user');
addTyping();
const formData = new FormData();
formData.append('repo', repo);
for (const file of files) {
formData.append('files', file, file.webkitRelativePath || file.name);
}
const res = await fetch('/upload-folder', {
method: 'POST',
body: formData
});
const data = await res.json();
document.getElementById('typing')?.remove();
data.results.forEach(r => addMsg(r, 'agent'));
}
</script>
</body>
</html>
"""
@app.route('/')
def index():
return render_template_string(HTML)
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
user_message = data.get('message', '')
reply = run_agent(user_message)
return jsonify({"reply": reply})
@app.route('/upload-folder', methods=['POST'])
def upload_folder():
repo = request.form.get('repo')
files = request.files.getlist('files')
if not files or not repo:
return jsonify({"results": ["❌ Missing files or repo name"]})
results = []
for file in files:
filename = file.filename
content = file.read().decode('utf-8', errors='replace')
result = call_tool("push_file", {
"repo": repo,
"path": filename,
"content": content,
"message": f"uploaded {filename} via MCP agent"
})
results.append(format_result("push_file", result))
return jsonify({"results": results})
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)