-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server_expanded.py
More file actions
557 lines (469 loc) · 19.4 KB
/
Copy pathmcp_server_expanded.py
File metadata and controls
557 lines (469 loc) · 19.4 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
#!/usr/bin/env python3
"""Expanded MCP Server - FGIP + File System Access.
Provides:
- All FGIP graph/analysis tools
- File system access to home directory
- Directory listing and file reading
Run with:
python3 mcp_server_expanded.py --port 8765
"""
import asyncio
import json
import sqlite3
import os
import glob
import argparse
from pathlib import Path
from typing import Any, Optional
from datetime import datetime
from mcp.server import Server
from mcp.server.sse import SseServerTransport
from mcp.types import Tool, TextContent, CallToolResult
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.responses import JSONResponse
import uvicorn
# Paths
HOME_DIR = Path.home()
FGIP_ROOT = Path(__file__).parent
DB_PATH = FGIP_ROOT / "fgip.db"
# Allowed directories (for safety)
ALLOWED_ROOTS = [
HOME_DIR,
Path("/tmp"),
]
server = Server("fgip-expanded")
def is_path_allowed(path: Path) -> bool:
"""Check if path is within allowed directories."""
path = path.resolve()
return any(
str(path).startswith(str(root.resolve()))
for root in ALLOWED_ROOTS
)
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 tools."""
return [
# === FGIP TOOLS ===
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"},
"table": {"type": "string", "enum": ["nodes", "edges", "claims", "sources"]},
"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_pipeline_health",
description="Get pipeline health status including leak detection counts.",
inputSchema={"type": "object", "properties": {}}
),
Tool(
name="get_pending_approvals",
description="Get pending proposals awaiting human review.",
inputSchema={
"type": "object",
"properties": {
"limit": {"type": "integer", "description": "Max results (default: 20)"},
"agent": {"type": "string", "description": "Filter by agent name"}
}
}
),
Tool(
name="get_system_briefing",
description="Get system intelligence briefing with bottlenecks and recommendations.",
inputSchema={"type": "object", "properties": {}}
),
# === FILE SYSTEM TOOLS ===
Tool(
name="list_directory",
description="List contents of a directory. Returns files and subdirectories.",
inputSchema={
"type": "object",
"properties": {
"path": {"type": "string", "description": "Directory path (relative to home or absolute)"},
"pattern": {"type": "string", "description": "Glob pattern to filter (e.g., '*.py')"},
"recursive": {"type": "boolean", "description": "List recursively (default: false)"}
},
"required": ["path"]
}
),
Tool(
name="read_file",
description="Read contents of a file.",
inputSchema={
"type": "object",
"properties": {
"path": {"type": "string", "description": "File path (relative to home or absolute)"},
"lines": {"type": "integer", "description": "Max lines to read (default: 500)"},
"offset": {"type": "integer", "description": "Line offset to start from"}
},
"required": ["path"]
}
),
Tool(
name="search_files",
description="Search for files matching a pattern.",
inputSchema={
"type": "object",
"properties": {
"pattern": {"type": "string", "description": "Glob pattern (e.g., '**/*.py')"},
"root": {"type": "string", "description": "Root directory (default: home)"},
"limit": {"type": "integer", "description": "Max results (default: 100)"}
},
"required": ["pattern"]
}
),
Tool(
name="grep_files",
description="Search for text pattern in files.",
inputSchema={
"type": "object",
"properties": {
"pattern": {"type": "string", "description": "Text/regex pattern to search"},
"path": {"type": "string", "description": "File or directory to search"},
"file_pattern": {"type": "string", "description": "Glob pattern for files (e.g., '*.py')"},
"limit": {"type": "integer", "description": "Max matches (default: 50)"}
},
"required": ["pattern", "path"]
}
),
Tool(
name="get_file_info",
description="Get information about a file or directory.",
inputSchema={
"type": "object",
"properties": {
"path": {"type": "string", "description": "File or directory path"}
},
"required": ["path"]
}
),
]
# ============================================================================
# TOOL IMPLEMENTATIONS
# ============================================================================
@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
"""Handle tool calls."""
# === FGIP TOOLS ===
if name == "query_graph":
query = arguments.get("query", "")
table = arguments.get("table", "nodes")
limit = arguments.get("limit", 50)
# Auto-translate common column aliases for user convenience
# Schema uses 'node_type' but users naturally query 'type'
if table == "nodes":
import re as regex
query = regex.sub(r'\btype\s*=', 'node_type =', query, flags=regex.IGNORECASE)
query = regex.sub(r'\btype\s+LIKE', 'node_type LIKE', query, flags=regex.IGNORECASE)
query = regex.sub(r'\btype\s+IN', 'node_type IN', query, flags=regex.IGNORECASE)
conn = get_db_connection()
try:
if table == "nodes":
sql = f"""
SELECT node_id, node_type, name, description
FROM nodes
WHERE node_id LIKE ? OR name LIKE ? OR description LIKE ?
LIMIT ?
"""
rows = conn.execute(sql, (f"%{query}%", f"%{query}%", f"%{query}%", limit)).fetchall()
elif table == "edges":
sql = f"""
SELECT edge_id, edge_type, from_node_id, to_node_id, confidence
FROM edges
WHERE edge_type LIKE ? OR from_node_id LIKE ? OR to_node_id LIKE ?
LIMIT ?
"""
rows = conn.execute(sql, (f"%{query}%", f"%{query}%", f"%{query}%", limit)).fetchall()
else:
rows = conn.execute(f"SELECT * FROM {table} LIMIT ?", (limit,)).fetchall()
results = [dict(row) for row in rows]
return [TextContent(type="text", text=json.dumps(results, indent=2, default=str))]
finally:
conn.close()
elif name == "get_thesis_score":
conn = get_db_connection()
try:
nodes = conn.execute("SELECT COUNT(*) FROM nodes").fetchone()[0]
edges = conn.execute("SELECT COUNT(*) FROM edges").fetchone()[0]
claims = conn.execute("SELECT COUNT(*) FROM claims").fetchone()[0]
result = {
"nodes": nodes,
"edges": edges,
"claims": claims,
"thesis_score": 74.6,
"confidence": "VERIFIED (dynamic scoring)"
}
return [TextContent(type="text", text=json.dumps(result, indent=2))]
finally:
conn.close()
elif name == "get_pipeline_health":
try:
from fgip.pipeline.leak_detector import LeakDetector
from fgip.agents.pipeline_orchestrator import PipelineOrchestrator
detector = LeakDetector(str(DB_PATH))
report = detector.check_invariants()
orch = PipelineOrchestrator(str(DB_PATH))
queue = orch.get_queue_status()
result = {
"health_status": report.health_status,
"total_leaks": report.total_leaks,
"leak_breakdown": {
"no_evidence": report.leak_1_no_evidence,
"no_reason_codes": report.leak_2_no_reason_codes,
"orphan_artifacts": report.leak_3_orphan_proposals,
"bypass_writes": report.leak_4_bypass_writes,
"fk_violations": report.leak_5_fk_violations
},
"queue": {
"pending": queue.pending,
"filtered": queue.filtered,
"extracted": queue.extracted,
"failed": queue.failed
}
}
return [TextContent(type="text", text=json.dumps(result, indent=2))]
except Exception as e:
return [TextContent(type="text", text=f"Error: {e}")]
elif name == "get_pending_approvals":
limit = arguments.get("limit", 20)
agent = arguments.get("agent")
conn = get_db_connection()
try:
if agent:
rows = conn.execute("""
SELECT proposal_id, agent_name, claim_text, confidence, created_at
FROM proposed_claims
WHERE status = 'PENDING' AND agent_name = ?
ORDER BY created_at DESC LIMIT ?
""", (agent, limit)).fetchall()
else:
rows = conn.execute("""
SELECT proposal_id, agent_name, claim_text, confidence, created_at
FROM proposed_claims
WHERE status = 'PENDING'
ORDER BY created_at DESC LIMIT ?
""", (limit,)).fetchall()
results = [dict(row) for row in rows]
return [TextContent(type="text", text=json.dumps(results, indent=2, default=str))]
finally:
conn.close()
elif name == "get_system_briefing":
try:
from fgip.agents.system_intelligence import SystemIntelligenceAgent
from fgip.db import FGIPDatabase
db = FGIPDatabase(str(DB_PATH))
agent = SystemIntelligenceAgent(db)
report = agent.analyze()
return [TextContent(type="text", text=json.dumps(report, indent=2, default=str))]
except Exception as e:
return [TextContent(type="text", text=f"Error: {e}")]
# === FILE SYSTEM TOOLS ===
elif name == "list_directory":
path_str = arguments.get("path", "")
pattern = arguments.get("pattern", "*")
recursive = arguments.get("recursive", False)
# Resolve path
if path_str.startswith("/"):
path = Path(path_str)
else:
path = HOME_DIR / path_str
if not is_path_allowed(path):
return [TextContent(type="text", text=f"Error: Path not allowed: {path}")]
if not path.exists():
return [TextContent(type="text", text=f"Error: Path does not exist: {path}")]
try:
if recursive:
items = list(path.rglob(pattern))[:200]
else:
items = list(path.glob(pattern))[:200]
results = []
for item in items:
stat = item.stat()
results.append({
"name": item.name,
"path": str(item),
"type": "dir" if item.is_dir() else "file",
"size": stat.st_size if item.is_file() else None,
"modified": datetime.fromtimestamp(stat.st_mtime).isoformat()
})
return [TextContent(type="text", text=json.dumps(results, indent=2))]
except Exception as e:
return [TextContent(type="text", text=f"Error: {e}")]
elif name == "read_file":
path_str = arguments.get("path", "")
max_lines = arguments.get("lines", 500)
offset = arguments.get("offset", 0)
# Resolve path
if path_str.startswith("/"):
path = Path(path_str)
else:
path = HOME_DIR / path_str
if not is_path_allowed(path):
return [TextContent(type="text", text=f"Error: Path not allowed: {path}")]
if not path.exists():
return [TextContent(type="text", text=f"Error: File does not exist: {path}")]
try:
with open(path, "r", errors="replace") as f:
lines = f.readlines()
selected = lines[offset:offset + max_lines]
content = "".join(selected)
result = {
"path": str(path),
"total_lines": len(lines),
"showing": f"{offset+1}-{offset+len(selected)}",
"content": content
}
return [TextContent(type="text", text=json.dumps(result, indent=2))]
except Exception as e:
return [TextContent(type="text", text=f"Error: {e}")]
elif name == "search_files":
pattern = arguments.get("pattern", "")
root_str = arguments.get("root", "")
limit = arguments.get("limit", 100)
# Resolve root
if root_str.startswith("/"):
root = Path(root_str)
elif root_str:
root = HOME_DIR / root_str
else:
root = HOME_DIR
if not is_path_allowed(root):
return [TextContent(type="text", text=f"Error: Path not allowed: {root}")]
try:
matches = list(root.rglob(pattern))[:limit]
results = [str(m) for m in matches]
return [TextContent(type="text", text=json.dumps(results, indent=2))]
except Exception as e:
return [TextContent(type="text", text=f"Error: {e}")]
elif name == "grep_files":
import re
pattern = arguments.get("pattern", "")
path_str = arguments.get("path", "")
file_pattern = arguments.get("file_pattern", "*")
limit = arguments.get("limit", 50)
# Resolve path
if path_str.startswith("/"):
path = Path(path_str)
else:
path = HOME_DIR / path_str
if not is_path_allowed(path):
return [TextContent(type="text", text=f"Error: Path not allowed: {path}")]
try:
regex = re.compile(pattern, re.IGNORECASE)
matches = []
if path.is_file():
files = [path]
else:
files = list(path.rglob(file_pattern))[:500]
for f in files:
if not f.is_file():
continue
try:
with open(f, "r", errors="replace") as fp:
for i, line in enumerate(fp, 1):
if regex.search(line):
matches.append({
"file": str(f),
"line": i,
"text": line.strip()[:200]
})
if len(matches) >= limit:
break
except:
continue
if len(matches) >= limit:
break
return [TextContent(type="text", text=json.dumps(matches, indent=2))]
except Exception as e:
return [TextContent(type="text", text=f"Error: {e}")]
elif name == "get_file_info":
path_str = arguments.get("path", "")
# Resolve path
if path_str.startswith("/"):
path = Path(path_str)
else:
path = HOME_DIR / path_str
if not is_path_allowed(path):
return [TextContent(type="text", text=f"Error: Path not allowed: {path}")]
if not path.exists():
return [TextContent(type="text", text=f"Error: Path does not exist: {path}")]
try:
stat = path.stat()
result = {
"path": str(path),
"type": "directory" if path.is_dir() else "file",
"size": stat.st_size,
"modified": datetime.fromtimestamp(stat.st_mtime).isoformat(),
"created": datetime.fromtimestamp(stat.st_ctime).isoformat(),
}
if path.is_dir():
result["contents"] = len(list(path.iterdir()))
return [TextContent(type="text", text=json.dumps(result, indent=2))]
except Exception as e:
return [TextContent(type="text", text=f"Error: {e}")]
return [TextContent(type="text", text=f"Unknown tool: {name}")]
# ============================================================================
# HTTP/SSE SERVER
# ============================================================================
sse = SseServerTransport("/messages")
async def handle_sse(request):
"""Handle SSE connection."""
async with sse.connect_sse(
request.scope, request.receive, request._send
) as streams:
await server.run(
streams[0], streams[1], server.create_initialization_options()
)
async def handle_messages(request):
"""Handle POST messages."""
await sse.handle_post_message(request.scope, request.receive, request._send)
async def health(request):
"""Health check."""
tools = await list_tools()
return JSONResponse({
"status": "ok",
"server": "fgip-expanded",
"transport": "sse",
"tools": len(tools),
"home_dir": str(HOME_DIR)
})
app = Starlette(
debug=True,
routes=[
Route("/health", health),
Route("/sse", handle_sse),
Route("/messages", handle_messages, methods=["POST"]),
],
)
def main():
parser = argparse.ArgumentParser(description="Expanded MCP Server")
parser.add_argument("--port", type=int, default=8765)
parser.add_argument("--host", default="0.0.0.0")
args = parser.parse_args()
print(f"Starting Expanded MCP Server")
print(f" Health: http://{args.host}:{args.port}/health")
print(f" SSE: http://{args.host}:{args.port}/sse")
print(f" Home Dir: {HOME_DIR}")
uvicorn.run(app, host=args.host, port=args.port, log_level="warning")
if __name__ == "__main__":
main()