okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1#!/usr/bin/env node 2/** 3* The app's own tool server for the tools a person writes in Settings -> Tools. 4* 5* node custom-tools-mcp.mjs instructions <tools.json> one tool per instruction tool: calling it returns the person's text 6* node custom-tools-mcp.mjs commands <tools.json> one tool per command tool: calling it runs the person's command 7* 8* It speaks the Model Context Protocol over stdio (newline-delimited JSON-RPC), the transport the Harness's MCP client uses for a program 9* it starts. The file is re-read on every request, so a change in Settings is seen without restarting anything. The assistant's text for 10* a command tool reaches the command in the STRATA_INPUT environment variable (never spliced into the command line, so it cannot add 11* commands of its own). A command runs in the shell that suits it (PowerShell for a Windows command that reads as PowerShell, cmd.exe for 12* the rest, sh elsewhere), in its own folder or the one the assistant names, gets 2 minutes unless its tool allows more (up to an hour) and 13* 20,000 characters of output (custom-command-run.mjs). 14*/ 15import { readFileSync } from 'node:fs' ; 16import { createInterface } from 'node:readline' ; 17import { runCommand } from './custom-command-run.mjs' ; 18 19const [ mode , file ] = process . argv . slice ( 2 ); 20if ( ! [ 'instructions' , 'commands' ]. includes ( mode ) || ! file ) { console . error ( 'usage: custom-tools-mcp.mjs instructions|commands <tools.json>' ); process . exit ( 2 ); } 21 22const load = () => { try { return JSON . parse ( readFileSync ( file , 'utf8' ))[ mode ] ?? {}; } catch { return {}; } }; 23 24const schema = mode === 'commands' 25 ?{ type :'object' , properties :{ 26input :{ type :'string' , description :'Text to give the command (optional).' }, 27cwd :{ type :'string' , description :'The folder to run the command in (optional). Give the working folder of this chat when the command uses relative paths.' }, 28}, additionalProperties :false } 29 :{ type :'object' , properties :{}, additionalProperties :false }; 30 31function listTools () { 32return Object . entries ( load ()). map (([ name , t ]) => ({ name, description :String ( t . description ?? '' ), inputSchema :schema , annotations :mode === 'instructions' ?{ readOnlyHint :true } :{} })); 33} 34 35async function call ( name , args ) { 36const t = load ()[ name ]; 37if ( ! t ) return { isError :true , content :[{ type :'text' , text :'That tool is no longer available.' }] }; 38if ( mode === 'instructions' ) return { content :[{ type :'text' , text :String ( t . text ?? '' ) }] }; 39const r = await runCommand ({ command :String ( t . command ?? '' ), input :args ?. input , callCwd :args ?. cwd , cwd :t . cwd , shell :t . shell , timeoutSec :t . timeoutSec }); 40return { isError :! r . ok , content :[{ type :'text' , text :r . text }] }; 41} 42 43const send = message => process . stdout . write ( JSON . stringify ({ jsonrpc :'2.0' , ...message }) + '\n' ); 44 45async function handle ( line ) { 46let msg ; 47try { msg = JSON . parse ( line ); } catch { return ; } 48const { id, method, params} = msg ; 49if ( id === undefined ) return ; // notifications need no answer 50try { 51if ( method === 'initialize' ) { 52return send ({ id, result :{ protocolVersion :params ?. protocolVersion ?? '2024-11-05' , capabilities :{ tools :{ listChanged :false } }, serverInfo :{ name :`strata- ${ mode } ` , version :'1.0.0' } } }); 53} 54if ( method === 'ping' ) return send ({ id, result :{} }); 55if ( method === 'tools/list' ) return send ({ id, result :{ tools :listTools () } }); 56if ( method === 'tools/call' ) return send ({ id, result :await call ( String ( params ?. name ?? '' ), params ?. arguments ?? {}) }); 57return send ({ id, error :{ code :- 32601 , message :`Method not found: ${ method } ` } }); 58} catch ( e ) { 59return send ({ id, error :{ code :- 32603 , message :String ( e ?. message ?? e ) } }); 60} 61} 62 63createInterface ({ input :process . stdin }). on ( 'line' , line => { void handle ( line ); }). on ( 'close' , () => process . exit ( 0 ));