@biolibs/bionet-agent 0.0.0-stage → 0.1.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 +101 -2
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/serve.d.ts +54 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +175 -0
- package/dist/serve.js.map +1 -0
- package/dist/tools.d.ts +115 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +451 -0
- package/dist/tools.js.map +1 -0
- package/package.json +44 -4
- package/src/index.ts +18 -0
- package/src/serve.ts +175 -0
- package/src/tools.ts +494 -0
package/README.md
CHANGED
|
@@ -1,3 +1,102 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @biolibs/bionet-agent
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lab's adviser: what an agent with no hands can read of a [Bionet](../bionet) lab, work out with
|
|
4
|
+
the lab's own tools, and propose for a person to carry out — served to it over the
|
|
5
|
+
[Model Context Protocol](https://modelcontextprotocol.io) from the device that hosts it.
|
|
6
|
+
|
|
7
|
+
An agent is good at seeing the whole of a cloning plan at once and bad at holding a pipette. So it
|
|
8
|
+
is let into a lab as what it is: a member that can propose and cannot do. It designs the primers,
|
|
9
|
+
works out what the PCR would make with the same engine the lab's windows use, and proposes the run.
|
|
10
|
+
A person finds the proposal at the thermocycler with its reasons, sets the bench up from it, reads
|
|
11
|
+
the working their own tools give, and does it or does not. What really happened is theirs to record.
|
|
12
|
+
|
|
13
|
+
## What keeps it to that
|
|
14
|
+
|
|
15
|
+
- **Its key has the role `adviser`.** In `@biolibs/bionet`, an adviser may add a part, propose a
|
|
16
|
+
run, and set aside a proposal of its own. Anything else it signs is refused where the logs are
|
|
17
|
+
merged, on every device and on the lab's peer. This is not something this package enforces; it is
|
|
18
|
+
something this package cannot get round.
|
|
19
|
+
- **It has a key of its own.** The device that hosts it keeps a second key for it
|
|
20
|
+
(`hostAdviser` in `@biolibs/bionet-client`). What it writes is in its own log under its own name,
|
|
21
|
+
never the person's, and a keeper has to let that key into the lab before it can write at all.
|
|
22
|
+
- **There is no tool for the rest.** Nothing here records a run, uses a tube, moves one, or touches
|
|
23
|
+
the roster.
|
|
24
|
+
- **The door is shut by default.** `serveAgent` listens on `127.0.0.1` only, wants a bearer token on
|
|
25
|
+
every request, and refuses anything that says it comes from a web page or is addressed to another
|
|
26
|
+
name for this machine.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install @biolibs/bionet-agent
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Serving the tools
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { Lab, memoryKeys, memoryStore } from "@biolibs/bionet-client";
|
|
38
|
+
import { agentTools, newAgentToken, serveAgent } from "@biolibs/bionet-agent";
|
|
39
|
+
|
|
40
|
+
// A device that can host an adviser is one given somewhere to keep its key.
|
|
41
|
+
const lab = await new Lab({ store: memoryStore(), keys: memoryKeys(), adviserKeys: memoryKeys() }).open();
|
|
42
|
+
await lab.setPerson("Ada");
|
|
43
|
+
await lab.found({ name: "The Wren Lab" });
|
|
44
|
+
|
|
45
|
+
// Two decisions, both a person's: host one, and let it in.
|
|
46
|
+
const { key } = await lab.hostAdviser();
|
|
47
|
+
await lab.addMember({ key, person: "Claude", role: "adviser" });
|
|
48
|
+
|
|
49
|
+
const token = newAgentToken();
|
|
50
|
+
const server = await serveAgent({ tools: agentTools(lab), token });
|
|
51
|
+
server.url; // http://127.0.0.1:<port>/mcp
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Connect an agent to it with the URL and the token as a bearer token. For Claude Code:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
claude mcp add --transport http bionet http://127.0.0.1:<port>/mcp --header "Authorization: Bearer <token>"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The Bionet desktop app does all of this from its Adviser tab, keeps the token sealed by the
|
|
61
|
+
system's keychain, and puts that command on the clipboard without showing the token.
|
|
62
|
+
|
|
63
|
+
## The tools
|
|
64
|
+
|
|
65
|
+
| Tool | What it does |
|
|
66
|
+
|---|---|
|
|
67
|
+
| `lab_overview` | Which lab, whether the adviser is in it, what it holds, its instruments |
|
|
68
|
+
| `list_parts`, `get_part` | The parts, and one read closely: sequence, features, tubes |
|
|
69
|
+
| `list_tubes` | Every tube: what it is, how much is left, where it is |
|
|
70
|
+
| `list_runs` | What has been done at the bench, failures included |
|
|
71
|
+
| `list_proposals` | What has been proposed, by whom, and what became of it |
|
|
72
|
+
| `find_cutters` | Which enzymes cut a part, and where |
|
|
73
|
+
| `design_primers` | A pair for a stretch or a feature, with why each won and what to watch for |
|
|
74
|
+
| `work_out_pcr`, `work_out_digest`, `work_out_ligation` | What each would give, from the lab's own engines |
|
|
75
|
+
| `add_part` | Say a sequence is a part, such as a primer just designed. Adds no tube |
|
|
76
|
+
| `propose_pcr`, `propose_digest`, `propose_ligation` | Propose a run. Nothing is used or recorded as done |
|
|
77
|
+
| `withdraw_proposal` | Take back a proposal of its own |
|
|
78
|
+
|
|
79
|
+
The last five are the only ones that write. A proposal may count on a part there is no tube of yet;
|
|
80
|
+
it then says that the part has to be ordered or made first.
|
|
81
|
+
|
|
82
|
+
## Showing it at work
|
|
83
|
+
|
|
84
|
+
`agentTools(lab, { watch })` tells `watch` of every use, with where in the lab it would be seen:
|
|
85
|
+
a part being read, a digest being worked out at the gel station. It is shaped like a stop on the
|
|
86
|
+
demo's walk, which is how a bench is already told what to show.
|
|
87
|
+
|
|
88
|
+
## Things to know
|
|
89
|
+
|
|
90
|
+
- **What it reads is data.** Names, notes and features were typed by lab members and by other labs.
|
|
91
|
+
The instructions the server gives an agent say to treat them as data and never as instructions;
|
|
92
|
+
an agent you connect should be one that does.
|
|
93
|
+
- **Before it is let in** the adviser can read and work things out. Anything it tries to write
|
|
94
|
+
says that a keeper has to let it in.
|
|
95
|
+
- **If a keeper makes its key a member or a keeper**, the device stops writing with it. A hosted
|
|
96
|
+
key is for advising.
|
|
97
|
+
- **Every device should be on `@biolibs/bionet` 1.5 or later** before an adviser adds parts: an
|
|
98
|
+
older one does not see what an adviser writes.
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* `@biolibs/bionet-agent`: a lab's adviser.
|
|
4
|
+
*
|
|
5
|
+
* An agent can be let into a lab as a member with no hands. It reads what the lab holds, works
|
|
6
|
+
* things out with the lab's own tools, and proposes runs for people to carry out. It cannot say
|
|
7
|
+
* that anything was done, used or moved: its key has the role `adviser`, and every device that
|
|
8
|
+
* merges the lab's logs holds it to that.
|
|
9
|
+
*
|
|
10
|
+
* - `tools.ts` — what an adviser is given: the lab to read, the engines, and three things to write
|
|
11
|
+
* - `serve.ts` — those tools served over the Model Context Protocol, on this machine only
|
|
12
|
+
*
|
|
13
|
+
* The device that hosts the adviser keeps its key (`hostAdviser` in `@biolibs/bionet-client`), and
|
|
14
|
+
* a keeper lets that key into the lab. Until then the adviser can read and work things out, and
|
|
15
|
+
* anything it tries to write says what is missing.
|
|
16
|
+
*/
|
|
17
|
+
export * from "./tools.js";
|
|
18
|
+
export * from "./serve.js";
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* `@biolibs/bionet-agent`: a lab's adviser.
|
|
4
|
+
*
|
|
5
|
+
* An agent can be let into a lab as a member with no hands. It reads what the lab holds, works
|
|
6
|
+
* things out with the lab's own tools, and proposes runs for people to carry out. It cannot say
|
|
7
|
+
* that anything was done, used or moved: its key has the role `adviser`, and every device that
|
|
8
|
+
* merges the lab's logs holds it to that.
|
|
9
|
+
*
|
|
10
|
+
* - `tools.ts` — what an adviser is given: the lab to read, the engines, and three things to write
|
|
11
|
+
* - `serve.ts` — those tools served over the Model Context Protocol, on this machine only
|
|
12
|
+
*
|
|
13
|
+
* The device that hosts the adviser keeps its key (`hostAdviser` in `@biolibs/bionet-client`), and
|
|
14
|
+
* a keeper lets that key into the lab. Until then the adviser can read and work things out, and
|
|
15
|
+
* anything it tries to write says what is missing.
|
|
16
|
+
*/
|
|
17
|
+
export * from "./tools.js";
|
|
18
|
+
export * from "./serve.js";
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/dist/serve.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type AgentTool } from "./tools.js";
|
|
2
|
+
/** The versions of the protocol this speaks, newest first. */
|
|
3
|
+
export declare const PROTOCOL_VERSIONS: readonly ["2025-06-18", "2025-03-26", "2024-11-05"];
|
|
4
|
+
/** A server that is up. */
|
|
5
|
+
export interface AgentServer {
|
|
6
|
+
/** Where an agent connects: `http://127.0.0.1:<port>/mcp`. */
|
|
7
|
+
url: string;
|
|
8
|
+
/** The port it took. */
|
|
9
|
+
port: number;
|
|
10
|
+
/** Stop it. */
|
|
11
|
+
close: () => Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
/** @returns A new token: 32 random bytes, in base64url. Whoever has it can reach the adviser's tools. */
|
|
14
|
+
export declare const newAgentToken: () => string;
|
|
15
|
+
/** One JSON-RPC answer, or `null` for a message that wants none. */
|
|
16
|
+
type Answer = Record<string, unknown> | null;
|
|
17
|
+
/**
|
|
18
|
+
* Answer one message of the protocol.
|
|
19
|
+
*
|
|
20
|
+
* @param message - What arrived.
|
|
21
|
+
* @param tools - The tools on offer.
|
|
22
|
+
* @param about - What to call this server and its version.
|
|
23
|
+
* @returns The answer, or `null` if the message was a notification.
|
|
24
|
+
*/
|
|
25
|
+
export declare function answerAgent(message: unknown, tools: readonly AgentTool[], about: {
|
|
26
|
+
name: string;
|
|
27
|
+
version: string;
|
|
28
|
+
}): Promise<Answer>;
|
|
29
|
+
/**
|
|
30
|
+
* Serve the adviser's tools on this machine.
|
|
31
|
+
*
|
|
32
|
+
* @param options - `tools` are the tools; `token` is what every request has to carry, as a bearer
|
|
33
|
+
* token; `port` is the port to take, or 0 for any that is free; `name` and `version` are what the
|
|
34
|
+
* server calls itself.
|
|
35
|
+
* @returns Where it is listening, and a way to stop it.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { agentTools, newAgentToken, serveAgent } from "@biolibs/bionet-agent";
|
|
40
|
+
*
|
|
41
|
+
* const token = newAgentToken();
|
|
42
|
+
* const server = await serveAgent({ tools: agentTools(lab), token });
|
|
43
|
+
* server.url; // http://127.0.0.1:53124/mcp — give this and the token to the agent
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function serveAgent(options: {
|
|
47
|
+
tools: readonly AgentTool[];
|
|
48
|
+
token: string;
|
|
49
|
+
port?: number;
|
|
50
|
+
name?: string;
|
|
51
|
+
version?: string;
|
|
52
|
+
}): Promise<AgentServer>;
|
|
53
|
+
export {};
|
|
54
|
+
//# sourceMappingURL=serve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serve.d.ts","sourceRoot":"","sources":["../src/serve.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAsB,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAEhE,8DAA8D;AAC9D,eAAO,MAAM,iBAAiB,qDAAsD,CAAC;AAKrF,2BAA2B;AAC3B,MAAM,WAAW,WAAW;IAC1B,8DAA8D;IAC9D,GAAG,EAAE,MAAM,CAAC;IACZ,wBAAwB;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,eAAe;IACf,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5B;AAED,yGAAyG;AACzG,eAAO,MAAM,aAAa,QAAO,MAA+C,CAAC;AAOjF,oEAAoE;AACpE,KAAK,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;AAE7C;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,SAAS,EAAE,EAAE,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CA0C1I;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,UAAU,CAAC,OAAO,EAAE;IAAE,KAAK,EAAE,SAAS,SAAS,EAAE,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,WAAW,CAAC,CA0D9J"}
|
package/dist/serve.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The adviser's tools, served to an agent over the Model Context Protocol.
|
|
3
|
+
*
|
|
4
|
+
* It is the smallest server that speaks the protocol's HTTP transport honestly: an agent posts a
|
|
5
|
+
* request and gets a JSON answer. There are no sessions and nothing is streamed, because nothing
|
|
6
|
+
* here takes long or happens unasked.
|
|
7
|
+
*
|
|
8
|
+
* It is a way into somebody's lab, so it is shut by default in every way it can be:
|
|
9
|
+
*
|
|
10
|
+
* - it listens on this machine only, never on the network;
|
|
11
|
+
* - every request has to carry the token, compared in constant time;
|
|
12
|
+
* - a request that says it comes from a web page is refused, and so is one addressed to any name
|
|
13
|
+
* but this machine's, which is how a page in a browser would otherwise reach a server like this;
|
|
14
|
+
* - and behind it the tools themselves can only read, work things out and propose.
|
|
15
|
+
*/
|
|
16
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
17
|
+
import { createServer } from "node:http";
|
|
18
|
+
import { AGENT_INSTRUCTIONS } from "./tools.js";
|
|
19
|
+
/** The versions of the protocol this speaks, newest first. */
|
|
20
|
+
export const PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
|
|
21
|
+
/** The most one request may be, in bytes. A sequence is the largest thing an agent sends. */
|
|
22
|
+
const MOST_BYTES = 2_000_000;
|
|
23
|
+
/** @returns A new token: 32 random bytes, in base64url. Whoever has it can reach the adviser's tools. */
|
|
24
|
+
export const newAgentToken = () => randomBytes(32).toString("base64url");
|
|
25
|
+
const sameText = (a, b) => {
|
|
26
|
+
const [left, right] = [Buffer.from(a), Buffer.from(b)];
|
|
27
|
+
return left.length === right.length && timingSafeEqual(left, right);
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Answer one message of the protocol.
|
|
31
|
+
*
|
|
32
|
+
* @param message - What arrived.
|
|
33
|
+
* @param tools - The tools on offer.
|
|
34
|
+
* @param about - What to call this server and its version.
|
|
35
|
+
* @returns The answer, or `null` if the message was a notification.
|
|
36
|
+
*/
|
|
37
|
+
export async function answerAgent(message, tools, about) {
|
|
38
|
+
const asked = message;
|
|
39
|
+
const id = asked !== null && typeof asked === "object" && (typeof asked.id === "string" || typeof asked.id === "number") ? asked.id : null;
|
|
40
|
+
const fail = (code, says) => ({ jsonrpc: "2.0", id, error: { code, message: says } });
|
|
41
|
+
if (asked === null || typeof asked !== "object" || asked.jsonrpc !== "2.0" || typeof asked.method !== "string")
|
|
42
|
+
return fail(-32600, "That is not a request.");
|
|
43
|
+
// A notification is told, not asked: it has no ID and gets no answer.
|
|
44
|
+
if (asked.id === undefined)
|
|
45
|
+
return null;
|
|
46
|
+
const done = (result) => ({ jsonrpc: "2.0", id, result });
|
|
47
|
+
switch (asked.method) {
|
|
48
|
+
case "initialize": {
|
|
49
|
+
const wanted = asked.params?.["protocolVersion"];
|
|
50
|
+
const version = PROTOCOL_VERSIONS.includes(wanted) ? wanted : PROTOCOL_VERSIONS[0];
|
|
51
|
+
return done({ protocolVersion: version, capabilities: { tools: { listChanged: false } }, serverInfo: { name: about.name, version: about.version }, instructions: AGENT_INSTRUCTIONS });
|
|
52
|
+
}
|
|
53
|
+
case "ping": return done({});
|
|
54
|
+
case "tools/list":
|
|
55
|
+
return done({ tools: tools.map((tool) => ({
|
|
56
|
+
name: tool.name, title: tool.title, description: tool.description, inputSchema: tool.input,
|
|
57
|
+
annotations: { title: tool.title, readOnlyHint: tool.readOnly, destructiveHint: false, idempotentHint: tool.readOnly, openWorldHint: false },
|
|
58
|
+
})) });
|
|
59
|
+
case "tools/call": {
|
|
60
|
+
const tool = tools.find((one) => one.name === asked.params?.["name"]);
|
|
61
|
+
if (tool === undefined)
|
|
62
|
+
return fail(-32602, `There is no tool called ${JSON.stringify(asked.params?.["name"] ?? null)}.`);
|
|
63
|
+
const args = asked.params?.["arguments"];
|
|
64
|
+
if (!(args === undefined || (typeof args === "object" && args !== null && !Array.isArray(args))))
|
|
65
|
+
return fail(-32602, "A tool's arguments are given by name.");
|
|
66
|
+
const given = (args ?? {});
|
|
67
|
+
// Something the tool was not told to expect is refused, not ignored: an agent that sends it
|
|
68
|
+
// meant something by it, and going on without it would do other than it asked.
|
|
69
|
+
const stray = Object.keys(given).find((name) => !(name in tool.input.properties));
|
|
70
|
+
const missing = (tool.input.required ?? []).find((name) => given[name] === undefined);
|
|
71
|
+
try {
|
|
72
|
+
if (stray !== undefined)
|
|
73
|
+
throw new Error(`${tool.name} takes nothing called ${JSON.stringify(stray)}. It takes: ${Object.keys(tool.input.properties).join(", ") || "nothing"}.`);
|
|
74
|
+
if (missing !== undefined)
|
|
75
|
+
throw new Error(`${tool.name} needs ${JSON.stringify(missing)}.`);
|
|
76
|
+
return done({ content: [{ type: "text", text: JSON.stringify(await tool.run(given), null, 1) }] });
|
|
77
|
+
}
|
|
78
|
+
catch (cause) {
|
|
79
|
+
// A tool that could not do what was asked says why to the agent, which can then do something about it.
|
|
80
|
+
return done({ content: [{ type: "text", text: cause instanceof Error ? cause.message : String(cause) }], isError: true });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
default: return fail(-32601, `This server does not do ${JSON.stringify(asked.method)}.`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Serve the adviser's tools on this machine.
|
|
88
|
+
*
|
|
89
|
+
* @param options - `tools` are the tools; `token` is what every request has to carry, as a bearer
|
|
90
|
+
* token; `port` is the port to take, or 0 for any that is free; `name` and `version` are what the
|
|
91
|
+
* server calls itself.
|
|
92
|
+
* @returns Where it is listening, and a way to stop it.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { agentTools, newAgentToken, serveAgent } from "@biolibs/bionet-agent";
|
|
97
|
+
*
|
|
98
|
+
* const token = newAgentToken();
|
|
99
|
+
* const server = await serveAgent({ tools: agentTools(lab), token });
|
|
100
|
+
* server.url; // http://127.0.0.1:53124/mcp — give this and the token to the agent
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export async function serveAgent(options) {
|
|
104
|
+
if (options.token.length < 32)
|
|
105
|
+
throw new Error("The token is too short to keep anybody out.");
|
|
106
|
+
const about = { name: options.name ?? "bionet-adviser", version: options.version ?? "0.1.0" };
|
|
107
|
+
let port = 0;
|
|
108
|
+
const say = (response, status, body, more = {}) => {
|
|
109
|
+
const text = body === null ? "" : JSON.stringify(body);
|
|
110
|
+
response.writeHead(status, { ...(body === null ? {} : { "content-type": "application/json" }), "cache-control": "no-store", ...more });
|
|
111
|
+
response.end(text);
|
|
112
|
+
};
|
|
113
|
+
const read = (request) => new Promise((resolve) => {
|
|
114
|
+
const chunks = [];
|
|
115
|
+
let size = 0;
|
|
116
|
+
request.on("data", (chunk) => {
|
|
117
|
+
size += chunk.length;
|
|
118
|
+
// Past the limit the rest is let go by without being kept, so that whoever sent it can be
|
|
119
|
+
// told it was too much instead of having the line cut under them.
|
|
120
|
+
if (size <= MOST_BYTES)
|
|
121
|
+
chunks.push(chunk);
|
|
122
|
+
else
|
|
123
|
+
chunks.length = 0;
|
|
124
|
+
});
|
|
125
|
+
request.on("end", () => resolve(size > MOST_BYTES ? null : Buffer.concat(chunks).toString("utf8")));
|
|
126
|
+
request.on("error", () => resolve(null));
|
|
127
|
+
});
|
|
128
|
+
const server = createServer((request, response) => {
|
|
129
|
+
void (async () => {
|
|
130
|
+
// A browser says where a request comes from and which name it was sent to. Nothing that is
|
|
131
|
+
// meant to use this is a web page, and nothing that is meant to use it calls it by another name.
|
|
132
|
+
if (request.headers.origin !== undefined)
|
|
133
|
+
return say(response, 403, { error: "This is not for web pages." });
|
|
134
|
+
const host = request.headers.host ?? "";
|
|
135
|
+
if (host !== `127.0.0.1:${port}` && host !== `localhost:${port}`)
|
|
136
|
+
return say(response, 403, { error: "This answers only to this machine's own name." });
|
|
137
|
+
if (new URL(request.url ?? "/", "http://x").pathname !== "/mcp")
|
|
138
|
+
return say(response, 404, { error: "There is nothing here." });
|
|
139
|
+
const given = /^Bearer (.+)$/.exec(request.headers.authorization ?? "")?.[1] ?? "";
|
|
140
|
+
if (!sameText(given, options.token))
|
|
141
|
+
return say(response, 401, { error: "That is not this adviser's token." }, { "www-authenticate": "Bearer" });
|
|
142
|
+
// Nothing is sent unasked, so there is no stream to open and no session to end.
|
|
143
|
+
if (request.method !== "POST")
|
|
144
|
+
return say(response, 405, { error: "Ask by POST." }, { allow: "POST" });
|
|
145
|
+
const body = await read(request);
|
|
146
|
+
if (body === null)
|
|
147
|
+
return say(response, 413, { error: "That is more than one request may be." });
|
|
148
|
+
let message;
|
|
149
|
+
try {
|
|
150
|
+
message = JSON.parse(body);
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return say(response, 400, { jsonrpc: "2.0", id: null, error: { code: -32700, message: "That is not JSON." } });
|
|
154
|
+
}
|
|
155
|
+
if (Array.isArray(message)) {
|
|
156
|
+
const answers = (await Promise.all(message.map((one) => answerAgent(one, options.tools, about)))).filter((one) => one !== null);
|
|
157
|
+
return answers.length === 0 ? say(response, 202, null) : say(response, 200, answers);
|
|
158
|
+
}
|
|
159
|
+
const answer = await answerAgent(message, options.tools, about);
|
|
160
|
+
return answer === null ? say(response, 202, null) : say(response, 200, answer);
|
|
161
|
+
})().catch(() => { if (!response.headersSent)
|
|
162
|
+
say(response, 500, { error: "Something went wrong here." }); });
|
|
163
|
+
});
|
|
164
|
+
await new Promise((resolve, reject) => {
|
|
165
|
+
server.once("error", reject);
|
|
166
|
+
// This machine only: never every address the machine has.
|
|
167
|
+
server.listen(options.port ?? 0, "127.0.0.1", resolve);
|
|
168
|
+
});
|
|
169
|
+
port = server.address().port;
|
|
170
|
+
return {
|
|
171
|
+
url: `http://127.0.0.1:${port}/mcp`, port,
|
|
172
|
+
close: () => new Promise((resolve) => { server.close(() => resolve()); server.closeAllConnections(); }),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=serve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serve.js","sourceRoot":"","sources":["../src/serve.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,YAAY,EAA0D,MAAM,WAAW,CAAC;AAEjG,OAAO,EAAE,kBAAkB,EAAkB,MAAM,YAAY,CAAC;AAEhE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CAAO,CAAC,OAAO,EAAE,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,mBAAmB,EAAE,CAAC,CAAC,CAAC,CAAC;KAC9G,CAAC;AACJ,CAAC"}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an adviser is given to work with: the lab to read, the lab's own tools to work things out
|
|
3
|
+
* with, and three things it may write.
|
|
4
|
+
*
|
|
5
|
+
* Every tool here is a plain function over a lab, described well enough for something that has
|
|
6
|
+
* never seen this code to use it. Nothing in this file knows how the tools are reached: `serve.ts`
|
|
7
|
+
* hands them over the Model Context Protocol, and a test calls them directly.
|
|
8
|
+
*
|
|
9
|
+
* Two things shape all of it.
|
|
10
|
+
*
|
|
11
|
+
* - **The adviser has no hands.** It can say what would happen and propose that somebody do it. It
|
|
12
|
+
* cannot say that anything was done, used or moved, and there is no tool here that tries: the
|
|
13
|
+
* lab would refuse it anyway, on every device, because of the role its key has.
|
|
14
|
+
* - **It is shown the working, not asked to believe.** What a tool works out comes from the same
|
|
15
|
+
* engines the lab's own windows use, so a proposal the adviser makes is one a person at the bench
|
|
16
|
+
* can set up and see the same numbers for.
|
|
17
|
+
*/
|
|
18
|
+
import type { Lab } from "@biolibs/bionet-client";
|
|
19
|
+
/** A JSON schema for what a tool takes: always an object, with these properties. */
|
|
20
|
+
export interface ToolInput {
|
|
21
|
+
type: "object";
|
|
22
|
+
properties: Record<string, Record<string, unknown>>;
|
|
23
|
+
required?: string[];
|
|
24
|
+
additionalProperties: false;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* What a tool's use would look like in the lab, for a window that wants to show the adviser at work.
|
|
28
|
+
* It is shaped like a stop on the demo's walk, which is already how a bench is told what to show.
|
|
29
|
+
*/
|
|
30
|
+
export type AgentShows = {
|
|
31
|
+
at: "lab";
|
|
32
|
+
focus: {
|
|
33
|
+
place: string | null;
|
|
34
|
+
item?: string;
|
|
35
|
+
};
|
|
36
|
+
} | {
|
|
37
|
+
at: "sequences";
|
|
38
|
+
part: string;
|
|
39
|
+
} | {
|
|
40
|
+
at: "primers";
|
|
41
|
+
part: string;
|
|
42
|
+
from: number;
|
|
43
|
+
to: number;
|
|
44
|
+
} | {
|
|
45
|
+
at: "pcr";
|
|
46
|
+
start: {
|
|
47
|
+
view: "result" | "tube";
|
|
48
|
+
template?: string;
|
|
49
|
+
forward?: string;
|
|
50
|
+
reverse?: string;
|
|
51
|
+
};
|
|
52
|
+
} | {
|
|
53
|
+
at: "digest";
|
|
54
|
+
start: {
|
|
55
|
+
view: "result" | "tube";
|
|
56
|
+
tube: string;
|
|
57
|
+
enzymes: string[];
|
|
58
|
+
};
|
|
59
|
+
} | {
|
|
60
|
+
at: "ligation";
|
|
61
|
+
start: {
|
|
62
|
+
view: "result" | "tube";
|
|
63
|
+
tubes: string[];
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
/** One tool an adviser can use. */
|
|
67
|
+
export interface AgentTool {
|
|
68
|
+
/** What it is called over the wire. */
|
|
69
|
+
name: string;
|
|
70
|
+
/** What it is called for a person. */
|
|
71
|
+
title: string;
|
|
72
|
+
/** What it does, when to use it, and what comes back. */
|
|
73
|
+
description: string;
|
|
74
|
+
/** What it takes. */
|
|
75
|
+
input: ToolInput;
|
|
76
|
+
/** Whether it only reads and works things out. The three that write say `false`. */
|
|
77
|
+
readOnly: boolean;
|
|
78
|
+
/** Do it. Throws, with a sentence a person could act on, if it cannot be done. */
|
|
79
|
+
run: (args: Record<string, unknown>) => Promise<unknown>;
|
|
80
|
+
}
|
|
81
|
+
/** One use of a tool, as a host is told of it. */
|
|
82
|
+
export interface AgentCall {
|
|
83
|
+
/** Which tool. */
|
|
84
|
+
tool: string;
|
|
85
|
+
/** What it was given. */
|
|
86
|
+
args: Record<string, unknown>;
|
|
87
|
+
/** Whether it worked. */
|
|
88
|
+
ok: boolean;
|
|
89
|
+
/** What it would look like in the lab, if it has a place there. */
|
|
90
|
+
shows: AgentShows | null;
|
|
91
|
+
}
|
|
92
|
+
/** What is said to an agent before it uses anything: who it is here, and how to treat what it reads. */
|
|
93
|
+
export declare const AGENT_INSTRUCTIONS: string;
|
|
94
|
+
/** The longest stretch of a sequence handed back at once, in bases. A whole genome is not something to read in one go. */
|
|
95
|
+
export declare const MOST_BASES = 20000;
|
|
96
|
+
/**
|
|
97
|
+
* The tools an adviser hosted on a device is given.
|
|
98
|
+
*
|
|
99
|
+
* @param lab - The lab, opened, on a device that hosts an adviser.
|
|
100
|
+
* @param options - `watch` is told of each use, with what it would look like in the lab, so that a
|
|
101
|
+
* window can show the adviser at work.
|
|
102
|
+
* @returns The tools, in the order to list them.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { agentTools } from "@biolibs/bionet-agent";
|
|
107
|
+
*
|
|
108
|
+
* const tools = agentTools(lab);
|
|
109
|
+
* const overview = await tools.find((one) => one.name === "lab_overview")!.run({});
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare function agentTools(lab: Lab, options?: {
|
|
113
|
+
watch?: (call: AgentCall) => void;
|
|
114
|
+
}): AgentTool[];
|
|
115
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,KAAK,EAAE,GAAG,EAAW,MAAM,wBAAwB,CAAC;AAI3D,oFAAoF;AACpF,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,QAAQ,CAAC;IACf,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACpD,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,oBAAoB,EAA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