Clean old files for 69f8e929da860fb4533faa2a
This commit is contained in:
@@ -1,75 +0,0 @@
|
|||||||
from fastmcp import FastMCP
|
|
||||||
import json
|
|
||||||
from datetime import datetime
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any, Optional
|
|
||||||
import fnmatch
|
|
||||||
|
|
||||||
class MemoryServer:
|
|
||||||
def __init__(self):
|
|
||||||
self.mcp = FastMCP("Memory-Server")
|
|
||||||
self.storage_path = Path("./memory_data.json")
|
|
||||||
|
|
||||||
def _load_memory(self) -> dict:
|
|
||||||
if not self.storage_path.exists():
|
|
||||||
return {}
|
|
||||||
with open(self.storage_path, 'r', encoding='utf-8') as f:
|
|
||||||
return json.load(f)
|
|
||||||
|
|
||||||
def _save_memory(self, data: dict):
|
|
||||||
self.storage_path.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
with open(self.storage_path, 'w', encoding='utf-8') as f:
|
|
||||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
|
||||||
|
|
||||||
@self.mcp.tool()
|
|
||||||
def save(self, key: str, value: Any) -> bool:
|
|
||||||
data = self._load_memory()
|
|
||||||
data[key] = {"value": value, "timestamp": datetime.utcnow().isoformat()}
|
|
||||||
self._save_memory(data)
|
|
||||||
return True
|
|
||||||
|
|
||||||
@self.mcp.tool()
|
|
||||||
def get(self, key: str) -> Optional[dict]:
|
|
||||||
data = self._load_memory()
|
|
||||||
if key in data:
|
|
||||||
entry = data[key]
|
|
||||||
return {"key": key, "value": entry["value"], "timestamp": entry["timestamp"]}
|
|
||||||
return None
|
|
||||||
|
|
||||||
@self.mcp.tool()
|
|
||||||
def delete(self, key: str) -> bool:
|
|
||||||
data = self._load_memory()
|
|
||||||
if key in data:
|
|
||||||
del data[key]
|
|
||||||
self._save_memory(data)
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
@self.mcp.tool()
|
|
||||||
def list_keys(self, pattern: str = "*") -> list[str]:
|
|
||||||
data = self._load_memory()
|
|
||||||
return [k for k in data.keys() if fnmatch.fnmatch(k, pattern)]
|
|
||||||
|
|
||||||
@self.mcp.tool()
|
|
||||||
def save_with_namespace(self, key: str, value: Any, namespace: str = "default") -> bool:
|
|
||||||
full_key = f"{namespace}:{key}"
|
|
||||||
return self.save(full_key, value)
|
|
||||||
|
|
||||||
@self.mcp.tool()
|
|
||||||
def get_by_namespace(self, namespace: str = "default") -> list[dict]:
|
|
||||||
data = self._load_memory()
|
|
||||||
prefix = f"{namespace}:"
|
|
||||||
result = []
|
|
||||||
for k, v in data.items():
|
|
||||||
if k.startswith(prefix):
|
|
||||||
short_key = k[len(prefix):]
|
|
||||||
result.append({"key": short_key, "value": v["value"], "timestamp": v["timestamp"]})
|
|
||||||
return result
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
server = MemoryServer()
|
|
||||||
server.mcp.run(
|
|
||||||
transport="stdio",
|
|
||||||
show_banner=False,
|
|
||||||
log_level='ERROR'
|
|
||||||
)
|
|
||||||
Reference in New Issue
Block a user