opentool-ts
v2.0.2
Published
OpenTool client and server SDK for TypeScript, providing a JSON Spec Parser.
Maintainers
Readme
OpenTool TypeScript SDK
English | 中文
A TypeScript SDK for hosting or consuming OpenTool-compatible agents, with HTTP/JSON-RPC client and server runtimes plus a JSON schema loader.
Library Entrypoints
opentool-ts- full SDK surfaceopentool-ts/opentool-server- server runtime, CLI helpers, and tool abstractionsopentool-ts/opentool-client- HTTP client, DTOs, and LLM modelsopentool-ts/opentool-schema- JSON specification models and loader
Quick Start
Installation
npm install opentool-tsRun Examples
- Start OpenTool server:
npm run dev:server - Start OpenTool client:
npm run dev:client
Development
Environment Setup
npm install
npm run buildScript Commands
npm run build- Build TypeScript projectnpm run dev:server- Run example servernpm run dev:client- Run example clientnpm run test- Run testsnpm run clean- Clean build artifacts
Usage
Server Example
import 'reflect-metadata';
import {
CLI_ARGUMENT_APIKEYS,
CLI_ARGUMENT_HOST,
CLI_ARGUMENT_PORT,
CLI_ARGUMENT_TAG,
CliArguments,
CliTool,
OpenToolServer
} from 'opentool-ts/opentool-server';
import { MockTool } from './mock-tool';
const cliArguments = new CliArguments(process.argv.slice(2))
.addCustomMultiOption('seed');
const tool: CliTool = new MockTool();
const server = new OpenToolServer(tool, '0.0.1', {
cliArguments
});
await server.start();Supported CLI flags:
--opentoolServerTag--opentoolServerHost--opentoolServerPort--opentoolServerApiKeys(repeatable)
Client Example
import 'reflect-metadata';
import { Client, OpenToolClient, FunctionCall, ToolReturn, VersionDto, OpenTool } from 'opentool-ts/opentool-client';
const client: Client = new OpenToolClient({
apiKey: "your-api-key"
});
try {
// Check version
const version: VersionDto = await client.version();
console.log(version.toJson());
// Call function
const functionCall = new FunctionCall("callId-0", "count", {});
const result: ToolReturn = await client.call(functionCall);
console.log(result.toJson());
// Load OpenTool specification
const openTool: OpenTool | null = await client.load();
console.log(openTool?.toJson());
} catch (error) {
console.error('Client error:', error);
}Configuration
- Default port:
9627 - Both client and server support custom ports
- Tools must implement
call, can optionally implementinit,streamCall,load, anddispose CliToolcan preprocess parsed arguments ininitArgs, then receive the final payload ininit- Recommended to use OpenTool specification JSON format to describe tools
API Reference
Core Models
OpenTool: Main OpenTool specification modelFunctionModel: Function definition modelParameter: Function parameter modelReturn: Return value modelSchema: JSON schema model with type validationInfo: Tool information model
Client and Server
OpenToolClient: HTTP client for OpenTool serverOpenToolServer: HTTP server implementationController: Server request controllerTool: Abstract tool interface
LLM Integration
FunctionCall: Function call request modelToolReturn: Tool execution result model
Utilities
OpenToolJsonLoader: Load OpenTool specification from JSONuniqueId(): Generate unique ID for requests
Exception Classes
FunctionNotSupportedException: Unsupported functionToolBreakException: Tool execution errorJsonParserException: JSON parsing errorResponseNullException: Null response errorErrorNullException: Null error responseOpenToolServerUnauthorizedException: Authentication failedOpenToolServerNoAccessException: Access denied
Data Transfer Objects
VersionDto: Version informationJsonRPCHttpRequestBody: JSON-RPC request formatJsonRPCHttpResponseBody: JSON-RPC response format
System Requirements
- Node.js >= 16.0.0
- TypeScript 5.0+
Testing
- Test coverage: 96.17% overall statement coverage
- 495 tests passing across 20 test suites
- Run tests with:
npm run test
Dependencies
axios: HTTP clientexpress: Web server frameworkuuid,nanoid: ID generationclass-transformer,class-validator: Object validationreflect-metadata: Metadata reflection support
