npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@edgen-ai/rpc-tool

v1.0.0

Published

LangChain StructuredTools JSON-RPC Server

Downloads

12

Readme

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:

  1. StructuredToolsRPCServer: Core server that converts LangChain tools to JSON-RPC methods
  2. RPCTool: Client-side representation of a remote JSON-RPC method as a LangChain tool
  3. 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.