@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/guide/pages/INSTALL.md
CHANGED
|
@@ -22,6 +22,49 @@ This one command needs the network or npm's cache. After it, onetool itself open
|
|
|
22
22
|
the address of a listed server that you gave as a `url`. Servers started by a command, such as the practice
|
|
23
23
|
server, need no network at all.
|
|
24
24
|
|
|
25
|
+
## Start with onetool init
|
|
26
|
+
|
|
27
|
+
The first thing to do is run one command. In a terminal:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npx @koneeric/onetool init
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
From the repository, `node bin/onetool.js init` is the same command. It sets up your settings file, then ends by
|
|
34
|
+
itself. It starts no server and connects to nothing.
|
|
35
|
+
|
|
36
|
+
**The file.** It makes the settings file when it is not there, with no server in it:
|
|
37
|
+
`~/.config/onetool/settings.json`. Give `--config` and a path to use another place. It never removes or changes what the file already holds.
|
|
38
|
+
|
|
39
|
+
**Claude Code.** When the command `claude` is found on your machine, and the file has no server that is Claude
|
|
40
|
+
Code, `init` proposes to add Claude Code as a server behind onetool. The answer by default is yes. What this
|
|
41
|
+
gives: the assistant's programs can run commands and read, search and edit files through Claude Code's own
|
|
42
|
+
tools. What it costs: Claude Code as a server asks no permission question: the rules of onetool are the control.
|
|
43
|
+
|
|
44
|
+
Two options answer for you. `--yes` adds it without asking. `--no-claude` proposes nothing. With nobody to ask (no
|
|
45
|
+
terminal) and no `--yes`, nothing is added, and the command says how to accept.
|
|
46
|
+
|
|
47
|
+
**What it adds.** This, to the file:
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"mcpServers": {
|
|
52
|
+
"claude": { "command": "/absolute/path/to/claude", "args": ["mcp", "serve"], "tools": ["Bash", "Read", "Edit", "Write", "Grep", "Glob"] }
|
|
53
|
+
},
|
|
54
|
+
"onetool": { "hostServers": ["claude"] }
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The command is written with the full path where `claude` was found. The key `tools` keeps six of its tools,
|
|
59
|
+
and closes no tool of the other servers. The key `hostServers` makes onetool apply the host program's own rules
|
|
60
|
+
to them: see "Folders and rules from the host program".
|
|
61
|
+
|
|
62
|
+
**The entry.** `init` ends by printing the entry to paste into your host program's MCP settings, with the full
|
|
63
|
+
path of the settings file. It changes no file of your host program: you paste it yourself, as the next sections
|
|
64
|
+
say.
|
|
65
|
+
|
|
66
|
+
**Again.** Run it again on a file that has Claude Code already, and it proposes nothing and says so.
|
|
67
|
+
|
|
25
68
|
## Try it first
|
|
26
69
|
|
|
27
70
|
You can try everything with no account and no network, from the root of the repository, once `npm ci` has run.
|
|
@@ -70,6 +113,8 @@ Your other servers move. Take each of them out of the host program's file and pu
|
|
|
70
113
|
file instead. If you leave a server in both, the assistant still sees its tools twice: once by itself and once
|
|
71
114
|
through onetool.
|
|
72
115
|
|
|
116
|
+
`onetool init` prints this entry for you, with the full path of your settings file; you only paste it.
|
|
117
|
+
|
|
73
118
|
Then start the host program again, so that it reads its settings.
|
|
74
119
|
|
|
75
120
|
- **Claude desktop** reads `claude_desktop_config.json`. On macOS it is in
|
|
@@ -86,6 +131,9 @@ The settings file is JSON. It has two top-level keys: `mcpServers`, the servers
|
|
|
86
131
|
`onetool`, its own options (optional). The names of servers are yours, but a name may not be empty and may
|
|
87
132
|
not hold a dot.
|
|
88
133
|
|
|
134
|
+
An entry may also have the key `tools`: the names of the tools of that server that onetool keeps. The others do
|
|
135
|
+
not exist for `search` or for a program. It closes no tool of another server.
|
|
136
|
+
|
|
89
137
|
A server is one of two kinds of entry. One is started by a command, with `command`, `args` and `env`; only
|
|
90
138
|
`command` is required:
|
|
91
139
|
|
|
@@ -118,6 +166,9 @@ or https address:
|
|
|
118
166
|
An entry may not have both `command` and `url`. An entry that breaks a rule is left out and does not stop the
|
|
119
167
|
others: onetool writes one line about it (see "When something does not work").
|
|
120
168
|
|
|
169
|
+
An entry reached at an address may also have the key `oauth`, for a server that asks you to sign in. It is
|
|
170
|
+
only needed when the server does not let a client register itself. See "Signing in".
|
|
171
|
+
|
|
121
172
|
The keys of `onetool`, all optional:
|
|
122
173
|
|
|
123
174
|
| Key | What it is | Default |
|
|
@@ -139,6 +190,7 @@ The keys of `onetool`, all optional:
|
|
|
139
190
|
| `backgroundSeconds` | How long a job, a program started in the background, may take before it is stopped (see "Background jobs") | 7200 |
|
|
140
191
|
| `maxJobs` | How many jobs may run at once; `0` lets none start | 3 |
|
|
141
192
|
| `libraryCap` | The most saved programs one library holds (see "The library") | 50 |
|
|
193
|
+
| `instructions` | `true` or `false`: whether onetool gives the host program its text for the session when it connects (see "Grouping the work") | on |
|
|
142
194
|
|
|
143
195
|
The file is read once, at the start. To change it, end onetool and start it again (for a host program, start
|
|
144
196
|
the host program again). A key of `onetool` that onetool does not know stops the start, with a line on
|
|
@@ -148,6 +200,76 @@ answer or on standard error. It may name a server, and the pattern of a rule whe
|
|
|
148
200
|
A server of yours may not be named `library`: that name is the built-in library's (see "The library"), and an
|
|
149
201
|
entry with it is not connected.
|
|
150
202
|
|
|
203
|
+
## Signing in
|
|
204
|
+
|
|
205
|
+
Some servers reached at an address ask you to sign in by OAuth. onetool can sit in front of such a server too.
|
|
206
|
+
It applies to a server reached at an address, with no `Authorization` header in the settings file. A server
|
|
207
|
+
with that header, and a server started by a command, never sign in this way. This entry is all it takes:
|
|
208
|
+
|
|
209
|
+
```json
|
|
210
|
+
{
|
|
211
|
+
"mcpServers": {
|
|
212
|
+
"notes": { "url": "https://example.com/mcp" }
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
**What you see before you sign in.** `search` with no words lists the server as `notes: sign in needed`. A
|
|
218
|
+
call of one of its tools fails with a message that gives the command to run. The other servers work as usual.
|
|
219
|
+
|
|
220
|
+
**The command.** In a terminal, run:
|
|
221
|
+
|
|
222
|
+
```sh
|
|
223
|
+
node /absolute/path/to/onetool/bin/onetool.js login notes --config /absolute/path/to/settings.json
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
`npx @koneeric/onetool login notes --config ...` is the same command. It prints the address to open, opens it
|
|
227
|
+
in your browser, and listens on `127.0.0.1` for the browser to come back. It waits 300 seconds at most, then
|
|
228
|
+
says in one line that it worked or why not. Two options change it:
|
|
229
|
+
|
|
230
|
+
- `--no-browser`: the address is only printed. Use it on a machine with no browser of its own: open the
|
|
231
|
+
address in a browser on that same machine, or forward the port.
|
|
232
|
+
- `--wait`, with a number of seconds: how long to wait for the browser, instead of 300.
|
|
233
|
+
|
|
234
|
+
**No restart.** A running onetool picks the sign-in up at the next `search` or `run`. A token that runs out
|
|
235
|
+
is refreshed by itself, with nothing asked. When the server refuses the refresh, it is `sign in needed` again,
|
|
236
|
+
and the same command repairs it.
|
|
237
|
+
|
|
238
|
+
**Where the sign-in is.** In `~/.onetool/auth`: one file per server, readable by you only (mode `600`). It is
|
|
239
|
+
tied to the name and the address of the server: when the address changes in the settings file, the sign-in is
|
|
240
|
+
not used, and you sign in again. Nothing of a sign-in is ever in an answer, in the trail, in the log or on
|
|
241
|
+
standard error.
|
|
242
|
+
|
|
243
|
+
**Forgetting it.** Run `onetool logout notes --config /absolute/path/to/settings.json`, in the same way. The
|
|
244
|
+
file is removed. Nothing is sent to the server.
|
|
245
|
+
|
|
246
|
+
**A server that does not let a client register itself.** Some services want you to get a client id from them
|
|
247
|
+
first. Put it in the key `oauth` of that server's entry. The keys are `clientId`, `clientSecret`, `scopes`
|
|
248
|
+
and `port`. You get the client id from the service. `port` is the port to listen on when the service wants a
|
|
249
|
+
fixed address to come back to, `http://127.0.0.1:<port>/callback`; by default any free port is used. The
|
|
250
|
+
key `redirectHost` is the host name written in that address: `127.0.0.1` by default, or `localhost` for a
|
|
251
|
+
service that wants `http://localhost:<port>/callback`. onetool listens on `127.0.0.1` either way, and you leave
|
|
252
|
+
the key out unless the service asks for the name. The
|
|
253
|
+
secret stays in the settings file and is never copied anywhere else.
|
|
254
|
+
|
|
255
|
+
```json
|
|
256
|
+
{
|
|
257
|
+
"mcpServers": {
|
|
258
|
+
"tracker": {
|
|
259
|
+
"url": "https://example.com/tracker/mcp",
|
|
260
|
+
"oauth": { "clientId": "your-client-id", "scopes": ["read"], "port": 8765 }
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
**What cannot be put behind onetool.** The connectors a host program signs in to itself cannot be put behind onetool.
|
|
267
|
+
They live in the host program, not in a settings file. Only servers you list by address can be signed in to
|
|
268
|
+
here.
|
|
269
|
+
|
|
270
|
+
**What it speaks to.** For a sign-in, and only then, onetool speaks to the sign-in server that the listed
|
|
271
|
+
server names. It writes nothing of OAuth itself: the official MCP package does all of it.
|
|
272
|
+
|
|
151
273
|
## Rules: allow, deny, ask
|
|
152
274
|
|
|
153
275
|
A pattern is matched against the whole id of a tool, `<server>.<tool>`, and upper and lower case count. A
|
|
@@ -255,22 +377,20 @@ command started keeps running after it. The shell is built for macOS and Linux.
|
|
|
255
377
|
### Another way: Claude Code as a listed server
|
|
256
378
|
|
|
257
379
|
Claude Code can itself be a listed server behind onetool. Started with `claude mcp serve`, it gives a program
|
|
258
|
-
its tools
|
|
380
|
+
its tools:
|
|
259
381
|
|
|
260
382
|
```json
|
|
261
383
|
{
|
|
262
384
|
"mcpServers": {
|
|
263
|
-
"claude": { "command": "claude", "args": ["mcp", "serve"] }
|
|
385
|
+
"claude": { "command": "claude", "args": ["mcp", "serve"], "tools": ["Bash", "Read", "Edit", "Write", "Grep", "Glob"] }
|
|
264
386
|
},
|
|
265
|
-
"onetool": {
|
|
266
|
-
"allow": ["claude.Bash", "claude.Read", "claude.Edit", "claude.Write", "claude.Grep", "claude.Glob"],
|
|
267
|
-
"hostServers": ["claude"]
|
|
268
|
-
}
|
|
387
|
+
"onetool": { "hostServers": ["claude"] }
|
|
269
388
|
}
|
|
270
389
|
```
|
|
271
390
|
|
|
272
391
|
That server offers many more tools than you may want behind onetool (it names at least `Agent` and
|
|
273
|
-
`WebFetch`), so the `
|
|
392
|
+
`WebFetch`), so the list `tools` keeps six. It closes no tool of the other servers, which a list `allow` would
|
|
393
|
+
do. `onetool init` writes this for you: see "Start with onetool init".
|
|
274
394
|
|
|
275
395
|
No permission question was shown in the try. The rules of onetool, `allow`, `deny` and `ask`, are therefore your
|
|
276
396
|
control over these tools. `hostServers` makes onetool apply the `Bash`, `Read`, `Edit` and `Write` rules of
|
|
@@ -496,6 +616,50 @@ that shows no prompt changes nothing else: you ask for the same work in your own
|
|
|
496
616
|
library, with its date. `library.restore(name)` brings the newest file of that name back, with its counts at
|
|
497
617
|
zero. It is refused when a program of that name is saved, or when the library is at its cap.
|
|
498
618
|
|
|
619
|
+
## Grouping the work
|
|
620
|
+
|
|
621
|
+
onetool saves context only when one program does several steps: a program that makes one call saves nothing.
|
|
622
|
+
So onetool leads the session to group its work. Put every step that needs no decision into one program.
|
|
623
|
+
|
|
624
|
+
**The instructions.** When the host program connects, onetool gives it a text for the session, the
|
|
625
|
+
instructions the MCP standard defines: 600 characters at most, the same whatever the servers. It asks the
|
|
626
|
+
assistant to list the steps before the first `run`, to put every step that needs no decision into one program,
|
|
627
|
+
to return only the small result needed to decide what comes next, to keep large results in `state`, and never
|
|
628
|
+
to use `run` for one tool call when the next call is already known. Whether the assistant reads it is the host
|
|
629
|
+
program's doing. `"instructions": false` switches the text off, and nothing else:
|
|
630
|
+
|
|
631
|
+
```json
|
|
632
|
+
{
|
|
633
|
+
"mcpServers": {
|
|
634
|
+
"practice": { "command": "node", "args": ["practice/src/server.js"] }
|
|
635
|
+
},
|
|
636
|
+
"onetool": { "instructions": false }
|
|
637
|
+
}
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
**The line of the trail.** When three programs in a row, in the foreground, each made exactly one call, and
|
|
641
|
+
nobody was asked a question in between, the trail of the third ends with this line. It is said again only after
|
|
642
|
+
three more programs. A program that calls a saved program does not count.
|
|
643
|
+
|
|
644
|
+
```
|
|
645
|
+
(the last 3 programs each made one call: they could have been one program)
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
**The number.** `search` with no words ends with one line for the session. Nothing is shown before the first
|
|
649
|
+
program. A low number of calls per program means the work is not grouped.
|
|
650
|
+
|
|
651
|
+
```
|
|
652
|
+
this session: 3 programs, 3 calls, 1.0 calls per program
|
|
653
|
+
```
|
|
654
|
+
|
|
655
|
+
**A paragraph to paste.** A host program that does not pass on the instructions of a server may still be told:
|
|
656
|
+
paste this paragraph into the standing instructions of your assistant, the file or the setting where the host
|
|
657
|
+
program keeps what the assistant is always told.
|
|
658
|
+
|
|
659
|
+
```text
|
|
660
|
+
When you use onetool, group the work. Before the first run, list the steps of the task. Put every step that needs no decision into one program: loops, filters, several tools. Return only the small result you need to decide what comes next, and keep large results in state. Never use run for one tool call when the next call is already known.
|
|
661
|
+
```
|
|
662
|
+
|
|
499
663
|
## The trail and the log
|
|
500
664
|
|
|
501
665
|
Every call a program makes through `run` is recorded, a tool of a listed server and a tool of the shell alike,
|
|
@@ -558,7 +722,15 @@ Standard output carries only MCP messages.
|
|
|
558
722
|
- `not connected (bad entry:`, followed by a reason: the entry of that server in the settings file breaks a
|
|
559
723
|
rule, for instance it has both `command` and `url`. Correct the entry.
|
|
560
724
|
- `no answer in`, followed by a number of seconds (the reason reads `no answer in 30 seconds`): the server did
|
|
561
|
-
not list its tools in time. Make sure it starts, or raise `connectSeconds`.
|
|
725
|
+
not list its tools in time. Make sure it starts, or raise `connectSeconds`. It also covers the `login` that
|
|
726
|
+
waited for the browser: no browser came back in time. Run it again, with `--wait` if you need longer.
|
|
727
|
+
- `nobody to ask`, said by `onetool init` when it proposes Claude Code and no terminal is there to answer: nothing
|
|
728
|
+
was added. Run the command the line gives, with `--yes`, or run `init` in a terminal.
|
|
729
|
+
- `sign in needed`, as `notes: sign in needed` in `search`, or in the message of a failed call: the server asks
|
|
730
|
+
for a sign-in and you have none, or it is no longer valid. Run the `login` command the message gives, in a
|
|
731
|
+
terminal. See "Signing in".
|
|
732
|
+
- `the sign-in could not be started`: the server names no sign-in server, or does not let a client register
|
|
733
|
+
itself. Give it a client id in the key `oauth` of its entry. See "Signing in".
|
|
562
734
|
|
|
563
735
|
Each of these lines has the form `onetool: server <name>: not connected (<reason>)`. A server that fails does
|
|
564
736
|
not stop the others, and does not stop onetool.
|
|
@@ -617,10 +789,12 @@ tools, or with the reason it is not connected.
|
|
|
617
789
|
|
|
618
790
|
- It does not change the settings file of another program.
|
|
619
791
|
- It does not speak the old SSE transport of MCP, the one with two addresses.
|
|
620
|
-
- It
|
|
792
|
+
- It never opens a browser by itself. You sign in with `onetool login`, from a terminal (see "Signing in").
|
|
621
793
|
- It does not connect again to a server that went away, and it does not follow a change of a server's list of
|
|
622
794
|
tools. Both need a new start.
|
|
623
|
-
- It writes its log; the file
|
|
795
|
+
- It writes its log; the settings file that `onetool init` is given, and only that one (see "Start with onetool
|
|
796
|
+
init"); the sign-ins, one file per server, in the folder `~/.onetool/auth`; the file of the
|
|
797
|
+
sequences, `sequences.jsonl`, in `~/.onetool`; the files of the library,
|
|
624
798
|
which are the programs that calls of `library.save` keep, their `stats.json` and their archive, in the
|
|
625
799
|
library folders; and, only with the shell switched on, what programs
|
|
626
800
|
write in the listed folders. It writes nothing else.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koneeric/onetool",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "One MCP server that runs programs over all the others",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"test": "node --test \"*/test/**/*.test.js\""
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
|
+
"auth/src/",
|
|
24
25
|
"bin/",
|
|
25
26
|
"core/src/",
|
|
26
27
|
"guide/pages/",
|
package/practice/src/server.js
CHANGED
|
@@ -4,6 +4,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
4
4
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
5
5
|
import { CallToolRequestSchema, ListToolsRequestSchema, ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
|
|
6
6
|
import { allRows } from "./rows.js";
|
|
7
|
+
import { createSignin } from "./signin.js";
|
|
7
8
|
|
|
8
9
|
const str = { type: "string" };
|
|
9
10
|
|
|
@@ -85,22 +86,36 @@ function createServer() {
|
|
|
85
86
|
return server;
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
const USAGE =
|
|
89
|
+
const USAGE =
|
|
90
|
+
"usage: node practice/src/server.js [--port <number> [--token <text>] [--signin [--expires <seconds>] [--client <id> [--secret <text>]]]]";
|
|
89
91
|
|
|
90
|
-
|
|
92
|
+
const WITH_WORD = ["--port", "--token", "--expires", "--client", "--secret"];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* `{port, token, signin}` from the arguments (`port` and `token` are `undefined` when not given; `signin` is
|
|
96
|
+
* `undefined` or `{expires, client, secret}`); `undefined` when they are not a form of the usage.
|
|
97
|
+
*/
|
|
91
98
|
function parseArguments(argv) {
|
|
92
|
-
if (argv.length === 0) return { port: undefined, token: undefined };
|
|
93
|
-
if (argv.length % 2 !== 0 || argv.length > 4) return undefined;
|
|
94
99
|
const given = {};
|
|
95
|
-
for (let i = 0; i < argv.length; i
|
|
100
|
+
for (let i = 0; i < argv.length; i++) {
|
|
96
101
|
const option = argv[i];
|
|
97
|
-
if (
|
|
98
|
-
given[option] =
|
|
102
|
+
if (Object.hasOwn(given, option)) return undefined;
|
|
103
|
+
if (option === "--signin") given[option] = true;
|
|
104
|
+
else if (WITH_WORD.includes(option) && i + 1 < argv.length) given[option] = argv[++i];
|
|
105
|
+
else return undefined;
|
|
99
106
|
}
|
|
100
|
-
const { "--port": port, "--token": token } = given;
|
|
107
|
+
const { "--port": port, "--token": token, "--signin": signin, "--expires": expires, "--client": client, "--secret": secret } = given;
|
|
108
|
+
if (argv.length === 0) return { port: undefined, token: undefined, signin: undefined };
|
|
101
109
|
if (port === undefined || !/^[0-9]+$/.test(port) || Number(port) > 65535) return undefined;
|
|
102
110
|
if (token === "") return undefined;
|
|
103
|
-
|
|
111
|
+
if (signin === undefined) {
|
|
112
|
+
if (expires !== undefined || client !== undefined || secret !== undefined) return undefined;
|
|
113
|
+
return { port: Number(port), token, signin: undefined };
|
|
114
|
+
}
|
|
115
|
+
if (token !== undefined) return undefined;
|
|
116
|
+
if (expires !== undefined && (!/^[0-9]+$/.test(expires) || Number(expires) < 1 || Number(expires) > 86400)) return undefined;
|
|
117
|
+
if (client === "" || secret === "" || (secret !== undefined && client === undefined)) return undefined;
|
|
118
|
+
return { port: Number(port), token: undefined, signin: { expires: expires === undefined ? 3600 : Number(expires), client, secret } };
|
|
104
119
|
}
|
|
105
120
|
|
|
106
121
|
/** The header may reach us as Latin-1 text of the bytes a client wrote in UTF-8; both spellings of the token are accepted. */
|
|
@@ -120,8 +135,21 @@ async function serveRequest(req, res) {
|
|
|
120
135
|
await transport.handleRequest(req, res);
|
|
121
136
|
}
|
|
122
137
|
|
|
123
|
-
function listen(port, token) {
|
|
138
|
+
function listen(port, token, signin) {
|
|
139
|
+
const sign = signin === undefined ? undefined : createSignin(signin);
|
|
140
|
+
const mcp = (req, res) =>
|
|
141
|
+
serveRequest(req, res).catch(() => {
|
|
142
|
+
if (!res.headersSent) res.writeHead(500);
|
|
143
|
+
res.end();
|
|
144
|
+
});
|
|
124
145
|
const httpServer = http.createServer((req, res) => {
|
|
146
|
+
if (sign !== undefined) {
|
|
147
|
+
sign.handle(req, res, () => mcp(req, res)).catch(() => {
|
|
148
|
+
if (!res.headersSent) res.writeHead(500);
|
|
149
|
+
res.end();
|
|
150
|
+
});
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
125
153
|
const path = (req.url ?? "").split("?")[0];
|
|
126
154
|
if (path !== "/mcp") {
|
|
127
155
|
res.writeHead(404).end();
|
|
@@ -131,10 +159,7 @@ function listen(port, token) {
|
|
|
131
159
|
res.writeHead(401).end();
|
|
132
160
|
return;
|
|
133
161
|
}
|
|
134
|
-
|
|
135
|
-
if (!res.headersSent) res.writeHead(500);
|
|
136
|
-
res.end();
|
|
137
|
-
});
|
|
162
|
+
mcp(req, res);
|
|
138
163
|
});
|
|
139
164
|
const cannot = () => {
|
|
140
165
|
process.stderr.write(`practice: cannot listen on port ${port}\n`);
|
|
@@ -152,7 +177,7 @@ if (options === undefined) {
|
|
|
152
177
|
process.stderr.write(`${USAGE}\n`);
|
|
153
178
|
process.exit(2);
|
|
154
179
|
} else if (options.port !== undefined) {
|
|
155
|
-
listen(options.port, options.token);
|
|
180
|
+
listen(options.port, options.token, options.signin);
|
|
156
181
|
} else {
|
|
157
182
|
const server = createServer();
|
|
158
183
|
process.stdin.on("end", () => {
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
const MAX_BODY = 1024 * 1024;
|
|
4
|
+
|
|
5
|
+
/** 32 characters of `A-Z`, `a-z`, `0-9`, `_`, `-` after `prefix`. */
|
|
6
|
+
const drawn = (prefix) => prefix + randomBytes(24).toString("base64url");
|
|
7
|
+
|
|
8
|
+
function send(res, status, body, headers = {}) {
|
|
9
|
+
const text = body === undefined ? "" : JSON.stringify(body);
|
|
10
|
+
res.writeHead(status, { ...(body === undefined ? {} : { "Content-Type": "application/json" }), "Cache-Control": "no-store", ...headers });
|
|
11
|
+
res.end(text);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function readBody(req) {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
const chunks = [];
|
|
17
|
+
let size = 0;
|
|
18
|
+
req.on("data", (chunk) => {
|
|
19
|
+
size += chunk.length;
|
|
20
|
+
if (size <= MAX_BODY) chunks.push(chunk);
|
|
21
|
+
});
|
|
22
|
+
req.on("end", () => resolve(size > MAX_BODY ? undefined : Buffer.concat(chunks).toString("utf8")));
|
|
23
|
+
req.on("error", () => resolve(undefined));
|
|
24
|
+
req.on("aborted", () => resolve(undefined));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const decoded = (text) => {
|
|
29
|
+
try {
|
|
30
|
+
return decodeURIComponent(text);
|
|
31
|
+
} catch {
|
|
32
|
+
return text;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** `{id, secret}` of an `Authorization: Basic` header, or `undefined`. */
|
|
37
|
+
function basicCredentials(header) {
|
|
38
|
+
if (header === undefined || !/^basic /i.test(header)) return undefined;
|
|
39
|
+
const pair = Buffer.from(header.slice(6).trim(), "base64").toString("utf8");
|
|
40
|
+
const colon = pair.indexOf(":");
|
|
41
|
+
if (colon < 0) return undefined;
|
|
42
|
+
return { id: decoded(pair.slice(0, colon)), secret: decoded(pair.slice(colon + 1)) };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const isLocalHttp = (text) => {
|
|
46
|
+
try {
|
|
47
|
+
const url = new URL(text);
|
|
48
|
+
return url.protocol === "http:" && (url.hostname === "127.0.0.1" || url.hostname === "localhost");
|
|
49
|
+
} catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The sign-in server: `{handle(req, res, next)}` for `--signin`, `--expires`, `--client` and `--secret`.
|
|
56
|
+
* Everything is kept in memory.
|
|
57
|
+
*/
|
|
58
|
+
export function createSignin({ expires, client, secret }) {
|
|
59
|
+
const clients = new Map();
|
|
60
|
+
const codes = new Map();
|
|
61
|
+
const accessTokens = new Map();
|
|
62
|
+
const refreshTokens = new Map();
|
|
63
|
+
const counts = { registrations: 0, authorizations: 0, tokens: 0, refreshes: 0, refused: 0 };
|
|
64
|
+
|
|
65
|
+
const known = (id) => (client !== undefined ? id === client : clients.has(id));
|
|
66
|
+
|
|
67
|
+
function issue(client_id) {
|
|
68
|
+
const access_token = drawn("practice-at-");
|
|
69
|
+
const refresh_token = drawn("practice-rt-");
|
|
70
|
+
accessTokens.set(access_token, { client_id, expiresAt: Date.now() + expires * 1000, refresh_token });
|
|
71
|
+
refreshTokens.set(refresh_token, { client_id, access_token });
|
|
72
|
+
return { access_token, token_type: "Bearer", expires_in: expires, refresh_token };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** True when the `Authorization` header is `Bearer` and an access token that is good. */
|
|
76
|
+
function mcpAllowed(header) {
|
|
77
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) return false;
|
|
78
|
+
const entry = accessTokens.get(header.slice(7));
|
|
79
|
+
if (entry === undefined) return false;
|
|
80
|
+
if (Date.now() >= entry.expiresAt) {
|
|
81
|
+
accessTokens.delete(header.slice(7));
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function documents(origin, path) {
|
|
88
|
+
if (path === "/.well-known/oauth-protected-resource/mcp" || path === "/.well-known/oauth-protected-resource") {
|
|
89
|
+
return { resource: `${origin}/mcp`, authorization_servers: [origin] };
|
|
90
|
+
}
|
|
91
|
+
if (path === "/.well-known/oauth-authorization-server") {
|
|
92
|
+
return {
|
|
93
|
+
issuer: origin,
|
|
94
|
+
authorization_endpoint: `${origin}/authorize`,
|
|
95
|
+
token_endpoint: `${origin}/token`,
|
|
96
|
+
...(client === undefined ? { registration_endpoint: `${origin}/register` } : {}),
|
|
97
|
+
response_types_supported: ["code"],
|
|
98
|
+
grant_types_supported: ["authorization_code", "refresh_token"],
|
|
99
|
+
code_challenge_methods_supported: ["S256"],
|
|
100
|
+
token_endpoint_auth_methods_supported: secret === undefined ? ["none"] : ["client_secret_basic", "client_secret_post"],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function register(req, res) {
|
|
107
|
+
const text = await readBody(req);
|
|
108
|
+
let body;
|
|
109
|
+
try {
|
|
110
|
+
body = JSON.parse(text);
|
|
111
|
+
} catch {
|
|
112
|
+
body = undefined;
|
|
113
|
+
}
|
|
114
|
+
const uris = body?.redirect_uris;
|
|
115
|
+
if (
|
|
116
|
+
body === null ||
|
|
117
|
+
typeof body !== "object" ||
|
|
118
|
+
Array.isArray(body) ||
|
|
119
|
+
!Array.isArray(uris) ||
|
|
120
|
+
uris.length === 0 ||
|
|
121
|
+
!uris.every((u) => typeof u === "string")
|
|
122
|
+
) {
|
|
123
|
+
send(res, 400, { error: "invalid_client_metadata" });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const client_id = `practice-client-${clients.size + 1}`;
|
|
127
|
+
clients.set(client_id, { redirect_uris: uris });
|
|
128
|
+
counts.registrations++;
|
|
129
|
+
const { client_secret: _dropped, ...held } = body;
|
|
130
|
+
send(res, 201, { ...held, client_id });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function authorize(query, res) {
|
|
134
|
+
const get = (key) => query.get(key) ?? "";
|
|
135
|
+
const client_id = get("client_id");
|
|
136
|
+
const redirect_uri = get("redirect_uri");
|
|
137
|
+
const code_challenge = get("code_challenge");
|
|
138
|
+
let target;
|
|
139
|
+
try {
|
|
140
|
+
target = new URL(redirect_uri);
|
|
141
|
+
} catch {
|
|
142
|
+
target = undefined;
|
|
143
|
+
}
|
|
144
|
+
const addressOk =
|
|
145
|
+
target !== undefined &&
|
|
146
|
+
(client !== undefined ? isLocalHttp(redirect_uri) : clients.get(client_id)?.redirect_uris.includes(redirect_uri));
|
|
147
|
+
if (
|
|
148
|
+
get("response_type") !== "code" ||
|
|
149
|
+
!known(client_id) ||
|
|
150
|
+
!addressOk ||
|
|
151
|
+
code_challenge === "" ||
|
|
152
|
+
get("code_challenge_method") !== "S256"
|
|
153
|
+
) {
|
|
154
|
+
send(res, 400, { error: "invalid_request" });
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const code = drawn("practice-code-");
|
|
158
|
+
codes.set(code, { client_id, redirect_uri, code_challenge });
|
|
159
|
+
target.searchParams.append("code", code);
|
|
160
|
+
if (query.has("state")) target.searchParams.append("state", query.get("state"));
|
|
161
|
+
counts.authorizations++;
|
|
162
|
+
res.writeHead(302, { Location: target.toString(), "Cache-Control": "no-store" });
|
|
163
|
+
res.end();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function tokenEndpoint(req, res) {
|
|
167
|
+
const text = await readBody(req);
|
|
168
|
+
const isForm = /^application\/x-www-form-urlencoded\s*(;|$)/i.test(req.headers["content-type"] ?? "");
|
|
169
|
+
const body = new URLSearchParams(isForm && text !== undefined ? text : "");
|
|
170
|
+
const basic = basicCredentials(req.headers.authorization);
|
|
171
|
+
const client_id = basic?.id ?? body.get("client_id") ?? "";
|
|
172
|
+
if (secret !== undefined) {
|
|
173
|
+
const given = basic !== undefined ? basic.secret : body.get("client_secret");
|
|
174
|
+
if (given !== secret) {
|
|
175
|
+
send(res, 401, { error: "invalid_client" });
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const grant = body.get("grant_type");
|
|
180
|
+
if (grant === "authorization_code") {
|
|
181
|
+
const code = body.get("code") ?? "";
|
|
182
|
+
const entry = codes.get(code);
|
|
183
|
+
const verifier = body.get("code_verifier");
|
|
184
|
+
const good =
|
|
185
|
+
entry !== undefined &&
|
|
186
|
+
entry.client_id === client_id &&
|
|
187
|
+
entry.redirect_uri === body.get("redirect_uri") &&
|
|
188
|
+
verifier !== null &&
|
|
189
|
+
createHash("sha256").update(verifier).digest("base64url") === entry.code_challenge;
|
|
190
|
+
if (entry !== undefined) codes.delete(code);
|
|
191
|
+
if (!good) {
|
|
192
|
+
send(res, 400, { error: "invalid_grant" });
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
counts.tokens++;
|
|
196
|
+
send(res, 200, issue(client_id));
|
|
197
|
+
} else if (grant === "refresh_token") {
|
|
198
|
+
const old = body.get("refresh_token") ?? "";
|
|
199
|
+
const entry = refreshTokens.get(old);
|
|
200
|
+
if (entry === undefined || entry.client_id !== client_id) {
|
|
201
|
+
send(res, 400, { error: "invalid_grant" });
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
refreshTokens.delete(old);
|
|
205
|
+
accessTokens.delete(entry.access_token);
|
|
206
|
+
counts.refreshes++;
|
|
207
|
+
send(res, 200, issue(client_id));
|
|
208
|
+
} else {
|
|
209
|
+
send(res, 400, { error: "unsupported_grant_type" });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Answers the requests of points 1 to 7 and 9; `next()` is called for `/mcp`, after the sign-in check of point 8. */
|
|
214
|
+
async function handle(req, res, next) {
|
|
215
|
+
const [path, ...rest] = (req.url ?? "").split("?");
|
|
216
|
+
const query = new URLSearchParams(rest.join("?"));
|
|
217
|
+
const origin = `http://127.0.0.1:${req.socket.localPort}`;
|
|
218
|
+
const { method } = req;
|
|
219
|
+
const document = method === "GET" ? documents(origin, path) : undefined;
|
|
220
|
+
if (document !== undefined) return send(res, 200, document);
|
|
221
|
+
if (path === "/register" && method === "POST" && client === undefined) return register(req, res);
|
|
222
|
+
if (path === "/authorize" && method === "GET") return authorize(query, res);
|
|
223
|
+
if (path === "/token" && method === "POST") return tokenEndpoint(req, res);
|
|
224
|
+
if (path === "/forget" && method === "POST") {
|
|
225
|
+
codes.clear();
|
|
226
|
+
accessTokens.clear();
|
|
227
|
+
refreshTokens.clear();
|
|
228
|
+
return send(res, 204);
|
|
229
|
+
}
|
|
230
|
+
if (path === "/seen" && method === "GET") return send(res, 200, counts);
|
|
231
|
+
if (path === "/mcp") {
|
|
232
|
+
if (mcpAllowed(req.headers.authorization)) return next();
|
|
233
|
+
counts.refused++;
|
|
234
|
+
return send(res, 401, undefined, {
|
|
235
|
+
"WWW-Authenticate": `Bearer resource_metadata="${origin}/.well-known/oauth-protected-resource/mcp"`,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
return send(res, 404);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return { handle };
|
|
242
|
+
}
|
package/sandbox/src/child.mjs
CHANGED
|
@@ -97,6 +97,23 @@ function bootstrap(host) {
|
|
|
97
97
|
});
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
+
// A server given as `true`: every name that is a string is a tool, made when first asked for. `then` and
|
|
101
|
+
// `toJSON` are not, so that awaiting or serialising the object calls nothing; the object has no key of its own.
|
|
102
|
+
const everyName = (server) => {
|
|
103
|
+
const made = new Map();
|
|
104
|
+
return new Proxy(Object.create(null), {
|
|
105
|
+
get(target, name) {
|
|
106
|
+
if (typeof name !== "string" || name === "then" || name === "toJSON") return undefined;
|
|
107
|
+
let tool = made.get(name);
|
|
108
|
+
if (tool === undefined) {
|
|
109
|
+
tool = makeTool(server, name);
|
|
110
|
+
made.set(name, tool);
|
|
111
|
+
}
|
|
112
|
+
return tool;
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
|
|
100
117
|
// The four names of `shell: true`: more ways to make a tool call of the server "shell", nothing else.
|
|
101
118
|
const defineShell = () => {
|
|
102
119
|
const callRead = makeTool("shell", "read");
|
|
@@ -282,11 +299,14 @@ function bootstrap(host) {
|
|
|
282
299
|
const spec = parse(toolsJson);
|
|
283
300
|
const tools = {};
|
|
284
301
|
for (const server of Object.keys(spec)) {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
302
|
+
let entry = {};
|
|
303
|
+
if (spec[server] === true) entry = everyName(server);
|
|
304
|
+
else {
|
|
305
|
+
for (const tool of spec[server]) {
|
|
306
|
+
Object.defineProperty(entry, tool, {
|
|
307
|
+
value: makeTool(server, tool), writable: true, enumerable: true, configurable: true,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
290
310
|
}
|
|
291
311
|
Object.defineProperty(tools, server, { value: entry, writable: true, enumerable: true, configurable: true });
|
|
292
312
|
}
|