Добавить memory_client.py
This commit is contained in:
@@ -0,0 +1,87 @@
|
|||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
|
BASE_URL = "http://localhost:8000"
|
||||||
|
|
||||||
|
|
||||||
|
def save(key: str, value: str) -> dict:
|
||||||
|
"""Сохраняет значение по ключу."""
|
||||||
|
payload = {"key": key, "value": value}
|
||||||
|
response = httpx.post(f"{BASE_URL}/save", json=payload)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
def get(key: str) -> dict:
|
||||||
|
"""Получает значение по ключу."""
|
||||||
|
params = {"key": key}
|
||||||
|
response = httpx.get(f"{BASE_URL}/get", params=params)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
def delete(key: str) -> dict:
|
||||||
|
"""Удаляет запись по ключу."""
|
||||||
|
payload = {"key": key}
|
||||||
|
response = httpx.delete(f"{BASE_URL}/delete", json=payload)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
def list_keys() -> dict:
|
||||||
|
"""Возвращает список всех ключей."""
|
||||||
|
response = httpx.get(f"{BASE_URL}/list_keys")
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
def save_with_namespace(namespace: str, key: str, value: str) -> dict:
|
||||||
|
"""Сохраняет значение в указанном пространстве имён."""
|
||||||
|
payload = {"namespace": namespace, "key": key, "value": value}
|
||||||
|
response = httpx.post(f"{BASE_URL}/save_with_namespace", json=payload)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
def get_by_namespace(namespace: str, key: str) -> dict:
|
||||||
|
"""Получает значение из указанного пространства имён."""
|
||||||
|
params = {"namespace": namespace, "key": key}
|
||||||
|
response = httpx.get(f"{BASE_URL}/get_by_namespace", params=params)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""Простой тестовый сценарий для проверки работы сервера."""
|
||||||
|
print("=== Тестирование MCP-сервера ===")
|
||||||
|
|
||||||
|
# 1. Сохраняем простое значение
|
||||||
|
print("\nСохраняем ключ 'foo' со значением 'bar'")
|
||||||
|
print(json.dumps(save("foo", "bar"), indent=2))
|
||||||
|
|
||||||
|
# 2. Получаем сохранённое значение
|
||||||
|
print("\nПолучаем ключ 'foo'")
|
||||||
|
print(json.dumps(get("foo"), indent=2))
|
||||||
|
|
||||||
|
# 3. Сохраняем в пространстве имён
|
||||||
|
ns = "experiment"
|
||||||
|
print(f"\nСохраняем ключ '{ns}:baz' со значением 'qux' в namespace '{ns}'")
|
||||||
|
print(json.dumps(save_with_namespace(ns, "baz", "qux"), indent=2))
|
||||||
|
|
||||||
|
# 4. Получаем из пространства имён
|
||||||
|
print(f"\nПолучаем ключ '{ns}:baz' из namespace '{ns}'")
|
||||||
|
print(json.dumps(get_by_namespace(ns, "baz"), indent=2))
|
||||||
|
|
||||||
|
# 5. Список всех ключей
|
||||||
|
print("\nСписок всех ключей:")
|
||||||
|
print(json.dumps(list_keys(), indent=2))
|
||||||
|
|
||||||
|
# 6. Удаляем ключ 'foo'
|
||||||
|
print("\nУдаляем ключ 'foo'")
|
||||||
|
print(json.dumps(delete("foo"), indent=2))
|
||||||
|
|
||||||
|
# 7. Проверяем, что ключ удалён
|
||||||
|
print("\nПроверка удаления ключа 'foo':")
|
||||||
|
print(json.dumps(get("foo"), indent=2))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
Reference in New Issue
Block a user