File size: 10,614 Bytes
376ee15 | 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 | # agents/mcp_server.py
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from flask import Flask, request, jsonify
from pydantic import BaseModel
from typing import List, Optional
from datetime import datetime
from models import GraphExport
from storage import Storage
from tools.concept_store import ConceptStore
from tools.notebook_store import NotebookStore
import random
app = FastAPI(title="HMP MCP-Agent API", version="0.1")
# Добавляем CORS (полезно, если API вызывается с веб-клиента)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Можем позже ограничить, если потребуется
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Инициализация хранилищ
concept_store = ConceptStore()
notebook_store = NotebookStore()
db = Storage()
# === Модели запроса/ответа ===
class EntryInput(BaseModel):
text: str
tags: Optional[List[str]] = []
timestamp: Optional[str] = None
class EntryOutput(BaseModel):
id: int
text: str
tags: List[str]
timestamp: str
class EntryListOutput(BaseModel):
entries: List[EntryOutput]
class ConceptInput(BaseModel):
name: str
description: Optional[str] = None
class ConceptOutput(BaseModel):
concept_id: int
class LinkInput(BaseModel):
source_id: int
target_id: int
relation: str
class LinkOutput(BaseModel):
link_id: int
class Node(BaseModel):
id: str
label: str
tags: List[str] = []
class Edge(BaseModel):
source: str
target: str
relation: str
class GraphImportData(BaseModel):
nodes: List[Node] = []
edges: List[Edge] = []
class GraphExpansionOutput(BaseModel):
links: List[Edge]
class Concept(BaseModel):
concept_id: int
name: str
description: Optional[str] = None
class ConceptQueryOutput(BaseModel):
matches: List[Concept]
class DiaryEntry(BaseModel):
id: int
text: str
tags: List[str]
timestamp: str
class DiaryExport(BaseModel):
entries: List[DiaryEntry]
class ConceptExport(BaseModel):
id: int
name: str
description: Optional[str] = None
class LinkExport(BaseModel):
id: int
source_id: int
target_id: int
relation: str
class GraphExport(BaseModel):
concepts: List[ConceptExport]
links: List[LinkExport]
class ConceptUpdate(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
# === Эндпойнты ===
@app.get("/status")
def status():
return {
"status": "ok",
"agent": "HMP-MCP",
"timestamp": datetime.utcnow().isoformat()
}
@app.post("/write_entry", response_model=dict)
def write_entry(entry: EntryInput):
db.write_entry(entry.text, entry.tags)
return {"result": "entry saved"}
@app.get("/read_entries", response_model=EntryListOutput)
def read_entries(limit: int = 5, tag: Optional[str] = None):
raw = db.read_entries(limit=limit, tag_filter=tag)
return {
"entries": [
{
"id": r[0],
"text": r[1],
"tags": r[2].split(",") if r[2] else [],
"timestamp": r[3]
} for r in raw
]
}
@app.get("/")
def root():
return {"message": "HMP MCP-Agent API is running"}
@app.post("/add_concept", response_model=ConceptOutput)
def add_concept(concept: ConceptInput):
cid = db.add_concept(concept.name, concept.description)
return {"concept_id": cid}
@app.post("/add_link", response_model=LinkOutput)
def add_link(link: LinkInput):
link_id = db.add_link(link.source_id, link.target_id, link.relation)
return {"link_id": link_id}
@app.get("/expand_graph", response_model=GraphExpansionOutput)
def expand_graph(start_id: int, depth: int = 1):
raw_links = db.expand_graph(start_id, depth)
edges = [{"source_id": s, "target_id": t, "relation": r} for s, t, r in raw_links]
return {"links": edges}
@app.get("/query_concept", response_model=ConceptQueryOutput)
def query_concept(name: str):
results = db.query_concept(name)
return {
"matches": [
{"concept_id": row[0], "name": row[1], "description": row[2]}
for row in results
]
}
@app.get("/list_concepts", response_model=List[Concept])
def list_concepts():
rows = db.list_concepts()
return [
{"concept_id": row[0], "name": row[1], "description": row[2]}
for row in rows
]
@app.get("/list_links", response_model=List[Edge])
def list_links():
rows = db.list_links()
return [
{"source_id": row[1], "target_id": row[2], "relation": row[3]}
for row in rows
]
@app.delete("/delete_concept/{concept_id}")
def delete_concept(concept_id: int):
db.delete_concept(concept_id)
return {"result": f"concept {concept_id} deleted"}
@app.delete("/delete_link/{link_id}")
def delete_link(link_id: int):
db.delete_link(link_id)
return {"result": f"link {link_id} deleted"}
@app.delete("/delete_entry/{entry_id}")
def delete_entry(entry_id: int):
db.delete_entry(entry_id)
return {"result": f"entry {entry_id} deleted"}
@app.get("/export_diary", response_model=DiaryExport)
def export_diary():
rows = db.export_diary()
return {
"entries": [
{
"id": r[0],
"text": r[1],
"tags": r[2].split(",") if r[2] else [],
"timestamp": r[3]
}
for r in rows
]
}
@app.get("/export_graph", response_model=GraphExport)
def export_graph():
return concept_store.export_as_json()
@app.put("/update_concept/{concept_id}")
def update_concept(concept_id: int, update: ConceptUpdate):
db.update_concept(concept_id, update.name, update.description)
return {"result": f"concept {concept_id} updated"}
@app.get("/tag_stats", response_model=dict)
def tag_stats():
return db.get_tag_stats()
@app.get("/search_links", response_model=List[LinkExport])
def search_links(relation: str):
rows = db.search_links_by_relation(relation)
return [
{
"id": row[0],
"source_id": row[1],
"target_id": row[2],
"relation": row[3]
}
for row in rows
]
@app.get("/search_concepts", response_model=List[Concept])
def search_concepts(query: str):
results = db.search_concepts(query)
return [
{"concept_id": row[0], "name": row[1], "description": row[2]}
for row in results
]
@app.post("/merge_concepts", response_model=dict)
def merge_concepts(source_id: int, target_id: int):
db.merge_concepts(source_id, target_id)
return {"result": f"concept {source_id} merged into {target_id}"}
@app.post("/relate_concepts", response_model=LinkOutput)
def relate_concepts(source_name: str, target_name: str, relation: str):
sid = db.find_concept_id_by_name(source_name)
tid = db.find_concept_id_by_name(target_name)
if sid is None or tid is None:
raise HTTPException(status_code=404, detail="Concept not found")
link_id = db.add_link(sid, tid, relation)
return {"link_id": link_id}
@app.get("/tag_cloud", response_model=dict)
def tag_cloud():
return db.get_tag_stats()
@app.get("/get_concept/{concept_id}")
def get_concept(concept_id: str):
concept = concept_store.get(concept_id)
if concept:
return concept
raise HTTPException(status_code=404, detail="Concept not found")
@app.get("/get_entry/{entry_id}")
def get_entry(entry_id: str):
entry = notebook_store.get(entry_id)
if entry:
return entry
raise HTTPException(status_code=404, detail="Entry not found")
@app.post("/search_entries")
def search_entries(query: str):
results = notebook_store.search(query)
return results
@app.post("/import_graph")
def import_graph(graph_data: GraphImportData):
concept_store.import_from_json(graph_data.dict())
print(f"[INFO] Imported {len(graph_data.nodes)} nodes, {len(graph_data.edges)} edges")
return {"status": "ok"}
# === Notebook API ===
@app.post("/notebook/add")
async def add_note(req: Request):
data = await req.json()
text = data.get("text", "").strip()
if not text:
return {"status": "error", "message": "Empty text"}
notebook.add_note(text, source="user")
return {"status": "ok", "message": "Note added"}
@app.get("/notebook/next")
def get_next_note():
note = notebook.get_first_unread_note()
if note:
note_id, text, source, timestamp, tags = note
return {
"id": note_id,
"text": text,
"source": source,
"timestamp": timestamp,
"tags": tags
}
return {"status": "empty", "message": "No unread notes"}
@app.post("/notebook/mark_read")
async def mark_note_read(req: Request):
data = await req.json()
note_id = data.get("id")
if note_id is not None:
notebook.mark_note_as_read(note_id)
return {"status": "ok"}
return {"status": "error", "message": "Missing note id"}
# === ✨ Дополнительные эндпоинты для заметок ===
@app.route("/notes/latest", methods=["GET"])
def get_latest_notes():
"""Вернуть последние N заметок (по умолчанию 10)."""
count = int(request.args.get("count", 10))
notes = storage.diary[-count:]
return jsonify([note.to_dict() for note in notes])
@app.route("/notes/random", methods=["GET"])
def get_random_note():
"""Вернуть случайную заметку из дневника."""
if not storage.diary:
return jsonify({})
note = random.choice(storage.diary)
return jsonify(note.to_dict())
@app.route("/notes/set_tags", methods=["POST"])
def set_tags():
"""Обновить теги у заметки по ID."""
data = request.json
note_id = data.get("id")
tags = data.get("tags", [])
for note in storage.diary:
if note.id == note_id:
note.tags = tags
return jsonify({"status": "ok"})
return jsonify({"error": "not found"}), 404
@app.route("/notes/by_tag", methods=["GET"])
def get_notes_by_tag():
tag = request.args.get("tag")
result = [note.to_dict() for note in storage.diary if tag in note.tags]
return jsonify(result)
# === Run ===
if __name__ == "__main__":
uvicorn.run("mcp_server:app", host="0.0.0.0", port=8080, reload=True)
# === Shutdown ===
@app.on_event("shutdown")
def shutdown():
db.close()
|