forked from kingdonb/mecris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
1722 lines (1489 loc) · 77.2 KB
/
Copy pathmcp_server.py
File metadata and controls
1722 lines (1489 loc) · 77.2 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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Mecris MCP Server - Personal LLM Accountability System
This version is refactored to use the MCP Python SDK for stdio communication with the Handler Pattern.
"""
import os
import logging
import asyncio
import sys
from datetime import datetime, timedelta, date, timezone
from typing import List, Dict, Any, Optional
from dotenv import load_dotenv
from mcp.server.fastmcp import FastMCP
from fastapi import FastAPI, Depends, HTTPException, Security
from fastapi.middleware.cors import CORSMiddleware
from services.auth_service import get_current_user, is_standalone_mode
from obsidian_client import ObsidianMCPClient
from beeminder_client import BeeminderClient
from usage_tracker import UsageTracker, get_budget_status as get_budget_status_from_tracker, record_usage, update_remaining_budget, get_goals, complete_goal as complete_goal_from_tracker, add_goal as add_goal_from_tracker
from virtual_budget_manager import VirtualBudgetManager
from billing_reconciliation import BillingReconciliation
from groq_odometer_tracker import get_groq_context_for_narrator, get_groq_reminder_status, record_groq_reading as record_groq_reading_from_tracker
from twilio_sender import smart_send_message, send_sms
from scripts.anthropic_cost_tracker import AnthropicCostTracker
from scripts.clozemaster_scraper import sync_clozemaster_to_beeminder
from services.weather_service import WeatherService
from services.neon_sync_checker import NeonSyncChecker
from services.reminder_service import ReminderService
from services.language_sync_service import LanguageSyncService
from services.review_pump import ReviewPump, ARABIC_POINTS_PER_CARD
from services.credentials_manager import credentials_manager
from ghost.presence import get_neon_store, StatusType
from services.rag_retriever import RAGRetriever
from services.rag_generator import generate_answer as _rag_generate
from tools.chrome_bookmarks import get_bookmarks_by_topic as _get_bookmarks_by_topic
from services.semantic_index import BookmarkIndex, search_bookmarks as _search_bookmarks
# Load environment variables
load_dotenv()
# Configure logging to stderr with ERROR level
logging.basicConfig(
level=logging.ERROR,
stream=sys.stderr,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("mecris")
async def _record_presence(user_id: str) -> None:
"""Record ACTIVE_HUMAN presence for user_id. No-op when Neon is unavailable."""
store = get_neon_store()
if store is None:
return
try:
await asyncio.to_thread(store.upsert, user_id, StatusType.ACTIVE_HUMAN, "mcp_server")
except Exception as e:
logger.warning(f"Presence record failed (non-fatal): {e}")
async def _get_presence_summary(user_id: str) -> Dict[str, Any]:
"""Return a summary of presence data for user_id, including heartbeat info."""
store = get_neon_store()
if store is None:
return {"status": "unknown", "error": "Neon store unavailable"}
try:
record = await asyncio.to_thread(store.get, user_id)
if not record:
return {"status": "none"}
now = datetime.now(timezone.utc)
# Calculate human age if possible
human_age = None
if record.last_human_activity:
human_age = (now - record.last_human_activity.replace(tzinfo=timezone.utc)).total_seconds()
# Calculate ghost age if possible
ghost_age = None
if record.last_ghost_activity:
ghost_age = (now - record.last_ghost_activity.replace(tzinfo=timezone.utc)).total_seconds()
return {
"status": record.status_type.value,
"source": record.source,
"last_active": record.last_active.isoformat() if record.last_active else None,
"last_human_activity": record.last_human_activity.isoformat() if record.last_human_activity else None,
"last_ghost_activity": record.last_ghost_activity.isoformat() if record.last_ghost_activity else None,
"human_age_seconds": human_age,
"ghost_age_seconds": ghost_age
}
except Exception as e:
return {"status": "error", "error": str(e)}
# Initialize the MCP Server
mcp = FastMCP("mecris")
# Create FastAPI app for HTTP endpoints
app = FastAPI(title="Mecris API")
# Add CORS middleware for Web UI development
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
async def get_authorized_user(user_id: Optional[str] = Depends(get_current_user)):
"""FastAPI Dependency: Enforces authentication and resolves target user ID."""
mode = os.getenv("MECRIS_MODE", "standalone")
# Cloud Cron Exception (Akamai/Fermyon): Special handling for unauthenticated triggers
# logic omitted here for brevity but handled in endpoint logic if needed
if user_id:
return user_id
if mode == "standalone":
# In standalone/trusted mode, we allow fallback to the local default user
resolved_id = credentials_manager.resolve_user_id()
if resolved_id:
return resolved_id
# In multi-tenant mode, or if no local user could be resolved: REJECT.
print(f"AUTH FAILURE: No valid user_id found (mode={mode})", file=sys.stderr)
raise HTTPException(status_code=401, detail="Authentication Required")
@app.get("/health")
async def health_check():
# Check Neon connectivity
neon_active = False
try:
import psycopg2
conn = psycopg2.connect(os.getenv("NEON_DB_URL"))
conn.close()
neon_active = True
except: pass
return {
"status": "healthy",
"home_server_active": True,
"neon_connected": neon_active,
"leader_pid": scheduler.process_id if scheduler else "unknown",
"last_seen": datetime.now(timezone.utc).isoformat()
}
@app.post("/walks")
async def upload_walk(walk_data: Dict[str, Any], user_id: str = Depends(get_authorized_user)):
try:
import psycopg2
neon_url = os.getenv("NEON_DB_URL")
# Encrypt gps_route_points if present and encryption is active
gps_points = str(walk_data.get("gps_route_points", "0"))
if gps_points != "0":
gps_points = usage_tracker.encryption.try_encrypt(gps_points)
with psycopg2.connect(neon_url) as conn:
with conn.cursor() as cur:
# Mirror Rust logic: Insert as 'logging' status
cur.execute("""
INSERT INTO walk_inferences
(user_id, start_time, end_time, step_count, distance_meters, distance_source, confidence_score, gps_route_points, status)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, 'logging')
ON CONFLICT (user_id, start_time) DO UPDATE SET
end_time = EXCLUDED.end_time,
step_count = EXCLUDED.step_count,
distance_meters = EXCLUDED.distance_meters,
gps_route_points = EXCLUDED.gps_route_points,
status = 'logging'
""", (
user_id,
walk_data.get("start_time"),
walk_data.get("end_time"),
walk_data.get("step_count"),
walk_data.get("distance_meters"),
walk_data.get("distance_source"),
walk_data.get("confidence_score", 0.9),
gps_points,
'logging'
))
# Trigger immediate sync for this user
asyncio.create_task(_global_walk_sync_job(user_id))
return {"status": "success", "message": "Walk ingested and sync triggered"}
except Exception as e:
logger.error(f"Failed to ingest walk: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/languages")
async def get_languages(user_id: str = Depends(get_authorized_user)):
# Use the unified velocity calculator to get enriched stats (absolute_target, target_flow_rate, goal_met)
stats_dict = await get_language_velocity_stats(user_id)
lang_list = []
for name, data in stats_dict.items():
# Mirror Android/Spec: Hide inactive languages that don't have an active Beeminder goal
# (exception for Arabic which is always prioritized)
has_goal = bool(data.get("beeminder_slug")) or name.lower() == "arabic"
# If lever is 'No Goal (Unplanned)' and it's not arabic, skip it to reduce noise
if not has_goal and data.get("lever_name") == "No Goal (Unplanned)":
continue
lang_list.append({
"name": name,
"current": data.get("debt_remaining", 0),
"tomorrow": data.get("tomorrow_liability", 0),
"next_7_days": data.get("next_7_days", 0),
"daily_rate": 0.0, # deprecated
"safebuf": data.get("safebuf", 0),
"derail_risk": "STABLE",
"pump_multiplier": data.get("multiplier", 1.0),
"has_goal": has_goal,
"daily_completions": data.get("current_flow_rate", 0),
"target_flow_rate": data.get("target_flow_rate", 0),
"absolute_target": data.get("absolute_target", 0),
"goal_met": data.get("goal_met", False)
})
return {"languages": lang_list}
@app.post("/languages/multiplier")
async def update_multiplier(request: Dict[str, Any], user_id: str = Depends(get_authorized_user)):
name = request.get("name")
multiplier = request.get("multiplier")
if not name or multiplier is None:
raise HTTPException(status_code=400, detail="Missing name or multiplier")
await set_review_pump_lever(name, multiplier, user_id)
return {"status": "success"}
@app.get("/aggregate-status")
async def get_aggregate_status_endpoint(user_id: str = Depends(get_authorized_user)):
status = await get_daily_aggregate_status(user_id)
return status
@app.post("/heartbeat")
async def post_heartbeat(data: Dict[str, Any], user_id: str = Depends(get_authorized_user)):
role = data.get("role", "android_client")
process_id = data.get("process_id", "unknown")
# scheduler manages the election table
await asyncio.to_thread(scheduler._update_heartbeat, role, process_id, user_id)
return {"status": "success", "mcp_server_active": True}
@app.post("/internal/cloud-sync", status_code=202)
async def trigger_cloud_sync_endpoint(user_id: str = Depends(get_authorized_user)):
logger.info(f"Android app triggered cloud sync for {user_id}")
# Run the slow sync in a background task
async def run_sync():
try:
await language_sync_service.sync_all(user_id=user_id)
logger.info(f"Background cloud sync complete for {user_id}")
except Exception as e:
logger.error(f"Background cloud sync failed for {user_id}: {e}")
asyncio.create_task(run_sync())
return {
"status": "accepted",
"message": "Cloud sync started in background. Please check /languages in a few moments for updates."
}
@app.post("/intelligent-reminder/trigger")
async def trigger_reminder_endpoint(user_id: str = Depends(get_authorized_user)):
return await trigger_reminder_check(user_id)
@app.get("/narrator/context")
async def narrator_context_endpoint(user_id: str = Depends(get_authorized_user)):
return await get_narrator_context(user_id)
@app.get("/beeminder/status")
async def beeminder_status_endpoint(user_id: str = Depends(get_authorized_user)):
return await get_beeminder_status(user_id)
@app.get("/budget/status")
async def budget_status_endpoint(user_id: str = Depends(get_authorized_user)):
return get_budget_status(user_id)
# Mount the MCP server's ASGI app
# This allows the same process to serve both the MCP protocol (via stdio or SSE)
# and custom HTTP endpoints.
app.mount("/mcp", mcp.sse_app())
from scheduler import MecrisScheduler, _global_walk_sync_job
# Initialize clients
obsidian_client = ObsidianMCPClient()
default_beeminder_client = BeeminderClient()
neon_checker = NeonSyncChecker()
language_sync_service = LanguageSyncService(default_beeminder_client)
# Trackers will use DEFAULT_USER_ID from env if not specified
usage_tracker = UsageTracker()
virtual_budget_manager = VirtualBudgetManager()
billing_reconciler = BillingReconciliation()
weather_service = WeatherService()
scheduler = MecrisScheduler()
try:
anthropic_cost_tracker = AnthropicCostTracker()
except Exception as e:
logger.warning(f"Failed to initialize AnthropicCostTracker: {e}")
anthropic_cost_tracker = None
# --- Cache Implementation ---
daily_activity_cache = {}
# Multi-tenant cache: {user_id: {"data": [...], "cache_expires": ...}}
beeminder_goals_cache: Dict[str, Dict[str, Any]] = {}
def get_user_beeminder_client(user_id: str = None) -> BeeminderClient:
"""Return a BeeminderClient for the specific user."""
target_user_id = usage_tracker.resolve_user_id(user_id)
return BeeminderClient(user_id=target_user_id)
async def get_cached_beeminder_goals(user_id: str = None) -> List[Dict[str, Any]]:
"""Get Beeminder goals with 30-minute cache per user."""
target_user_id = usage_tracker.resolve_user_id(user_id)
now = datetime.now()
user_cache = beeminder_goals_cache.get(target_user_id, {})
if ("data" in user_cache and "cache_expires" in user_cache and now < user_cache["cache_expires"]):
return user_cache["data"]
try:
client = get_user_beeminder_client(target_user_id)
goals_data = await client.get_all_goals()
beeminder_goals_cache[target_user_id] = {
"data": goals_data,
"last_check": now,
"cache_expires": now + timedelta(minutes=30)
}
return goals_data
except Exception as e:
logger.error(f"Failed to fetch Beeminder goals for user {target_user_id}: {e}")
return user_cache.get("data", [])
async def get_cached_daily_activity(goal_slug: str = "bike", user_id: str = None) -> Dict[str, Any]:
"""Get daily activity status with 15-minute cache (refreshed for Cloud sync)."""
target_user_id = usage_tracker.resolve_user_id(user_id)
import zoneinfo
eastern = zoneinfo.ZoneInfo("US/Eastern")
local_now = datetime.now(eastern)
today_str = local_now.strftime("%Y-%m-%d")
cache_key = f"{target_user_id}:{goal_slug}:{today_str}"
if cache_key in daily_activity_cache:
cache_entry = daily_activity_cache[cache_key]
if local_now < cache_entry["cache_expires"]:
return {
"goal_slug": goal_slug,
"has_activity_today": cache_entry["has_activity_today"],
"status": "completed" if cache_entry["has_activity_today"] else "needed",
"source": cache_entry.get("source", "cache"),
"cached": True
}
try:
# Phase 2: Check Neon Cloud DB first for 'bike' (walks)
if goal_slug == "bike":
has_walk = await asyncio.to_thread(neon_checker.has_walk_today, target_user_id)
if has_walk:
latest = await asyncio.to_thread(neon_checker.get_latest_walk, target_user_id)
walk_info = f" (Steps: {latest['step_count']})" if latest else ""
activity_status = {
"goal_slug": goal_slug,
"has_activity_today": True,
"status": "completed",
"check_time": local_now.isoformat(),
"message": f"✅ Walk detected in Cloud Sync (Neon){walk_info}",
"source": "neon_cloud"
}
daily_activity_cache[cache_key] = {
"last_check": local_now,
"has_activity_today": True,
"cache_expires": local_now + timedelta(minutes=15),
"source": "neon_cloud"
}
activity_status["cached"] = False
return activity_status
# Fallback to Beeminder (Legacy or non-walk goals)
client = get_user_beeminder_client(target_user_id)
activity_status = await client.get_daily_activity_status(goal_slug)
daily_activity_cache[cache_key] = {
"last_check": local_now,
"has_activity_today": activity_status["has_activity_today"],
"cache_expires": local_now + timedelta(minutes=15 if goal_slug == "bike" else 60),
"source": "beeminder"
}
activity_status["cached"] = False
activity_status["source"] = "beeminder"
return activity_status
except Exception as e:
logger.error(f"Failed to fetch daily activity for {goal_slug}: {e}")
if cache_key in daily_activity_cache:
return {"goal_slug": goal_slug, "has_activity_today": daily_activity_cache[cache_key]["has_activity_today"], "status": "stale", "error": str(e)}
return {"goal_slug": goal_slug, "has_activity_today": False, "status": "unknown", "error": str(e)}
# --- Tool Implementations ---
def _enrich_bookmarks_for_narrator(goals: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Build a BookmarkIndex once and search using active goal titles (sync, run via to_thread).
Returns up to 5 deduplicated bookmark matches annotated with which goal_slug triggered them.
Returns an empty list gracefully when no bookmarks file is present (e.g. CI).
Plan: yebyen/mecris#281 / kingdonb/mecris#208
"""
from tools.chrome_bookmarks import _default_bookmarks_path, flatten_bookmarks, load_bookmarks
path = _default_bookmarks_path()
raw = load_bookmarks(path)
if not raw:
return []
all_bm = flatten_bookmarks(raw)
index = BookmarkIndex()
index.fit(all_bm)
# Query most at-risk goals first
sorted_goals = sorted(goals, key=lambda g: (
0 if g.get("derail_risk") == "CRITICAL" else
1 if g.get("derail_risk") in ("WARNING", "CAUTION") else 2
))
seen_urls: set = set()
results: List[Dict[str, Any]] = []
for goal in sorted_goals[:5]:
query = goal.get("title") or goal.get("slug", "")
if not query:
continue
for match in index.search(query, top_k=2):
url = match.get("url", "")
if url and url not in seen_urls:
seen_urls.add(url)
results.append({**match, "goal_slug": goal.get("slug")})
if len(results) >= 5:
return results
return results
@mcp.tool(description="Get unified strategic context with goals, budget, and recommendations.")
async def get_narrator_context(user_id: str = None) -> Dict[str, Any]:
"""Get unified strategic context with goals, budget, and recommendations."""
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {
"error": "Authentication Required",
"instruction": "Please run `mecris login` in your terminal to authenticate."
}
await _record_presence(target_user_id)
try:
goals = usage_tracker.get_goals(target_user_id)
active_goals = [g for g in goals if g.get("status") == "active"]
try:
todos = await obsidian_client.get_todos()
except Exception as e:
logger.warning(f"Failed to fetch Obsidian todos (server may be offline): {e}")
todos = []
try:
beeminder_goals = await get_cached_beeminder_goals(target_user_id)
except Exception as e:
logger.error(f"Failed to fetch Beeminder goals: {e}")
beeminder_goals = []
client = get_user_beeminder_client(target_user_id)
emergencies = []
try:
emergencies = await client.get_emergencies(beeminder_goals)
except Exception as e:
logger.error(f"Failed to fetch Beeminder emergencies: {e}")
goal_runway = []
try:
goal_runway = await client.get_runway_summary(limit=6, all_goals=beeminder_goals)
except Exception as e:
logger.error(f"Failed to fetch goal runway: {e}")
budget_status = {}
try:
budget_status = await asyncio.to_thread(usage_tracker.get_budget_status, target_user_id)
except Exception as e:
logger.error(f"Failed to fetch budget status: {e}")
daily_walk_status = {}
try:
daily_walk_status = await get_cached_daily_activity("bike", target_user_id)
except Exception as e:
logger.error(f"Failed to fetch daily activity: {e}")
groq_context = {}
try:
groq_context = await asyncio.to_thread(get_groq_context_for_narrator, target_user_id)
except Exception as e:
logger.error(f"Failed to fetch Groq context: {e}")
# Greek backlog boost: check if 7-day review forecast exceeds threshold
lang_stats = await asyncio.to_thread(neon_checker.get_language_stats, target_user_id)
greek_backlog_boost = language_sync_service._greek_backlog_active(lang_stats)
greek_backlog_cards = int(lang_stats.get("greek", lang_stats.get("GREEK", {})).get("next_7_days") or 0)
# Add latest cloud walk info if available (use to_thread to avoid blocking event loop)
latest_cloud_walk = await asyncio.to_thread(neon_checker.get_latest_walk, target_user_id)
if latest_cloud_walk:
# Convert datetime to ISO string for JSON serialization
if isinstance(latest_cloud_walk.get("start_time"), datetime):
latest_cloud_walk["start_time"] = latest_cloud_walk["start_time"].isoformat()
# Fetch user preferences for vacation_mode and time windows from Neon
user_prefs = await asyncio.to_thread(neon_checker.get_notification_prefs, target_user_id)
vacation_mode = user_prefs.get("vacation_mode", False)
time_window_start = user_prefs.get("time_window_start", 13)
time_window_end = user_prefs.get("time_window_end", 17)
# Weather-aware logic
weather = await asyncio.to_thread(weather_service.get_weather)
is_appropriate, weather_msg = weather_service.is_walk_appropriate(weather)
pending_todos = [t for t in todos if not t.get("completed", False)]
critical_beeminder = [g for g in beeminder_goals if g.get("derail_risk") == "CRITICAL"]
budget_days = budget_status.get("days_remaining", 0)
summary = f"Active goals: {len(active_goals)}, Pending todos: {len(pending_todos)}, Beeminder goals: {len(beeminder_goals)}, Budget: {budget_days:.1f} days left"
urgent_items = [f"DERAILING: {g['slug']}" for g in critical_beeminder]
if budget_days <= 1: urgent_items.append(f"BUDGET CRITICAL: {budget_days:.1f} days left")
elif budget_days <= 2: urgent_items.append(f"BUDGET WARNING: {budget_days:.1f} days left")
recommendations = []
if len(pending_todos) > 10: recommendations.append("Consider prioritizing todos - large backlog detected")
if critical_beeminder: recommendations.append("Address critical Beeminder goals immediately")
if budget_days <= 2: recommendations.append("Urgent: Focus on highest-value work due to budget constraints")
# Majesty Cake: surface aggregate daily goal status early for discoverability (kingdonb/mecris#170)
try:
daily_aggregate = await get_daily_aggregate_status(target_user_id)
if not daily_aggregate.get("error"):
if daily_aggregate.get("all_clear"):
recommendations.insert(0, f"🎂 Majesty Cake! All daily goals complete ({daily_aggregate.get('score', '?/?')})")
else:
score = daily_aggregate.get("score", "?/?")
recommendations.append(f"🎯 Daily goals progress: {score} — keep going!")
except Exception as e:
logger.error(f"get_narrator_context: daily aggregate status failed: {e}")
daily_aggregate = {"error": str(e)}
# Greek Stack Vitality Coaching (kingdonb/mecris#129)
if greek_backlog_boost:
recommendations.append(f"🏺 Greek Overload: {greek_backlog_cards} cards pending. Focus on REVIEWS to clear the backlog.")
elif greek_backlog_cards < 100 and not vacation_mode:
recommendations.append("🏺 Greek Pipe Thinning: Future reviews are low. Consider PLAYING new cards to build future momentum.")
# Enhanced walk logic: Only recommend if walk needed AND weather/sun is appropriate
if daily_walk_status.get("status") == "needed":
if vacation_mode:
recommendations.append("🏃 Personal activity: Recommended (Vacation mode active)")
elif is_appropriate:
recommendations.append(f"🐾 Priority: {weather_msg} - Physical Activity Needed!")
urgent_items.append("WALK NEEDED: Activity Log")
else:
recommendations.append(f"🐕 Walk status: Needed, but {weather_msg}")
if anthropic_cost_tracker:
recommendations.append("📊 Real-time budget tracking is active via Anthropic Admin API")
if groq_context.get("groq_tracking", {}).get("needs_action"):
groq_urgent = groq_context["groq_tracking"].get("urgent_reminder")
if groq_urgent:
urgent_items.append(f"GROQ: {groq_urgent}")
recommendations.append(groq_urgent)
presence_info = await _get_presence_summary(target_user_id)
if presence_info.get("ghost_age_seconds") is not None:
ghost_age_min = presence_info["ghost_age_seconds"] / 60
if ghost_age_min < 120:
recommendations.insert(0, f"👻 Ghost Heartbeat: Bot was active {ghost_age_min:.0f}m ago.")
else:
recommendations.insert(0, f"👻 Ghost Heartbeat: Bot hasn't been seen for {ghost_age_min/60:.1f}h.")
# Narrator bookmark enrichment (kingdonb/mecris#208 phase 2 / yebyen/mecris#281)
related_bookmarks = []
try:
related_bookmarks = await asyncio.to_thread(_enrich_bookmarks_for_narrator, beeminder_goals)
except Exception as e:
logger.warning(f"get_narrator_context: bookmark enrichment failed: {e}")
return {
"summary": summary, "goals_status": {"total": len(active_goals)},
"urgent_items": urgent_items, "beeminder_alerts": [e.get("message", "") for e in emergencies[:5]],
"goal_runway": goal_runway, "budget_status": budget_status, "recommendations": recommendations,
"daily_walk_status": daily_walk_status,
"latest_cloud_walk": latest_cloud_walk,
"daily_aggregate_status": daily_aggregate,
"system_pulse": {
"running": scheduler.running,
"is_leader": scheduler.is_leader,
"process_id": scheduler.process_id,
"last_status": scheduler.last_status,
"intent": scheduler.intent,
"last_error": scheduler.last_error,
},
"vacation_mode": vacation_mode,
"time_window_start": time_window_start,
"time_window_end": time_window_end,
"greek_backlog_boost": greek_backlog_boost,
"greek_backlog_cards": greek_backlog_cards,
"budget_governor": _budget_governor.get_narrator_summary(),
"presence": presence_info,
"presence_status": presence_info.get("status", "unknown"),
"related_bookmarks": related_bookmarks,
"last_updated": datetime.now().isoformat()
}
except Exception as e:
logger.error(f"Failed to build narrator context: {e}")
return {"error": f"Failed to build narrator context: {e}"}
@mcp.tool(description="Get Beeminder goal portfolio status with risk assessment.")
async def get_beeminder_status(user_id: str = None) -> Dict[str, Any]:
"""Get Beeminder goal portfolio status with risk assessment."""
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
try:
client = get_user_beeminder_client(target_user_id)
goals = await client.get_all_goals()
emergencies = await client.get_emergencies()
return {
"goals": goals, "emergencies": emergencies,
"safe_count": len([g for g in goals if g.get("derail_risk") == "SAFE"]),
"warning_count": len([g for g in goals if g.get("derail_risk") in ["WARNING", "CAUTION"]]),
"critical_count": len([g for g in goals if g.get("derail_risk") == "CRITICAL"])
}
except Exception as e:
logger.error(f"Failed to fetch Beeminder status: {e}")
return {"error": f"Failed to fetch Beeminder status: {e}"}
def resolve_target_user(user_id: Optional[str]) -> Optional[str]:
"""Resolve user ID and enforce authentication if required."""
return credentials_manager.resolve_user_id(user_id)
@mcp.tool(description="Get current usage and budget status with days remaining.")
def get_budget_status(user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
return usage_tracker.get_budget_status(target_user_id)
@mcp.tool(description="Get recent usage sessions.")
def get_recent_usage(limit: int = 10, user_id: str = None) -> List[Dict[str, Any]]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return [{"error": "Authentication Required"}]
return usage_tracker.get_recent_sessions(limit, target_user_id)
@mcp.tool(description="Record Claude usage session with token counts.")
def record_usage_session(input_tokens: int, output_tokens: int, model: str = "claude-3-5-haiku-20241022", session_type: str = "interactive", notes: str = "", user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
try:
cost = record_usage(input_tokens, output_tokens, model, session_type, notes, target_user_id)
_record_governor_spend(model, cost)
return {"recorded": True, "estimated_cost": cost, "updated_status": usage_tracker.get_budget_status(target_user_id)}
except Exception as e:
logger.error(f"Failed to record usage: {e}")
return {"error": f"Failed to record usage: {e}"}
@mcp.tool(description="Record Claude Code CLI usage specifically.")
def record_claude_code_usage(input_tokens: int, output_tokens: int, model: str = "claude-3- Haiku", notes: str = "", user_id: str = None) -> Dict[str, Any]:
"""Specific tool for Claude Code CLI to report its own usage."""
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
try:
cost = record_usage(input_tokens, output_tokens, model, "claude-code", notes, target_user_id)
_record_governor_spend(model, cost)
return {
"recorded": True,
"estimated_cost": cost,
"message": f"Recorded ${cost:.4f} usage for Claude Code session.",
"updated_status": usage_tracker.get_budget_status(target_user_id)
}
except Exception as e:
logger.error(f"Failed to record Claude Code usage: {e}")
return {"error": str(e)}
def _record_governor_spend(model: str, cost: float):
"""Internal helper to route spend to the correct BudgetGovernor bucket."""
bucket = "anthropic_api" # Default
m_lower = model.lower()
if "gemini" in m_lower:
bucket = "gemini"
elif "groq" in m_lower:
bucket = "groq"
elif os.getenv("ANTHROPIC_BASE_URL") and "helix" in os.getenv("ANTHROPIC_BASE_URL").lower():
bucket = "helix"
try:
_budget_governor.record_spend(bucket, cost)
except Exception as e:
logger.warning(f"BudgetGovernor: Failed to record spend for {bucket}: {e}")
@mcp.tool(description="Get real usage data from Anthropic Admin API (organization level).")
async def get_real_anthropic_usage(days: int = 1) -> Dict[str, Any]:
"""Fetch actual usage data from Anthropic organization report."""
guard = _budget_governor.budget_gate("anthropic_api")
if guard and guard.get("budget_halted"):
return guard
if not anthropic_cost_tracker:
return {"error": "Anthropic Admin API key not configured or tracker failed to initialize."}
try:
from datetime import datetime, timedelta, UTC
end_time = datetime.now(UTC)
start_time = end_time - timedelta(days=days)
usage = anthropic_cost_tracker.get_usage(start_time, end_time)
# Summarize usage
total_input = 0
total_output = 0
for bucket in usage.get('data', []):
for result in bucket.get('results', []):
total_input += result.get('uncached_input_tokens', 0)
total_input += result.get('cache_read_input_tokens', 0)
if 'cache_creation' in result:
total_input += result['cache_creation'].get('ephemeral_1h_input_tokens', 0)
total_output += result.get('output_tokens', 0)
# Estimate cost based on Sonnet pricing
est_cost = (total_input * 3.0 / 1_000_000) + (total_output * 15.0 / 1_000_000)
return {
"start_time": start_time.isoformat(),
"end_time": end_time.isoformat(),
"total_input_tokens": total_input,
"total_output_tokens": total_output,
"estimated_cost": round(est_cost, 4),
"raw_data": usage
}
except Exception as e:
logger.error(f"Failed to fetch real Anthropic usage: {e}")
return {"error": str(e)}
@mcp.tool(description="Check for beemergencies and send SMS alerts if critical.")
async def send_beeminder_alert(user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
try:
client = get_user_beeminder_client(target_user_id)
emergencies = await client.get_emergencies()
critical_emergencies = [e for e in emergencies if e.get("urgency") == "IMMEDIATE"]
if critical_emergencies and usage_tracker.should_send_alert("beeminder", "critical", cooldown_minutes=90):
alert_message = f"🚨 BEEMERGENCY: {len(critical_emergencies)} goals need immediate attention!"
send_sms(alert_message)
usage_tracker.log_alert("beeminder", "critical", alert_message, f"count: {len(critical_emergencies)}")
return {"alert_sent": True, "count": len(critical_emergencies)}
return {"alert_sent": False, "reason": "No critical emergencies or on cooldown"}
except Exception as e:
logger.error(f"Failed to send beeminder alert: {e}")
return {"error": f"Failed to send beeminder alert: {e}"}
@mcp.tool(description="Check if daily activity was logged for a specific goal.")
async def get_daily_activity(goal_slug: str = "bike", user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
return await get_cached_daily_activity(goal_slug, target_user_id)
@mcp.tool(description="Get current weather and a recommendation for outdoor activity.")
def get_weather_report() -> Dict[str, Any]:
"""Get current weather and a walk suitability check."""
weather = weather_service.get_weather()
is_appropriate, message = weather_service.is_walk_appropriate(weather)
return {
"weather": weather,
"is_appropriate": is_appropriate,
"recommendation": message
}
@mcp.tool(description="Get the full, raw weather status data from the OneCall API.")
def get_weather_full_report() -> Dict[str, Any]:
"""Get the complete, cached weather status including all OneCall data fields."""
return weather_service.get_weather()
@mcp.tool(description="Force an immediate scrape of Clozemaster and push to Beeminder.")
async def trigger_language_sync() -> Dict[str, Any]:
"""Manually trigger the Clozemaster to Beeminder sync process."""
guard = _budget_governor.budget_gate("anthropic_api")
if guard and guard.get("budget_halted"):
return guard
result = await language_sync_service.sync_all(dry_run=False)
if not result.get("success", False):
return {"error": result.get("error", "Sync failed")}
return result
@mcp.tool(description="Add a new goal to the local database.")
def add_goal(title: str, description: str = "", priority: str = "medium", due_date: Optional[str] = None, user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
return usage_tracker.add_goal(title, description, priority, due_date, target_user_id)
@mcp.tool(description="Mark a goal as completed.")
def complete_goal(goal_id: int, user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
return usage_tracker.complete_goal(goal_id, target_user_id)
@mcp.tool(description="Manually update budget information.")
def update_budget(remaining_budget: float, total_budget: Optional[float] = None, period_end: Optional[str] = None, user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
return usage_tracker.update_budget(remaining_budget, total_budget, period_end, target_user_id)
@mcp.tool(description="Record manual Groq odometer reading with cumulative cost.")
async def record_groq_reading(value: float, notes: str = "", month: Optional[str] = None, user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
# Record locally in Neon first
result = record_groq_reading_from_tracker(value, notes, month, target_user_id)
if result.get("recorded"):
# Sync to Beeminder groqspend goal
try:
# 1. Check goal start date restriction
GROQSPEND_START_DATE = "2026-04-13"
# Use the timestamp from the result
import zoneinfo
eastern = zoneinfo.ZoneInfo("US/Eastern")
if "timestamp" in result:
# result["timestamp"] is ISO format from groq_odometer_tracker
ts_str = result["timestamp"]
# If it doesn't have TZ info, assume it's UTC or Local?
# groq_odometer_tracker uses datetime.now() (naive local) or historical_date (naive)
# To be safe, let's treat it as naive and attach system local if it's missing
record_dt = datetime.fromisoformat(ts_str)
if record_dt.tzinfo is None:
# Treat naive as UTC for consistent mapping if coming from DB,
# but groq_tracker uses .now() which is local.
# Best to just use the date part if it's an odometer.
record_dt = record_dt.replace(tzinfo=timezone.utc)
else:
record_dt = datetime.now(timezone.utc)
daystamp = record_dt.astimezone(eastern).strftime("%Y%m%d")
if daystamp < GROQSPEND_START_DATE.replace("-", ""):
logger.info(f"Skipping Beeminder sync for {daystamp}: before goal start date {GROQSPEND_START_DATE}")
return result
# 2. Initialize Beeminder client
bm_client = get_user_beeminder_client(target_user_id)
goal_slug = "groqspend"
# 3. Handle @TARE reset if detected
if result.get("reset_detected"):
tare_comment = f"@TARE reset for {result.get('month', 'new month')} Transition"
await bm_client.add_datapoint(goal_slug, 0.0, comment=tare_comment, daystamp=daystamp)
logger.info(f"Sent @TARE datapoint to Beeminder for {goal_slug}")
# 4. Push the actual reading
reading_comment = notes if notes else f"Manual update: {result.get('month', 'current month')} spend"
await bm_client.add_datapoint(goal_slug, value, comment=reading_comment, daystamp=daystamp)
logger.info(f"Sent reading {value} to Beeminder goal {goal_slug}")
result["beeminder_sync"] = "success"
except Exception as e:
import traceback
tb = traceback.format_exc()
logger.error(f"Failed to sync Groq reading to Beeminder:\n{tb}")
result["beeminder_sync"] = f"failed: {str(e)}\nTraceback: {tb}"
return result
@mcp.tool(description="Get Groq odometer status and usage reminders.")
def get_groq_status(user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
from groq_odometer_tracker import get_groq_reminder_status
return get_groq_reminder_status(target_user_id)
@mcp.tool(description="Get Groq odometer context for narrator integration.")
def get_groq_context(user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
return get_groq_context_for_narrator(target_user_id)
@mcp.tool(description="Get unified cost status combining Claude budget and Groq usage data.")
async def get_unified_cost_status(user_id: str = None) -> Dict[str, Any]:
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
try:
from groq_odometer_tracker import _get_tracker
budget_data = await asyncio.to_thread(usage_tracker.get_budget_status, target_user_id)
groq_tracker = _get_tracker()
groq_status = await asyncio.to_thread(groq_tracker.check_reminder_needs, target_user_id)
groq_usage = await asyncio.to_thread(groq_tracker.get_usage_for_virtual_budget, target_user_id)
return {"claude": budget_data, "groq": {**groq_status, **groq_usage}}
except Exception as e:
logger.error(f"Failed to get unified cost status: {e}")
return {"error": f"Failed to get unified cost status: {e}"}
@mcp.tool(description="Check for needed reminders and send them intelligently.")
async def trigger_reminder_check(user_id: str = None, apply_fuzz: bool = False) -> Dict[str, Any]:
"""Manually trigger the reminder logic. If apply_fuzz is True, delays the actual check/send by a random interval."""
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required. Run `mecris login`."}
try:
check_result = await check_reminder_needed(target_user_id)
if not check_result.get("should_send"):
return {"triggered": False, "reason": check_result.get("reason")}
# If we want to fuzz the delivery time and this is the initial background check
if apply_fuzz:
import random
from datetime import datetime, timedelta, timezone
from apscheduler.triggers.date import DateTrigger
# Fuzz between 3 and 25 minutes to break up exact 2-hour formulas
fuzz_minutes = random.randint(3, 25)
run_time = datetime.now(timezone.utc) + timedelta(minutes=fuzz_minutes)
job_id = f"fuzzed_reminder_{int(run_time.timestamp())}"
# Enqueue a one-off job to actually send the reminder after the fuzz delay
scheduler.scheduler.add_job(
trigger_reminder_check,
trigger=DateTrigger(run_date=run_time),
args=[target_user_id, False],
id=job_id
)
logger.info(f"Fuzzed reminder scheduled for {fuzz_minutes} minutes from now (Job: {job_id})")
return {"triggered": False, "reason": f"Fuzzed for {fuzz_minutes} minutes"}
send_result = await send_reminder_message(check_result, target_user_id)
return {"triggered": True, "check": check_result, "send": send_result}
except Exception as e:
logger.error(f"Reminder trigger failed: {e}")
return {"error": f"Reminder trigger failed: {e}"}
# Link real function to scheduler
scheduler.trigger_reminder_func = trigger_reminder_check
@mcp.tool(description="Sidekiq-like: Enqueue a message to be sent after a delay (in minutes).")
def enqueue_message(message: str, delay_minutes: int, to_number: Optional[str] = None) -> Dict[str, Any]:
"""Sidekiq-like: Enqueue a message to be sent after a delay."""
return scheduler.enqueue_delayed_message(message, delay_minutes, to_number)
@mcp.tool(description="View the current background job queue and leader status.")
def get_scheduler_queue() -> Dict[str, Any]:
"""View the shared job queue and coordination status."""
return {
"process_id": scheduler.process_id,
"is_leader": scheduler.is_leader,
"queue": scheduler.get_queue()
}
from services.health_checker import HealthChecker as _HealthChecker
_health_checker = _HealthChecker()
@mcp.tool(description="Get unified health status for all registered system processes (Python MCP, Android client, Spin cloud) from the scheduler_election table.")
async def get_system_health(user_id: str = None) -> Dict[str, Any]:
"""Read the scheduler_election table and return active/stale status for every registered process."""
target_user_id = resolve_target_user(user_id)
if not target_user_id:
return {"error": "Authentication Required"}
result = await asyncio.to_thread(_health_checker.get_system_health, target_user_id)
if "error" not in result:
result["leader_process_id"] = scheduler.process_id
result["is_leader"] = scheduler.is_leader
return result
from services.coaching_service import CoachingService
@mcp.tool(description="Get a personalized coaching insight based on momentum and current needs.")
async def get_coaching_insight(user_id: str = None) -> Dict[str, Any]:
"""Analyze current state and provide a momentum-aware coaching pivot."""