57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
"""
|
||
Memory client example for the MCP memory server.
|
||
|
||
The script demonstrates how an external agent or CLI tool can connect to the
|
||
``memory_server`` via the stdio transport and invoke its tools.
|
||
|
||
It performs three operations:
|
||
1. Saves a value under ``default:username`` using ``save_with_namespace``.
|
||
2. Retrieves all items in the ``default`` namespace with ``get_by_namespace``.
|
||
3. Lists keys that match a glob pattern via ``list_keys``.
|
||
|
||
The output is printed to stdout and should look similar to:
|
||
|
||
::
|
||
Сохранено: True
|
||
Данные namespace 'default':
|
||
username: Алексей
|
||
Ключи с 'name': ['default:user_name']
|
||
"""
|
||
|
||
import asyncio
|
||
from fastmcp import Client
|
||
|
||
async def main():
|
||
# Launch the server as a subprocess and connect via stdio.
|
||
client = Client("python memory_server.py")
|
||
await client.connect()
|
||
|
||
try:
|
||
# 1. Save a value in the default namespace.
|
||
result = await client.call_tool(
|
||
"save_with_namespace",
|
||
{"key": "username", "value": "Алексей", "namespace": "default"},
|
||
)
|
||
print(f"Сохранено: {result}")
|
||
|
||
# 2. Retrieve all items in the default namespace.
|
||
items = await client.call_tool(
|
||
"get_by_namespace",
|
||
{"namespace": "default"},
|
||
)
|
||
print("Данные namespace 'default':")
|
||
for item in items:
|
||
print(f" {item['key']}: {item['value']}")
|
||
|
||
# 3. List keys matching a pattern.
|
||
keys = await client.call_tool(
|
||
"list_keys",
|
||
{"pattern": "*name"},
|
||
)
|
||
print("Ключи с 'name':", keys)
|
||
finally:
|
||
await client.close()
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main())
|