Files
task-69f8e929da860fb4533faa2a/memory_client.py
T
2026-05-28 10:04:17 +00:00

44 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Пример клиента, демонстрирующий работу с Memory‑Server через MCP.
"""
import asyncio
from fastmcp import Client
async def main() -> None:
# Подключаемся к серверу через stdio (т.е. запускаем его как дочерний процесс)
client = Client("python memory_server.py")
await client.connect()
try:
# 1️⃣ Сохраняем данные в namespace "default"
result = await client.call_tool(
"save_with_namespace",
{"key": "username", "value": "Алексей", "namespace": "default"},
)
print(f"Сохранено: {result}")
# 2️⃣ Читаем все ключи из того же namespace
items = await client.call_tool("get_by_namespace", {"namespace": "default"})
print("\nДанные namespace 'default':")
for item in items:
print(f" {item['key']}: {item['value']} (timestamp: {item['timestamp']})")
# 3️⃣ Ищем ключи по паттерну
keys = await client.call_tool("list_keys", {"pattern": "*name"})
print("\nКлючи с 'name':")
for k in keys:
print(f" {k}")
finally:
await client.close()
if __name__ == "__main__":
asyncio.run(main())