-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
executable file
·1368 lines (1206 loc) · 50.6 KB
/
Copy pathmcp_server.py
File metadata and controls
executable file
·1368 lines (1206 loc) · 50.6 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
#!/usr/bin/env python3
"""FGIP MCP Server - Exposes FGIP tools to Claude Code via Model Context Protocol.
Run with:
python3 mcp_server.py
Or as a persistent service:
systemctl --user start fgip-mcp
"""
import asyncio
import json
import sqlite3
from pathlib import Path
from typing import Any, Optional
from datetime import datetime
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import (
Tool,
TextContent,
CallToolResult,
)
# FGIP paths
FGIP_ROOT = Path(__file__).parent
DB_PATH = FGIP_ROOT / "fgip.db"
server = Server("fgip-server")
def get_db_connection() -> sqlite3.Connection:
"""Get database connection with row factory."""
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
return conn
# ============================================================================
# TOOL DEFINITIONS
# ============================================================================
@server.list_tools()
async def list_tools() -> list[Tool]:
"""List available FGIP tools."""
return [
Tool(
name="query_graph",
description="Query the FGIP knowledge graph. Returns nodes and edges matching the query.",
inputSchema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "SQL WHERE clause or search term for nodes/edges"
},
"table": {
"type": "string",
"enum": ["nodes", "edges", "claims", "sources"],
"description": "Table to query (default: nodes)"
},
"limit": {
"type": "integer",
"description": "Max results (default: 50)"
}
},
"required": ["query"]
}
),
Tool(
name="get_thesis_score",
description="Get the current FGIP thesis verification score and breakdown.",
inputSchema={
"type": "object",
"properties": {}
}
),
Tool(
name="get_convergence_report",
description="Get the signal convergence report showing Promethean predictions vs POTUS actions vs market response.",
inputSchema={
"type": "object",
"properties": {}
}
),
Tool(
name="explore_connections",
description="Find all connections (edges) for a given node, showing the network around an entity.",
inputSchema={
"type": "object",
"properties": {
"node_id": {
"type": "string",
"description": "Node ID to explore (e.g., 'blackrock', 'chips-act', 'genius-act-2025')"
},
"depth": {
"type": "integer",
"description": "How many hops to traverse (default: 1, max: 3)"
}
},
"required": ["node_id"]
}
),
Tool(
name="find_causal_chains",
description="Find causal chains in the graph connecting problems to corrections.",
inputSchema={
"type": "object",
"properties": {
"start_node": {
"type": "string",
"description": "Optional starting node ID"
},
"end_node": {
"type": "string",
"description": "Optional ending node ID"
}
}
}
),
Tool(
name="get_both_sides_patterns",
description="Find entities appearing on both 'problem' and 'correction' sides of the thesis.",
inputSchema={
"type": "object",
"properties": {
"min_confidence": {
"type": "number",
"description": "Minimum confidence threshold (0-1, default: 0.7)"
}
}
}
),
Tool(
name="run_agent",
description="Run an FGIP agent to collect and propose new evidence.",
inputSchema={
"type": "object",
"properties": {
"agent": {
"type": "string",
"enum": [
"promethean", "scotus", "tic", "stablecoin",
"congress", "fec", "edgar", "gao", "fara",
"opensecrets", "usaspending", "federal_register"
],
"description": "Agent to run"
},
"dry_run": {
"type": "boolean",
"description": "If true, don't write to database (default: false)"
}
},
"required": ["agent"]
}
),
Tool(
name="get_graph_stats",
description="Get statistics about the FGIP knowledge graph.",
inputSchema={
"type": "object",
"properties": {}
}
),
Tool(
name="search_nodes",
description="Search for nodes by name or description using full-text search.",
inputSchema={
"type": "object",
"properties": {
"search_term": {
"type": "string",
"description": "Text to search for in node names/descriptions"
},
"node_type": {
"type": "string",
"description": "Optional filter by node type (ORGANIZATION, PERSON, LEGISLATION, etc.)"
}
},
"required": ["search_term"]
}
),
Tool(
name="get_debt_domestication_metrics",
description="Get current debt domestication metrics: foreign holdings, stablecoin absorption, leverage reduction.",
inputSchema={
"type": "object",
"properties": {}
}
),
Tool(
name="run_compression_analysis",
description="Run compression-based pattern detection to find motifs, similar entities, and anomalies using SHAKE256 fingerprinting.",
inputSchema={
"type": "object",
"properties": {
"include_anomalies": {
"type": "boolean",
"description": "Include anomaly detection (default: true)"
},
"include_similarity": {
"type": "boolean",
"description": "Include entity similarity search (default: true)"
}
}
}
),
Tool(
name="get_personal_runway",
description="Calculate personal financial runway using FGIP-validated inflation rates (M2=6.3%, not CPI=2.7%). Shows real savings yield, real debt cost, and runway under different scenarios. No data is stored - pure calculation.",
inputSchema={
"type": "object",
"properties": {
"monthly_expenses": {
"type": "number",
"description": "Monthly expenses/burn rate in USD"
},
"current_savings": {
"type": "number",
"description": "Current liquid savings in USD"
},
"savings_yield": {
"type": "number",
"description": "Current savings APY as decimal (e.g., 0.045 for 4.5%). Default: 0.045"
},
"debt_balance": {
"type": "number",
"description": "Total debt balance in USD (optional, default: 0)"
},
"debt_apr": {
"type": "number",
"description": "Weighted average debt APR as decimal (optional, default: 0)"
},
"income_monthly": {
"type": "number",
"description": "Monthly income in USD (optional, default: 0)"
}
},
"required": ["monthly_expenses", "current_savings"]
}
),
Tool(
name="get_allocation_candidates",
description="Get allocation candidates from FGIP graph for portfolio construction. Returns reshoring beneficiaries (CHIPS Act), gold proxy, T-Bills, and optionally mining assets - each with graph metadata (tier, confidence, edge counts). Designed for Echo Hedge integration.",
inputSchema={
"type": "object",
"properties": {
"include_mining": {
"type": "boolean",
"description": "Include mining pool assets from Echo Hedge schema (default: false)"
},
"base_expected_return": {
"type": "number",
"description": "Base expected nominal return assumption as decimal (default: 0.10 for 10%)"
}
}
}
),
Tool(
name="get_candidate_risk_context",
description="Get risk context for allocation candidates from FGIP graph neighborhoods. Returns tier distribution, both-sides motif hits, anomaly score, and edge breakdown. Use after get_allocation_candidates to inform position sizing.",
inputSchema={
"type": "object",
"properties": {
"candidate_ids": {
"type": "array",
"items": {"type": "string"},
"description": "List of node IDs to get risk context for"
}
},
"required": ["candidate_ids"]
}
),
Tool(
name="ingest_youtube_history",
description="Ingest YouTube watch history from Google Takeout and build signal layer connecting viewing patterns to FGIP thesis. Extracts guests, channels, topics and cross-references against the knowledge graph.",
inputSchema={
"type": "object",
"properties": {
"html_path": {
"type": "string",
"description": "Path to watch-history.html from Google Takeout"
}
},
"required": ["html_path"]
}
),
Tool(
name="get_system_briefing",
description="Get system intelligence briefing: pending approvals, parser gaps, API health, and work orders for Claude Code. Use this to understand what the FGIP system needs - bottlenecks, missing capabilities, and tasks for you to build.",
inputSchema={
"type": "object",
"properties": {
"include_work_orders": {
"type": "boolean",
"description": "Include work orders for Claude Code (default: true)"
},
"include_health": {
"type": "boolean",
"description": "Include API health status (default: true)"
}
}
}
),
]
# ============================================================================
# TOOL IMPLEMENTATIONS
# ============================================================================
@server.call_tool()
async def call_tool(name: str, arguments: dict[str, Any]) -> CallToolResult:
"""Handle tool calls."""
if name == "query_graph":
return await query_graph(arguments)
elif name == "get_thesis_score":
return await get_thesis_score()
elif name == "get_convergence_report":
return await get_convergence_report()
elif name == "explore_connections":
return await explore_connections(arguments)
elif name == "find_causal_chains":
return await find_causal_chains(arguments)
elif name == "get_both_sides_patterns":
return await get_both_sides_patterns(arguments)
elif name == "run_agent":
return await run_agent(arguments)
elif name == "get_graph_stats":
return await get_graph_stats()
elif name == "search_nodes":
return await search_nodes(arguments)
elif name == "get_debt_domestication_metrics":
return await get_debt_domestication_metrics()
elif name == "run_compression_analysis":
return await run_compression_analysis(arguments)
elif name == "get_personal_runway":
return await get_personal_runway(arguments)
elif name == "get_allocation_candidates":
return await get_allocation_candidates(arguments)
elif name == "get_candidate_risk_context":
return await get_candidate_risk_context(arguments)
elif name == "ingest_youtube_history":
return await ingest_youtube_history(arguments)
elif name == "get_system_briefing":
return await get_system_briefing(arguments)
else:
return CallToolResult(
content=[TextContent(type="text", text=f"Unknown tool: {name}")]
)
async def query_graph(args: dict) -> CallToolResult:
"""Query the knowledge graph."""
query = args.get("query", "")
table = args.get("table", "nodes")
limit = min(args.get("limit", 50), 200)
# Auto-translate common column aliases for user convenience
# Schema uses 'node_type' but users naturally query 'type'
if table == "nodes":
query = re.sub(r'\btype\s*=', 'node_type =', query, flags=re.IGNORECASE)
query = re.sub(r'\btype\s+LIKE', 'node_type LIKE', query, flags=re.IGNORECASE)
query = re.sub(r'\btype\s+IN', 'node_type IN', query, flags=re.IGNORECASE)
conn = get_db_connection()
try:
# Handle simple search vs SQL WHERE clause
if not any(op in query.upper() for op in ["=", "LIKE", "IN", ">", "<", "AND", "OR"]):
# Simple search - use LIKE
if table == "nodes":
sql = f"SELECT * FROM nodes WHERE name LIKE ? OR node_id LIKE ? LIMIT ?"
rows = conn.execute(sql, (f"%{query}%", f"%{query}%", limit)).fetchall()
elif table == "edges":
sql = f"SELECT * FROM edges WHERE from_node_id LIKE ? OR to_node_id LIKE ? OR edge_type LIKE ? LIMIT ?"
rows = conn.execute(sql, (f"%{query}%", f"%{query}%", f"%{query}%", limit)).fetchall()
else:
sql = f"SELECT * FROM {table} LIMIT ?"
rows = conn.execute(sql, (limit,)).fetchall()
else:
# SQL WHERE clause
sql = f"SELECT * FROM {table} WHERE {query} LIMIT ?"
rows = conn.execute(sql, (limit,)).fetchall()
results = [dict(row) for row in rows]
return CallToolResult(
content=[TextContent(
type="text",
text=json.dumps({"count": len(results), "results": results}, indent=2)
)]
)
except Exception as e:
return CallToolResult(
content=[TextContent(type="text", text=f"Error: {str(e)}")]
)
finally:
conn.close()
async def get_thesis_score() -> CallToolResult:
"""Get thesis verification score."""
conn = get_db_connection()
try:
# Count verified edges by type
problem_types = ("LOBBIED_FOR", "DONATED_TO", "FUNDED_BY", "REGISTERED_AS_AGENT",
"FILED_AMICUS", "EMPLOYED", "OWNS_MEDIA", "HAS_LEVERAGE_OVER",
"BLOCKS", "HOLDS_TREASURY")
correction_types = ("AWARDED_GRANT", "BUILT_IN", "FUNDED_PROJECT", "IMPLEMENTED_BY",
"RULEMAKING_FOR", "AUTHORIZED_BY", "CORRECTS", "ENABLES",
"REDUCES", "FUNDS", "CONTRIBUTES_TO")
problem_count = conn.execute(
f"SELECT COUNT(*) FROM edges WHERE edge_type IN ({','.join('?' * len(problem_types))})",
problem_types
).fetchone()[0]
correction_count = conn.execute(
f"SELECT COUNT(*) FROM edges WHERE edge_type IN ({','.join('?' * len(correction_types))})",
correction_types
).fetchone()[0]
total_edges = conn.execute("SELECT COUNT(*) FROM edges").fetchone()[0]
total_nodes = conn.execute("SELECT COUNT(*) FROM nodes").fetchone()[0]
# Both-sides detection
both_sides_sql = """
SELECT DISTINCT e1.from_node_id
FROM edges e1
JOIN edges e2 ON e1.from_node_id = e2.from_node_id
WHERE e1.edge_type IN ('OWNS', 'INVESTED_IN', 'HOLDS')
AND e2.edge_type IN ('AWARDED_GRANT', 'RECEIVED_FUNDING', 'BUILT_IN')
"""
both_sides = conn.execute(both_sides_sql).fetchall()
# Load convergence report if available
convergence_path = FGIP_ROOT / "data" / "reports" / "convergence_report.json"
convergence_score = None
if convergence_path.exists():
with open(convergence_path) as f:
convergence = json.load(f)
convergence_score = convergence.get("convergence_score")
score = {
"timestamp": datetime.now().isoformat(),
"graph_stats": {
"total_nodes": total_nodes,
"total_edges": total_edges,
"problem_edges": problem_count,
"correction_edges": correction_count,
},
"both_sides_entities": len(both_sides),
"convergence_score": convergence_score,
"thesis": "Structural capital concentration creates mechanical both-sides exposure across policy pendulum swings."
}
return CallToolResult(
content=[TextContent(type="text", text=json.dumps(score, indent=2))]
)
finally:
conn.close()
async def get_convergence_report() -> CallToolResult:
"""Get signal convergence report."""
report_path = FGIP_ROOT / "data" / "reports" / "convergence_report.json"
if not report_path.exists():
return CallToolResult(
content=[TextContent(type="text", text="Convergence report not found. Run: python3 fgip/analysis/signal_convergence.py")]
)
with open(report_path) as f:
report = json.load(f)
return CallToolResult(
content=[TextContent(type="text", text=json.dumps(report, indent=2))]
)
async def explore_connections(args: dict) -> CallToolResult:
"""Explore connections around a node."""
node_id = args.get("node_id")
depth = min(args.get("depth", 1), 3)
if not node_id:
return CallToolResult(
content=[TextContent(type="text", text="Error: node_id required")]
)
conn = get_db_connection()
try:
# Get the node
node = conn.execute("SELECT * FROM nodes WHERE node_id = ?", (node_id,)).fetchone()
if not node:
# Try partial match
nodes = conn.execute(
"SELECT * FROM nodes WHERE node_id LIKE ? LIMIT 5",
(f"%{node_id}%",)
).fetchall()
if nodes:
return CallToolResult(
content=[TextContent(
type="text",
text=f"Node not found. Did you mean: {[dict(n)['node_id'] for n in nodes]}"
)]
)
return CallToolResult(
content=[TextContent(type="text", text=f"Node not found: {node_id}")]
)
# BFS to find connections
visited = {node_id}
connections = []
current_level = [node_id]
for d in range(depth):
next_level = []
for nid in current_level:
# Outgoing edges
out_edges = conn.execute(
"SELECT * FROM edges WHERE from_node_id = ?", (nid,)
).fetchall()
for edge in out_edges:
connections.append({
"depth": d + 1,
"direction": "outgoing",
"edge": dict(edge)
})
if edge["to_node_id"] not in visited:
visited.add(edge["to_node_id"])
next_level.append(edge["to_node_id"])
# Incoming edges
in_edges = conn.execute(
"SELECT * FROM edges WHERE to_node_id = ?", (nid,)
).fetchall()
for edge in in_edges:
connections.append({
"depth": d + 1,
"direction": "incoming",
"edge": dict(edge)
})
if edge["from_node_id"] not in visited:
visited.add(edge["from_node_id"])
next_level.append(edge["from_node_id"])
current_level = next_level
result = {
"node": dict(node),
"total_connections": len(connections),
"connections": connections[:100] # Limit output
}
return CallToolResult(
content=[TextContent(type="text", text=json.dumps(result, indent=2))]
)
finally:
conn.close()
async def find_causal_chains(args: dict) -> CallToolResult:
"""Find causal chains connecting problems to corrections."""
start_node = args.get("start_node")
end_node = args.get("end_node")
# Import reasoning agent for chain detection
import sys
sys.path.insert(0, str(FGIP_ROOT))
try:
from fgip.db import FGIPDatabase
from fgip.agents.reasoning import ReasoningAgent
db = FGIPDatabase(str(DB_PATH))
agent = ReasoningAgent(db)
chains = agent.find_causal_chains(start_node, end_node) if start_node or end_node else agent.find_causal_chains()
return CallToolResult(
content=[TextContent(
type="text",
text=json.dumps({"chains_found": len(chains), "chains": chains[:20]}, indent=2)
)]
)
except Exception as e:
return CallToolResult(
content=[TextContent(type="text", text=f"Error: {str(e)}")]
)
async def get_both_sides_patterns(args: dict) -> CallToolResult:
"""Find entities on both sides of the thesis."""
min_confidence = args.get("min_confidence", 0.7)
conn = get_db_connection()
try:
# Problem edge types
problem_types = ("LOBBIED_FOR", "DONATED_TO", "FUNDED_BY", "REGISTERED_AS_AGENT",
"FILED_AMICUS", "OWNS", "INVESTED_IN", "HAS_LEVERAGE_OVER")
# Correction edge types
correction_types = ("AWARDED_GRANT", "BUILT_IN", "FUNDED_PROJECT", "RECEIVED_FUNDING",
"INVESTED_IN", "ENABLES", "FUNDS")
# Find entities with edges in both categories
sql = """
SELECT
n.node_id, n.name, n.node_type,
GROUP_CONCAT(DISTINCT e1.edge_type) as problem_edges,
GROUP_CONCAT(DISTINCT e2.edge_type) as correction_edges
FROM nodes n
JOIN edges e1 ON n.node_id = e1.from_node_id
JOIN edges e2 ON n.node_id = e2.from_node_id
WHERE e1.edge_type IN ({}) AND e2.edge_type IN ({})
GROUP BY n.node_id
""".format(','.join('?' * len(problem_types)), ','.join('?' * len(correction_types)))
rows = conn.execute(sql, problem_types + correction_types).fetchall()
patterns = []
for row in rows:
pattern = dict(row)
pattern["confidence"] = 0.95 # Based on SEC EDGAR data
if pattern["confidence"] >= min_confidence:
patterns.append(pattern)
return CallToolResult(
content=[TextContent(
type="text",
text=json.dumps({"count": len(patterns), "patterns": patterns}, indent=2)
)]
)
finally:
conn.close()
async def run_agent(args: dict) -> CallToolResult:
"""Run an FGIP agent."""
agent_name = args.get("agent")
dry_run = args.get("dry_run", False)
agent_map = {
"promethean": "fgip.agents.promethean",
"scotus": "fgip.agents.scotus",
"tic": "fgip.agents.tic",
"stablecoin": "fgip.agents.stablecoin",
"congress": "fgip.agents.congress",
"fec": "fgip.agents.fec",
"edgar": "fgip.agents.edgar",
"gao": "fgip.agents.gao",
"fara": "fgip.agents.fara",
"opensecrets": "fgip.agents.opensecrets",
"usaspending": "fgip.agents.usaspending",
"federal_register": "fgip.agents.federal_register",
}
if agent_name not in agent_map:
return CallToolResult(
content=[TextContent(type="text", text=f"Unknown agent: {agent_name}")]
)
import subprocess
cmd = ["python3", "-m", agent_map[agent_name], str(DB_PATH)]
if dry_run:
cmd.append("--dry-run")
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=120,
cwd=str(FGIP_ROOT),
env={"PYTHONPATH": str(FGIP_ROOT)}
)
output = result.stdout + result.stderr
return CallToolResult(
content=[TextContent(type="text", text=f"Agent {agent_name} completed:\n{output[-2000:]}")]
)
except subprocess.TimeoutExpired:
return CallToolResult(
content=[TextContent(type="text", text=f"Agent {agent_name} timed out after 120s")]
)
except Exception as e:
return CallToolResult(
content=[TextContent(type="text", text=f"Error running agent: {str(e)}")]
)
async def get_graph_stats() -> CallToolResult:
"""Get graph statistics."""
conn = get_db_connection()
try:
stats = {}
# Total counts
stats["total_nodes"] = conn.execute("SELECT COUNT(*) FROM nodes").fetchone()[0]
stats["total_edges"] = conn.execute("SELECT COUNT(*) FROM edges").fetchone()[0]
stats["total_claims"] = conn.execute("SELECT COUNT(*) FROM claims").fetchone()[0]
stats["total_sources"] = conn.execute("SELECT COUNT(*) FROM sources").fetchone()[0]
# Node types
node_types = conn.execute(
"SELECT node_type, COUNT(*) as count FROM nodes GROUP BY node_type ORDER BY count DESC"
).fetchall()
stats["node_types"] = {row["node_type"]: row["count"] for row in node_types}
# Edge types
edge_types = conn.execute(
"SELECT edge_type, COUNT(*) as count FROM edges GROUP BY edge_type ORDER BY count DESC LIMIT 20"
).fetchall()
stats["edge_types"] = {row["edge_type"]: row["count"] for row in edge_types}
# Source tiers
tiers = conn.execute(
"SELECT tier, COUNT(*) as count FROM sources GROUP BY tier ORDER BY tier"
).fetchall()
stats["source_tiers"] = {f"tier_{row['tier']}": row["count"] for row in tiers}
return CallToolResult(
content=[TextContent(type="text", text=json.dumps(stats, indent=2))]
)
finally:
conn.close()
async def search_nodes(args: dict) -> CallToolResult:
"""Search nodes by text."""
search_term = args.get("search_term", "")
node_type = args.get("node_type")
conn = get_db_connection()
try:
if node_type:
sql = """
SELECT * FROM nodes
WHERE (name LIKE ? OR description LIKE ? OR node_id LIKE ?)
AND node_type = ?
LIMIT 50
"""
rows = conn.execute(sql, (f"%{search_term}%", f"%{search_term}%", f"%{search_term}%", node_type)).fetchall()
else:
sql = """
SELECT * FROM nodes
WHERE name LIKE ? OR description LIKE ? OR node_id LIKE ?
LIMIT 50
"""
rows = conn.execute(sql, (f"%{search_term}%", f"%{search_term}%", f"%{search_term}%")).fetchall()
results = [dict(row) for row in rows]
return CallToolResult(
content=[TextContent(
type="text",
text=json.dumps({"count": len(results), "results": results}, indent=2)
)]
)
finally:
conn.close()
async def get_debt_domestication_metrics() -> CallToolResult:
"""Get debt domestication metrics."""
# Load from stablecoin agent constants
metrics = {
"foreign_holdings": {
"china": {"holdings_b": 759.0, "is_ally": False},
"japan": {"holdings_b": 1060.0, "is_ally": True},
"total_foreign": 8500.0,
"unit": "$B"
},
"stablecoin_absorption": {
"tether": {"market_cap_b": 120.0, "treasury_holdings_b": 72.0},
"circle": {"market_cap_b": 45.0, "treasury_holdings_b": 36.0},
"total_treasury_holdings_b": 115.0,
},
"genius_act": {
"signed": "2025-07-18",
"holder_yield": 0.0,
"issuer_yield": 4.5,
"reserve_requirement": 1.0,
},
"domestication_metrics": {
"current_pct": 1.35, # 115B / 8500B
"projected_2028_pct": 23.53, # 2000B / 8500B
"leverage_reduction": "23.53% of foreign leverage neutralized at $2T stablecoins"
},
"mechanism": "GENIUS Act → stablecoin Treasury absorption → debt domestication → foreign leverage reduction → tariff enablement"
}
return CallToolResult(
content=[TextContent(type="text", text=json.dumps(metrics, indent=2))]
)
async def run_compression_analysis(args: dict) -> CallToolResult:
"""Run compression-based pattern detection."""
include_anomalies = args.get("include_anomalies", True)
include_similarity = args.get("include_similarity", True)
import sys
sys.path.insert(0, str(FGIP_ROOT))
try:
from fgip.analysis.compression_patterns import CompressionPatternAnalyzer
analyzer = CompressionPatternAnalyzer(str(DB_PATH))
analyzer.connect()
report = analyzer.run_full_analysis(
include_sketches=True,
include_anomalies=include_anomalies,
include_similarity=include_similarity,
)
# Summarize for output
summary = {
"timestamp": report.timestamp,
"evidence_level": report.evidence_level,
"total_nodes": report.total_nodes,
"total_edges": report.total_edges,
"motif_matches": [
{
"pattern_name": m.pattern_name,
"nodes": m.nodes_involved,
"confidence": m.confidence,
"compression_ratio": m.compression_ratio,
}
for m in report.motif_matches[:10]
],
"similar_entities": [
{
"pair": [s.node_a, s.node_b],
"similarity": s.similarity,
"shared_types": s.shared_edge_types[:3],
}
for s in report.similar_entities[:10]
],
"anomalies": [
{
"node": a.node_name,
"type": a.node_type,
"score": a.anomaly_score,
"unusual": a.unusual_edges[:3],
}
for a in report.anomalies[:10]
],
"counts": {
"motifs": len(report.motif_matches),
"similar_pairs": len(report.similar_entities),
"anomalies": len(report.anomalies),
}
}
return CallToolResult(
content=[TextContent(type="text", text=json.dumps(summary, indent=2))]
)
except Exception as e:
return CallToolResult(
content=[TextContent(type="text", text=f"Error: {str(e)}")]
)
async def get_personal_runway(args: dict) -> CallToolResult:
"""Calculate personal financial runway using FGIP-validated inflation."""
monthly_expenses = args.get("monthly_expenses")
current_savings = args.get("current_savings")
savings_yield = args.get("savings_yield", 0.045)
debt_balance = args.get("debt_balance", 0.0)
debt_apr = args.get("debt_apr", 0.0)
income_monthly = args.get("income_monthly", 0.0)
if not monthly_expenses or not current_savings:
return CallToolResult(
content=[TextContent(
type="text",
text="Error: monthly_expenses and current_savings are required"
)]
)
import sys
sys.path.insert(0, str(FGIP_ROOT))
try:
from fgip.analysis.purchasing_power import (
PurchasingPowerAnalyzer,
PersonalScenario,
)
# Build scenario
scenario = PersonalScenario(
monthly_expenses=float(monthly_expenses),
current_savings=float(current_savings),
savings_yield=float(savings_yield),
debt_balance=float(debt_balance),
debt_apr=float(debt_apr),
income_monthly=float(income_monthly),
)
# Run analysis
analyzer = PurchasingPowerAnalyzer(str(DB_PATH))
report = analyzer.analyze(scenario)
return CallToolResult(
content=[TextContent(type="text", text=report.to_json(indent=2))]
)
except Exception as e:
return CallToolResult(
content=[TextContent(type="text", text=f"Error: {str(e)}")]
)
async def get_allocation_candidates(args: dict) -> CallToolResult:
"""
Get allocation candidates from FGIP graph for Echo Hedge integration.
Returns reshoring beneficiaries (CHIPS Act), gold proxy, T-Bills,
and optionally mining assets - each with graph metadata.
"""
include_mining = args.get("include_mining", False)
base_expected_return = args.get("base_expected_return", 0.10)
import sys
sys.path.insert(0, str(FGIP_ROOT))
try:
from fgip.analysis.purchasing_power import PurchasingPowerAnalyzer
analyzer = PurchasingPowerAnalyzer(str(DB_PATH))
conn = get_db_connection()
candidates = []
# 1. Reshoring beneficiaries from graph
reshoring = analyzer.get_reshoring_beneficiaries(
base_expected_return=base_expected_return
)
for r in reshoring:
# Use the real node_id from the graph query (no string munging)
node_id = r.node_id
edge_info = conn.execute("""
SELECT
COUNT(*) as edge_count,
MAX(confidence) as max_confidence,
GROUP_CONCAT(DISTINCT assertion_level) as tiers
FROM edges
WHERE from_node_id = ? OR to_node_id = ?
""", (node_id, node_id)).fetchone()
candidates.append({
"candidate_id": node_id,
"name": r.name,
"category": "reshoring",
"expected_nominal_return": r.expected_nominal_return,
"expected_return_is_assumption": r.expected_return_is_assumption,
"volatility_note": r.volatility_note,
"liquidity_note": r.liquidity_note,
"graph_metadata": {
"edge_count": edge_info["edge_count"] or 0,
"max_confidence": edge_info["max_confidence"] or 0.5,
"tiers": (edge_info["tiers"] or "").split(","),
},
})
# 2. Gold proxy
gold = analyzer.get_gold_assumption()
candidates.append({
"candidate_id": gold.node_id,
"name": gold.name,
"category": "commodity",
"expected_nominal_return": gold.expected_nominal_return,
"expected_return_is_assumption": gold.expected_return_is_assumption,
"volatility_note": gold.volatility_note,
"liquidity_note": gold.liquidity_note,
"graph_metadata": {
"edge_count": 0, # Not in graph
"max_confidence": 0.80, # M2 correlation is proven
"tiers": ["EXTERNAL"],
},
})
# 3. T-Bills
tbill = analyzer.get_tbill_assumption()
candidates.append({
"candidate_id": tbill.node_id,
"name": tbill.name,
"category": "fixed_income",
"expected_nominal_return": tbill.expected_nominal_return,
"expected_return_is_assumption": tbill.expected_return_is_assumption,
"volatility_note": tbill.volatility_note,
"liquidity_note": tbill.liquidity_note,
"graph_metadata": {
"edge_count": 0,
"max_confidence": 0.95, # Treasury is Tier-0
"tiers": ["TIER_0"],
},
})