-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
137 lines (104 loc) · 3.19 KB
/
mcp_server.py
File metadata and controls
137 lines (104 loc) · 3.19 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
from mcp.server.fastmcp import FastMCP
import subprocess
import requests
from pathlib import Path
import os
import sys
from dotenv import load_dotenv
import ollama
load_dotenv()
mcp = FastMCP("website-tools")
BASE_DIR = Path(__file__).parent
SITE_DIR = BASE_DIR / "site"
REPO_DIR = BASE_DIR
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
TELEGRAM_CHAT_IDS = os.getenv("TELEGRAM_CHAT_IDS", "")
def log(msg):
"""Log only to stderr (never stdout in MCP servers)."""
print(msg, file=sys.stderr)
def run_git(cmd):
"""Run git command safely."""
try:
subprocess.run(
cmd,
cwd=REPO_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True
)
except subprocess.CalledProcessError as e:
log(f"Git error: {e.stderr}")
raise
def send_notification(message):
"""Send Telegram message to multiple recipients."""
if not TELEGRAM_TOKEN or not TELEGRAM_CHAT_IDS:
log("Telegram configuration missing")
return
chat_ids = [c.strip() for c in TELEGRAM_CHAT_IDS.split(",") if c.strip()]
for chat_id in chat_ids:
try:
requests.post(
f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage",
json={
"chat_id": chat_id,
"text": message
},
timeout=5
)
except Exception as e:
log(f"Telegram send failed for {chat_id}: {e}")
def deploy(path):
"""Commit and push changes."""
try:
run_git(["git", "add", str(path)])
try:
run_git(["git", "commit", "-m", "AI website update"])
except:
log("Nothing new to commit")
run_git(["git", "push"])
send_notification("🚀 Website updated and deployed successfully")
except Exception as e:
log(f"Deployment failed: {e}")
raise
def generate_html(prompt):
"""Use local LLM to generate HTML."""
try:
response = ollama.chat(
model="llama3.1:8b",
messages=[
{
"role": "system",
"content": "Generate a clean simple HTML homepage. Return only valid HTML."
},
{
"role": "user",
"content": prompt
}
]
)
return response["message"]["content"]
except Exception as e:
log(f"Ollama generation failed: {e}")
raise
@mcp.tool()
def update_homepage_from_prompt(prompt: str):
"""
Generate HTML from a natural-language prompt,
update index.html, push to GitHub, and notify Telegram users.
"""
try:
html = generate_html(prompt)
path = SITE_DIR / "index.html"
if not SITE_DIR.exists():
return "Site directory not found"
with open(path, "w", encoding="utf-8") as f:
f.write(html)
deploy(path)
return "Homepage updated from prompt and deployed."
except Exception as e:
log(e)
return f"Update failed: {str(e)}"
if __name__ == "__main__":
log("Starting MCP server")
mcp.run()