@edgen-ai/rpc-tool v1.0.0
LangChain StructuredTools JSON-RPC Server
A powerful and flexible JSON-RPC server that exposes LangChain's StructuredTools as JSON-RPC endpoints. This package enables seamless integration between LangChain tools and any application that supports JSON-RPC 2.0, allowing you to leverage AI tools in various environments.
Features
- Automatic Tool Conversion: Transforms LangChain StructuredTools into JSON-RPC methods with preserved metadata
- Dual Transport Support: Offers both HTTP and WebSocket interfaces for flexible integration
- Schema Validation: Validates parameters using Zod schemas from the original tools
- OpenRPC Discovery: Implements the
rpc.discover
method for automatic API documentation - Comprehensive Error Handling: Provides detailed error messages with appropriate error codes
- CORS Support: Enables cross-origin requests for web applications
- Bidirectional Communication: Supports both client-to-server and server-to-client communication via WebSockets
- Request Logging: Includes built-in logging for debugging and monitoring
Installation
# Install the package
npm install @edgen-ai/rpc-tool
# Install peer dependencies if not already installed
npm install @langchain/core @open-rpc/server-js @open-rpc/client-js
Usage
Server-Side: Exposing LangChain Tools
import { StructuredToolsRPCServer } from '@edgen-ai/rpc-tool';
import { TACalculateTool, ArtemisMetricsTool } from '@edgen-ai/tools';
async function startServer() {
// Collect all StructuredTools you want to expose
const tools = [
new TACalculateTool(),
new ArtemisMetricsTool('YOUR_API_KEY')
];
// Create and start the RPC server
const rpcServer = new StructuredToolsRPCServer(tools);
// Configure ports and host
const httpPort = 3030;
const wsPort = 3031;
const host = '0.0.0.0'; // Use 0.0.0.0 to listen on all interfaces
await rpcServer.start(httpPort, wsPort, host);
console.log(`Server running at http://${host}:${httpPort} and ws://${host}:${wsPort}`);
}
startServer();
Client-Side: Consuming JSON-RPC Tools
HTTP Client Example
import { Client, HTTPTransport, RequestManager } from "@open-rpc/client-js";
// Create client with HTTP transport
const client = new Client(new RequestManager([new HTTPTransport("http://localhost:3030")]));
/**
* Example: Call the TA Calculate tool
*/
async function exampleTaCalculate() {
try {
const result = await client.request({
method: 'ta-calculate',
params: [{
in_real: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
rsi: {
opt_in_time_period: 5
}
}]
});
console.log('TA Calculate Result:');
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error calling ta-calculate:', error);
}
}
// Get OpenRPC document for API discovery
async function getOpenRPCDocument() {
const result = await client.request({
method: 'rpc.discover',
params: []
});
return result;
}
// Run the examples
async function runExamples() {
console.log('Running JSON-RPC client examples...');
// Get OpenRPC document
const openrpcDocument = await getOpenRPCDocument();
console.log('OpenRPC Document:', JSON.stringify(openrpcDocument, null, 2));
// Run the examples
await exampleTaCalculate();
console.log('Examples completed.');
}
runExamples().catch(console.error);
WebSocket Client Example
import { Client, WebSocketTransport, RequestManager } from "@open-rpc/client-js";
// Create client with WebSocket transport
const client = new Client(new RequestManager([new WebSocketTransport("ws://localhost:3031")]));
// The rest of the code is the same as the HTTP example
async function exampleTaCalculate() {
try {
const result = await client.request({
method: 'ta-calculate',
params: [{
in_real: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
rsi: {
opt_in_time_period: 5
}
}]
});
console.log('TA Calculate Result:');
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error calling ta-calculate:', error);
}
}
Using the RPC Tool Generator
The package also provides a client-side tool generator that can automatically create LangChain tools from a remote JSON-RPC server:
import Client, { HTTPTransport, RequestManager } from "@open-rpc/client-js";
import { RpcToolGenerator } from "@edgen-ai/rpc-tool";
// Create a client connected to the RPC server and generate tools
new RpcToolGenerator({
client: new Client(new RequestManager([new HTTPTransport("http://localhost:3030")]))
}).generateRpcTools().then((tools) => {
console.log(`Generated ${tools.length} tools from the server`);
// Example: Call the TA Calculate tool
tools.filter((tool) => tool.name === 'ta-calculate')[0].invoke({
in_real: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
rsi: {
opt_in_time_period: 5
}
}).then((result) => {
console.log(result);
});
});
Environment Variables
You can configure the server using the following environment variables:
Variable | Description | Default |
---|---|---|
JSON_RPC_PORT | HTTP port for the JSON-RPC server | 3030 |
JSON_RPC_WS_PORT | WebSocket port for the JSON-RPC server | 3031 |
JSON_RPC_HOST | Host address to bind the server | 0.0.0.0 |
Architecture
The package consists of three main components:
- StructuredToolsRPCServer: Core server that converts LangChain tools to JSON-RPC methods
- RPCTool: Client-side representation of a remote JSON-RPC method as a LangChain tool
- RpcToolGenerator: Utility to discover and generate client tools from a remote server
OpenRPC Discovery
The server implements the standard rpc.discover
method, which returns an OpenRPC document describing all available methods. This enables automatic documentation and client code generation.
To view the API documentation, send a request to the rpc.discover
method:
const response = await fetch('http://localhost:3030', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'rpc.discover',
params: []
}),
});
const apiDoc = await response.json();
console.log(apiDoc.result);
Error Handling
The server provides detailed error messages following the JSON-RPC 2.0 specification:
Error Code | Description |
---|---|
-32600 | Invalid Request |
-32601 | Method not found |
-32602 | Invalid params (validation errors) |
-32000 | Server error |
Error responses include detailed information to help diagnose issues:
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32602,
"message": "Parameter validation failed: data: Required",
"data": {
"details": [
{
"path": ["data"],
"message": "Required"
}
]
}
}
}
Advanced Configuration
You can customize the server by providing additional options to the StructuredToolsRPCServer
constructor:
const rpcServer = new StructuredToolsRPCServer(tools, {
openrpcDocument: {
openrpc: '1.2.6',
info: {
title: 'Custom API Title',
version: '2.0.0',
description: 'Custom API description'
},
methods: [] // Will be populated automatically
}
});
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
6 months ago