Transport-level routing for MCP/ACP protocols
We appreciate your patience. The service is temporarily operating in an external runtime environment.
Transport-level routing for MCP/ACP protocols
We appreciate your patience. The service is temporarily operating in an external runtime environment.
Simple reference implementation demonstrating the NDJSON worker protocol for stdio Bus kernel.
The Echo Worker is a minimal worker implementation that echoes back any JSON-RPC messages it receives. It serves as both a testing tool for stdio Bus kernel functionality and a reference implementation for developers creating custom workers.
Key Features:
The Echo Worker serves multiple purposes:
Run the Echo Worker directly to test the NDJSON protocol:
echo '{"jsonrpc":"2.0","id":"1","method":"test","params":{"foo":"bar"}}' | \node node_modules/@stdiobus/workers-registry/workers/echo-worker/echo-worker.js
Expected output:
{"jsonrpc":"2.0","id":"1","result":{"method":"test","params":{"foo":"bar"}}}
Configuration file (echo-worker-config.json):
{"pools": [{"id": "echo-worker","command": "npx","args": ["@stdiobus/workers-registry","echo-worker"],"instances": 1}]}
Run with Docker:
docker run \--name stdiobus-echo \-p 9000:9000 \-v $(pwd):/stdiobus:ro \-v $(pwd)/echo-worker-config.json:/config.json:ro \stdiobus/stdiobus:latest \--config /config.json --tcp 0.0.0.0:9000
Run with binary:
./stdio_bus --config echo-worker-config.json --tcp 0.0.0.0:9000
Send a test message to the Echo Worker:
echo '{"jsonrpc":"2.0","id":"1","method":"echo","params":{"test":true}}' | nc localhost 9000
Response:
{"jsonrpc":"2.0","id":"1","result":{"method":"echo","params":{"test":true}}}
Minimal configuration for running a single Echo Worker instance:
{"pools": [{"id": "echo-worker","command": "npx","args": ["@stdiobus/workers-registry","echo-worker"],"instances": 1}]}
Run multiple Echo Worker instances for load testing:
{"pools": [{"id": "echo-worker","command": "npx","args": ["@stdiobus/workers-registry","echo-worker"],"instances": 4}]}
Configure resource limits for testing backpressure and error handling:
{"pools": [{"id": "echo-worker","command": "npx","args": ["@stdiobus/workers-registry","echo-worker"],"instances": 1}],"limits": {"max_input_buffer": 1048576,"max_output_queue": 4194304,"max_restarts": 5,"restart_window_sec": 60,"drain_timeout_sec": 30,"backpressure_timeout_sec": 60}}
The Echo Worker demonstrates the core NDJSON protocol requirements:
import readline from 'readline';const rl = readline.createInterface({input: process.stdin,output: process.stdout,terminal: false});rl.on('line', (line) => {try {const msg = JSON.parse(line);handleMessage(msg);} catch (err) {console.error('Parse error:', err.message);}});
function sendResponse(id, result, sessionId) {const response = {jsonrpc: '2.0',id: id,result: result};if (sessionId) {response.sessionId = sessionId;}console.log(JSON.stringify(response));}
process.on('SIGTERM', () => {console.error('Shutting down...');rl.close();});rl.on('close', () => process.exit(0));
Test that stdio Bus correctly routes messages to workers:
# Send requestecho '{"jsonrpc":"2.0","id":"1","method":"test","params":{}}' | nc localhost 9000# Verify response format# Should receive: {"jsonrpc":"2.0","id":"1","result":{"method":"test","params":{}}}
Test that messages with the same sessionId are routed to the same worker instance:
# Send multiple messages with same sessionIdecho '{"jsonrpc":"2.0","id":"1","method":"test","sessionId":"sess-123","params":{}}' | nc localhost 9000echo '{"jsonrpc":"2.0","id":"2","method":"test","sessionId":"sess-123","params":{}}' | nc localhost 9000echo '{"jsonrpc":"2.0","id":"3","method":"test","sessionId":"sess-123","params":{}}' | nc localhost 9000# All responses should come from the same worker instance
Test stdio Bus performance with multiple concurrent connections:
# Run multiple clients in parallelfor i in {1..100}; doecho '{"jsonrpc":"2.0","id":"'$i'","method":"test","params":{}}' | nc localhost 9000 &donewait
Test how stdio Bus handles malformed messages:
# Send invalid JSONecho 'not-json' | nc localhost 9000# Send JSON-RPC without required fieldsecho '{"method":"test"}' | nc localhost 9000
The Echo Worker source code serves as a reference for creating custom workers. Key implementation patterns:
function handleMessage(msg) {// Requests have an 'id' field and require a responseif (msg.id !== undefined) {const result = {method: msg.method,params: msg.params};sendResponse(msg.id, result, msg.sessionId);}// Notifications don't have an 'id' and don't require a responseelse {console.error('Received notification:', msg.method);}}
function sendError(id, code, message, sessionId) {const response = {jsonrpc: '2.0',id: id,error: {code: code,message: message}};if (sessionId) {response.sessionId = sessionId;}console.log(JSON.stringify(response));}
// Always log to stderr, never to stdoutconsole.error('Worker started');console.error('Processing message:', msg.method);console.error('Error occurred:', err.message);
When using the Echo Worker as a reference for custom workers:
console.error() for all logging and debugging