OpenTool SDK for Python
English | 中文
OpenTool SDK for Python provides the core building blocks to host or consume OpenTool-compatible tools in Python: an HTTP/JSON-RPC server runtime, an HTTP client with unary + SSE streaming calls, and a JSON schema loader. This project is designed to align with opentool-dart so multi-language SDK behavior stays consistent.
Library Entrypoints
opentool_python.opentool_server- server runtime, CLI argument helper, and Tool base class.opentool_python.opentool_client- HTTP/JSON-RPC client plus DTO/LLM models.opentool_python.opentool_schema- JSON specification models and loader.
Features
OpenToolServermounts/opentoolJSON-RPC endpoints backed by yourToolimplementation, with optional Bearer API-key auth.OpenToolClientsupports version lookup, unarycall, SSEstream_call, metadataload, and remotestop.OpenToolJsonLoaderparses OpenTool JSON specs and resolves$refschema references.- End-to-end examples in
example/server.pyandexample/client.pydemonstrate CRUD + streaming usage.
Requirements
- Python
>=3.10 - Schema validation uses
jsonschemaandreferencing, installed with the SDK.
Installation
Install from PyPI:
python -m pip install opentool-python
Install as editable package in local development:
pip install -e .
Or install with source path from another project:
pip install /absolute/path/to/opentool-python
Quick Start
Implement a Tool
from pathlib import Path
from opentool_python.json_loader import OpenToolJsonLoader
from opentool_python.opentool_server import (
CliTool,
EventType,
FunctionNotSupportedException,
OpenTool,
)
class MockTool(CliTool):
def call(self, name, arguments):
if name == "count":
return {"count": 1}
return FunctionNotSupportedException(function_name=name).to_json()
def stream_call(self, name, arguments, on_event):
if name == "sequentiallyRead":
on_event(EventType.DATA, {"data": "Hello"})
else:
on_event(
EventType.ERROR,
FunctionNotSupportedException(function_name=name).to_json(),
)
def load(self) -> OpenTool | None:
return OpenToolJsonLoader().load_from_file(
str(Path("example/mock_tool.json"))
)
Launch the server
PYTHONPATH=src python example/server.py
CliArguments can pass built-in and custom CLI switches into your CliTool.init_args, so you can preload state before handlers accept requests.
Call the tool from Python
from opentool_python.opentool_client import FunctionCall, HostType, OpenToolClient
client = OpenToolClient(
tool_host=HostType.LOCALHOST,
tool_port=17002,
tool_api_key="your-key",
)
print(client.version().to_json())
print(client.call(FunctionCall(id="call-0", name="count", arguments={})).to_json())
client.stream_call(
FunctionCall(id="call-1", name="sequentiallyRead", arguments={}),
lambda event, tool_return: print(event, tool_return.result),
)
print(client.load().to_json())
print(client.stop().to_json())
CLI and HTTP Reference
- Base path defaults to
/opentool. - Endpoints:
GET /versionPOST /callPOST /streamCallGET /loadreturns HTTP 404 with codeTOOL_NOT_FOUNDwhen metadata is unavailable.POST /stop
- Built-in CLI flags (via
CliArguments):--opentoolServerTag--opentoolServerHost--opentoolServerPort- repeatable
--opentoolServerApiKeys
- Streaming protocol is Server-Sent Events with event names
start,data,error, anddone.
JSON Specification Loader
- Import
opentool_python.opentool_schemaif you only need spec models and parsing. OpenToolJsonLoader().load_from_file(path)loads local files and resolves$refentries under#/schemas/*.OpenToolJsonLoader().load(json_string)loads from raw JSON strings.- Specification reference: OpenTool Spec.
Schemas default to JSON Schema Draft 2020-12. A recognized $schema selects its declared draft. Schema.to_json() returns a dictionary or boolean and preserves references, annotations, and extension keywords. Named #/schemas/... references are scoped to each OpenTool document, including documents loaded over HTTP.
from opentool_python.opentool_schema import Schema
schema = Schema.from_json({"type": "string", "minLength": 3})
result = schema.validate("ab")
print(result.is_valid, result.errors)
Use validate_formats=True to assert formats. To supply external schemas, pass a synchronous schema_reference_resolver callback to OpenTool.from_json, OpenToolJsonLoader, or OpenToolClient. The callback receives the resource URI and returns an object, a boolean, or None if unavailable.
Examples and Development
- Example files:
example/server.pyexample/client.pyexample/mock_tool.json
- Common local workflow:
python -m compileall src example
PYTHONPATH=src python example/server.py
PYTHONPATH=src python example/client.py
Compatibility Note
This Python SDK is currently a baseline implementation focused on API shape and protocol compatibility with opentool-dart.
Metadata
Release files for opentool-python 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| opentool_python-0.1.0.tar.gz | 19.2 kB | Without provenance | Details for opentool_python-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| opentool_python-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for opentool_python-0.1.0-py3-none-any.whl |
Total release size: 44.9 kB
Release files / opentool_python-0.1.0.tar.gz
| Download URL | opentool_python-0.1.0.tar.gz |
|---|---|
| Size | 19.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4c3891f428c244fa109c9b0de7c6fc6541365a1a715c7458c2208c62506914cf
|
|
BLAKE2b-256 checksum How to use checksums |
8156af286d7ed8d9c5f43f7c630387754ec7a5e916a8ef069aa7edff907e4a00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Release files / opentool_python-0.1.0-py3-none-any.whl
| Download URL | opentool_python-0.1.0-py3-none-any.whl |
|---|---|
| Size | 25.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4723fb880fc64627641c009a4dbee5fd1afe7da29728a072400a50f38b0a19cd
|
|
BLAKE2b-256 checksum How to use checksums |
32af8609a366b5b9bed8106c71bbd6653cea330bc72841057091f72a6db9389a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Security for opentool-python 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.