Skip to main content
Glama

MemOS-MCP

by qinshu1109
Apache 2.0
3
  • Linux
  • Apple
test_mem_ops_cli.py6.83 kB
#!/usr/bin/env python3 """ Memory Ops SDK CLI演示脚本 演示MultiMemoryMemCube适配后的Memory Ops SDK的基本功能 """ import sys import os from pathlib import Path # 添加项目根目录到Python路径 sys.path.insert(0, str(Path(__file__).parent)) from simple_memory_ops_sdk import SimpleMemoryOps as MemoryOps def demo_basic_operations(): """演示基本操作""" print("🚀 开始Memory Ops SDK演示...") # 初始化SDK print("\n1. 初始化SDK") mem = MemoryOps(verbose=True) if not mem._initialized: print("❌ SDK初始化失败") return False # 列出Memory类型 print("\n2. 列出可用的Memory类型") types = mem.list_memory_types() print(f"可用类型: {types}") # 添加不同类型的记忆 print("\n3. 添加记忆到不同类型") # 添加代码片段记忆 success1 = mem.add( "def hello_world():\n print('Hello, World!')", memory_type='code_snippet_mem', tags=["Python", "函数", "示例"], metadata={"language": "python", "complexity": "simple"} ) # 添加错误日志记忆 success2 = mem.add( "ImportError: No module named 'requests'", memory_type='error_log_mem', tags=["错误", "导入", "requests"], metadata={"error_type": "ImportError", "severity": "high"} ) # 添加一般记忆 success3 = mem.add( "今天学习了MultiMemoryMemCube的使用方法", memory_type='general_mem', tags=["学习", "MemOS", "MultiMemory"], metadata={"topic": "学习笔记", "date": "2025-07-16"} ) print(f"添加结果: code_snippet={success1}, error_log={success2}, general={success3}") # 查询记忆 print("\n4. 查询记忆") # 在特定类型中查询 print("在code_snippet_mem中查询'Python':") results1 = mem.query("Python", memory_type='code_snippet_mem', limit=3) for i, result in enumerate(results1): print(f" 结果{i+1}: {result['memory'][:50]}... (类型: {result['memory_type']})") # 在所有类型中查询 print("\n在所有类型中查询'学习':") results2 = mem.query("学习", limit=5) for i, result in enumerate(results2): print(f" 结果{i+1}: {result['memory'][:50]}... (类型: {result['memory_type']})") # 获取统计信息 print("\n5. 获取统计信息") stats = mem.stats() for mem_type, info in stats.items(): if mem_type != "timestamp": print(f" {mem_type}: {info}") # 导出记忆 print("\n6. 导出记忆") # 导出特定类型 code_memories = mem.dump(memory_type='code_snippet_mem') print(f"代码片段记忆数量: {len(code_memories)}") # 导出所有记忆到文件 all_memories = mem.dump(output_file="demo_backup.json") print(f"总记忆数量: {len(all_memories)}") # 关闭SDK print("\n7. 关闭SDK") mem.close() print("\n✅ 演示完成!") return True def demo_error_handling(): """演示错误处理""" print("\n🔧 错误处理演示...") mem = MemoryOps(verbose=True) # 测试无效的Memory类型 print("\n测试无效的Memory类型:") success = mem.add("测试内容", memory_type='invalid_type') print(f"添加到无效类型的结果: {success}") # 测试查询无效类型 print("\n测试查询无效类型:") results = mem.query("测试", memory_type='invalid_type') print(f"查询无效类型的结果数量: {len(results)}") mem.close() def interactive_demo(): """交互式演示""" print("\n🎮 交互式演示模式") print("输入 'help' 查看可用命令,输入 'quit' 退出") mem = MemoryOps(verbose=True) while True: try: command = input("\n> ").strip().lower() if command == 'quit': break elif command == 'help': print("可用命令:") print(" add <type> <content> - 添加记忆") print(" query <content> [type] - 查询记忆") print(" stats - 显示统计信息") print(" types - 显示Memory类型") print(" dump [type] - 导出记忆") print(" quit - 退出") elif command == 'types': types = mem.list_memory_types() print(f"可用类型: {types}") elif command == 'stats': stats = mem.stats() for mem_type, info in stats.items(): if mem_type != "timestamp": print(f" {mem_type}: {info}") elif command.startswith('add '): parts = command.split(' ', 3) if len(parts) >= 3: mem_type = parts[1] content = ' '.join(parts[2:]) success = mem.add(content, memory_type=mem_type) print(f"添加结果: {success}") else: print("用法: add <type> <content>") elif command.startswith('query '): parts = command.split(' ', 2) if len(parts) >= 2: query_text = parts[1] mem_type = parts[2] if len(parts) > 2 else None results = mem.query(query_text, memory_type=mem_type, limit=3) print(f"找到 {len(results)} 条结果:") for i, result in enumerate(results): print(f" {i+1}. {result['memory'][:100]}... (类型: {result['memory_type']})") else: print("用法: query <content> [type]") elif command.startswith('dump'): parts = command.split(' ', 1) mem_type = parts[1] if len(parts) > 1 else None memories = mem.dump(memory_type=mem_type) print(f"导出了 {len(memories)} 条记忆") else: print("未知命令,输入 'help' 查看帮助") except KeyboardInterrupt: print("\n\n用户中断,退出...") break except Exception as e: print(f"错误: {e}") mem.close() print("再见!") def main(): """主函数""" print("Memory Ops SDK CLI演示") print("=" * 50) if len(sys.argv) > 1 and sys.argv[1] == 'interactive': interactive_demo() else: # 运行基本演示 if demo_basic_operations(): demo_error_handling() print("\n提示: 运行 'python test_mem_ops_cli.py interactive' 进入交互模式") if __name__ == "__main__": main()

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/qinshu1109/memos-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server