@koneeric/onetool 0.1.0 → 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/README.md +14 -6
- package/auth/src/index.js +302 -0
- package/core/src/index.js +121 -10
- package/guide/pages/INSTALL.md +184 -10
- package/package.json +2 -1
- package/practice/src/server.js +40 -15
- package/practice/src/signin.js +242 -0
- package/sandbox/src/child.mjs +25 -5
- package/sandbox/src/index.js +6 -5
- package/server/src/connect.js +71 -9
- package/server/src/descriptions.js +1 -0
- package/server/src/init.js +243 -0
- package/server/src/main.js +47 -11
- package/server/src/sign.js +98 -0
- package/server/src/tools.js +109 -17
package/sandbox/src/index.js
CHANGED
|
@@ -107,18 +107,19 @@ function checkLibrary(library) {
|
|
|
107
107
|
function check(code, { tools, callTool, timeoutMs, memoryMb, shell, onProgress, signal, library, onLibrary }) {
|
|
108
108
|
if (typeof code !== "string") throw new TypeError("code must be a string");
|
|
109
109
|
if (typeof tools !== "object" || tools === null || Array.isArray(tools)) {
|
|
110
|
-
throw new TypeError("tools must be an object of arrays of strings");
|
|
110
|
+
throw new TypeError("tools must be an object of arrays of strings or the value true");
|
|
111
111
|
}
|
|
112
112
|
let named = false;
|
|
113
113
|
for (const list of Object.values(tools)) {
|
|
114
|
-
if (
|
|
115
|
-
|
|
114
|
+
if (list === true) named = true;
|
|
115
|
+
else if (!Array.isArray(list) || !list.every((name) => typeof name === "string")) {
|
|
116
|
+
throw new TypeError("tools must be an object of arrays of strings or the value true");
|
|
116
117
|
}
|
|
117
|
-
if (list.length > 0) named = true;
|
|
118
|
+
else if (list.length > 0) named = true;
|
|
118
119
|
}
|
|
119
120
|
if (typeof shell !== "boolean") throw new TypeError("shell must be true or false");
|
|
120
121
|
if ((named || shell) && typeof callTool !== "function") {
|
|
121
|
-
throw new TypeError("callTool must be a function when tools names a tool or shell is true");
|
|
122
|
+
throw new TypeError("callTool must be a function when tools names a tool or a server given as true, or shell is true");
|
|
122
123
|
}
|
|
123
124
|
if (typeof timeoutMs !== "number" || !Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
124
125
|
throw new TypeError("timeoutMs must be a finite number above 0");
|
package/server/src/connect.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// The connection to one listed server: start its process, speak MCP to it, read its list of tools.
|
|
2
2
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
3
|
+
import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js";
|
|
3
4
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
4
5
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
5
6
|
import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
|
|
7
|
+
import { quietProvider, signInStamp } from "../../auth/src/index.js";
|
|
8
|
+
import { keptTools, signsIn } from "../../core/src/index.js";
|
|
6
9
|
|
|
7
10
|
/** The transport of the package, which also knows when the process of the server has ended. */
|
|
8
11
|
class Transport extends StdioClientTransport {
|
|
@@ -27,8 +30,26 @@ export function isServerError(err) {
|
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
33
|
+
* Whether `err` is the server asking for a sign-in that is not there or not good: the package's `UnauthorizedError`, or
|
|
34
|
+
* the error of its transport for an HTTP status 401. Written against the package at 1.32.2.
|
|
35
|
+
*/
|
|
36
|
+
export function isSignInFailure(err) {
|
|
37
|
+
return err instanceof UnauthorizedError || err?.code === 401;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The stamp of the sign-in of a sign-in server in the home folder: `null` when there is none usable. */
|
|
41
|
+
function stampOf(entry) {
|
|
42
|
+
try {
|
|
43
|
+
return signInStamp(entry.name, entry.url);
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* One listed server. `tools` is its list once connected; `error` the reason when it is not; `needed` that it is a
|
|
51
|
+
* sign-in server that needs a sign-in (neither `tools` nor `error` then). `connect()` never rejects: it settles one of
|
|
52
|
+
* the three, and may be called again for a server that is sign in needed.
|
|
32
53
|
*/
|
|
33
54
|
export class Connection {
|
|
34
55
|
constructor(name, entry, connectSeconds, version) {
|
|
@@ -41,6 +62,11 @@ export class Connection {
|
|
|
41
62
|
this.client = null;
|
|
42
63
|
this.transport = null;
|
|
43
64
|
this.closing = null;
|
|
65
|
+
this.needed = false;
|
|
66
|
+
this.signIn = !Object.hasOwn(entry, "url") ? false : signsIn(entry);
|
|
67
|
+
// the stamp of the sign-in the last connection used, and whether it was given a provider
|
|
68
|
+
this.stampTried = null;
|
|
69
|
+
this.withSignIn = false;
|
|
44
70
|
}
|
|
45
71
|
|
|
46
72
|
/** Whether it is reached at an address: it then has no process of its own. */
|
|
@@ -52,10 +78,22 @@ export class Connection {
|
|
|
52
78
|
return this.tools !== null && (this.remote || !this.transport.ended);
|
|
53
79
|
}
|
|
54
80
|
|
|
81
|
+
/** The stamp of the sign-in file now; `null` for a server that is not a sign-in server or has none. */
|
|
82
|
+
stamp() {
|
|
83
|
+
return this.signIn ? stampOf(this.entry) : null;
|
|
84
|
+
}
|
|
85
|
+
|
|
55
86
|
makeTransport() {
|
|
56
87
|
if (this.remote) {
|
|
57
88
|
const { url, headers } = this.entry;
|
|
58
|
-
|
|
89
|
+
const options = { requestInit: { headers } };
|
|
90
|
+
if (this.signIn) {
|
|
91
|
+
const stamp = stampOf(this.entry);
|
|
92
|
+
this.stampTried = stamp;
|
|
93
|
+
this.withSignIn = stamp !== null;
|
|
94
|
+
if (stamp !== null) options.authProvider = quietProvider(this.entry.name, url, { oauth: this.entry.oauth });
|
|
95
|
+
}
|
|
96
|
+
return new StreamableHTTPClientTransport(new URL(url), options);
|
|
59
97
|
}
|
|
60
98
|
const { command, args, env } = this.entry;
|
|
61
99
|
return new Transport({ command, args, env, stderr: "inherit" });
|
|
@@ -63,9 +101,15 @@ export class Connection {
|
|
|
63
101
|
|
|
64
102
|
async connect() {
|
|
65
103
|
const ms = this.connectSeconds * 1000;
|
|
104
|
+
this.closing = null;
|
|
105
|
+
let tools = null;
|
|
106
|
+
let error = null;
|
|
107
|
+
let needed = false;
|
|
66
108
|
try {
|
|
67
109
|
this.transport = this.makeTransport();
|
|
68
110
|
} catch {
|
|
111
|
+
this.tools = null;
|
|
112
|
+
this.needed = false;
|
|
69
113
|
this.error = "could not connect";
|
|
70
114
|
return this;
|
|
71
115
|
}
|
|
@@ -91,24 +135,42 @@ export class Connection {
|
|
|
91
135
|
try {
|
|
92
136
|
const got = await Promise.race([work, timeout]);
|
|
93
137
|
if (got === "timeout") {
|
|
94
|
-
|
|
138
|
+
error = `no answer in ${String(this.connectSeconds)} seconds`;
|
|
95
139
|
} else {
|
|
96
|
-
|
|
140
|
+
// an entry with `tools` offers only those tools
|
|
141
|
+
tools = Object.hasOwn(this.entry, "tools") ? keptTools(got, this.entry.tools) : got;
|
|
97
142
|
}
|
|
98
143
|
} catch (err) {
|
|
99
|
-
if (this.
|
|
100
|
-
|
|
144
|
+
if (this.signIn && isSignInFailure(err)) {
|
|
145
|
+
needed = true;
|
|
146
|
+
} else if (this.remote) {
|
|
147
|
+
error = isServerError(err) ? "refused the connection" : "could not connect";
|
|
101
148
|
} else {
|
|
102
149
|
const refused = err instanceof McpError && !this.transport.ended;
|
|
103
|
-
|
|
150
|
+
error = refused ? "refused the connection" : "could not start";
|
|
104
151
|
}
|
|
105
152
|
} finally {
|
|
106
153
|
clearTimeout(timer);
|
|
107
154
|
}
|
|
108
|
-
|
|
155
|
+
this.tools = tools;
|
|
156
|
+
this.error = error;
|
|
157
|
+
this.needed = needed;
|
|
158
|
+
if (error !== null || needed) this.close();
|
|
109
159
|
return this;
|
|
110
160
|
}
|
|
111
161
|
|
|
162
|
+
/**
|
|
163
|
+
* The connection of a sign-in server is closed and its tools are forgotten: it is sign in needed, and `stamp` is the
|
|
164
|
+
* stamp of the file that did not work.
|
|
165
|
+
*/
|
|
166
|
+
goNeeded(stamp) {
|
|
167
|
+
this.tools = null;
|
|
168
|
+
this.error = null;
|
|
169
|
+
this.needed = true;
|
|
170
|
+
this.stampTried = stamp;
|
|
171
|
+
return this.close();
|
|
172
|
+
}
|
|
173
|
+
|
|
112
174
|
/** Ends the connection and its process. Gives a promise that is settled once they are gone. */
|
|
113
175
|
close() {
|
|
114
176
|
if (!this.transport) return Promise.resolve();
|
|
@@ -4,6 +4,7 @@ export const RUN_DESCRIPTION =
|
|
|
4
4
|
"Runs a JavaScript program that calls the tools of the listed servers and gives back only what it returns or prints. " +
|
|
5
5
|
"Call a tool as `await tools.<server>.<tool>(args)` (args an object); filter and count inside, so that only a small answer comes back. " +
|
|
6
6
|
"The answer is the value of the last expression; `console.log` lines follow. " +
|
|
7
|
+
"Put every step that needs no decision into one program. " +
|
|
7
8
|
"`state` is kept from one call to the next. " +
|
|
8
9
|
"`background: true` starts a job and answers at once with its id; `progress(...)` posts a line. " +
|
|
9
10
|
"Use `search` first to find tools.";
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// The command `onetool init`: sets up the settings file and proposes Claude Code as a server behind onetool.
|
|
2
|
+
import { accessSync, chmodSync, constants, mkdirSync, readFileSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { basename, delimiter, dirname, join, resolve } from "node:path";
|
|
5
|
+
import { createInterface } from "node:readline";
|
|
6
|
+
|
|
7
|
+
export const INIT_USAGE = "usage: onetool init [--config <settings file>] [--yes] [--no-claude]";
|
|
8
|
+
|
|
9
|
+
const SIX = ["Bash", "Read", "Edit", "Write", "Grep", "Glob"];
|
|
10
|
+
const PACKAGE_NAME = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).name;
|
|
11
|
+
|
|
12
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13
|
+
|
|
14
|
+
/** The arguments after `init`: `{ config, yes, noClaude }` (`config` is `null` when not given), or `null` when they are wrong. */
|
|
15
|
+
export function parseInitArgs(argv) {
|
|
16
|
+
const parsed = { config: null, yes: false, noClaude: false };
|
|
17
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
18
|
+
const option = argv[i];
|
|
19
|
+
if (option === "--config" && parsed.config === null) {
|
|
20
|
+
if (i + 1 >= argv.length || argv[i + 1] === "") return null;
|
|
21
|
+
parsed.config = argv[(i += 1)];
|
|
22
|
+
} else if (option === "--yes" && !parsed.yes) {
|
|
23
|
+
parsed.yes = true;
|
|
24
|
+
} else if (option === "--no-claude" && !parsed.noClaude) {
|
|
25
|
+
parsed.noClaude = true;
|
|
26
|
+
} else {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return parsed;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The place of `claude` in a text of folders, found by reading, never by starting anything; `null` when it is not found. */
|
|
34
|
+
export function findClaude(pathText) {
|
|
35
|
+
for (const item of pathText.split(delimiter)) {
|
|
36
|
+
if (item === "") continue;
|
|
37
|
+
const candidate = join(resolve(item), "claude");
|
|
38
|
+
try {
|
|
39
|
+
if (!statSync(candidate).isFile()) continue;
|
|
40
|
+
accessSync(candidate, constants.X_OK);
|
|
41
|
+
return candidate;
|
|
42
|
+
} catch {
|
|
43
|
+
// passed over
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Whether an entry of `mcpServers` is `claude mcp serve`. */
|
|
50
|
+
function startsClaudeCode(entry) {
|
|
51
|
+
return (
|
|
52
|
+
isObject(entry) &&
|
|
53
|
+
typeof entry.command === "string" &&
|
|
54
|
+
basename(entry.command) === "claude" &&
|
|
55
|
+
Array.isArray(entry.args) &&
|
|
56
|
+
entry.args[0] === "mcp" &&
|
|
57
|
+
entry.args[1] === "serve"
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The key of the first entry of `mcpServers` that is Claude Code, or `null`. */
|
|
62
|
+
function claudeCodeKey(mcpServers, hostServers) {
|
|
63
|
+
const named = Array.isArray(hostServers) ? hostServers : [];
|
|
64
|
+
return Object.keys(mcpServers).find((key) => named.includes(key) || startsClaudeCode(mcpServers[key])) ?? null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The text at `path`, or `null` when nothing is there; throws for any other fault. */
|
|
68
|
+
function readText(path) {
|
|
69
|
+
try {
|
|
70
|
+
return readFileSync(path, "utf8");
|
|
71
|
+
} catch (err) {
|
|
72
|
+
if (err?.code === "ENOENT") return null;
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Writes `text` at `path`, which exists: beside it and renamed over it when that can be, else in place. A link stays a link. */
|
|
78
|
+
function rewrite(path, text) {
|
|
79
|
+
const real = realpathSync(path);
|
|
80
|
+
const mode = statSync(real).mode & 0o7777;
|
|
81
|
+
const beside = join(dirname(real), `.${basename(real)}.${process.pid}.tmp`);
|
|
82
|
+
try {
|
|
83
|
+
writeFileSync(beside, text, { mode });
|
|
84
|
+
chmodSync(beside, mode);
|
|
85
|
+
renameSync(beside, real);
|
|
86
|
+
} catch {
|
|
87
|
+
try {
|
|
88
|
+
unlinkSync(beside);
|
|
89
|
+
} catch {
|
|
90
|
+
// there was none
|
|
91
|
+
}
|
|
92
|
+
writeFileSync(real, text, "utf8");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Makes the file, with the folders it lacks, and gives it the mode 600 whatever the umask. */
|
|
97
|
+
function make(path, text) {
|
|
98
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
99
|
+
writeFileSync(path, text, { mode: 0o600 });
|
|
100
|
+
chmodSync(path, 0o600);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const textOf = (value) => `${JSON.stringify(value, null, 2)}\n`;
|
|
104
|
+
|
|
105
|
+
/** Asks on standard output, reads one line of standard input, then lets go of it; `null` at the end of the input. */
|
|
106
|
+
function askStdin(question) {
|
|
107
|
+
return new Promise((done) => {
|
|
108
|
+
process.stdout.write(question);
|
|
109
|
+
const lines = createInterface({ input: process.stdin });
|
|
110
|
+
let answered = false;
|
|
111
|
+
lines.once("line", (line) => {
|
|
112
|
+
answered = true;
|
|
113
|
+
lines.close();
|
|
114
|
+
process.stdin.pause();
|
|
115
|
+
done(line);
|
|
116
|
+
});
|
|
117
|
+
lines.once("close", () => {
|
|
118
|
+
if (!answered) done(null);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const outDefault = (text) => process.stdout.write(`${text}\n`);
|
|
124
|
+
const errDefault = (text) => process.stderr.write(`${text}\n`);
|
|
125
|
+
|
|
126
|
+
/** `onetool init`: `argv` is what follows the word. Resolves with the exit code. */
|
|
127
|
+
export async function runInit(
|
|
128
|
+
argv,
|
|
129
|
+
{ home = homedir(), pathText = process.env.PATH ?? "", terminal = process.stdin.isTTY === true, ask = askStdin, out = outDefault, err = errDefault } = {},
|
|
130
|
+
) {
|
|
131
|
+
const parsed = parseInitArgs(argv);
|
|
132
|
+
if (parsed === null) {
|
|
133
|
+
err(INIT_USAGE);
|
|
134
|
+
return 2;
|
|
135
|
+
}
|
|
136
|
+
const P = parsed.config === null ? join(home, ".config", "onetool", "settings.json") : resolve(parsed.config);
|
|
137
|
+
const fault = (reason) => {
|
|
138
|
+
err(`onetool: init: ${P}: ${reason}`);
|
|
139
|
+
return 1;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
let text;
|
|
143
|
+
try {
|
|
144
|
+
text = readText(P);
|
|
145
|
+
} catch {
|
|
146
|
+
return fault("cannot be read");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let value;
|
|
150
|
+
if (text === null) {
|
|
151
|
+
value = { mcpServers: {} };
|
|
152
|
+
try {
|
|
153
|
+
make(P, textOf(value));
|
|
154
|
+
} catch {
|
|
155
|
+
return fault("cannot be written");
|
|
156
|
+
}
|
|
157
|
+
out(`onetool: init: made ${P}`);
|
|
158
|
+
} else {
|
|
159
|
+
try {
|
|
160
|
+
value = JSON.parse(text);
|
|
161
|
+
} catch {
|
|
162
|
+
return fault("not JSON");
|
|
163
|
+
}
|
|
164
|
+
if (!isObject(value)) return fault("not a JSON object");
|
|
165
|
+
if (Object.hasOwn(value, "mcpServers") && !isObject(value.mcpServers)) return fault("mcpServers is not an object");
|
|
166
|
+
if (Object.hasOwn(value, "onetool") && !isObject(value.onetool)) return fault("onetool is not an object");
|
|
167
|
+
if (isObject(value.onetool) && Object.hasOwn(value.onetool, "hostServers") && !Array.isArray(value.onetool.hostServers)) {
|
|
168
|
+
return fault("onetool.hostServers is not a list");
|
|
169
|
+
}
|
|
170
|
+
out(`onetool: init: found ${P}`);
|
|
171
|
+
if (!Object.hasOwn(value, "mcpServers")) {
|
|
172
|
+
value.mcpServers = {};
|
|
173
|
+
try {
|
|
174
|
+
rewrite(P, textOf(value));
|
|
175
|
+
} catch {
|
|
176
|
+
return fault("cannot be written");
|
|
177
|
+
}
|
|
178
|
+
out(`onetool: init: added an empty mcpServers to ${P}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (!parsed.noClaude) {
|
|
183
|
+
const code = await proposeClaude(P, value, parsed, { pathText, terminal, ask, out });
|
|
184
|
+
if (code !== 0) return fault("cannot be written");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
out("onetool: init: add this entry to the MCP settings of your host program:");
|
|
188
|
+
out(JSON.stringify({ mcpServers: { onetool: { command: "npx", args: ["-y", PACKAGE_NAME, "--config", P] } } }, null, 2));
|
|
189
|
+
return 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** "`onetool init`: Claude Code". Gives 0, or 1 when the write failed. */
|
|
193
|
+
async function proposeClaude(P, value, { yes }, { pathText, terminal, ask, out }) {
|
|
194
|
+
const named = claudeCodeKey(value.mcpServers, value.onetool?.hostServers);
|
|
195
|
+
if (named !== null) {
|
|
196
|
+
out(`onetool: init: ${P} already has Claude Code as the server ${named}, nothing is proposed`);
|
|
197
|
+
return 0;
|
|
198
|
+
}
|
|
199
|
+
if (Object.hasOwn(value.mcpServers, "claude")) {
|
|
200
|
+
out(`onetool: init: ${P} has another server named claude, nothing is proposed`);
|
|
201
|
+
return 0;
|
|
202
|
+
}
|
|
203
|
+
const place = findClaude(pathText);
|
|
204
|
+
if (place === null) {
|
|
205
|
+
out("onetool: init: the command claude was not found on this machine, Claude Code is not proposed");
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
out("onetool: init: Claude Code can be a server behind onetool: the assistant's programs can then run commands and read, search and edit files through Claude Code's own tools.");
|
|
209
|
+
out("onetool: init: Claude Code as a server asks no permission question: the rules of onetool are the control.");
|
|
210
|
+
|
|
211
|
+
if (!yes) {
|
|
212
|
+
if (terminal !== true) {
|
|
213
|
+
out(`onetool: init: nobody to ask, nothing is added. To add it, run: onetool init --config ${P} --yes`);
|
|
214
|
+
return 0;
|
|
215
|
+
}
|
|
216
|
+
let answer;
|
|
217
|
+
try {
|
|
218
|
+
answer = await ask(`Add the server claude to ${P}? [Y/n] `);
|
|
219
|
+
} catch {
|
|
220
|
+
answer = null;
|
|
221
|
+
}
|
|
222
|
+
const said = typeof answer === "string" ? answer.trim().toLowerCase() : null;
|
|
223
|
+
if (said !== "" && said !== "y" && said !== "yes") {
|
|
224
|
+
out("onetool: init: Claude Code is not added");
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
value.mcpServers.claude = { command: place, args: ["mcp", "serve"], tools: SIX };
|
|
230
|
+
if (!Object.hasOwn(value, "onetool")) value.onetool = { hostServers: ["claude"] };
|
|
231
|
+
else if (!Object.hasOwn(value.onetool, "hostServers")) value.onetool.hostServers = ["claude"];
|
|
232
|
+
else if (!value.onetool.hostServers.includes("claude")) value.onetool.hostServers.push("claude");
|
|
233
|
+
try {
|
|
234
|
+
rewrite(P, textOf(value));
|
|
235
|
+
} catch {
|
|
236
|
+
return 1;
|
|
237
|
+
}
|
|
238
|
+
out(`onetool: init: added the server claude to ${P} (claude mcp serve), with the tools ${SIX.join(", ")}`);
|
|
239
|
+
if (Array.isArray(value.onetool.allow) && value.onetool.allow.length > 0) {
|
|
240
|
+
out(`onetool: init: onetool.allow of ${P} is not empty: add "claude.*" to it to open these tools`);
|
|
241
|
+
}
|
|
242
|
+
return 0;
|
|
243
|
+
}
|
package/server/src/main.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// The command: arguments, settings, the server on standard input and output, the end.
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
3
4
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
5
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
6
|
import {
|
|
@@ -10,11 +11,13 @@ import {
|
|
|
10
11
|
ListToolsRequestSchema,
|
|
11
12
|
McpError,
|
|
12
13
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
-
import { readSettings, SettingsError } from "../../core/src/index.js";
|
|
14
|
+
import { INSTRUCTIONS, readSettings, SettingsError } from "../../core/src/index.js";
|
|
14
15
|
import { missingFolders } from "../../shell/src/index.js";
|
|
15
16
|
import { Connection } from "./connect.js";
|
|
16
17
|
import { makeHost } from "./host.js";
|
|
18
|
+
import { runInit } from "./init.js";
|
|
17
19
|
import { TIDY_DESCRIPTION, TIDY_NAME, TIDY_TEXT } from "./prompt.js";
|
|
20
|
+
import { LOGIN_USAGE, LOGOUT_USAGE, parseSignArgs, runLogin, runLogout } from "./sign.js";
|
|
18
21
|
import { makeTools } from "./tools.js";
|
|
19
22
|
|
|
20
23
|
const USAGE = "usage: onetool --config <settings file>";
|
|
@@ -24,21 +27,45 @@ const version = JSON.parse(readFileSync(new URL("../../package.json", import.met
|
|
|
24
27
|
|
|
25
28
|
const say = (line) => process.stderr.write(`${line}\n`);
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
say(USAGE);
|
|
30
|
-
process.exitCode = 2;
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
let settings;
|
|
30
|
+
/** The settings of the file at `path`, or `null` once its problems are written (the exit code is then 2). */
|
|
31
|
+
function settingsOf(path) {
|
|
34
32
|
try {
|
|
35
|
-
|
|
33
|
+
return readSettings(path);
|
|
36
34
|
} catch (err) {
|
|
37
35
|
if (!(err instanceof SettingsError)) throw err;
|
|
38
36
|
for (const problem of err.problems) say(`onetool: ${problem}`);
|
|
39
37
|
process.exitCode = 2;
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `login` or `logout`: one thing is done, and the process ends. */
|
|
43
|
+
async function signCommand(word, rest) {
|
|
44
|
+
const parsed = parseSignArgs(word, rest);
|
|
45
|
+
if (parsed === null) {
|
|
46
|
+
say(word === "login" ? LOGIN_USAGE : LOGOUT_USAGE);
|
|
47
|
+
process.exitCode = 2;
|
|
40
48
|
return;
|
|
41
49
|
}
|
|
50
|
+
const settings = settingsOf(parsed.config);
|
|
51
|
+
if (settings === null) return;
|
|
52
|
+
if (word === "login") await runLogin(parsed.server, settings, parsed);
|
|
53
|
+
else runLogout(parsed.server);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function main(argv) {
|
|
57
|
+
if (argv[0] === "init") {
|
|
58
|
+
process.exitCode = await runInit(argv.slice(1));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (argv[0] === "login" || argv[0] === "logout") return signCommand(argv[0], argv.slice(1));
|
|
62
|
+
if (argv.length !== 2 || argv[0] !== "--config") {
|
|
63
|
+
say(USAGE);
|
|
64
|
+
process.exitCode = 2;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const settings = settingsOf(argv[1]);
|
|
68
|
+
if (settings === null) return;
|
|
42
69
|
|
|
43
70
|
if (settings.shell !== null && settings.shell.folders !== null) {
|
|
44
71
|
const missing = await missingFolders(settings.shell.folders);
|
|
@@ -54,9 +81,17 @@ export async function main(argv) {
|
|
|
54
81
|
);
|
|
55
82
|
// a list of tools that can change: the description of `run` follows the folders of the host program
|
|
56
83
|
const listChanged = settings.shell !== null && settings.shell.folders === null;
|
|
57
|
-
|
|
84
|
+
// ... and so does what a sign-in server offers, once the person has signed in or out
|
|
85
|
+
const signIns = connections.some((connection) => connection.signIn);
|
|
86
|
+
const server = new Server(
|
|
87
|
+
{ name: "onetool", version },
|
|
88
|
+
{
|
|
89
|
+
capabilities: { tools: listChanged || signIns ? { listChanged: true } : {}, prompts: {} },
|
|
90
|
+
...(settings.instructions ? { instructions: INSTRUCTIONS } : {}),
|
|
91
|
+
},
|
|
92
|
+
);
|
|
58
93
|
const host = makeHost(settings, server);
|
|
59
|
-
const tools = makeTools(settings, connections, server, host);
|
|
94
|
+
const tools = makeTools(settings, connections, server, host, resolve(argv[1]));
|
|
60
95
|
|
|
61
96
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
62
97
|
host.start();
|
|
@@ -101,6 +136,7 @@ export async function main(argv) {
|
|
|
101
136
|
connections.map(async (connection) => {
|
|
102
137
|
await connection.connect();
|
|
103
138
|
if (connection.error !== null) say(`onetool: server ${connection.name}: not connected (${connection.error})`);
|
|
139
|
+
else if (connection.needed) say(`onetool: server ${connection.name}: sign in needed`);
|
|
104
140
|
}),
|
|
105
141
|
);
|
|
106
142
|
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// The commands `onetool login` and `onetool logout`: the arguments, and what each prints. The sign-in itself is part `auth`'s.
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { browserCommand, login, logout } from "../../auth/src/index.js";
|
|
4
|
+
import { signsIn } from "../../core/src/index.js";
|
|
5
|
+
|
|
6
|
+
export const LOGIN_USAGE = "usage: onetool login <server> --config <settings file> [--no-browser] [--wait <seconds>]";
|
|
7
|
+
export const LOGOUT_USAGE = "usage: onetool logout <server> --config <settings file>";
|
|
8
|
+
|
|
9
|
+
const WAIT_DEFAULT = 300;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The arguments of `login` or `logout` (`word` is the first, `rest` the others): `{ server, config, noBrowser, waitSeconds }`,
|
|
13
|
+
* or `null` when they are not as the table says.
|
|
14
|
+
*/
|
|
15
|
+
export function parseSignArgs(word, rest) {
|
|
16
|
+
const isLogin = word === "login";
|
|
17
|
+
if (rest.length === 0 || rest[0] === "") return null;
|
|
18
|
+
const parsed = { server: rest[0], config: null, noBrowser: false, waitSeconds: WAIT_DEFAULT };
|
|
19
|
+
let waitSeen = false;
|
|
20
|
+
for (let i = 1; i < rest.length; i += 1) {
|
|
21
|
+
const option = rest[i];
|
|
22
|
+
if (option === "--config" && parsed.config === null) {
|
|
23
|
+
if (i + 1 >= rest.length) return null;
|
|
24
|
+
parsed.config = rest[(i += 1)];
|
|
25
|
+
} else if (isLogin && option === "--no-browser" && !parsed.noBrowser) {
|
|
26
|
+
parsed.noBrowser = true;
|
|
27
|
+
} else if (isLogin && option === "--wait" && !waitSeen) {
|
|
28
|
+
if (i + 1 >= rest.length || !/^[0-9]+$/.test(rest[i + 1])) return null;
|
|
29
|
+
const seconds = Number(rest[(i += 1)]);
|
|
30
|
+
if (seconds < 1 || seconds > 3600) return null;
|
|
31
|
+
parsed.waitSeconds = seconds;
|
|
32
|
+
waitSeen = true;
|
|
33
|
+
} else {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return parsed.config === null ? null : parsed;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const out = (line) => process.stdout.write(`${line}\n`);
|
|
41
|
+
|
|
42
|
+
// the last line is flushed, then the process ends at once, whatever is still in flight
|
|
43
|
+
function finish(stream, line, code) {
|
|
44
|
+
stream.on("error", () => {});
|
|
45
|
+
stream.write(`${line}\n`, () => process.exit(code));
|
|
46
|
+
return new Promise(() => {});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Starts the browser on `address` and does not wait for it; a program that cannot be started is ignored. */
|
|
50
|
+
function openBrowser(address) {
|
|
51
|
+
const command = browserCommand(address);
|
|
52
|
+
if (command === null) return;
|
|
53
|
+
try {
|
|
54
|
+
const child = spawn(command[0], command.slice(1), { stdio: "ignore", detached: true, shell: false });
|
|
55
|
+
child.on("error", () => {});
|
|
56
|
+
child.unref();
|
|
57
|
+
} catch {
|
|
58
|
+
// the printed address is the way
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** One sign-in, then the end of the process. `settings` is what `readSettings` gave. */
|
|
63
|
+
export async function runLogin(server, settings, { noBrowser, waitSeconds }) {
|
|
64
|
+
const entry = settings.servers.find((candidate) => candidate.name === server);
|
|
65
|
+
if (entry === undefined) return fail(`onetool: login: ${server} is not a server of the settings file`);
|
|
66
|
+
if (!Object.hasOwn(entry, "url")) return fail(`onetool: login: ${server} is started by a command, there is nothing to sign in to`);
|
|
67
|
+
if (!signsIn(entry)) return fail(`onetool: login: ${server} has an Authorization header in the settings file`);
|
|
68
|
+
let result;
|
|
69
|
+
try {
|
|
70
|
+
result = await login(
|
|
71
|
+
{ name: entry.name, url: entry.url, oauth: entry.oauth },
|
|
72
|
+
{
|
|
73
|
+
waitSeconds,
|
|
74
|
+
say: (address) => out(`onetool: to sign in to ${server}, open ${address}`),
|
|
75
|
+
...(noBrowser ? {} : { open: openBrowser }),
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
} catch {
|
|
79
|
+
result = { ok: false, reason: "the sign-in could not be started" };
|
|
80
|
+
}
|
|
81
|
+
if (result.ok) {
|
|
82
|
+
return finish(process.stdout, `onetool: signed in to ${server}`, 0);
|
|
83
|
+
}
|
|
84
|
+
return fail(`onetool: login: ${server}: ${result.reason}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The sign-in of `server` is forgotten, then the end of the process. */
|
|
88
|
+
export function runLogout(server) {
|
|
89
|
+
let had;
|
|
90
|
+
try {
|
|
91
|
+
had = logout(server);
|
|
92
|
+
} catch {
|
|
93
|
+
return fail(`onetool: logout: ${server}: the sign-in could not be removed`);
|
|
94
|
+
}
|
|
95
|
+
return finish(process.stdout, had ? `onetool: signed out of ${server}` : `onetool: ${server} had no sign-in`, 0);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const fail = (line) => finish(process.stderr, line, 1);
|