@procedures/mcp-dev-tools 0.63.0 → 0.65.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/CHANGELOG.md +55 -0
- package/README.md +46 -3
- package/dist/config.d.ts +20 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +27 -1
- package/dist/config.js.map +1 -1
- package/dist/host.d.ts +31 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +73 -8
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/peers.d.ts +53 -0
- package/dist/peers.d.ts.map +1 -0
- package/dist/peers.js +89 -0
- package/dist/peers.js.map +1 -0
- package/package.json +8 -7
- package/src/config.ts +41 -1
- package/src/host.ts +76 -7
- package/src/index.ts +2 -1
- package/src/peers.ts +112 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,60 @@
|
|
|
1
1
|
# @procedures/mcp-dev-tools
|
|
2
2
|
|
|
3
|
+
## 0.65.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [e7ec32b]
|
|
8
|
+
- Updated dependencies [4e20391]
|
|
9
|
+
- Updated dependencies [abf52af]
|
|
10
|
+
- Updated dependencies [a2a09b7]
|
|
11
|
+
- Updated dependencies [ffb85af]
|
|
12
|
+
- Updated dependencies [420ebdc]
|
|
13
|
+
- Updated dependencies [f1c599f]
|
|
14
|
+
- @procedures/compiler@0.65.0
|
|
15
|
+
- @procedures/client@0.65.0
|
|
16
|
+
- @procedures/fs@0.65.0
|
|
17
|
+
- @procedures/bundle-mcp@0.65.0
|
|
18
|
+
- @procedures/mcp-server@0.65.0
|
|
19
|
+
|
|
20
|
+
## 0.64.0
|
|
21
|
+
|
|
22
|
+
### Minor Changes
|
|
23
|
+
|
|
24
|
+
- 49b516e: The packages use `@mark1russell7/service` 0.10.0 and `@mark1russell7/algebra` 0.9.
|
|
25
|
+
|
|
26
|
+
This is a breaking change. Before, a timer of service work (a backoff, a retry, a time-out) did not keep a Node process alive. A script ended while such a timer waited. After this change, the process waits for the timer, as it does for `setTimeout`.
|
|
27
|
+
|
|
28
|
+
- The `realClock` of `@procedures/client` worked this way before. Its timers do not change.
|
|
29
|
+
- The watch timers of service (checks, the watchdog, the sweeps) do not keep a process alive.
|
|
30
|
+
- The heartbeat of the WebSocket client uses `unrefClock` of service. Its timers do not keep a process alive: the socket does.
|
|
31
|
+
- A new end-to-end test runs a command with `main()` of `@procedures/mark` in a script. After the command, no timer keeps the script alive.
|
|
32
|
+
|
|
33
|
+
Migration: a script that must end before a pending timer of service fires stops its work first. Close each client, host and transport, or abort the call. For a timer that only watches other work, give the service a clock made with `unrefClock` of `@mark1russell7/service/clock`.
|
|
34
|
+
|
|
35
|
+
### Patch Changes
|
|
36
|
+
|
|
37
|
+
- 5d5a86b: `MCP_BUNDLES` takes module files next to the bundles: a `file:` URL, an absolute path, or a path that starts with `./` or `../`. The default export of the file is a procedure module. Its `mcpNamespaces`, or else the namespaces of its own procedures, give its tools. Thus a repository outside the monorepo gives its tools to this server, with no second MCP server. The file can use its own copy of `@procedures/client`.
|
|
38
|
+
- ce0d511: The dev-tools MCP server forwards the tools of running peers (`MCP_PEERS`, `mark mcp --peers`).
|
|
39
|
+
|
|
40
|
+
- An entry `name=source` names a peer. A source is the base URL of the procedures of the peer, or its discovery file: a JSON object with `url` or `endpoint`, and `token`.
|
|
41
|
+
- At the start, the server reads the manifest of each peer with `peerModule`, with a time limit of 5 seconds. Each procedure of the peer becomes a tool, and its calls go to the peer.
|
|
42
|
+
- The server reads a discovery file at each call. Thus the calls follow a peer that starts again with a new port and token.
|
|
43
|
+
- A peer that does not answer gives no tools until the next start. The server writes the reason to stderr, and `DevToolsHost.peers` gives the state of each peer.
|
|
44
|
+
- The new exports of `@procedures/mcp-dev-tools` are `parsePeers`, `loadPeers`, `peerAddress`, `readDiscovery`, `DEFAULT_PEER_TIMEOUT_MS` and their types.
|
|
45
|
+
- `peerModule` of `@procedures/client` has the new option `signal` for the read of the manifest.
|
|
46
|
+
- Updated dependencies [6f8219e]
|
|
47
|
+
- Updated dependencies [bdca30d]
|
|
48
|
+
- Updated dependencies [dfd04b7]
|
|
49
|
+
- Updated dependencies [7d303f3]
|
|
50
|
+
- Updated dependencies [ce0d511]
|
|
51
|
+
- Updated dependencies [49b516e]
|
|
52
|
+
- @procedures/client@0.64.0
|
|
53
|
+
- @procedures/fs@0.64.0
|
|
54
|
+
- @procedures/mcp-server@0.64.0
|
|
55
|
+
- @procedures/bundle-mcp@0.64.0
|
|
56
|
+
- @procedures/compiler@0.64.0
|
|
57
|
+
|
|
3
58
|
## 0.63.0
|
|
4
59
|
|
|
5
60
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ When the client closes the input of stdio, the server stops: the calls in flight
|
|
|
39
39
|
|
|
40
40
|
| Environment variable | Default | Function |
|
|
41
41
|
|---|---|---|
|
|
42
|
-
| `MCP_BUNDLES` | `@procedures/bundle-mcp` | The bundles to load, separated by commas. The server imports `<bundle>/
|
|
42
|
+
| `MCP_BUNDLES` | `@procedures/bundle-mcp` | The bundles and the module files to load, separated by commas. The server imports `<bundle>/module` for each bundle. A path is a [module file](#the-module-files-of-other-repositories). `@procedures/bundle-assistant` adds the tools of the assistant. |
|
|
43
43
|
| `MCP_SERVER_NAME` | `impl-mcp-dev` | The server name in the MCP handshake |
|
|
44
44
|
| `MCP_SERVER_VERSION` | `1.0.0` | The server version in the MCP handshake |
|
|
45
45
|
| `MCP_DEBUG` | `false` | With `true`, the server writes debug lines to stderr |
|
|
@@ -50,8 +50,9 @@ When the client closes the input of stdio, the server stops: the calls in flight
|
|
|
50
50
|
| `MCP_PROGRAM_CHECK` | `true` | With `false`, a program starts without the check before the run |
|
|
51
51
|
| `MCP_FS_ROOT` | not set | The root of the files: each tool keeps its paths in this folder, or the host refuses it. Refer to "The root of the files". |
|
|
52
52
|
| `MCP_LISTING` | `each` | With `search`, the list has three tools: `search_tools`, `describe_tool` and `call_tool`. Refer to "The cost of the search listing". |
|
|
53
|
+
| `MCP_PEERS` | not set | The running peers, as `name=source` entries separated by commas. The server forwards the calls of their tools. Refer to "The procedures of running peers". |
|
|
53
54
|
|
|
54
|
-
`mark mcp` starts the same server. Its flags replace the variables: `--bundles`, `--tool-timeout`, `--read-only`, `--http`, `--host`, `--token`, `--max-sessions`, `--fs-root` and `--listing`.
|
|
55
|
+
`mark mcp` starts the same server. Its flags replace the variables: `--bundles`, `--peers`, `--tool-timeout`, `--read-only`, `--http`, `--host`, `--token`, `--max-sessions`, `--fs-root` and `--listing`.
|
|
55
56
|
|
|
56
57
|
The server operates under `run()` of `@mark1russell7/service` (CLI-11). The runner owns the signals, the stop in stages and the exit of the process.
|
|
57
58
|
|
|
@@ -102,6 +103,46 @@ The rules and the table of all the procedures are in [CAPABILITIES-2026-10.md](.
|
|
|
102
103
|
|
|
103
104
|
The server sends `console.log` output to stderr, because stdout carries the MCP protocol.
|
|
104
105
|
|
|
106
|
+
## The module files of other repositories
|
|
107
|
+
|
|
108
|
+
A repository outside this monorepo can give its tools to this server, with no second MCP server. Put the path of its module file in `MCP_BUNDLES`:
|
|
109
|
+
|
|
110
|
+
```json
|
|
111
|
+
"env": { "MCP_BUNDLES": "@procedures/bundle-mcp,/path/to/tools/module.mjs" }
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- An entry is a module file when it is a `file:` URL, an absolute path, or a path that starts with `./` or `../`. A relative path starts from the folder of the process.
|
|
115
|
+
- The default export of the file is a procedure module: an object with a list of procedures.
|
|
116
|
+
- The export `mcpNamespaces` of the file gives its tools. Without it, the tools are the namespaces of the procedures of the module. The modules in `requires` add no tool.
|
|
117
|
+
- The file can use its own copy of `@procedures/client`. A procedure is data, so the validators of that copy examine each input.
|
|
118
|
+
- When a file does not load, the server stops with a message that names the file. A bundle without a `module` entry cannot have module files next to it.
|
|
119
|
+
|
|
120
|
+
This module file of one line gives the five tools of Vex. Put it in a folder with `@mark1russell7/vex-mcp` in its `node_modules`:
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
export { vexModule as default } from "@mark1russell7/vex-mcp";
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`src/module-file.test.ts` loads a module file with no imports next to `bundle-mcp`, and starts its tool through the server.
|
|
127
|
+
|
|
128
|
+
## The procedures of running peers
|
|
129
|
+
|
|
130
|
+
A process with a state, for example a daemon, keeps its procedures in its own process. A second copy of its module in this server has a different state. Thus the server forwards the calls. Put the peer in `MCP_PEERS`:
|
|
131
|
+
|
|
132
|
+
```json
|
|
133
|
+
"env": { "MCP_PEERS": "jobs=/path/to/jobs-daemon.json,files=http://127.0.0.1:4000/api" }
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
- The name of a peer has lowercase letters, digits and hyphens, and it starts with a letter.
|
|
137
|
+
- A source is the base URL of the procedures of the peer, or its discovery file. A URL sends no token.
|
|
138
|
+
- A discovery file is a JSON object with the URL in `url` or `endpoint`, and a bearer token in `token`. A peer that starts again writes a new port and token in the file. The server reads the file at each call, so the calls follow the peer.
|
|
139
|
+
- At the start, the server reads the manifest of each peer through `procedure.manifest`, with a time limit of 5 seconds. Each procedure of the manifest becomes a tool, with its description, its JSON Schemas and its effects. `client.*` and `procedure.*` stay out.
|
|
140
|
+
- The peer examines each input with its own schemas, and applies its own policy. `MCP_READ_ONLY` keeps only the procedures that declare no writes.
|
|
141
|
+
- A peer that does not answer at the start gives no tools until the next start. The server writes the reason to stderr, and `host.peers` gives the state of each peer.
|
|
142
|
+
- When a path of a peer is also a path of the server or of another peer, the server does not start. The message names the path.
|
|
143
|
+
|
|
144
|
+
`src/peers.test.ts` starts peers on free ports of `127.0.0.1`, with and without a token, and calls their tools through the server.
|
|
145
|
+
|
|
105
146
|
## The tool list
|
|
106
147
|
|
|
107
148
|
`tools.snapshot.txt` has the names of the 94 tools. `src/tools.test.ts` starts the real server over stdio and compares its tools with the file. It also examines that a `docker.*` tool cannot start a shell command. To change the tools, change `bundle-mcp` and `tools.snapshot.txt` in one commit. The [bundle-mcp README](../bundle-mcp/README.md) lists the tools and the reasons for the procedures that stay out.
|
|
@@ -128,4 +169,6 @@ The bytes are a measure of the tokens. A task is the search listing, one result
|
|
|
128
169
|
|
|
129
170
|
- **No tools in Claude.** Build the monorepo, and examine the path in the configuration. Start the server with `MCP_DEBUG=true` and read stderr.
|
|
130
171
|
- **Old tools or old behavior.** Restart the MCP server after a rebuild.
|
|
131
|
-
- **A bundle fails to load.** The server stops with the
|
|
172
|
+
- **A bundle fails to load.** The server stops with the error of the import. Build the bundle, and examine the name in `MCP_BUNDLES`.
|
|
173
|
+
- **A peer gives no tools.** The server writes `The peer <name> (<source>) gives no tools until the next start` and the reason to stderr. Start the peer, and then start the server again.
|
|
174
|
+
- **A module file fails to load.** The server stops with `MCP_BUNDLES: the module file <path> did not load` and the reason. Examine the path, and install the packages that the file imports.
|
package/dist/config.d.ts
CHANGED
|
@@ -46,12 +46,24 @@ export interface McpServerConfig {
|
|
|
46
46
|
* `"search"` lists three tools: `search_tools`, `describe_tool` and `call_tool`.
|
|
47
47
|
*/
|
|
48
48
|
listing?: "each" | "search" | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The running peers (d40-tool-surface, step 2). The host reads the manifest of each peer, and each
|
|
51
|
+
* tool of a peer sends its call to the peer. A peer that does not answer at the start gives no tools.
|
|
52
|
+
*/
|
|
53
|
+
peers?: readonly PeerConfig[] | undefined;
|
|
54
|
+
}
|
|
55
|
+
/** One entry of `MCP_PEERS`: the name of the peer, and its URL or its discovery file. */
|
|
56
|
+
export interface PeerConfig {
|
|
57
|
+
/** The name of the peer: a lowercase letter, then lowercase letters, digits and hyphens. */
|
|
58
|
+
readonly name: string;
|
|
59
|
+
/** The base URL of the procedures of the peer, or the path or `file:` URL of its discovery file. */
|
|
60
|
+
readonly source: string;
|
|
49
61
|
}
|
|
50
62
|
/**
|
|
51
63
|
* Load configuration from environment variables.
|
|
52
64
|
*
|
|
53
65
|
* Environment variables:
|
|
54
|
-
* - MCP_BUNDLES: Comma-separated list of bundles (default: "@procedures/bundle-mcp")
|
|
66
|
+
* - MCP_BUNDLES: Comma-separated list of bundles and module files (default: "@procedures/bundle-mcp")
|
|
55
67
|
* - MCP_SERVER_NAME: Server name (default: "impl-mcp-dev")
|
|
56
68
|
* - MCP_SERVER_VERSION: Server version (default: "1.0.0")
|
|
57
69
|
* - MCP_DEBUG: Enable debug logging (default: "false")
|
|
@@ -62,6 +74,7 @@ export interface McpServerConfig {
|
|
|
62
74
|
* - MCP_PROGRAM_CHECK: "false" or "0" starts a program without the check before the run
|
|
63
75
|
* - MCP_FS_ROOT: the root of the files: each tool keeps its paths in it, or it is refused (default: no root)
|
|
64
76
|
* - MCP_LISTING: "search" lists the three search tools in place of each tool (default: "each")
|
|
77
|
+
* - MCP_PEERS: the running peers, as `name=source` entries with commas (default: no peer)
|
|
65
78
|
*/
|
|
66
79
|
/** The default time limit of a tool call: a build or a test run can take minutes. */
|
|
67
80
|
export declare const DEFAULT_TOOL_TIMEOUT_MS = 600000;
|
|
@@ -83,5 +96,11 @@ export declare function parseMaxSessions(value: string | undefined): number | un
|
|
|
83
96
|
* server starts with no token, so a mistake in the environment opens it.
|
|
84
97
|
*/
|
|
85
98
|
export declare function parseHttpToken(value: string | undefined): string | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* This function reads `MCP_PEERS`. The value has entries `name=source`, with commas between them. A
|
|
101
|
+
* source is an `http://` or `https://` URL, or a discovery file. An entry that is not of this form is
|
|
102
|
+
* an error. Without this check, a mistake removes the tools of a peer with no message.
|
|
103
|
+
*/
|
|
104
|
+
export declare function parsePeers(value: string | undefined): PeerConfig[];
|
|
86
105
|
export declare function loadConfig(): McpServerConfig;
|
|
87
106
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,WAAW,eAAe;IAC9B,sDAAsD;IACtD,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,mCAAmC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,sCAAsC;IACtC,aAAa,EAAE,MAAM,CAAC;IACtB,2BAA2B;IAC3B,KAAK,EAAE,OAAO,CAAC;IACf,sEAAsE;IACtE,aAAa,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;OAIG;IACH,IAAI,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,GAAG,SAAS,CAAC;IAC7H;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACnC;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,WAAW,eAAe;IAC9B,sDAAsD;IACtD,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,mCAAmC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,sCAAsC;IACtC,aAAa,EAAE,MAAM,CAAC;IACtB,2BAA2B;IAC3B,KAAK,EAAE,OAAO,CAAC;IACf,sEAAsE;IACtE,aAAa,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB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S,GAAG,UAAU,EAAE,CAclE;AAED,wBAAgB,UAAU,IAAI,eAAe,CA+B5C"}
|
package/dist/config.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* Load configuration from environment variables.
|
|
8
8
|
*
|
|
9
9
|
* Environment variables:
|
|
10
|
-
* - MCP_BUNDLES: Comma-separated list of bundles (default: "@procedures/bundle-mcp")
|
|
10
|
+
* - MCP_BUNDLES: Comma-separated list of bundles and module files (default: "@procedures/bundle-mcp")
|
|
11
11
|
* - MCP_SERVER_NAME: Server name (default: "impl-mcp-dev")
|
|
12
12
|
* - MCP_SERVER_VERSION: Server version (default: "1.0.0")
|
|
13
13
|
* - MCP_DEBUG: Enable debug logging (default: "false")
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* - MCP_PROGRAM_CHECK: "false" or "0" starts a program without the check before the run
|
|
19
19
|
* - MCP_FS_ROOT: the root of the files: each tool keeps its paths in it, or it is refused (default: no root)
|
|
20
20
|
* - MCP_LISTING: "search" lists the three search tools in place of each tool (default: "each")
|
|
21
|
+
* - MCP_PEERS: the running peers, as `name=source` entries with commas (default: no peer)
|
|
21
22
|
*/
|
|
22
23
|
/** The default time limit of a tool call: a build or a test run can take minutes. */
|
|
23
24
|
export const DEFAULT_TOOL_TIMEOUT_MS = 600_000;
|
|
@@ -59,11 +60,35 @@ export function parseHttpToken(value) {
|
|
|
59
60
|
throw new Error("MCP_HTTP_TOKEN is set and empty: give it a token, or unset it");
|
|
60
61
|
return value;
|
|
61
62
|
}
|
|
63
|
+
const PEER_NAME = /^[a-z][a-z0-9-]*$/;
|
|
64
|
+
/**
|
|
65
|
+
* This function reads `MCP_PEERS`. The value has entries `name=source`, with commas between them. A
|
|
66
|
+
* source is an `http://` or `https://` URL, or a discovery file. An entry that is not of this form is
|
|
67
|
+
* an error. Without this check, a mistake removes the tools of a peer with no message.
|
|
68
|
+
*/
|
|
69
|
+
export function parsePeers(value) {
|
|
70
|
+
const peers = [];
|
|
71
|
+
if (value === undefined)
|
|
72
|
+
return peers;
|
|
73
|
+
for (const entry of value.split(",").map((part) => part.trim()).filter(Boolean)) {
|
|
74
|
+
const eq = entry.indexOf("=");
|
|
75
|
+
const name = eq === -1 ? "" : entry.slice(0, eq).trim();
|
|
76
|
+
const source = eq === -1 ? "" : entry.slice(eq + 1).trim();
|
|
77
|
+
if (!PEER_NAME.test(name) || source === "") {
|
|
78
|
+
throw new Error(`MCP_PEERS: the entry "${entry}" is not name=source, with a lowercase name and a URL or a discovery file`);
|
|
79
|
+
}
|
|
80
|
+
if (peers.some((peer) => peer.name === name))
|
|
81
|
+
throw new Error(`MCP_PEERS: two entries have the name ${name}`);
|
|
82
|
+
peers.push({ name, source });
|
|
83
|
+
}
|
|
84
|
+
return peers;
|
|
85
|
+
}
|
|
62
86
|
export function loadConfig() {
|
|
63
87
|
const bundlesEnv = process.env["MCP_BUNDLES"] ?? "@procedures/bundle-mcp";
|
|
64
88
|
const bundles = bundlesEnv.split(",").map((b) => b.trim()).filter(Boolean);
|
|
65
89
|
const fsRoot = process.env["MCP_FS_ROOT"]?.trim();
|
|
66
90
|
const listing = parseListing(process.env["MCP_LISTING"]);
|
|
91
|
+
const peers = parsePeers(process.env["MCP_PEERS"]);
|
|
67
92
|
const maxSessions = parseMaxSessions(process.env["MCP_HTTP_MAX_SESSIONS"]);
|
|
68
93
|
const httpToken = process.env["MCP_HTTP_PORT"] ? parseHttpToken(process.env["MCP_HTTP_TOKEN"]) : undefined;
|
|
69
94
|
return {
|
|
@@ -76,6 +101,7 @@ export function loadConfig() {
|
|
|
76
101
|
programCheck: !(process.env["MCP_PROGRAM_CHECK"] === "false" || process.env["MCP_PROGRAM_CHECK"] === "0"),
|
|
77
102
|
...(fsRoot ? { fsRoot } : {}),
|
|
78
103
|
...(listing ? { listing } : {}),
|
|
104
|
+
...(peers.length > 0 ? { peers } : {}),
|
|
79
105
|
...(process.env["MCP_HTTP_PORT"]
|
|
80
106
|
? {
|
|
81
107
|
http: {
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAwDH;;;;;;;;;;;;;;;;GAgBG;AACH,qFAAqF;AACrF,MAAM,CAAC,MAAM,uBAAuB,GAAG,OAAO,CAAC;AAE/C;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,KAAyB;IACpD,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,SAAS,CAAC;IACjE,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,IAAI,OAAO,KAAK,MAAM,IAAI,OAAO,KAAK,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,2CAA2C,OAAO,GAAG,CAAC,CAAC;IACvH,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,oGAAoG;AACpG,MAAM,UAAU,YAAY,CAAC,KAAyB;IACpD,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,uBAAuB,CAAC;IAC/E,MAAM,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IACzB,OAAO,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,uBAAuB,CAAC;AACvE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAyB;IACxD,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,SAAS,CAAC;IACjE,OAAO,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;AAC9B,CAAC;AA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import { ProcedureServer, type HostPolicy, type ProcedureModule, type ProcedureR
|
|
|
20
20
|
import { type BundleSurfaceModule, type McpHost } from "@procedures/mcp-server";
|
|
21
21
|
import { type Lifecycle, type RunOptions, type ServiceBuilder } from "@mark1russell7/service";
|
|
22
22
|
import { type McpServerConfig } from "./config.js";
|
|
23
|
+
import { type PeerState } from "./peers.js";
|
|
24
|
+
/** The exports of a bundle's `module` entry: its module and the lists of its tools. */
|
|
25
|
+
interface BundleModuleExports extends BundleSurfaceModule {
|
|
26
|
+
default?: ProcedureModule;
|
|
27
|
+
/**
|
|
28
|
+
* The modules that the host gives to `createRegistry()` before the bundles, for example a module
|
|
29
|
+
* with the host policy of `@procedures/http`. They replace the modules of the bundles with their names.
|
|
30
|
+
*/
|
|
31
|
+
mcpHostModules?: readonly ProcedureModule[];
|
|
32
|
+
}
|
|
23
33
|
/** The modules and the tool lists of some bundles. */
|
|
24
34
|
export interface LoadedBundles {
|
|
25
35
|
modules: ProcedureModule[];
|
|
@@ -30,7 +40,24 @@ export interface LoadedBundles {
|
|
|
30
40
|
*/
|
|
31
41
|
global: boolean;
|
|
32
42
|
}
|
|
33
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* This function tells if an entry of `MCP_BUNDLES` names a module file. A module file is a `file:`
|
|
45
|
+
* URL, an absolute path, or a path that starts with `./` or `../`. Each other entry names a bundle package.
|
|
46
|
+
*/
|
|
47
|
+
export declare function isModuleFile(bundle: string): boolean;
|
|
48
|
+
/** This function gives the URL of a module file. A relative path starts from `cwd`. */
|
|
49
|
+
export declare function moduleFileUrl(bundle: string, cwd?: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* This function loads a module file of `MCP_BUNDLES`: a file outside this package, for example
|
|
52
|
+
* the module of the tools of another repository. Its default export is the module. Its
|
|
53
|
+
* `mcpNamespaces` give its tools. Without them, its tools are the namespaces of its own
|
|
54
|
+
* procedures, so the modules in its `requires` add no tool. A file that does not load stops the
|
|
55
|
+
* host: the global registry cannot serve a file.
|
|
56
|
+
*/
|
|
57
|
+
export declare function loadModuleFile(bundle: string, cwd?: string): Promise<BundleModuleExports & {
|
|
58
|
+
default: ProcedureModule;
|
|
59
|
+
}>;
|
|
60
|
+
/** This function loads the module entry of each bundle and each module file (it registers nothing). */
|
|
34
61
|
export declare function loadBundles(bundles: readonly string[]): Promise<LoadedBundles>;
|
|
35
62
|
/** The registry, the surface, the policy and the server of the dev-tools host. */
|
|
36
63
|
export interface DevToolsHost {
|
|
@@ -44,6 +71,8 @@ export interface DevToolsHost {
|
|
|
44
71
|
refusedUnderRoot?: string[] | undefined;
|
|
45
72
|
/** The MCP host: `mcp.start()` starts its face (stdio, or Streamable HTTP with `config.http`). */
|
|
46
73
|
mcp: McpHost;
|
|
74
|
+
/** The state of each peer of `config.peers`: its count of forwarded procedures, or its error. */
|
|
75
|
+
peers: PeerState[];
|
|
47
76
|
}
|
|
48
77
|
/** This function builds the host. It starts no transport. */
|
|
49
78
|
export declare function createDevToolsHost(config?: McpServerConfig): Promise<DevToolsHost>;
|
|
@@ -68,4 +97,5 @@ export interface RunningDevToolsServer {
|
|
|
68
97
|
* After a startup that failed, `life.errors` tells why, and the process exits with the code 1.
|
|
69
98
|
*/
|
|
70
99
|
export declare function runDevToolsServer(config?: McpServerConfig, run?: RunOptions): Promise<RunningDevToolsServer>;
|
|
100
|
+
export {};
|
|
71
101
|
//# sourceMappingURL=host.d.ts.map
|
package/dist/host.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAEL,eAAe,
|
|
1
|
+
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAEL,eAAe,EAIf,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,iBAAiB,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AAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,eAAe,CAAC;IACxB,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,6FAA6F;IAC7F,gBAAgB,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;IACxC,kGAAkG;IAClG,GAAG,EAAE,OAAO,CAAC;IACb,iGAAiG;IACjG,KAAK,EAAE,SAAS,EAAE,CAAC;CACpB;AAcD,6DAA6D;AAC7D,wBAAsB,kBAAkB,CAAC,MAAM,GAAE,eAA8B,GAAG,OAAO,CAAC,YAAY,CAAC,CA4CtG;AASD,4GAA4G;AAC5G,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,6EAA6E;IAC7E,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,mEAAmE;IACnE,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CAAC,MAAM,GAAE,eAA8B,EAAE,GAAG,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAwChI"}
|
package/dist/host.js
CHANGED
|
@@ -16,30 +16,78 @@
|
|
|
16
16
|
* each tool keeps its paths in it, or the root refuses the tool. The host serves no remote
|
|
17
17
|
* events: its transport carries only MCP, and `events.subscribe` is not a tool.
|
|
18
18
|
*/
|
|
19
|
-
import { PROCEDURE_REGISTRY, ProcedureServer, createRegistry, registerClientProcedures, } from "@procedures/client";
|
|
19
|
+
import { PROCEDURE_REGISTRY, ProcedureServer, createRegistry, registerClientProcedures, registerModules, } from "@procedures/client";
|
|
20
20
|
import { createMcpHost, mergeSurfaces, redirectConsoleToStderr, } from "@procedures/mcp-server";
|
|
21
21
|
import { programCheckInterceptor } from "@procedures/compiler";
|
|
22
|
+
import { errorMessage } from "@mark1russell7/text";
|
|
22
23
|
import { createServer } from "node:http";
|
|
24
|
+
import { isAbsolute, resolve } from "node:path";
|
|
25
|
+
import { pathToFileURL } from "node:url";
|
|
23
26
|
import { ephemeral, localDev, service } from "@mark1russell7/service";
|
|
24
27
|
import { httpTransport } from "@mark1russell7/service/http";
|
|
25
28
|
import { loadConfig } from "./config.js";
|
|
29
|
+
import { loadPeers } from "./peers.js";
|
|
26
30
|
import { ms, seconds } from "@procedures/client";
|
|
27
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* This function tells if an entry of `MCP_BUNDLES` names a module file. A module file is a `file:`
|
|
33
|
+
* URL, an absolute path, or a path that starts with `./` or `../`. Each other entry names a bundle package.
|
|
34
|
+
*/
|
|
35
|
+
export function isModuleFile(bundle) {
|
|
36
|
+
return bundle.startsWith("file:") || isAbsolute(bundle) || /^\.\.?[\\/]/.test(bundle);
|
|
37
|
+
}
|
|
38
|
+
/** This function gives the URL of a module file. A relative path starts from `cwd`. */
|
|
39
|
+
export function moduleFileUrl(bundle, cwd = process.cwd()) {
|
|
40
|
+
return bundle.startsWith("file:") ? bundle : pathToFileURL(resolve(cwd, bundle)).href;
|
|
41
|
+
}
|
|
42
|
+
/** The first segment of each path of the own procedures of a module, not of its `requires`. */
|
|
43
|
+
function namespacesOf(module) {
|
|
44
|
+
return [...new Set(module.procedures.map((procedure) => procedure.path[0]).filter((name) => name !== undefined))].sort();
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* This function loads a module file of `MCP_BUNDLES`: a file outside this package, for example
|
|
48
|
+
* the module of the tools of another repository. Its default export is the module. Its
|
|
49
|
+
* `mcpNamespaces` give its tools. Without them, its tools are the namespaces of its own
|
|
50
|
+
* procedures, so the modules in its `requires` add no tool. A file that does not load stops the
|
|
51
|
+
* host: the global registry cannot serve a file.
|
|
52
|
+
*/
|
|
53
|
+
export async function loadModuleFile(bundle, cwd = process.cwd()) {
|
|
54
|
+
let exports;
|
|
55
|
+
try {
|
|
56
|
+
exports = (await import(moduleFileUrl(bundle, cwd)));
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
throw new Error(`MCP_BUNDLES: the module file ${bundle} did not load: ${errorMessage(error)}`, { cause: error });
|
|
60
|
+
}
|
|
61
|
+
const module = exports.default;
|
|
62
|
+
if (typeof module !== "object" || module === null || !Array.isArray(module.procedures)) {
|
|
63
|
+
throw new Error(`MCP_BUNDLES: the module file ${bundle} has no default export with a list of procedures`);
|
|
64
|
+
}
|
|
65
|
+
return { ...exports, default: module, mcpNamespaces: exports.mcpNamespaces ?? namespacesOf(module) };
|
|
66
|
+
}
|
|
67
|
+
/** This function loads the module entry of each bundle and each module file (it registers nothing). */
|
|
28
68
|
export async function loadBundles(bundles) {
|
|
29
69
|
const modules = [];
|
|
30
70
|
const hostModules = [];
|
|
31
71
|
const surfaces = [];
|
|
72
|
+
const files = bundles.filter(isModuleFile);
|
|
32
73
|
for (const bundle of bundles) {
|
|
74
|
+
if (isModuleFile(bundle)) {
|
|
75
|
+
const exports = await loadModuleFile(bundle);
|
|
76
|
+
modules.push(exports.default);
|
|
77
|
+
hostModules.push(...(exports.mcpHostModules ?? []));
|
|
78
|
+
surfaces.push(exports);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
33
81
|
let exports;
|
|
34
82
|
try {
|
|
35
83
|
// The import resolves from this package, which depends on its bundles
|
|
36
84
|
exports = (await import(`${bundle}/module`));
|
|
37
85
|
}
|
|
38
86
|
catch {
|
|
39
|
-
return loadBundlesGlobally(bundles);
|
|
87
|
+
return loadBundlesGlobally(bundles, files);
|
|
40
88
|
}
|
|
41
89
|
if (!exports.default)
|
|
42
|
-
return loadBundlesGlobally(bundles);
|
|
90
|
+
return loadBundlesGlobally(bundles, files);
|
|
43
91
|
modules.push(exports.default);
|
|
44
92
|
hostModules.push(...(exports.mcpHostModules ?? []));
|
|
45
93
|
surfaces.push(exports);
|
|
@@ -49,7 +97,10 @@ export async function loadBundles(bundles) {
|
|
|
49
97
|
return { modules: [...hostModules, ...modules], surfaces, global: false };
|
|
50
98
|
}
|
|
51
99
|
/** The old way, for a bundle without a module entry: import each `register.js`. */
|
|
52
|
-
async function loadBundlesGlobally(bundles) {
|
|
100
|
+
async function loadBundlesGlobally(bundles, files) {
|
|
101
|
+
if (files.length > 0) {
|
|
102
|
+
throw new Error(`MCP_BUNDLES: the module file ${files[0]} needs the module entry of each bundle, and a bundle has none`);
|
|
103
|
+
}
|
|
53
104
|
// The global registry gets the control flow only from this call (ARCHITECTURE-PROPOSALS-2026-10, P9)
|
|
54
105
|
registerClientProcedures();
|
|
55
106
|
const surfaces = [];
|
|
@@ -73,7 +124,10 @@ function facesOf(config) {
|
|
|
73
124
|
/** This function builds the host. It starts no transport. */
|
|
74
125
|
export async function createDevToolsHost(config = loadConfig()) {
|
|
75
126
|
const loaded = await loadBundles(config.bundles);
|
|
76
|
-
|
|
127
|
+
// MCP_PEERS (d40-tool-surface, step 2): a stub for each procedure of each running peer. A path of a
|
|
128
|
+
// peer that the host already has stops the start, with the error of the registry.
|
|
129
|
+
const peers = await loadPeers(config.peers ?? []);
|
|
130
|
+
const registry = loaded.global ? registerModules(PROCEDURE_REGISTRY, peers.modules) : createRegistry(...loaded.modules, ...peers.modules);
|
|
77
131
|
// MCP_FS_ROOT (roadmap R33): each procedure of the registry keeps its paths in the root, also
|
|
78
132
|
// in the nested calls, or the root refuses it. It adds no procedure: the tools stay the same.
|
|
79
133
|
let fsRoot;
|
|
@@ -82,8 +136,11 @@ export async function createDevToolsHost(config = loadConfig()) {
|
|
|
82
136
|
const { confineToRoot } = await import("@procedures/fs/module");
|
|
83
137
|
({ root: fsRoot, refused: refusedUnderRoot } = confineToRoot(registry, config.fsRoot));
|
|
84
138
|
}
|
|
85
|
-
// Bundles without mcpNamespaces say nothing about their tools: then every procedure is a tool
|
|
86
|
-
|
|
139
|
+
// Bundles without mcpNamespaces say nothing about their tools: then every procedure is a tool,
|
|
140
|
+
// also each stub of a peer. Else the namespaces of each peer add to the tools of the bundles.
|
|
141
|
+
const bundleSurface = mergeSurfaces(loaded.surfaces);
|
|
142
|
+
const peerSurfaces = peers.modules.map((module) => ({ mcpNamespaces: namespacesOf(module) }));
|
|
143
|
+
const surface = bundleSurface && peerSurfaces.length > 0 ? mergeSurfaces([...loaded.surfaces, ...peerSurfaces]) : bundleSurface;
|
|
87
144
|
// Read only: the tools are the procedures that the capability permits, so the list shows only
|
|
88
145
|
// what a call can start (CAPABILITIES-2026-10). The check before the run (roadmap R22): a
|
|
89
146
|
// program with an error does not start, so a refusal in step 3 comes before the write of step 1.
|
|
@@ -106,6 +163,7 @@ export async function createDevToolsHost(config = loadConfig()) {
|
|
|
106
163
|
policy: mcp.policy,
|
|
107
164
|
server: mcp.server,
|
|
108
165
|
mcp,
|
|
166
|
+
peers: peers.states,
|
|
109
167
|
...(fsRoot !== undefined ? { fsRoot, refusedUnderRoot } : {}),
|
|
110
168
|
};
|
|
111
169
|
}
|
|
@@ -154,6 +212,13 @@ export async function runDevToolsServer(config = loadConfig(), run) {
|
|
|
154
212
|
const face = host.mcp.state().faces[0];
|
|
155
213
|
if (config.debug)
|
|
156
214
|
console.error(`[mcp-server] Started with ${face?.toolCount ?? 0} tools`);
|
|
215
|
+
// A peer that did not answer gives no tools: the line tells why, so the gap is not silent
|
|
216
|
+
for (const peer of host.peers) {
|
|
217
|
+
if ("error" in peer)
|
|
218
|
+
console.error(`[mcp-server] The peer ${peer.name} (${peer.source}) gives no tools until the next start: ${peer.error}`);
|
|
219
|
+
else if (config.debug)
|
|
220
|
+
console.error(`[mcp-server] The peer ${peer.name} gives ${peer.procedures} procedures`);
|
|
221
|
+
}
|
|
157
222
|
if (host.fsRoot !== undefined)
|
|
158
223
|
console.error(`[mcp-server] Each tool refuses a path outside ${host.fsRoot}. Refused under the root: ${String(host.refusedUnderRoot)}`);
|
|
159
224
|
let url;
|
package/dist/host.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.js","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,wBAAwB,
|
|
1
|
+
{"version":3,"file":"host.js","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,wBAAwB,EACxB,eAAe,GAKhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,aAAa,EACb,aAAa,EACb,uBAAuB,GAIxB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAsE,MAAM,wBAAwB,CAAC;AAC1I,OAAO,EAAE,aAAa,EAAsC,MAAM,6BAA6B,CAAC;AAChG,OAAO,EAAE,UAAU,EAAwB,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAkB,MAAM,YAAY,CAAC;AACvD,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAuBjD;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,MAAc;IACzC,OAAO,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACxF,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,GAAG,GAAW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,UAAU,aAAa,CAAC,CAAC;IACjH,CAAC;IACD,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,CAAC,KAAK,CAAC,iDAAiD,IAAI,CAAC,MAAM,6BAA6B,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC;IACvK,IAAI,GAAuB,CAAC;IAC5B,MAAM,KAAK,GAAG,QAAQ,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC;IACzC,IAAI,MAAM,CAAC,IAAI,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACtD,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC;QACvJ,GAAG,GAAG,UAAU,KAAK,IAAI,KAAK,CAAC,IAAI,MAAM,CAAC;QAC1C,OAAO,CAAC,KAAK,CAAC,mCAAmC,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,uCAAuC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC7H,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;AAC/D,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* `dist/server.js` and `mark mcp` do), and `createDevToolsHost()` builds the host without a
|
|
7
7
|
* transport.
|
|
8
8
|
*/
|
|
9
|
-
export { loadConfig, type McpServerConfig } from "./config.js";
|
|
9
|
+
export { loadConfig, parsePeers, type McpServerConfig, type PeerConfig } from "./config.js";
|
|
10
|
+
export { DEFAULT_PEER_TIMEOUT_MS, loadPeers, peerAddress, readDiscovery, type LoadPeersOptions, type LoadedPeers, type PeerState } from "./peers.js";
|
|
10
11
|
export { createDevToolsHost, runDevToolsServer, loadBundles, type DevToolsHost, type LoadedBundles, type RunningDevToolsServer, } from "./host.js";
|
|
11
12
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,eAAe,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAC5F,OAAO,EAAE,uBAAuB,EAAE,SAAS,EAAE,WAAW,EAAE,aAAa,EAAE,KAAK,gBAAgB,EAAE,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AACrJ,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,WAAW,EACX,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,qBAAqB,GAC3B,MAAM,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* `dist/server.js` and `mark mcp` do), and `createDevToolsHost()` builds the host without a
|
|
7
7
|
* transport.
|
|
8
8
|
*/
|
|
9
|
-
export { loadConfig } from "./config.js";
|
|
9
|
+
export { loadConfig, parsePeers } from "./config.js";
|
|
10
|
+
export { DEFAULT_PEER_TIMEOUT_MS, loadPeers, peerAddress, readDiscovery } from "./peers.js";
|
|
10
11
|
export { createDevToolsHost, runDevToolsServer, loadBundles, } from "./host.js";
|
|
11
12
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAyC,MAAM,aAAa,CAAC;AAC5F,OAAO,EAAE,uBAAuB,EAAE,SAAS,EAAE,WAAW,EAAE,aAAa,EAA2D,MAAM,YAAY,CAAC;AACrJ,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,WAAW,GAIZ,MAAM,WAAW,CAAC"}
|
package/dist/peers.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The running peers of `MCP_PEERS` (d40-tool-surface, step 2).
|
|
3
|
+
*
|
|
4
|
+
* A peer with a state, for example a daemon, keeps its procedures in its own process. The host does
|
|
5
|
+
* not load a second copy of them. It reads the manifest of each peer with `peerModule` of
|
|
6
|
+
* `@procedures/client`, and each tool of the peer sends its call to the peer. The peer examines the
|
|
7
|
+
* input with its own schemas and applies its own policy.
|
|
8
|
+
*/
|
|
9
|
+
import { type PeerAddress, type ProcedureModule } from "@procedures/client";
|
|
10
|
+
import type { PeerConfig } from "./config.js";
|
|
11
|
+
/** The time limit of the read of the manifest of a peer at the start of the host. */
|
|
12
|
+
export declare const DEFAULT_PEER_TIMEOUT_MS = 5000;
|
|
13
|
+
/** The result of one peer at the start: its count of forwarded procedures, or the reason that it gives none. */
|
|
14
|
+
export type PeerState = {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly source: string;
|
|
17
|
+
readonly procedures: number;
|
|
18
|
+
} | {
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly source: string;
|
|
21
|
+
readonly error: string;
|
|
22
|
+
};
|
|
23
|
+
/** The modules and the states of the peers. */
|
|
24
|
+
export interface LoadedPeers {
|
|
25
|
+
readonly modules: ProcedureModule[];
|
|
26
|
+
readonly states: PeerState[];
|
|
27
|
+
}
|
|
28
|
+
/** The options of `loadPeers`. */
|
|
29
|
+
export interface LoadPeersOptions {
|
|
30
|
+
/** The time limit of the read of each manifest, in milliseconds. The default is `DEFAULT_PEER_TIMEOUT_MS`. */
|
|
31
|
+
readonly timeoutMs?: number | undefined;
|
|
32
|
+
/** The fetch function of the requests. The default is the global `fetch`. */
|
|
33
|
+
readonly fetch?: typeof fetch | undefined;
|
|
34
|
+
/** The folder of a relative path of a discovery file. The default is the folder of the process. */
|
|
35
|
+
readonly cwd?: string | undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* This function reads a discovery file. The file is a JSON object with the base URL of the procedures
|
|
39
|
+
* in `url` or `endpoint`, and an optional bearer token in `token`. A running peer writes the file. It
|
|
40
|
+
* writes a new port and a new token when it starts again.
|
|
41
|
+
*/
|
|
42
|
+
export declare function readDiscovery(file: string): Promise<PeerAddress>;
|
|
43
|
+
/**
|
|
44
|
+
* This function gives the address of a peer. A URL is the address. For a discovery file, it gives a
|
|
45
|
+
* function that reads the file at each call, so the calls follow the peer after it starts again.
|
|
46
|
+
*/
|
|
47
|
+
export declare function peerAddress(source: string, cwd?: string): PeerAddress | (() => Promise<PeerAddress>);
|
|
48
|
+
/**
|
|
49
|
+
* This function reads the manifest of each peer, all at one time. A peer that does not answer in
|
|
50
|
+
* the time limit gives no module, and its state tells why. Thus the other tools of the host stay available.
|
|
51
|
+
*/
|
|
52
|
+
export declare function loadPeers(peers: readonly PeerConfig[], options?: LoadPeersOptions): Promise<LoadedPeers>;
|
|
53
|
+
//# sourceMappingURL=peers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
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C,MAAM,OAAO,CAAC,WAAW,CAAC,CAAC,CAInH;AAED;;;GAGG;AACH,wBAAsB,SAAS,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,EAAE,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,WAAW,CAAC,CA2BlH"}
|
package/dist/peers.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The running peers of `MCP_PEERS` (d40-tool-surface, step 2).
|
|
3
|
+
*
|
|
4
|
+
* A peer with a state, for example a daemon, keeps its procedures in its own process. The host does
|
|
5
|
+
* not load a second copy of them. It reads the manifest of each peer with `peerModule` of
|
|
6
|
+
* `@procedures/client`, and each tool of the peer sends its call to the peer. The peer examines the
|
|
7
|
+
* input with its own schemas and applies its own policy.
|
|
8
|
+
*/
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import { resolve } from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { peerModule } from "@procedures/client";
|
|
13
|
+
import { errorMessage } from "@mark1russell7/text";
|
|
14
|
+
/** The time limit of the read of the manifest of a peer at the start of the host. */
|
|
15
|
+
export const DEFAULT_PEER_TIMEOUT_MS = 5_000;
|
|
16
|
+
/** This function tells if a text is an HTTP or HTTPS URL. */
|
|
17
|
+
function isHttpUrl(text) {
|
|
18
|
+
return /^https?:\/\//i.test(text);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* This function reads a discovery file. The file is a JSON object with the base URL of the procedures
|
|
22
|
+
* in `url` or `endpoint`, and an optional bearer token in `token`. A running peer writes the file. It
|
|
23
|
+
* writes a new port and a new token when it starts again.
|
|
24
|
+
*/
|
|
25
|
+
export async function readDiscovery(file) {
|
|
26
|
+
let text;
|
|
27
|
+
try {
|
|
28
|
+
text = await readFile(file, "utf8");
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
throw new Error(`the discovery file ${file} does not open (is the peer running?): ${errorMessage(error)}`, { cause: error });
|
|
32
|
+
}
|
|
33
|
+
let value;
|
|
34
|
+
try {
|
|
35
|
+
value = JSON.parse(text);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
throw new Error(`the discovery file ${file} is not JSON`, { cause: error });
|
|
39
|
+
}
|
|
40
|
+
const record = (typeof value === "object" && value !== null ? value : {});
|
|
41
|
+
const url = typeof record.url === "string" ? record.url : record.endpoint;
|
|
42
|
+
if (typeof url !== "string" || !isHttpUrl(url))
|
|
43
|
+
throw new Error(`the discovery file ${file} has no HTTP URL in "url" or "endpoint"`);
|
|
44
|
+
if (record.token !== undefined && typeof record.token !== "string")
|
|
45
|
+
throw new Error(`the token of the discovery file ${file} is not a text`);
|
|
46
|
+
return { url, ...(typeof record.token === "string" ? { token: record.token } : {}) };
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* This function gives the address of a peer. A URL is the address. For a discovery file, it gives a
|
|
50
|
+
* function that reads the file at each call, so the calls follow the peer after it starts again.
|
|
51
|
+
*/
|
|
52
|
+
export function peerAddress(source, cwd = process.cwd()) {
|
|
53
|
+
if (isHttpUrl(source))
|
|
54
|
+
return { url: source };
|
|
55
|
+
const file = source.startsWith("file:") ? fileURLToPath(source) : resolve(cwd, source);
|
|
56
|
+
return () => readDiscovery(file);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* This function reads the manifest of each peer, all at one time. A peer that does not answer in
|
|
60
|
+
* the time limit gives no module, and its state tells why. Thus the other tools of the host stay available.
|
|
61
|
+
*/
|
|
62
|
+
export async function loadPeers(peers, options = {}) {
|
|
63
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_PEER_TIMEOUT_MS;
|
|
64
|
+
const results = await Promise.all(peers.map(async (peer) => {
|
|
65
|
+
try {
|
|
66
|
+
const module = await peerModule({
|
|
67
|
+
name: `peer:${peer.name}`,
|
|
68
|
+
address: peerAddress(peer.source, options.cwd),
|
|
69
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
70
|
+
...(options.fetch !== undefined ? { fetch: options.fetch } : {}),
|
|
71
|
+
});
|
|
72
|
+
return { peer, module };
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
return { peer, error: errorMessage(error) };
|
|
76
|
+
}
|
|
77
|
+
}));
|
|
78
|
+
const loaded = { modules: [], states: [] };
|
|
79
|
+
for (const result of results) {
|
|
80
|
+
if (result.module === undefined) {
|
|
81
|
+
loaded.states.push({ name: result.peer.name, source: result.peer.source, error: result.error ?? "no module" });
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
loaded.modules.push(result.module);
|
|
85
|
+
loaded.states.push({ name: result.peer.name, source: result.peer.source, procedures: result.module.procedures.length });
|
|
86
|
+
}
|
|
87
|
+
return loaded;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=peers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"peers.js","sourceRoot":"","sources":["../src/peers.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,UAAU,EAA0C,MAAM,oBAAoB,CAAC;AACxF,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGnD,qFAAqF;AACrF,MAAM,CAAC,MAAM,uBAAuB,GAAG,KAAK,CAAC;AAuB7C,6DAA6D;AAC7D,SAAS,SAAS,CAAC,IAAY;IAC7B,OAAO,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACpC,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,IAAY;IAC9C,IAAI,IAAY,CAAC;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,MA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AC9C,CAAC;IACH,CAAC,CAAC,CACH,CAAC;IACF,MAAM,MAAM,GAAgB,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;IACxD,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAChC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,WAAW,EAAE,CAAC,CAAC;YAC/G,SAAS;QACX,CAAC;QACD,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACnC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;IAC1H,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@procedures/mcp-dev-tools",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.65.0",
|
|
5
5
|
"description": "A ready-to-use MCP server for Claude Code and Claude Desktop: 91 developer tools (docker, mongo, sqlite, cue, vitest, workspace) with a strict call policy.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"mcp",
|
|
@@ -54,12 +54,13 @@
|
|
|
54
54
|
"provenance": true
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@mark1russell7/service": "^0.
|
|
58
|
-
"@
|
|
59
|
-
"@procedures/
|
|
60
|
-
"@procedures/
|
|
61
|
-
"@procedures/
|
|
62
|
-
"@procedures/
|
|
57
|
+
"@mark1russell7/service": "^0.10.0",
|
|
58
|
+
"@mark1russell7/text": "^0.0.13",
|
|
59
|
+
"@procedures/bundle-mcp": "0.65.0",
|
|
60
|
+
"@procedures/client": "0.65.0",
|
|
61
|
+
"@procedures/compiler": "0.65.0",
|
|
62
|
+
"@procedures/fs": "0.65.0",
|
|
63
|
+
"@procedures/mcp-server": "0.65.0"
|
|
63
64
|
},
|
|
64
65
|
"devDependencies": {
|
|
65
66
|
"@mark1russell7/cue": "^2.1.1",
|
package/src/config.ts
CHANGED
|
@@ -42,13 +42,27 @@ export interface McpServerConfig {
|
|
|
42
42
|
* `"search"` lists three tools: `search_tools`, `describe_tool` and `call_tool`.
|
|
43
43
|
*/
|
|
44
44
|
listing?: "each" | "search" | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* The running peers (d40-tool-surface, step 2). The host reads the manifest of each peer, and each
|
|
47
|
+
* tool of a peer sends its call to the peer. A peer that does not answer at the start gives no tools.
|
|
48
|
+
*/
|
|
49
|
+
peers?: readonly PeerConfig[] | undefined;
|
|
45
50
|
}
|
|
46
51
|
|
|
52
|
+
/** One entry of `MCP_PEERS`: the name of the peer, and its URL or its discovery file. */
|
|
53
|
+
export interface PeerConfig {
|
|
54
|
+
/** The name of the peer: a lowercase letter, then lowercase letters, digits and hyphens. */
|
|
55
|
+
readonly name: string;
|
|
56
|
+
/** The base URL of the procedures of the peer, or the path or `file:` URL of its discovery file. */
|
|
57
|
+
readonly source: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
47
61
|
/**
|
|
48
62
|
* Load configuration from environment variables.
|
|
49
63
|
*
|
|
50
64
|
* Environment variables:
|
|
51
|
-
* - MCP_BUNDLES: Comma-separated list of bundles (default: "@procedures/bundle-mcp")
|
|
65
|
+
* - MCP_BUNDLES: Comma-separated list of bundles and module files (default: "@procedures/bundle-mcp")
|
|
52
66
|
* - MCP_SERVER_NAME: Server name (default: "impl-mcp-dev")
|
|
53
67
|
* - MCP_SERVER_VERSION: Server version (default: "1.0.0")
|
|
54
68
|
* - MCP_DEBUG: Enable debug logging (default: "false")
|
|
@@ -59,6 +73,7 @@ export interface McpServerConfig {
|
|
|
59
73
|
* - MCP_PROGRAM_CHECK: "false" or "0" starts a program without the check before the run
|
|
60
74
|
* - MCP_FS_ROOT: the root of the files: each tool keeps its paths in it, or it is refused (default: no root)
|
|
61
75
|
* - MCP_LISTING: "search" lists the three search tools in place of each tool (default: "each")
|
|
76
|
+
* - MCP_PEERS: the running peers, as `name=source` entries with commas (default: no peer)
|
|
62
77
|
*/
|
|
63
78
|
/** The default time limit of a tool call: a build or a test run can take minutes. */
|
|
64
79
|
export const DEFAULT_TOOL_TIMEOUT_MS = 600_000;
|
|
@@ -100,11 +115,35 @@ export function parseHttpToken(value: string | undefined): string | undefined {
|
|
|
100
115
|
return value;
|
|
101
116
|
}
|
|
102
117
|
|
|
118
|
+
const PEER_NAME = /^[a-z][a-z0-9-]*$/;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* This function reads `MCP_PEERS`. The value has entries `name=source`, with commas between them. A
|
|
122
|
+
* source is an `http://` or `https://` URL, or a discovery file. An entry that is not of this form is
|
|
123
|
+
* an error. Without this check, a mistake removes the tools of a peer with no message.
|
|
124
|
+
*/
|
|
125
|
+
export function parsePeers(value: string | undefined): PeerConfig[] {
|
|
126
|
+
const peers: PeerConfig[] = [];
|
|
127
|
+
if (value === undefined) return peers;
|
|
128
|
+
for (const entry of value.split(",").map((part) => part.trim()).filter(Boolean)) {
|
|
129
|
+
const eq = entry.indexOf("=");
|
|
130
|
+
const name = eq === -1 ? "" : entry.slice(0, eq).trim();
|
|
131
|
+
const source = eq === -1 ? "" : entry.slice(eq + 1).trim();
|
|
132
|
+
if (!PEER_NAME.test(name) || source === "") {
|
|
133
|
+
throw new Error(`MCP_PEERS: the entry "${entry}" is not name=source, with a lowercase name and a URL or a discovery file`);
|
|
134
|
+
}
|
|
135
|
+
if (peers.some((peer) => peer.name === name)) throw new Error(`MCP_PEERS: two entries have the name ${name}`);
|
|
136
|
+
peers.push({ name, source });
|
|
137
|
+
}
|
|
138
|
+
return peers;
|
|
139
|
+
}
|
|
140
|
+
|
|
103
141
|
export function loadConfig(): McpServerConfig {
|
|
104
142
|
const bundlesEnv = process.env["MCP_BUNDLES"] ?? "@procedures/bundle-mcp";
|
|
105
143
|
const bundles = bundlesEnv.split(",").map((b) => b.trim()).filter(Boolean);
|
|
106
144
|
const fsRoot = process.env["MCP_FS_ROOT"]?.trim();
|
|
107
145
|
const listing = parseListing(process.env["MCP_LISTING"]);
|
|
146
|
+
const peers = parsePeers(process.env["MCP_PEERS"]);
|
|
108
147
|
const maxSessions = parseMaxSessions(process.env["MCP_HTTP_MAX_SESSIONS"]);
|
|
109
148
|
const httpToken = process.env["MCP_HTTP_PORT"] ? parseHttpToken(process.env["MCP_HTTP_TOKEN"]) : undefined;
|
|
110
149
|
|
|
@@ -118,6 +157,7 @@ export function loadConfig(): McpServerConfig {
|
|
|
118
157
|
programCheck: !(process.env["MCP_PROGRAM_CHECK"] === "false" || process.env["MCP_PROGRAM_CHECK"] === "0"),
|
|
119
158
|
...(fsRoot ? { fsRoot } : {}),
|
|
120
159
|
...(listing ? { listing } : {}),
|
|
160
|
+
...(peers.length > 0 ? { peers } : {}),
|
|
121
161
|
...(process.env["MCP_HTTP_PORT"]
|
|
122
162
|
? {
|
|
123
163
|
http: {
|
package/src/host.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
ProcedureServer,
|
|
23
23
|
createRegistry,
|
|
24
24
|
registerClientProcedures,
|
|
25
|
+
registerModules,
|
|
25
26
|
type HostPolicy,
|
|
26
27
|
type ProcedureModule,
|
|
27
28
|
type ProcedureRegistry,
|
|
@@ -36,10 +37,14 @@ import {
|
|
|
36
37
|
type McpHostFaces,
|
|
37
38
|
} from "@procedures/mcp-server";
|
|
38
39
|
import { programCheckInterceptor } from "@procedures/compiler";
|
|
40
|
+
import { errorMessage } from "@mark1russell7/text";
|
|
39
41
|
import { createServer } from "node:http";
|
|
42
|
+
import { isAbsolute, resolve } from "node:path";
|
|
43
|
+
import { pathToFileURL } from "node:url";
|
|
40
44
|
import { ephemeral, localDev, service, type Lifecycle, type Profile, type RunOptions, type ServiceBuilder } from "@mark1russell7/service";
|
|
41
45
|
import { httpTransport, type HttpKnobs, type HttpTransport } from "@mark1russell7/service/http";
|
|
42
46
|
import { loadConfig, type McpServerConfig } from "./config.js";
|
|
47
|
+
import { loadPeers, type PeerState } from "./peers.js";
|
|
43
48
|
import { ms, seconds } from "@procedures/client";
|
|
44
49
|
|
|
45
50
|
/** The exports of a bundle's `module` entry: its module and the lists of its tools. */
|
|
@@ -63,20 +68,67 @@ export interface LoadedBundles {
|
|
|
63
68
|
global: boolean;
|
|
64
69
|
}
|
|
65
70
|
|
|
66
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* This function tells if an entry of `MCP_BUNDLES` names a module file. A module file is a `file:`
|
|
73
|
+
* URL, an absolute path, or a path that starts with `./` or `../`. Each other entry names a bundle package.
|
|
74
|
+
*/
|
|
75
|
+
export function isModuleFile(bundle: string): boolean {
|
|
76
|
+
return bundle.startsWith("file:") || isAbsolute(bundle) || /^\.\.?[\\/]/.test(bundle);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** This function gives the URL of a module file. A relative path starts from `cwd`. */
|
|
80
|
+
export function moduleFileUrl(bundle: string, cwd: string = process.cwd()): string {
|
|
81
|
+
return bundle.startsWith("file:") ? bundle : pathToFileURL(resolve(cwd, bundle)).href;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The first segment of each path of the own procedures of a module, not of its `requires`. */
|
|
85
|
+
function namespacesOf(module: ProcedureModule): string[] {
|
|
86
|
+
return [...new Set(module.procedures.map((procedure) => procedure.path[0]).filter((name): name is string => name !== undefined))].sort();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* This function loads a module file of `MCP_BUNDLES`: a file outside this package, for example
|
|
91
|
+
* the module of the tools of another repository. Its default export is the module. Its
|
|
92
|
+
* `mcpNamespaces` give its tools. Without them, its tools are the namespaces of its own
|
|
93
|
+
* procedures, so the modules in its `requires` add no tool. A file that does not load stops the
|
|
94
|
+
* host: the global registry cannot serve a file.
|
|
95
|
+
*/
|
|
96
|
+
export async function loadModuleFile(bundle: string, cwd: string = process.cwd()): Promise<BundleModuleExports & { default: ProcedureModule }> {
|
|
97
|
+
let exports: BundleModuleExports;
|
|
98
|
+
try {
|
|
99
|
+
exports = (await import(moduleFileUrl(bundle, cwd))) as BundleModuleExports;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
throw new Error(`MCP_BUNDLES: the module file ${bundle} did not load: ${errorMessage(error)}`, { cause: error });
|
|
102
|
+
}
|
|
103
|
+
const module = exports.default;
|
|
104
|
+
if (typeof module !== "object" || module === null || !Array.isArray(module.procedures)) {
|
|
105
|
+
throw new Error(`MCP_BUNDLES: the module file ${bundle} has no default export with a list of procedures`);
|
|
106
|
+
}
|
|
107
|
+
return { ...exports, default: module, mcpNamespaces: exports.mcpNamespaces ?? namespacesOf(module) };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** This function loads the module entry of each bundle and each module file (it registers nothing). */
|
|
67
111
|
export async function loadBundles(bundles: readonly string[]): Promise<LoadedBundles> {
|
|
68
112
|
const modules: ProcedureModule[] = [];
|
|
69
113
|
const hostModules: ProcedureModule[] = [];
|
|
70
114
|
const surfaces: BundleSurfaceModule[] = [];
|
|
115
|
+
const files = bundles.filter(isModuleFile);
|
|
71
116
|
for (const bundle of bundles) {
|
|
117
|
+
if (isModuleFile(bundle)) {
|
|
118
|
+
const exports = await loadModuleFile(bundle);
|
|
119
|
+
modules.push(exports.default);
|
|
120
|
+
hostModules.push(...(exports.mcpHostModules ?? []));
|
|
121
|
+
surfaces.push(exports);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
72
124
|
let exports: BundleModuleExports;
|
|
73
125
|
try {
|
|
74
126
|
// The import resolves from this package, which depends on its bundles
|
|
75
127
|
exports = (await import(`${bundle}/module`)) as BundleModuleExports;
|
|
76
128
|
} catch {
|
|
77
|
-
return loadBundlesGlobally(bundles);
|
|
129
|
+
return loadBundlesGlobally(bundles, files);
|
|
78
130
|
}
|
|
79
|
-
if (!exports.default) return loadBundlesGlobally(bundles);
|
|
131
|
+
if (!exports.default) return loadBundlesGlobally(bundles, files);
|
|
80
132
|
modules.push(exports.default);
|
|
81
133
|
hostModules.push(...(exports.mcpHostModules ?? []));
|
|
82
134
|
surfaces.push(exports);
|
|
@@ -87,7 +139,10 @@ export async function loadBundles(bundles: readonly string[]): Promise<LoadedBun
|
|
|
87
139
|
}
|
|
88
140
|
|
|
89
141
|
/** The old way, for a bundle without a module entry: import each `register.js`. */
|
|
90
|
-
async function loadBundlesGlobally(bundles: readonly string[]): Promise<LoadedBundles> {
|
|
142
|
+
async function loadBundlesGlobally(bundles: readonly string[], files: readonly string[]): Promise<LoadedBundles> {
|
|
143
|
+
if (files.length > 0) {
|
|
144
|
+
throw new Error(`MCP_BUNDLES: the module file ${files[0]} needs the module entry of each bundle, and a bundle has none`);
|
|
145
|
+
}
|
|
91
146
|
// The global registry gets the control flow only from this call (ARCHITECTURE-PROPOSALS-2026-10, P9)
|
|
92
147
|
registerClientProcedures();
|
|
93
148
|
const surfaces: BundleSurfaceModule[] = [];
|
|
@@ -109,6 +164,8 @@ export interface DevToolsHost {
|
|
|
109
164
|
refusedUnderRoot?: string[] | undefined;
|
|
110
165
|
/** The MCP host: `mcp.start()` starts its face (stdio, or Streamable HTTP with `config.http`). */
|
|
111
166
|
mcp: McpHost;
|
|
167
|
+
/** The state of each peer of `config.peers`: its count of forwarded procedures, or its error. */
|
|
168
|
+
peers: PeerState[];
|
|
112
169
|
}
|
|
113
170
|
|
|
114
171
|
/**
|
|
@@ -126,7 +183,10 @@ function facesOf(config: McpServerConfig): McpHostFaces {
|
|
|
126
183
|
/** This function builds the host. It starts no transport. */
|
|
127
184
|
export async function createDevToolsHost(config: McpServerConfig = loadConfig()): Promise<DevToolsHost> {
|
|
128
185
|
const loaded = await loadBundles(config.bundles);
|
|
129
|
-
|
|
186
|
+
// MCP_PEERS (d40-tool-surface, step 2): a stub for each procedure of each running peer. A path of a
|
|
187
|
+
// peer that the host already has stops the start, with the error of the registry.
|
|
188
|
+
const peers = await loadPeers(config.peers ?? []);
|
|
189
|
+
const registry = loaded.global ? registerModules(PROCEDURE_REGISTRY, peers.modules) : createRegistry(...loaded.modules, ...peers.modules);
|
|
130
190
|
// MCP_FS_ROOT (roadmap R33): each procedure of the registry keeps its paths in the root, also
|
|
131
191
|
// in the nested calls, or the root refuses it. It adds no procedure: the tools stay the same.
|
|
132
192
|
let fsRoot: string | undefined;
|
|
@@ -135,8 +195,11 @@ export async function createDevToolsHost(config: McpServerConfig = loadConfig())
|
|
|
135
195
|
const { confineToRoot } = await import("@procedures/fs/module");
|
|
136
196
|
({ root: fsRoot, refused: refusedUnderRoot } = confineToRoot(registry, config.fsRoot));
|
|
137
197
|
}
|
|
138
|
-
// Bundles without mcpNamespaces say nothing about their tools: then every procedure is a tool
|
|
139
|
-
|
|
198
|
+
// Bundles without mcpNamespaces say nothing about their tools: then every procedure is a tool,
|
|
199
|
+
// also each stub of a peer. Else the namespaces of each peer add to the tools of the bundles.
|
|
200
|
+
const bundleSurface = mergeSurfaces(loaded.surfaces);
|
|
201
|
+
const peerSurfaces = peers.modules.map((module) => ({ mcpNamespaces: namespacesOf(module) }));
|
|
202
|
+
const surface = bundleSurface && peerSurfaces.length > 0 ? mergeSurfaces([...loaded.surfaces, ...peerSurfaces]) : bundleSurface;
|
|
140
203
|
// Read only: the tools are the procedures that the capability permits, so the list shows only
|
|
141
204
|
// what a call can start (CAPABILITIES-2026-10). The check before the run (roadmap R22): a
|
|
142
205
|
// program with an error does not start, so a refusal in step 3 comes before the write of step 1.
|
|
@@ -159,6 +222,7 @@ export async function createDevToolsHost(config: McpServerConfig = loadConfig())
|
|
|
159
222
|
policy: mcp.policy,
|
|
160
223
|
server: mcp.server,
|
|
161
224
|
mcp,
|
|
225
|
+
peers: peers.states,
|
|
162
226
|
...(fsRoot !== undefined ? { fsRoot, refusedUnderRoot } : {}),
|
|
163
227
|
};
|
|
164
228
|
}
|
|
@@ -217,6 +281,11 @@ export async function runDevToolsServer(config: McpServerConfig = loadConfig(),
|
|
|
217
281
|
const life = await svc.run(run);
|
|
218
282
|
const face = host.mcp.state().faces[0];
|
|
219
283
|
if (config.debug) console.error(`[mcp-server] Started with ${face?.toolCount ?? 0} tools`);
|
|
284
|
+
// A peer that did not answer gives no tools: the line tells why, so the gap is not silent
|
|
285
|
+
for (const peer of host.peers) {
|
|
286
|
+
if ("error" in peer) console.error(`[mcp-server] The peer ${peer.name} (${peer.source}) gives no tools until the next start: ${peer.error}`);
|
|
287
|
+
else if (config.debug) console.error(`[mcp-server] The peer ${peer.name} gives ${peer.procedures} procedures`);
|
|
288
|
+
}
|
|
220
289
|
if (host.fsRoot !== undefined) console.error(`[mcp-server] Each tool refuses a path outside ${host.fsRoot}. Refused under the root: ${String(host.refusedUnderRoot)}`);
|
|
221
290
|
let url: string | undefined;
|
|
222
291
|
const bound = listener?.server.address();
|
package/src/index.ts
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* transport.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
export { loadConfig, type McpServerConfig } from "./config.js";
|
|
10
|
+
export { loadConfig, parsePeers, type McpServerConfig, type PeerConfig } from "./config.js";
|
|
11
|
+
export { DEFAULT_PEER_TIMEOUT_MS, loadPeers, peerAddress, readDiscovery, type LoadPeersOptions, type LoadedPeers, type PeerState } from "./peers.js";
|
|
11
12
|
export {
|
|
12
13
|
createDevToolsHost,
|
|
13
14
|
runDevToolsServer,
|
package/src/peers.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The running peers of `MCP_PEERS` (d40-tool-surface, step 2).
|
|
3
|
+
*
|
|
4
|
+
* A peer with a state, for example a daemon, keeps its procedures in its own process. The host does
|
|
5
|
+
* not load a second copy of them. It reads the manifest of each peer with `peerModule` of
|
|
6
|
+
* `@procedures/client`, and each tool of the peer sends its call to the peer. The peer examines the
|
|
7
|
+
* input with its own schemas and applies its own policy.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readFile } from "node:fs/promises";
|
|
11
|
+
import { resolve } from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { peerModule, type PeerAddress, type ProcedureModule } from "@procedures/client";
|
|
14
|
+
import { errorMessage } from "@mark1russell7/text";
|
|
15
|
+
import type { PeerConfig } from "./config.js";
|
|
16
|
+
|
|
17
|
+
/** The time limit of the read of the manifest of a peer at the start of the host. */
|
|
18
|
+
export const DEFAULT_PEER_TIMEOUT_MS = 5_000;
|
|
19
|
+
|
|
20
|
+
/** The result of one peer at the start: its count of forwarded procedures, or the reason that it gives none. */
|
|
21
|
+
export type PeerState =
|
|
22
|
+
| { readonly name: string; readonly source: string; readonly procedures: number }
|
|
23
|
+
| { readonly name: string; readonly source: string; readonly error: string };
|
|
24
|
+
|
|
25
|
+
/** The modules and the states of the peers. */
|
|
26
|
+
export interface LoadedPeers {
|
|
27
|
+
readonly modules: ProcedureModule[];
|
|
28
|
+
readonly states: PeerState[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The options of `loadPeers`. */
|
|
32
|
+
export interface LoadPeersOptions {
|
|
33
|
+
/** The time limit of the read of each manifest, in milliseconds. The default is `DEFAULT_PEER_TIMEOUT_MS`. */
|
|
34
|
+
readonly timeoutMs?: number | undefined;
|
|
35
|
+
/** The fetch function of the requests. The default is the global `fetch`. */
|
|
36
|
+
readonly fetch?: typeof fetch | undefined;
|
|
37
|
+
/** The folder of a relative path of a discovery file. The default is the folder of the process. */
|
|
38
|
+
readonly cwd?: string | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** This function tells if a text is an HTTP or HTTPS URL. */
|
|
42
|
+
function isHttpUrl(text: string): boolean {
|
|
43
|
+
return /^https?:\/\//i.test(text);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* This function reads a discovery file. The file is a JSON object with the base URL of the procedures
|
|
48
|
+
* in `url` or `endpoint`, and an optional bearer token in `token`. A running peer writes the file. It
|
|
49
|
+
* writes a new port and a new token when it starts again.
|
|
50
|
+
*/
|
|
51
|
+
export async function readDiscovery(file: string): Promise<PeerAddress> {
|
|
52
|
+
let text: string;
|
|
53
|
+
try {
|
|
54
|
+
text = await readFile(file, "utf8");
|
|
55
|
+
} catch (error) {
|
|
56
|
+
throw new Error(`the discovery file ${file} does not open (is the peer running?): ${errorMessage(error)}`, { cause: error });
|
|
57
|
+
}
|
|
58
|
+
let value: unknown;
|
|
59
|
+
try {
|
|
60
|
+
value = JSON.parse(text);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
throw new Error(`the discovery file ${file} is not JSON`, { cause: error });
|
|
63
|
+
}
|
|
64
|
+
const record = (typeof value === "object" && value !== null ? value : {}) as { url?: unknown; endpoint?: unknown; token?: unknown };
|
|
65
|
+
const url = typeof record.url === "string" ? record.url : record.endpoint;
|
|
66
|
+
if (typeof url !== "string" || !isHttpUrl(url)) throw new Error(`the discovery file ${file} has no HTTP URL in "url" or "endpoint"`);
|
|
67
|
+
if (record.token !== undefined && typeof record.token !== "string") throw new Error(`the token of the discovery file ${file} is not a text`);
|
|
68
|
+
return { url, ...(typeof record.token === "string" ? { token: record.token } : {}) };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* This function gives the address of a peer. A URL is the address. For a discovery file, it gives a
|
|
73
|
+
* function that reads the file at each call, so the calls follow the peer after it starts again.
|
|
74
|
+
*/
|
|
75
|
+
export function peerAddress(source: string, cwd: string = process.cwd()): PeerAddress | (() => Promise<PeerAddress>) {
|
|
76
|
+
if (isHttpUrl(source)) return { url: source };
|
|
77
|
+
const file = source.startsWith("file:") ? fileURLToPath(source) : resolve(cwd, source);
|
|
78
|
+
return () => readDiscovery(file);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* This function reads the manifest of each peer, all at one time. A peer that does not answer in
|
|
83
|
+
* the time limit gives no module, and its state tells why. Thus the other tools of the host stay available.
|
|
84
|
+
*/
|
|
85
|
+
export async function loadPeers(peers: readonly PeerConfig[], options: LoadPeersOptions = {}): Promise<LoadedPeers> {
|
|
86
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_PEER_TIMEOUT_MS;
|
|
87
|
+
const results = await Promise.all(
|
|
88
|
+
peers.map(async (peer) => {
|
|
89
|
+
try {
|
|
90
|
+
const module = await peerModule({
|
|
91
|
+
name: `peer:${peer.name}`,
|
|
92
|
+
address: peerAddress(peer.source, options.cwd),
|
|
93
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
94
|
+
...(options.fetch !== undefined ? { fetch: options.fetch } : {}),
|
|
95
|
+
});
|
|
96
|
+
return { peer, module };
|
|
97
|
+
} catch (error) {
|
|
98
|
+
return { peer, error: errorMessage(error) };
|
|
99
|
+
}
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
const loaded: LoadedPeers = { modules: [], states: [] };
|
|
103
|
+
for (const result of results) {
|
|
104
|
+
if (result.module === undefined) {
|
|
105
|
+
loaded.states.push({ name: result.peer.name, source: result.peer.source, error: result.error ?? "no module" });
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
loaded.modules.push(result.module);
|
|
109
|
+
loaded.states.push({ name: result.peer.name, source: result.peer.source, procedures: result.module.procedures.length });
|
|
110
|
+
}
|
|
111
|
+
return loaded;
|
|
112
|
+
}
|