withruntime 1.1.1 → 1.1.2
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/dist/v2/login.d.ts +2 -0
- package/dist/v2/login.js +1 -0
- package/dist/v2/mcp.d.ts +3 -0
- package/dist/v2/mcp.js +132 -28
- package/dist/v2/openapi.json +399 -399
- package/package.json +1 -1
package/dist/v2/login.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ export type LoginOptions = {
|
|
|
25
25
|
sleep?: (ms: number) => Promise<void>;
|
|
26
26
|
now?: () => number;
|
|
27
27
|
say?: (line: string) => void;
|
|
28
|
+
/** Told the link and code once the API has given them, before the wait for the approval. */
|
|
29
|
+
started?: (link: string, userCode: string) => void;
|
|
28
30
|
};
|
|
29
31
|
export declare function login(baseUrl: string, o?: LoginOptions): Promise<number>;
|
|
30
32
|
/** Forgets the key a login saved for `baseUrl`. The key itself works until
|
package/dist/v2/login.js
CHANGED
|
@@ -141,6 +141,7 @@ export async function login(baseUrl, o = {}) {
|
|
|
141
141
|
const link = started.verification_uri_complete ?? started.verification_uri;
|
|
142
142
|
say(`Approve this computer at ${link}`);
|
|
143
143
|
say(`and check that the page shows the code ${started.user_code}.`);
|
|
144
|
+
o.started?.(link, started.user_code);
|
|
144
145
|
const page = sitePage(link, baseUrl);
|
|
145
146
|
if (page)
|
|
146
147
|
(o.open ?? openBrowser)(page);
|
package/dist/v2/mcp.d.ts
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
+
import { type LoginOptions } from "./login.js";
|
|
2
|
+
/** The bridge: `handle` takes one line a client sent, and each answer goes to `write`. */
|
|
3
|
+
export declare function bridge(baseUrl: string, write: (m: unknown) => void, fetcher?: typeof fetch, connect?: LoginOptions): (line: string) => Promise<void>;
|
|
1
4
|
export declare function serveMcp(baseUrl: string, fetcher?: typeof fetch): Promise<number>;
|
package/dist/v2/mcp.js
CHANGED
|
@@ -3,60 +3,164 @@
|
|
|
3
3
|
to the API's own MCP endpoint (POST /mcp, which serves every verb and
|
|
4
4
|
shortcut the spec names) under RUNTIME_API_KEY, and its answer written
|
|
5
5
|
back as one line: the tools are always the server's, never this
|
|
6
|
-
package's, so a verb is a tool the moment it is live.
|
|
6
|
+
package's, so a verb is a tool the moment it is live.
|
|
7
|
+
|
|
8
|
+
With no key (none set, none saved by `runtime login`), the bridge answers
|
|
9
|
+
itself with one tool, runtime_connect, which signs this computer in as
|
|
10
|
+
`runtime login` does, from inside the agent; once approved, the server's
|
|
11
|
+
tools are listed. 24 Nov 2026: without a key every message was refused,
|
|
12
|
+
`initialize` too, so a client added before `npx withruntime login` showed
|
|
13
|
+
the server as failed and nothing said what to do. */
|
|
7
14
|
import { createInterface } from "node:readline";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
import { login, savedKey } from "./login.js";
|
|
16
|
+
const CONNECT = "runtime_connect";
|
|
17
|
+
const NOT_CONNECTED = "Runtime is not connected on this computer. Call runtime_connect to sign in: it returns a link and a code for the person you work for to approve in a browser, and Runtime's tools are listed once they have. With RUNTIME_API_KEY set, or after `npx withruntime login`, the server starts connected.";
|
|
18
|
+
const connectTool = {
|
|
19
|
+
name: CONNECT,
|
|
20
|
+
title: "Connect this computer to Runtime",
|
|
21
|
+
description: "Signs this computer in to Runtime with a browser approval, with no key to copy. The first call returns a link and a code: show both to the person you work for and ask them to approve. Call it again after they have; it waits up to 45 seconds for the approval, then Runtime's tools are listed.",
|
|
22
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
23
|
+
};
|
|
24
|
+
/** How long a call of runtime_connect after the first waits for the approval. */
|
|
25
|
+
const WAIT_MS = 45_000;
|
|
26
|
+
/** The bridge: `handle` takes one line a client sent, and each answer goes to `write`. */
|
|
27
|
+
export function bridge(baseUrl, write, fetcher = fetch, connect = {}) {
|
|
28
|
+
const env = connect.env ?? process.env;
|
|
29
|
+
let key = env["RUNTIME_API_KEY"] || undefined;
|
|
30
|
+
let pending;
|
|
31
|
+
/** Starts a sign-in, resolved with its link and code once the API gave them; its `done` is the error, or "". */
|
|
32
|
+
const begin = () => new Promise((started, failed) => {
|
|
33
|
+
const said = [];
|
|
34
|
+
const done = login(baseUrl, {
|
|
35
|
+
fetch: fetcher,
|
|
36
|
+
...connect,
|
|
37
|
+
env,
|
|
38
|
+
tty: () => false,
|
|
39
|
+
say: (l) => void (said.push(l), process.stderr.write(`${l}\n`)),
|
|
40
|
+
started: (link, code) => started({ link, code, done }),
|
|
41
|
+
}).then((code) => {
|
|
42
|
+
const saved = code === 0 ? savedKey(baseUrl, env) : undefined;
|
|
43
|
+
if (saved) {
|
|
44
|
+
key = saved;
|
|
45
|
+
write({ jsonrpc: "2.0", method: "notifications/tools/list_changed" });
|
|
46
|
+
return "";
|
|
47
|
+
}
|
|
48
|
+
pending = undefined;
|
|
49
|
+
const why = said.at(-1)?.replace(/^runtime login: /, "") ?? "the sign-in ended.";
|
|
50
|
+
failed(new Error(why));
|
|
51
|
+
return why;
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
const text = (t, isError = false) => ({ content: [{ type: "text", text: t }], ...(isError ? { isError } : {}) });
|
|
55
|
+
const ask = (p) => `Open ${p.link} and check that the page shows the code ${p.code}, then choose Connect. Show the link and code to the person you work for. When they have approved, call runtime_connect again.`;
|
|
56
|
+
async function connectCall() {
|
|
57
|
+
if (key)
|
|
58
|
+
return text("Connected. Runtime's tools are listed.");
|
|
59
|
+
if (!pending) {
|
|
60
|
+
try {
|
|
61
|
+
pending = await begin();
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
return text(`The sign-in could not start: ${e.message}`, true);
|
|
65
|
+
}
|
|
66
|
+
return text(ask(pending));
|
|
67
|
+
}
|
|
68
|
+
const p = pending;
|
|
69
|
+
const why = await Promise.race([p.done, new Promise((r) => setTimeout(() => r(null), WAIT_MS).unref())]);
|
|
70
|
+
if (why === "")
|
|
71
|
+
return text("Connected. Runtime's tools are listed.");
|
|
72
|
+
if (why === null)
|
|
73
|
+
return text(`Not approved yet. ${ask(p)}`);
|
|
74
|
+
return text(`The sign-in ended: ${why} Call runtime_connect for a new link.`, true);
|
|
75
|
+
}
|
|
76
|
+
/** The answer to `m` while no key is held. */
|
|
77
|
+
async function unconnected(m) {
|
|
78
|
+
switch (m.method) {
|
|
79
|
+
case "initialize":
|
|
80
|
+
return {
|
|
81
|
+
protocolVersion: typeof m.params?.protocolVersion === "string" ? m.params.protocolVersion : "2025-06-18",
|
|
82
|
+
capabilities: { tools: { listChanged: true } },
|
|
83
|
+
serverInfo: { name: "runtime-cloud", version: "not connected" },
|
|
84
|
+
instructions: NOT_CONNECTED,
|
|
85
|
+
};
|
|
86
|
+
case "ping":
|
|
87
|
+
return {};
|
|
88
|
+
case "tools/list":
|
|
89
|
+
return { tools: [connectTool] };
|
|
90
|
+
case "tools/call":
|
|
91
|
+
return m.params?.name === CONNECT ? connectCall() : text(NOT_CONNECTED, true);
|
|
92
|
+
default:
|
|
93
|
+
throw new Error(NOT_CONNECTED);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return async (line) => {
|
|
14
97
|
if (!line.trim())
|
|
15
|
-
|
|
16
|
-
let
|
|
98
|
+
return;
|
|
99
|
+
let m;
|
|
17
100
|
try {
|
|
18
|
-
|
|
101
|
+
m = JSON.parse(line);
|
|
19
102
|
}
|
|
20
103
|
catch {
|
|
21
104
|
write({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "The line is not JSON." } });
|
|
22
|
-
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const id = m.id ?? null;
|
|
108
|
+
if (!key || (m.method === "tools/call" && m.params?.name === CONNECT)) {
|
|
109
|
+
if (m.id === undefined)
|
|
110
|
+
return; // a notification
|
|
111
|
+
try {
|
|
112
|
+
write({ jsonrpc: "2.0", id, result: await (key ? connectCall() : unconnected(m)) });
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
write({ jsonrpc: "2.0", id, error: { code: -32001, message: e.message } });
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
23
118
|
}
|
|
24
119
|
try {
|
|
25
|
-
const res = await fetcher(
|
|
120
|
+
const res = await fetcher(`${baseUrl}/mcp`, {
|
|
26
121
|
method: "POST",
|
|
27
122
|
headers: {
|
|
28
123
|
"content-type": "application/json",
|
|
29
124
|
accept: "application/json, text/event-stream",
|
|
30
|
-
|
|
125
|
+
authorization: `Bearer ${key}`,
|
|
31
126
|
},
|
|
32
127
|
body: line,
|
|
33
128
|
});
|
|
34
129
|
if (res.status === 202)
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
let
|
|
130
|
+
return;
|
|
131
|
+
const body = await res.text();
|
|
132
|
+
let parsed;
|
|
38
133
|
try {
|
|
39
|
-
|
|
134
|
+
parsed = JSON.parse(body);
|
|
40
135
|
}
|
|
41
136
|
catch {
|
|
42
|
-
write({ jsonrpc: "2.0", id, error: { code: -32603, message: `The API answered ${res.status}: ${
|
|
43
|
-
|
|
137
|
+
write({ jsonrpc: "2.0", id, error: { code: -32603, message: `The API answered ${res.status}: ${body.slice(0, 200)}` } });
|
|
138
|
+
return;
|
|
44
139
|
}
|
|
45
140
|
if (res.ok) {
|
|
46
|
-
write(
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
// A refusal of the call itself (
|
|
50
|
-
//
|
|
51
|
-
const api =
|
|
52
|
-
const message =
|
|
53
|
-
? "No API key. Set RUNTIME_API_KEY, or run `npx withruntime login` once on this machine."
|
|
54
|
-
: `${api?.code ?? res.status}: ${api?.message ?? text.slice(0, 200)}${api?.hint ? ` ${api.hint}` : ""}`;
|
|
141
|
+
write(parsed);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// A refusal of the call itself (a bad key, a limit) is the API's error body, not a JSON-RPC answer: the client
|
|
145
|
+
// is told it as this request's error, or it waits for an answer that never comes.
|
|
146
|
+
const api = parsed.error;
|
|
147
|
+
const message = `${api?.code ?? res.status}: ${api?.message ?? body.slice(0, 200)}${api?.hint ? ` ${api.hint}` : ""}`;
|
|
55
148
|
write({ jsonrpc: "2.0", id, error: { code: -32001, message, data: { status: res.status, ...api } } });
|
|
56
149
|
}
|
|
57
150
|
catch (e) {
|
|
58
151
|
write({ jsonrpc: "2.0", id, error: { code: -32603, message: `The API did not answer: ${e.message}` } });
|
|
59
152
|
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
export async function serveMcp(baseUrl, fetcher = fetch) {
|
|
156
|
+
const handle = bridge(baseUrl, (m) => process.stdout.write(`${JSON.stringify(m)}\n`), fetcher);
|
|
157
|
+
const lines = createInterface({ input: process.stdin, crlfDelay: Infinity });
|
|
158
|
+
// Each line at once: a runtime_connect that waits for an approval holds up no other call.
|
|
159
|
+
const running = new Set();
|
|
160
|
+
for await (const line of lines) {
|
|
161
|
+
const p = handle(line).finally(() => running.delete(p));
|
|
162
|
+
running.add(p);
|
|
60
163
|
}
|
|
164
|
+
await Promise.all(running);
|
|
61
165
|
return 0;
|
|
62
166
|
}
|