Type-safe JSON-RPC 2.0 server implementation.
This package provides a transport-agnostic JSON-RPC 2.0 server that handles protocol details while allowing you to define your own method handlers and context.
npm install @ts-json-rpc/server @ts-json-rpc/coreimport { createJsonRpcServer } from '@ts-json-rpc/server';
// Define your method handlers
const methods = {
add: (params: { a: number; b: number }) => {
return params.a + params.b;
},
subtract: async (params: { a: number; b: number }) => {
return params.a - params.b;
},
greet: (params: { name: string }, context: { userId?: string }) => {
return `Hello, ${params.name}! User ID: ${context.userId || 'anonymous'}`;
},
};
// Create server
const server = createJsonRpcServer(methods, {
strictMethodHandling: true,
logger: console,
});
// Handle incoming requests
app.post('/api/jsonrpc', async (req, res) => {
const response = await server.handleJsonRpcRequest(req.body, {
userId: req.user?.id,
});
if (response !== null) {
res.json(response);
} else {
res.status(204).send(); // No response for notifications
}
});createJsonRpcServer<TContext>(methods: JsonRpcMethodMap<TContext>, options?: JsonRpcServerOptions): JsonRpcServerInstance
Creates a new JSON-RPC server with the provided method handlers and options.
Type for method handler functions: (params: TParams, context: TContext) => Promise<TResult> | TResult
logger- Optional logger withinfo,warn,errormethods (defaults toconsole)strictMethodHandling- Boolean, defaulttrue. Iftrue, calls to unregistered methods return "Method not found" error
handleJsonRpcRequest<TContext>(rawJsonPayload: unknown, context?: TContext): Promise<unknown>- Process JSON-RPC requests
The server automatically handles standard JSON-RPC errors:
- Parse Error (-32700): Invalid JSON
- Invalid Request (-32600): Invalid JSON-RPC structure
- Method Not Found (-32601): Unknown method (when strictMethodHandling is true)
- Invalid Params (-32602): Thrown by method handlers for invalid parameters
- Internal Error (-32603): Unhandled exceptions in method handlers
Method handlers can throw custom errors by using createJsonRpcError from @ts-json-rpc/core.
MIT