-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
541 lines (433 loc) · 17.9 KB
/
server.py
File metadata and controls
541 lines (433 loc) · 17.9 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/bin/env python3
"""
Module 3: Slack Notification Integration - Complete Solution
Combines all MCP primitives (Tools and Prompts) for complete team communication workflows.
"""
import json
import os
import subprocess
import requests
from typing import Optional
from pathlib import Path
# Load environment variables from .env file
try:
from dotenv import load_dotenv
from pathlib import Path
# Get the directory where this script is located
script_dir = Path(__file__).parent
env_file = script_dir / ".env"
load_dotenv(env_file)
print(f"🔑 Environment loaded from {env_file}. SLACK_WEBHOOK_URL present: {'SLACK_WEBHOOK_URL' in os.environ}")
except ImportError:
# python-dotenv not installed, that's fine
print("⚠️ python-dotenv not available")
pass
from mcp.server.fastmcp import FastMCP
# Initialize the FastMCP server
mcp = FastMCP("pr-agent-slack")
# PR template directory (in the same project)
TEMPLATES_DIR = Path(__file__).parent / "templates"
# Default PR templates
DEFAULT_TEMPLATES = {
"bug.md": "Bug Fix",
"feature.md": "Feature",
"docs.md": "Documentation",
"refactor.md": "Refactor",
"test.md": "Test",
"performance.md": "Performance",
"security.md": "Security"
}
# File where webhook server stores events
EVENTS_FILE = Path(__file__).parent / "github_events.json"
# Type mapping for PR templates
TYPE_MAPPING = {
"bug": "bug.md",
"fix": "bug.md",
"feature": "feature.md",
"enhancement": "feature.md",
"docs": "docs.md",
"documentation": "docs.md",
"refactor": "refactor.md",
"cleanup": "refactor.md",
"test": "test.md",
"testing": "test.md",
"performance": "performance.md",
"optimization": "performance.md",
"security": "security.md"
}
# ===== Tools from Modules 1 & 2 (Complete with output limiting) =====
@mcp.tool()
async def analyze_file_changes(
base_branch: str = "main",
include_diff: bool = True,
max_diff_lines: int = 500,
working_directory: Optional[str] = None
) -> str:
"""Get the full diff and list of changed files in the current git repository.
Args:
base_branch: Base branch to compare against (default: main)
include_diff: Include the full diff content (default: true)
max_diff_lines: Maximum number of diff lines to include (default: 500)
working_directory: Directory to run git commands in (default: current directory)
"""
try:
# Try to get working directory from roots first
if working_directory is None:
try:
context = mcp.get_context()
roots_result = await context.session.list_roots()
# Get the first root - Claude Code sets this to the CWD
root = roots_result.roots[0]
# FileUrl object has a .path property that gives us the path directly
working_directory = root.uri.path
except Exception:
# If we can't get roots, fall back to current directory
pass
# Use provided working directory or current directory
cwd = working_directory if working_directory else os.getcwd()
# Get list of changed files
files_result = subprocess.run(
["git", "diff", "--name-status", f"{base_branch}...HEAD"],
capture_output=True,
text=True,
check=True,
cwd=cwd
)
# Get diff statistics
stat_result = subprocess.run(
["git", "diff", "--stat", f"{base_branch}...HEAD"],
capture_output=True,
text=True,
cwd=cwd
)
# Get the actual diff if requested
diff_content = ""
truncated = False
if include_diff:
diff_result = subprocess.run(
["git", "diff", f"{base_branch}...HEAD"],
capture_output=True,
text=True,
cwd=cwd
)
diff_lines = diff_result.stdout.split('\n')
# Check if we need to truncate
if len(diff_lines) > max_diff_lines:
diff_content = '\n'.join(diff_lines[:max_diff_lines])
diff_content += f"\n\n... Output truncated. Showing {max_diff_lines} of {len(diff_lines)} lines ..."
diff_content += "\n... Use max_diff_lines parameter to see more ..."
truncated = True
else:
diff_content = diff_result.stdout
# Get commit messages for context
commits_result = subprocess.run(
["git", "log", "--oneline", f"{base_branch}..HEAD"],
capture_output=True,
text=True,
cwd=cwd
)
analysis = {
"base_branch": base_branch,
"files_changed": files_result.stdout,
"statistics": stat_result.stdout,
"commits": commits_result.stdout,
"diff": diff_content if include_diff else "Diff not included (set include_diff=true to see full diff)",
"truncated": truncated,
"total_diff_lines": len(diff_lines) if include_diff else 0
}
return json.dumps(analysis, indent=2)
except subprocess.CalledProcessError as e:
return json.dumps({"error": f"Git error: {e.stderr}"})
except Exception as e:
return json.dumps({"error": str(e)})
@mcp.tool()
async def get_pr_templates() -> str:
"""List available PR templates with their content."""
try:
templates = []
for filename, template_type in DEFAULT_TEMPLATES.items():
template_path = TEMPLATES_DIR / filename
if not template_path.exists():
return json.dumps({"error": f"Template file not found: {template_path}"})
try:
content = template_path.read_text(encoding='utf-8')
templates.append({
"filename": filename,
"type": template_type,
"content": content
})
except Exception as read_error:
return json.dumps({"error": f"Failed to read {filename}: {str(read_error)}"})
return json.dumps(templates, indent=2)
except Exception as e:
return json.dumps({"error": f"Error in get_pr_templates: {str(e)}", "templates_dir": str(TEMPLATES_DIR)})
@mcp.tool()
async def suggest_template(changes_summary: str, change_type: str) -> str:
"""Let Claude analyze the changes and suggest the most appropriate PR template.
Args:
changes_summary: Your analysis of what the changes do
change_type: The type of change you've identified (bug, feature, docs, refactor, test, etc.)
"""
try:
# Get available templates
templates_response = await get_pr_templates()
# Check if templates loading failed
templates_data = json.loads(templates_response)
if isinstance(templates_data, dict) and "error" in templates_data:
return json.dumps({"error": f"Failed to load templates: {templates_data['error']}"})
templates = templates_data
# Find matching template
template_file = TYPE_MAPPING.get(change_type.lower(), "feature.md")
selected_template = next(
(t for t in templates if t["filename"] == template_file),
templates[0] if templates else None # Default to first template if no match
)
if not selected_template:
return json.dumps({"error": "No templates available"})
suggestion = {
"recommended_template": selected_template,
"reasoning": f"Based on your analysis: '{changes_summary}', this appears to be a {change_type} change.",
"template_content": selected_template["content"],
"usage_hint": "Claude can help you fill out this template based on the specific changes in your PR."
}
return json.dumps(suggestion, indent=2)
except Exception as e:
return json.dumps({"error": f"Error in suggest_template: {str(e)}"})
@mcp.tool()
async def get_recent_actions_events(limit: int = 10) -> str:
"""Get recent GitHub Actions events received via webhook.
Args:
limit: Maximum number of events to return (default: 10)
"""
# Read events from file
if not EVENTS_FILE.exists():
return json.dumps([])
with open(EVENTS_FILE, 'r') as f:
events = json.load(f)
# Return most recent events
recent = events[-limit:]
return json.dumps(recent, indent=2)
@mcp.tool()
async def get_workflow_status(workflow_name: Optional[str] = None) -> str:
"""Get the current status of GitHub Actions workflows.
Args:
workflow_name: Optional specific workflow name to filter by
"""
# Read events from file
if not EVENTS_FILE.exists():
return json.dumps({"message": "No GitHub Actions events received yet"})
with open(EVENTS_FILE, 'r') as f:
events = json.load(f)
if not events:
return json.dumps({"message": "No GitHub Actions events received yet"})
# Filter for workflow events
workflow_events = [
e for e in events
if e.get("workflow_run") is not None
]
if workflow_name:
workflow_events = [
e for e in workflow_events
if e["workflow_run"].get("name") == workflow_name
]
# Group by workflow and get latest status
workflows = {}
for event in workflow_events:
run = event["workflow_run"]
name = run["name"]
if name not in workflows or run["updated_at"] > workflows[name]["updated_at"]:
workflows[name] = {
"name": name,
"status": run["status"],
"conclusion": run.get("conclusion"),
"run_number": run["run_number"],
"updated_at": run["updated_at"],
"html_url": run["html_url"]
}
return json.dumps(list(workflows.values()), indent=2)
# ===== New Module 3: Slack Integration Tools =====
@mcp.tool()
async def send_slack_notification(message: str) -> str:
"""Send a formatted notification to the team Slack channel.
Args:
message: The message to send to Slack (supports Slack markdown)
IMPORTANT: For CI failures, use format_ci_failure_alert prompt first!
IMPORTANT: For deployments, use format_ci_success_summary prompt first!
"""
webhook_url = os.getenv("SLACK_WEBHOOK_URL")
if not webhook_url:
# Debug: List all environment variables that start with SLACK
slack_vars = {k: v for k, v in os.environ.items() if k.startswith("SLACK")}
return f"Error: SLACK_WEBHOOK_URL environment variable not set. Found env vars: {list(slack_vars.keys())}"
try:
# Prepare the payload with proper Slack formatting
payload = {
"text": message,
"mrkdwn": True
}
# Send POST request to Slack webhook
response = requests.post(
webhook_url,
json=payload,
timeout=10
)
# Check if request was successful
if response.status_code == 200:
return "✅ Message sent successfully to Slack"
else:
return f"❌ Failed to send message. Status: {response.status_code}, Response: {response.text}"
except requests.exceptions.Timeout:
return "❌ Request timed out. Check your internet connection and try again."
except requests.exceptions.ConnectionError:
return "❌ Connection error. Check your internet connection and webhook URL."
except Exception as e:
return f"❌ Error sending message: {str(e)}"
# ===== New Module 3: Slack Formatting Prompts =====
@mcp.prompt()
async def format_ci_failure_alert():
"""Create a Slack alert for CI/CD failures with rich formatting."""
return """Format this GitHub Actions failure as a Slack message using ONLY Slack markdown syntax:
:rotating_light: *CI Failure Alert* :rotating_light:
A CI workflow has failed:
*Workflow*: workflow_name
*Branch*: branch_name
*Status*: Failed
*View Details*: <https://github.com/test/repo/actions/runs/123|View Logs>
Please check the logs and address any issues.
CRITICAL: Use EXACT Slack link format: <https://full-url|Link Text>
Examples:
- CORRECT: <https://github.com/user/repo|Repository>
- WRONG: [Repository](https://github.com/user/repo)
- WRONG: https://github.com/user/repo
Slack formatting rules:
- *text* for bold (NOT **text**)
- `text` for code
- > text for quotes
- Use simple bullet format without special characters
- :emoji_name: for emojis"""
@mcp.prompt()
async def format_ci_success_summary():
"""Create a Slack message celebrating successful deployments."""
return """Format this successful GitHub Actions run as a Slack message using ONLY Slack markdown syntax:
:white_check_mark: *Deployment Successful* :white_check_mark:
Deployment completed successfully for [Repository Name]
*Changes:*
- Key feature or fix 1
- Key feature or fix 2
*Links:*
<https://github.com/user/repo|View Changes>
CRITICAL: Use EXACT Slack link format: <https://full-url|Link Text>
Examples:
- CORRECT: <https://github.com/user/repo|Repository>
- WRONG: [Repository](https://github.com/user/repo)
- WRONG: https://github.com/user/repo
Slack formatting rules:
- *text* for bold (NOT **text**)
- `text` for code
- > text for quotes
- Use simple bullet format with - or *
- :emoji_name: for emojis"""
# ===== Prompts from Module 2 (Complete) =====
@mcp.prompt()
async def analyze_ci_results():
"""Analyze recent CI/CD results and provide insights."""
return """Please analyze the recent CI/CD results from GitHub Actions:
1. First, call get_recent_actions_events() to fetch the latest CI/CD events
2. Then call get_workflow_status() to check current workflow states
3. Identify any failures or issues that need attention
4. Provide actionable next steps based on the results
Format your response as:
## CI/CD Status Summary
- *Overall Health*: [Good/Warning/Critical]
- *Failed Workflows*: [List any failures with links]
- *Successful Workflows*: [List recent successes]
- *Recommendations*: [Specific actions to take]
- *Trends*: [Any patterns you notice]"""
@mcp.prompt()
async def create_deployment_summary():
"""Generate a deployment summary for team communication."""
return """Create a deployment summary for team communication:
1. Check workflow status with get_workflow_status()
2. Look specifically for deployment-related workflows
3. Note the deployment outcome, timing, and any issues
Format as a concise message suitable for Slack:
🚀 *Deployment Update*
- *Status*: [✅ Success / ❌ Failed / ⏳ In Progress]
- *Environment*: [Production/Staging/Dev]
- *Version/Commit*: [If available from workflow data]
- *Duration*: [If available]
- *Key Changes*: [Brief summary if available]
- *Issues*: [Any problems encountered]
- *Next Steps*: [Required actions if failed]
Keep it brief but informative for team awareness."""
@mcp.prompt()
async def generate_pr_status_report():
"""Generate a comprehensive PR status report including CI/CD results."""
return """Generate a comprehensive PR status report:
1. Use analyze_file_changes() to understand what changed
2. Use get_workflow_status() to check CI/CD status
3. Use suggest_template() to recommend the appropriate PR template
4. Combine all information into a cohesive report
Create a detailed report with:
## 📋 PR Status Report
### 📝 Code Changes
- *Files Modified*: [Count by type - .py, .js, etc.]
- *Change Type*: [Feature/Bug/Refactor/etc.]
- *Impact Assessment*: [High/Medium/Low with reasoning]
- *Key Changes*: [Bullet points of main modifications]
### 🔄 CI/CD Status
- *All Checks*: [✅ Passing / ❌ Failing / ⏳ Running]
- *Test Results*: [Pass rate, failed tests if any]
- *Build Status*: [Success/Failed with details]
- *Code Quality*: [Linting, coverage if available]
### 📌 Recommendations
- *PR Template*: [Suggested template and why]
- *Next Steps*: [What needs to happen before merge]
- *Reviewers*: [Suggested reviewers based on files changed]
### ⚠️ Risks & Considerations
- [Any deployment risks]
- [Breaking changes]
- [Dependencies affected]"""
@mcp.prompt()
async def troubleshoot_workflow_failure():
"""Help troubleshoot a failing GitHub Actions workflow."""
return """Help troubleshoot failing GitHub Actions workflows:
1. Use get_recent_actions_events() to find recent failures
2. Use get_workflow_status() to see which workflows are failing
3. Analyze the failure patterns and timing
4. Provide systematic troubleshooting steps
Structure your response as:
## 🔧 Workflow Troubleshooting Guide
### ❌ Failed Workflow Details
- *Workflow Name*: [Name of failing workflow]
- *Failure Type*: [Test/Build/Deploy/Lint]
- *First Failed*: [When did it start failing]
- *Failure Rate*: [Intermittent or consistent]
### 🔍 Diagnostic Information
- *Error Patterns*: [Common error messages or symptoms]
- *Recent Changes*: [What changed before failures started]
- *Dependencies*: [External services or resources involved]
### 💡 Possible Causes (ordered by likelihood)
1. *[Most Likely]*: [Description and why]
2. *[Likely]*: [Description and why]
3. *[Possible]*: [Description and why]
### ✅ Suggested Fixes
**Immediate Actions:**
- [ ] [Quick fix to try first]
- [ ] [Second quick fix]
**Investigation Steps:**
- [ ] [How to gather more info]
- [ ] [Logs or data to check]
**Long-term Solutions:**
- [ ] [Preventive measure]
- [ ] [Process improvement]
### 📚 Resources
- [Relevant documentation links]
- [Similar issues or solutions]"""
if __name__ == "__main__":
# Run MCP server normally
print("Starting PR Agent Slack MCP server...")
print("Make sure to set SLACK_WEBHOOK_URL environment variable")
print("To receive GitHub webhooks, run the webhook server separately:")
print(" python webhook_server.py")
mcp.run()