44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
import asyncio
|
|
from fastmcp import Client
|
|
|
|
async def main():
|
|
"""Demonstrates client usage against the MemoryServer.
|
|
|
|
The client launches the server as a subprocess using ``python -u memory_server.py``.
|
|
It then performs a series of tool calls to showcase the available API.
|
|
"""
|
|
# Launch the server process. ``-u`` forces unbuffered stdout which is required by FastMCP stdio transport.
|
|
client = Client("python -u memory_server.py")
|
|
await client.connect()
|
|
|
|
try:
|
|
# 1. Save a value in the default namespace
|
|
saved = await client.call_tool(
|
|
"save_with_namespace",
|
|
{"key": "username", "value": "Alexey", "namespace": "default"},
|
|
)
|
|
print(f"Saved: {saved}")
|
|
|
|
# 2. Retrieve all entries from the default namespace
|
|
data = await client.call_tool(
|
|
"get_by_namespace",
|
|
{"namespace": "default"},
|
|
)
|
|
print("Namespace 'default' contents:")
|
|
for k, v in data.items():
|
|
print(f" {k}: {v['value']} (saved at {v['timestamp']})")
|
|
|
|
# 3. List keys matching a pattern
|
|
keys = await client.call_tool(
|
|
"list_keys_in_namespace",
|
|
{"namespace": "default", "pattern": "*name"},
|
|
)
|
|
print("Keys matching '*name':", keys)
|
|
|
|
finally:
|
|
# Gracefully close the client and terminate the server process.
|
|
await client.close()
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|