diff --git a/client.py b/client.py new file mode 100644 index 0000000..857364b --- /dev/null +++ b/client.py @@ -0,0 +1,54 @@ +"""Client script that connects to the MCP server via stdio and demonstrates tool usage. + +Run this script after starting the server: ``python server.py``. +In another terminal, execute ``python client.py``. +""" + +import subprocess +import json + +# Helper to send a request via stdin and read the JSON response + +def invoke_tool(tool_name, **kwargs): + # Build the JSON request payload + payload = { + "name": tool_name, + "arguments": kwargs, + } + # Start a subprocess that runs the server in stdio mode + proc = subprocess.Popen( + ["python", "server.py"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + # Send the payload and close stdin to signal end of input + stdout, stderr = proc.communicate(json.dumps(payload)) + if stderr: + raise RuntimeError(f"Server error: {stderr.strip()}") + # Parse the server's response + return json.loads(stdout.strip()) + +# Demonstrate each tool +if __name__ == "__main__": + print("--- Saving key 'foo' with value 'bar' ---") + print(invoke_tool("save", key="foo", value="bar")) + + print("\n--- Getting key 'foo' ---") + print(invoke_tool("get", key="foo")) + + print("\n--- Listing keys ---") + print(invoke_tool("list_keys")) + + print("\n--- Saving namespaced key 'test:count' with value '1' ---") + print(invoke_tool("save_with_namespace", namespace="test", key="count", value="1")) + + print("\n--- Getting namespaced key 'test:count' ---") + print(invoke_tool("get_by_namespace", namespace="test", key="count")) + + print("\n--- Deleting key 'foo' ---") + print(invoke_tool("delete", key="foo")) + + print("\n--- Listing keys after deletion ---") + print(invoke_tool("list_keys"))