A Python client for Tool Server Protocol (TSP)
Project description
pyTSPClient
A lightweight Python client for the Tool Server Protocol (TSP). It provides a simple, asynchronous way to interact with tool servers (like gtsp) via the stdio mode.
Features
- Asynchronous IO: Built with
asyncio. - Full Protocol Support: Methods for
initialize,tool,sandbox, andshutdown. - Event Handling: Easy subscription to server-sent events.
- Error Handling: Custom
TSPExceptionwith error codes. - Logging: Captures server
stderrfor easier debugging.
Installation
For Users
pip install pytspclient
For Developers (Editable Mode)
If you are developing locally:
git clone https://github.com/alexazhou/TSP.git
cd TSP/client/pytspclient
pip install -e .
Quick Start
import asyncio
from pytspclient import TSPClient, TSPException
async def main():
# command to launch the TSP server
client = TSPClient(["./gtsp", "--mode", "stdio"])
# 1. Connect (starts the subprocess)
await client.connect()
try:
# 2. Initialize handshake
# protocol_version is hardcoded to 0.3 internally
init_data = await client.initialize(
client_info={"name": "my-agent"},
include=["read_file", "write_file"]
)
print(f"Connected to: {init_data.server_info.get('name')}")
# 3. Call a tool
try:
result = await client.tool("read_file", {"file_path": "test.txt"})
print(f"File content: {result['content']}")
except TSPException as e:
print(f"Tool failed: [{e.code}] {e.message}")
# 4. Graceful Shutdown
await client.shutdown()
finally:
# Ensure resources are cleaned up
await client.disconnect()
if __name__ == "__main__":
asyncio.run(main())
API Reference
TSPClient
__init__(command: List[str], request_timeout_sec: int = 30)
command: The shell command to run the TSP server.request_timeout_sec: Timeout for each request.
async connect()
Spawns the TSP server process and starts the internal read loops for stdout and stderr.
async disconnect()
Forcefully terminates the process and fails all pending requests.
async initialize(...) -> TSPInitializeResult
Handshake with the server. Protocol version is internally set to 0.3.
Parameters:
client_info: optional metadata about the client.include: optional list of tools to enable.exclude: optional list of tools to disable.
Returns a TSPInitializeResult object.
async tool(tool_name: str, input_params: Dict[str, Any]) -> Dict[str, Any]
Executes a specific tool on the server.
async sandbox(config: Dict[str, Any]) -> Dict[str, Any]
Configures the server's sandbox/workspace environment.
async shutdown()
Sends a shutdown request and then calls disconnect().
add_event_handler(handler: Callable[[TSPEvent], None])
Registers a callback for server-sent events.
def on_event(event: TSPEvent):
print(f"Received event: {event.event} with data: {event.data}")
client.add_event_handler(on_event)
Data Classes
TSPInitializeResult
protocol_version: strcapabilities: Dict[str, Any]server_info: Dict[str, Any]
Exceptions
TSPException
Raised when the server returns an error response.
code: The error code (e.g.,tsp/error,tool/not_found).message: Human-readable error message.
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytspclient-0.2.2.tar.gz.
File metadata
- Download URL: pytspclient-0.2.2.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a854d137c78f4dbdf51c6e998aced8f3830a6ce215b28cc4c5ebd3fc4a79e6a8
|
|
| MD5 |
6049aeb5de9ca5351b6148631de6d4ec
|
|
| BLAKE2b-256 |
9e8a71227d38932c6fc7e85aceee1173a81553eab4901af5b4b1f1cb338e873c
|
File details
Details for the file pytspclient-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pytspclient-0.2.2-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b0c925e763b40a47f331fbf896809073fd432ffe857cc0cc230b4df9da72187
|
|
| MD5 |
528e6b6a60dd90ff4116cf505ba4f977
|
|
| BLAKE2b-256 |
ee8ef5721cc831e440ec388be3081af61b0fc3bb6f3d17d4dbbef40ae4a9fd1f
|