@koneeric/onetool 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 KoneEric
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,77 @@
1
- # Temporary Holding Version
1
+ # onetool
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ One MCP server in front of all the others.
4
+
5
+ ## What it is
6
+
7
+ onetool is an MCP server that stands in front of the MCP servers you already use. Your host program, the
8
+ assistant you talk to, sees two tools instead of hundreds: `run` and `search`.
9
+
10
+ - `search` finds the tools you have, by words.
11
+ - `run` takes a short JavaScript program that calls the real tools. The assistant writes the program, onetool
12
+ runs it in a process of its own, and only the program's answer comes back. The long answers of the tools in
13
+ between never reach the assistant.
14
+
15
+ You list your own servers in a settings file. onetool connects to them and offers their tools to the programs.
16
+
17
+ ## Install
18
+
19
+ You need Node 22 or later. The first thing to do is to run this command once:
20
+
21
+ ```sh
22
+ npx @koneeric/onetool init
23
+ ```
24
+
25
+ It makes the settings file, `~/.config/onetool/settings.json`, when it is not there. When the command `claude`
26
+ is found on this machine, it proposes Claude Code as a server behind onetool. It then prints the entry to paste
27
+ into the MCP settings of your host program. It takes `--config` to set up another settings file than that one.
28
+
29
+ A host program starts onetool with one of these two commands:
30
+
31
+ ```sh
32
+ npx @koneeric/onetool
33
+ # or straight from this repository:
34
+ npx github:KoneEric/onetool
35
+ ```
36
+
37
+ The one option is `--config`, the path of the settings file. Add this entry to the MCP settings of your host
38
+ program:
39
+
40
+ ```json
41
+ {
42
+ "mcpServers": {
43
+ "onetool": {
44
+ "command": "npx",
45
+ "args": ["-y", "@koneeric/onetool", "--config", "/absolute/path/to/settings.json"]
46
+ }
47
+ }
48
+ }
49
+ ```
50
+
51
+ The settings file lists your own servers in the same `mcpServers` shape that host programs use. Paths in it are
52
+ given in full, not relative.
53
+
54
+ ## Try it
55
+
56
+ You can try onetool with no account and no network once the package is installed (`npm ci` needs the network or
57
+ npm's cache):
58
+
59
+ ```sh
60
+ git clone https://github.com/KoneEric/onetool.git
61
+ cd onetool
62
+ npm ci
63
+ npm test
64
+ node bin/onetool.js --config guide/pages/settings.example.json
65
+ ```
66
+
67
+ The tests run the real command against the practice server, a table of 5,000 rows with five small tools. The
68
+ last command starts onetool with the example settings file, which lists the practice server. It then waits for a
69
+ host program to speak to it on its standard input; press Ctrl-C to end it.
70
+
71
+ ## More
72
+
73
+ - [Install page](guide/pages/INSTALL.md): the settings file, rules that allow, deny or ask, the shell and files,
74
+ the library of saved programs, the trail and the log, and what to do when something does not work.
75
+ - [Licence](LICENSE): MIT.
76
+ - [Security](SECURITY.md): how to report a weakness, and what counts as one.
77
+ - [Contributing](CONTRIBUTING.md): how to run the tests and send a change.
@@ -0,0 +1,302 @@
1
+ // The sign-in by OAuth to a listed server reached at an address. See the meeting point of auth.
2
+ import crypto from "node:crypto";
3
+ import fs from "node:fs";
4
+ import http from "node:http";
5
+ import os from "node:os";
6
+ import path from "node:path";
7
+ import { auth } from "@modelcontextprotocol/sdk/client/auth.js";
8
+
9
+ const isText = (v) => typeof v === "string" && v.length > 0;
10
+ const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
11
+
12
+ function checkName(name) {
13
+ if (!isText(name)) throw new TypeError("name must be a non-empty string");
14
+ }
15
+
16
+ function checkUrl(url) {
17
+ if (!isText(url)) throw new TypeError("url must be a non-empty string");
18
+ }
19
+
20
+ function checkOauth(oauth) {
21
+ if (oauth === undefined) return;
22
+ if (!isObject(oauth) || typeof oauth.clientId !== "string") throw new TypeError("oauth must be an object with a string clientId");
23
+ if ("redirectHost" in oauth && oauth.redirectHost !== "127.0.0.1" && oauth.redirectHost !== "localhost") throw new TypeError("oauth.redirectHost must be 127.0.0.1 or localhost");
24
+ }
25
+
26
+ export function authFolder({ home = os.homedir() } = {}) {
27
+ return path.join(home, ".onetool", "auth");
28
+ }
29
+
30
+ export function authFile(name, { home = os.homedir() } = {}) {
31
+ checkName(name);
32
+ return path.join(authFolder({ home }), encodeURIComponent(name) + ".json");
33
+ }
34
+
35
+ /** The parsed file when it belongs to `(name, url)`, whatever else it holds; else `null`. Never throws. */
36
+ function readDoc(name, url, home) {
37
+ try {
38
+ const doc = JSON.parse(fs.readFileSync(authFile(name, { home }), "utf8"));
39
+ if (isObject(doc) && doc.server === name && doc.url === url) return doc;
40
+ } catch {}
41
+ return null;
42
+ }
43
+
44
+ const usableTokens = (doc) => (doc && isObject(doc.tokens) && isText(doc.tokens.access_token) ? doc.tokens : undefined);
45
+
46
+ /** Writes the file whole: beside it, then renamed over it. Throws when it cannot. */
47
+ function writeDoc(name, doc, home) {
48
+ const folder = authFolder({ home });
49
+ const made = fs.mkdirSync(folder, { recursive: true, mode: 0o700 });
50
+ if (made !== undefined) fs.chmodSync(folder, 0o700);
51
+ const file = authFile(name, { home });
52
+ const temp = path.join(folder, `.${crypto.randomBytes(8).toString("hex")}.tmp`);
53
+ try {
54
+ const fd = fs.openSync(temp, "wx", 0o600);
55
+ try {
56
+ fs.fchmodSync(fd, 0o600);
57
+ fs.writeSync(fd, JSON.stringify({ ...doc, stamp: crypto.randomUUID() }, null, 2) + "\n");
58
+ fs.fsyncSync(fd);
59
+ } finally {
60
+ fs.closeSync(fd);
61
+ }
62
+ fs.renameSync(temp, file);
63
+ } catch (e) {
64
+ try {
65
+ fs.unlinkSync(temp);
66
+ } catch {}
67
+ throw e;
68
+ }
69
+ }
70
+
71
+ export function signInStamp(name, url, { home = os.homedir() } = {}) {
72
+ checkName(name);
73
+ checkUrl(url);
74
+ const doc = readDoc(name, url, home);
75
+ if (!usableTokens(doc)) return null;
76
+ if (isText(doc.stamp)) return doc.stamp;
77
+ try {
78
+ const st = fs.statSync(authFile(name, { home }));
79
+ return `m-${st.mtimeMs}-${st.size}-${st.ino}`;
80
+ } catch {
81
+ return null;
82
+ }
83
+ }
84
+
85
+ export function logout(name, { home = os.homedir() } = {}) {
86
+ checkName(name);
87
+ try {
88
+ fs.unlinkSync(authFile(name, { home }));
89
+ return true;
90
+ } catch (e) {
91
+ if (e?.code === "ENOENT" || e?.code === "ENOTDIR") return false;
92
+ throw new Error("the sign-in could not be removed");
93
+ }
94
+ }
95
+
96
+ const clientOf = (oauth) => ({ client_id: oauth.clientId, ...(oauth.clientSecret !== undefined ? { client_secret: oauth.clientSecret } : {}) });
97
+ const scopeOf = (oauth) => (Array.isArray(oauth?.scopes) && oauth.scopes.length > 0 ? oauth.scopes.join(" ") : undefined);
98
+
99
+ export function quietProvider(name, url, { home = os.homedir(), oauth } = {}) {
100
+ checkName(name);
101
+ checkUrl(url);
102
+ checkOauth(oauth);
103
+ const redirect = "http://127.0.0.1/callback";
104
+ return {
105
+ get redirectUrl() {
106
+ return redirect;
107
+ },
108
+ get clientMetadata() {
109
+ return { client_name: "onetool", redirect_uris: [redirect], scope: scopeOf(oauth) };
110
+ },
111
+ clientInformation() {
112
+ if (oauth) return clientOf(oauth);
113
+ const doc = readDoc(name, url, home);
114
+ return isObject(doc?.client) ? doc.client : undefined;
115
+ },
116
+ tokens() {
117
+ return usableTokens(readDoc(name, url, home));
118
+ },
119
+ saveTokens(tokens) {
120
+ const doc = readDoc(name, url, home) ?? { server: name, url };
121
+ writeDoc(name, { ...doc, tokens }, home);
122
+ },
123
+ saveCodeVerifier() {},
124
+ codeVerifier() {
125
+ throw new Error("no code verifier");
126
+ },
127
+ redirectToAuthorization() {},
128
+ invalidateCredentials(scope) {
129
+ if (scope !== "all" && scope !== "tokens") return;
130
+ // The tokens go, the client stays: the request that follows ends as a plain 401, not as a refused registration.
131
+ const doc = readDoc(name, url, home);
132
+ if (!doc || !("tokens" in doc)) return;
133
+ const { tokens: _gone, ...rest } = doc;
134
+ try {
135
+ writeDoc(name, rest, home);
136
+ } catch {}
137
+ },
138
+ };
139
+ }
140
+
141
+ export function browserCommand(address, platform = process.platform) {
142
+ if (platform === "darwin") return ["open", address];
143
+ if (platform === "linux") return ["xdg-open", address];
144
+ return null;
145
+ }
146
+
147
+ const MAX_TIMER_MS = 2 ** 31 - 1;
148
+ const PLAIN = { "content-type": "text/plain; charset=utf-8", connection: "close" };
149
+
150
+ export async function login({ name, url, oauth } = {}, { home = os.homedir(), waitSeconds = 300, say, open } = {}) {
151
+ checkName(name);
152
+ checkUrl(url);
153
+ checkOauth(oauth);
154
+ if (typeof waitSeconds !== "number" || !Number.isFinite(waitSeconds) || waitSeconds <= 0) throw new TypeError("waitSeconds must be a finite number above 0");
155
+ if (say !== undefined && typeof say !== "function") throw new TypeError("say must be a function");
156
+ if (open !== undefined && typeof open !== "function") throw new TypeError("open must be a function");
157
+
158
+ return new Promise((resolve) => {
159
+ const state = crypto.randomBytes(16).toString("base64url");
160
+ const abort = new AbortController();
161
+ const server = http.createServer();
162
+ let settled = false;
163
+ let started = false;
164
+ let exchanging = false;
165
+ let written = false;
166
+ let keepFailed = false;
167
+ let timer;
168
+
169
+ const finish = (result) => {
170
+ if (settled) return;
171
+ settled = true;
172
+ clearTimeout(timer);
173
+ abort.abort();
174
+ server.close();
175
+ server.closeIdleConnections?.();
176
+ setImmediate(() => server.closeAllConnections?.());
177
+ resolve(result);
178
+ };
179
+ const fail = (reason) => finish({ ok: false, reason });
180
+ const fetchFn = (input, init) =>
181
+ fetch(input, { ...init, signal: init?.signal ? AbortSignal.any([init.signal, abort.signal]) : abort.signal });
182
+ const answer = (res, status, text, then) => {
183
+ res.writeHead(status, PLAIN);
184
+ res.end(text, then);
185
+ };
186
+
187
+ let port = 0;
188
+ const callback = () => `http://${oauth?.redirectHost ?? "127.0.0.1"}:${port}/callback`;
189
+ let client;
190
+ let verifier;
191
+ let discovery;
192
+ let tokens;
193
+ const provider = {
194
+ get redirectUrl() {
195
+ return callback();
196
+ },
197
+ get clientMetadata() {
198
+ return {
199
+ client_name: "onetool",
200
+ redirect_uris: [callback()],
201
+ grant_types: ["authorization_code", "refresh_token"],
202
+ response_types: ["code"],
203
+ token_endpoint_auth_method: oauth?.clientSecret !== undefined ? "client_secret_post" : "none",
204
+ scope: scopeOf(oauth),
205
+ };
206
+ },
207
+ state: () => state,
208
+ clientInformation: () => (oauth ? clientOf(oauth) : client),
209
+ tokens: () => tokens,
210
+ saveTokens(t) {
211
+ if (settled) throw new Error("late");
212
+ const doc = { server: name, url, tokens: t, ...(client && !oauth ? { client } : {}) };
213
+ try {
214
+ writeDoc(name, doc, home);
215
+ } catch (e) {
216
+ keepFailed = true;
217
+ throw e;
218
+ }
219
+ written = true;
220
+ tokens = t;
221
+ },
222
+ saveCodeVerifier(v) {
223
+ verifier = v;
224
+ },
225
+ codeVerifier: () => verifier,
226
+ discoveryState: () => discovery,
227
+ saveDiscoveryState(d) {
228
+ discovery = d;
229
+ },
230
+ invalidateCredentials() {},
231
+ redirectToAuthorization(address) {
232
+ started = true;
233
+ if (settled) return;
234
+ const text = String(address);
235
+ for (const fn of [say, open]) {
236
+ if (!fn) continue;
237
+ try {
238
+ const r = fn(text);
239
+ if (r && typeof r.then === "function") r.then(undefined, () => {});
240
+ } catch {}
241
+ }
242
+ },
243
+ };
244
+ // The client is the one `oauth` names, or one registered now: never the one of an earlier sign-in.
245
+ if (!oauth) {
246
+ provider.saveClientInformation = (info) => {
247
+ client = info;
248
+ };
249
+ }
250
+ const scope = scopeOf(oauth);
251
+
252
+ const exchange = async (code, res) => {
253
+ try {
254
+ const r = await auth(provider, { serverUrl: url, authorizationCode: code, scope, fetchFn });
255
+ if (settled) return;
256
+ if (r !== "AUTHORIZED") throw new Error("not authorized");
257
+ answer(res, 200, "signed in, you can close this page", () => finish({ ok: true }));
258
+ } catch {
259
+ if (settled) return;
260
+ answer(res, 400, "not signed in", () => fail(keepFailed ? "the sign-in could not be kept" : "the sign-in could not be finished"));
261
+ }
262
+ };
263
+
264
+ server.on("request", (req, res) => {
265
+ if (settled) {
266
+ answer(res, 404, "");
267
+ return;
268
+ }
269
+ let u;
270
+ try {
271
+ u = new URL(req.url, "http://127.0.0.1");
272
+ } catch {
273
+ answer(res, 404, "");
274
+ return;
275
+ }
276
+ if (u.pathname !== "/callback" || req.method !== "GET") return answer(res, 404, "");
277
+ if (u.searchParams.get("state") !== state) return answer(res, 400, "not signed in");
278
+ if (exchanging) return answer(res, 400, "not signed in");
279
+ const code = u.searchParams.get("code");
280
+ if (u.searchParams.has("error") || !code) {
281
+ exchanging = true;
282
+ return answer(res, 400, "not signed in", () => fail("the sign-in was refused"));
283
+ }
284
+ exchanging = true;
285
+ exchange(code, res);
286
+ });
287
+
288
+ timer = setTimeout(() => finish(written ? { ok: true } : { ok: false, reason: `no answer in ${String(waitSeconds)} seconds` }), Math.min(waitSeconds * 1000, MAX_TIMER_MS));
289
+ server.once("error", () => fail(`cannot listen on port ${oauth?.port ?? 0}`));
290
+ server.listen({ port: oauth?.port ?? 0, host: "127.0.0.1" }, async () => {
291
+ server.removeAllListeners("error");
292
+ server.on("error", () => {});
293
+ port = server.address().port;
294
+ try {
295
+ const r = await auth(provider, { serverUrl: url, scope, fetchFn });
296
+ if (r !== "REDIRECT" || !started) fail("the sign-in could not be started");
297
+ } catch {
298
+ if (!started) fail("the sign-in could not be started");
299
+ }
300
+ });
301
+ });
302
+ }
package/bin/onetool.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { main } from "../server/src/main.js";
3
+
4
+ await main(process.argv.slice(2));