@parcha/agentrun-kernel 0.1.0-beta.30 → 0.1.0-beta.31
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 +4 -0
- package/dist/host/index.d.ts +2 -0
- package/dist/host/index.js +1 -0
- package/dist/host/mcp-route.d.ts +11 -0
- package/dist/host/mcp-route.js +121 -0
- package/dist/host/proxy.d.ts +3 -0
- package/dist/host/proxy.js +17 -0
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +31 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## 0.1.0-beta.31 (2026-11-06)
|
|
6
|
+
|
|
7
|
+
- Agents call a saved MCP server's tools on pi and claude-agent: grant `mcp:<server>/<tool>` in `definition.tools`. A save checks the name against the server's saved tools (`422` naming them). A run's first launch pins the granted tools as saved and the server's URL; its credential reaches only runs that grant one of its tools, over the run's secrets pipe. pi binds each tool in the run's process; claude-agent's own MCP client calls it at the session's model proxy (`/mcp/<server>`, behind the session key), so the CLI never holds the credential. A call follows no redirect, a crash-cut call is answered interrupted and never sent again, a moved server is sent nothing, and the timeline reads `<server> · <tool>`. `features.mcp_client.harnesses` lists the harnesses that run them. Every call passes the run's tool gate and its spend cap.
|
|
8
|
+
|
|
5
9
|
## 0.1.0-beta.30 (2026-11-05)
|
|
6
10
|
|
|
7
11
|
- A Codex run stopped just after its message was sent to Codex no longer loses the message. Codex answers `turn/start` before it writes the message to its rollout: on the real app-server, killed at the answer or 20 ms after it, the rollout held no user message; 100 ms after, it did. A stop in that window kills Codex with the message sent and never recorded, and the next Codex, resumed from the session's rollout, never has it. The stop's settle now reads Codex's rollout as the session keeps it, the rollout the next Codex is resumed from, and carries a placed message it lacks into the next one, as claude-agent's settle reads its CLI's transcript. The `turn/start` send mark is gone (the kernel's `markSent` and `sentMarks` with it); a stopped drive still starts no turn.
|
package/dist/host/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { ANTHROPIC_IMAGE_TOKENS_MAX, ANTHROPIC_TOOL_USE_SYSTEM_PROMPT_TOKENS, ASSUMED_TOOL_PROMPT_TOKENS, INPUT_OVERHEAD_APIS, OPENAI_IMAGE_TOKENS_MAX, providerInputOverhead, type InputShape } from "./overhead.js";
|
|
2
2
|
export { hostHeaderProblems, inputTokensMaxOf, matchRoute, parseStrict, providerIdsOf, ProxyRefusal, reportedCostOf, sameKey, startModelProxy } from "./proxy.js";
|
|
3
3
|
export type { ModelProxy, ModelProxyHooks, ModelProxyOptions, ModelUsageReport, ProviderPolicy, ProxyRequest, ProxyRoute, ProxyUsage } from "./proxy.js";
|
|
4
|
+
export { MCP_ROUTE_VERSIONS, mcpRoute } from "./mcp-route.js";
|
|
5
|
+
export type { McpHandler, McpRouteOptions } from "./mcp-route.js";
|
|
4
6
|
export { processAlive } from "./alive.js";
|
|
5
7
|
export { serialWriter } from "./serial.js";
|
|
6
8
|
export { addressBytes, checkAddress, EGRESS_RANGES } from "./egress-table.js";
|
package/dist/host/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// provider's rules in a `ProviderPolicy`. Written for AgentRun.
|
|
3
3
|
export { ANTHROPIC_IMAGE_TOKENS_MAX, ANTHROPIC_TOOL_USE_SYSTEM_PROMPT_TOKENS, ASSUMED_TOOL_PROMPT_TOKENS, INPUT_OVERHEAD_APIS, OPENAI_IMAGE_TOKENS_MAX, providerInputOverhead } from "./overhead.js";
|
|
4
4
|
export { hostHeaderProblems, inputTokensMaxOf, matchRoute, parseStrict, providerIdsOf, ProxyRefusal, reportedCostOf, sameKey, startModelProxy } from "./proxy.js";
|
|
5
|
+
export { MCP_ROUTE_VERSIONS, mcpRoute } from "./mcp-route.js";
|
|
5
6
|
export { processAlive } from "./alive.js";
|
|
6
7
|
export { serialWriter } from "./serial.js";
|
|
7
8
|
export { addressBytes, checkAddress, EGRESS_RANGES } from "./egress-table.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { SessionMcp } from "../types.js";
|
|
3
|
+
/** The protocol revisions answered, newest first: a client asking for another gets the newest. */
|
|
4
|
+
export declare const MCP_ROUTE_VERSIONS: readonly string[];
|
|
5
|
+
export interface McpRouteOptions {
|
|
6
|
+
/** How often a call in flight sends a keepalive. Default 15 s. */
|
|
7
|
+
keepaliveMs?: number;
|
|
8
|
+
}
|
|
9
|
+
/** The handler a model proxy mounts at `/mcp/<server>` (ModelProxyOptions.mcp). */
|
|
10
|
+
export type McpHandler = (server: string, req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
11
|
+
export declare function mcpRoute(mcp: SessionMcp, options?: McpRouteOptions): McpHandler;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { parseStrict } from "./proxy.js";
|
|
2
|
+
/** The protocol revisions answered, newest first: a client asking for another gets the newest. */
|
|
3
|
+
export const MCP_ROUTE_VERSIONS = ["2025-11-25", "2025-06-18", "2025-03-26"];
|
|
4
|
+
const MAX_BODY = 4 * 1024 * 1024;
|
|
5
|
+
const KEEPALIVE_MS = 15_000;
|
|
6
|
+
const isPlain = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
7
|
+
const isId = (v) => typeof v === "string" || (typeof v === "number" && Number.isSafeInteger(v));
|
|
8
|
+
export function mcpRoute(mcp, options = {}) {
|
|
9
|
+
const keepaliveMs = options.keepaliveMs ?? KEEPALIVE_MS;
|
|
10
|
+
const servers = new Map(mcp.servers.map((server) => [server.name, server]));
|
|
11
|
+
/** Calls in flight, by server and JSON-RPC id. */
|
|
12
|
+
const running = new Map();
|
|
13
|
+
const keyOf = (server, id) => `${server}\0${typeof id}:${id}`;
|
|
14
|
+
const json = (res, status, body) => {
|
|
15
|
+
res.writeHead(status, body === undefined ? { "content-length": "0" } : { "content-type": "application/json" });
|
|
16
|
+
res.end(body === undefined ? undefined : JSON.stringify(body));
|
|
17
|
+
};
|
|
18
|
+
const failure = (id, code, message) => ({ jsonrpc: "2.0", id, error: { code, message } });
|
|
19
|
+
return async (server, req, res) => {
|
|
20
|
+
const served = servers.get(server);
|
|
21
|
+
if (!served)
|
|
22
|
+
return json(res, 404, failure(null, -32601, `this session has no MCP server ${server}`));
|
|
23
|
+
if (req.method !== "POST") {
|
|
24
|
+
res.writeHead(405, { allow: "POST", "content-length": "0" });
|
|
25
|
+
return void res.end();
|
|
26
|
+
}
|
|
27
|
+
const chunks = [];
|
|
28
|
+
let size = 0;
|
|
29
|
+
for await (const chunk of req) {
|
|
30
|
+
size += chunk.length;
|
|
31
|
+
if (size > MAX_BODY)
|
|
32
|
+
return json(res, 413, failure(null, -32600, "the request is too large"));
|
|
33
|
+
chunks.push(chunk);
|
|
34
|
+
}
|
|
35
|
+
let message;
|
|
36
|
+
try {
|
|
37
|
+
const parsed = parseStrict(Buffer.concat(chunks).toString("utf8"));
|
|
38
|
+
if (!isPlain(parsed))
|
|
39
|
+
return json(res, 400, failure(null, -32600, "one JSON-RPC message per request; no batches"));
|
|
40
|
+
message = parsed;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return json(res, 400, failure(null, -32700, "the body is not JSON"));
|
|
44
|
+
}
|
|
45
|
+
const params = isPlain(message.params) ? message.params : {};
|
|
46
|
+
// A notification, or the client's answer to a request: nothing to say back.
|
|
47
|
+
if (typeof message.method !== "string" || message.id === undefined) {
|
|
48
|
+
if (message.method === "notifications/cancelled" && isId(params.requestId))
|
|
49
|
+
running.get(keyOf(server, params.requestId))?.abort();
|
|
50
|
+
return json(res, 202);
|
|
51
|
+
}
|
|
52
|
+
if (!isId(message.id))
|
|
53
|
+
return json(res, 400, failure(null, -32600, "the request's id must be a string or an integer"));
|
|
54
|
+
const id = message.id;
|
|
55
|
+
const answer = (result) => json(res, 200, { jsonrpc: "2.0", id, result });
|
|
56
|
+
switch (message.method) {
|
|
57
|
+
case "initialize": {
|
|
58
|
+
const asked = params.protocolVersion;
|
|
59
|
+
const protocolVersion = typeof asked === "string" && MCP_ROUTE_VERSIONS.includes(asked) ? asked : MCP_ROUTE_VERSIONS[0];
|
|
60
|
+
return answer({ protocolVersion, capabilities: { tools: {} }, serverInfo: { name: `agentrun-${server}`, version: "1.0.0" } });
|
|
61
|
+
}
|
|
62
|
+
case "ping":
|
|
63
|
+
return answer({});
|
|
64
|
+
case "tools/list":
|
|
65
|
+
return answer({ tools: served.tools.map((tool) => ({ name: tool.name, ...(tool.title ? { title: tool.title } : {}), description: tool.description ?? "", inputSchema: tool.inputSchema, ...(tool.annotations ? { annotations: tool.annotations } : {}) })) });
|
|
66
|
+
case "tools/call":
|
|
67
|
+
return call(server, id, params, req, res);
|
|
68
|
+
default:
|
|
69
|
+
return json(res, 200, failure(id, -32601, `${String(message.method).slice(0, 64)} is not a method this server answers`));
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
async function call(server, id, params, req, res) {
|
|
73
|
+
const meta = isPlain(params._meta) ? params._meta : {};
|
|
74
|
+
const progressToken = isId(meta.progressToken) ? meta.progressToken : undefined;
|
|
75
|
+
const callId = typeof meta["claudecode/toolUseId"] === "string" ? meta["claudecode/toolUseId"] : undefined;
|
|
76
|
+
const stream = String(req.headers.accept ?? "").includes("text/event-stream");
|
|
77
|
+
const controller = new AbortController();
|
|
78
|
+
const key = keyOf(server, id);
|
|
79
|
+
running.set(key, controller);
|
|
80
|
+
// A client gone before the answer: its call is aborted, upstream too.
|
|
81
|
+
res.once("close", () => { if (!res.writableFinished)
|
|
82
|
+
controller.abort(); });
|
|
83
|
+
const send = (body) => res.write(`event: message\ndata: ${JSON.stringify(body)}\n\n`);
|
|
84
|
+
let timer;
|
|
85
|
+
if (stream) {
|
|
86
|
+
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" });
|
|
87
|
+
res.flushHeaders();
|
|
88
|
+
let beat = 0;
|
|
89
|
+
timer = setInterval(() => {
|
|
90
|
+
beat += 1;
|
|
91
|
+
if (progressToken === undefined)
|
|
92
|
+
res.write(": keepalive\n\n");
|
|
93
|
+
else
|
|
94
|
+
send({ jsonrpc: "2.0", method: "notifications/progress", params: { progressToken, progress: beat } });
|
|
95
|
+
}, keepaliveMs);
|
|
96
|
+
}
|
|
97
|
+
let result;
|
|
98
|
+
try {
|
|
99
|
+
const name = params.name;
|
|
100
|
+
const args = isPlain(params.arguments) ? params.arguments : {};
|
|
101
|
+
const granted = typeof name === "string" && servers.get(server).tools.some((tool) => tool.name === name);
|
|
102
|
+
result = granted
|
|
103
|
+
? await mcp.call(server, name, args, { signal: controller.signal, ...(callId ? { callId } : {}) })
|
|
104
|
+
: { content: [{ type: "text", text: `${String(name).slice(0, 64)} is not a tool of the MCP server ${server} in this session.` }], isError: true };
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
result = { content: [{ type: "text", text: `The MCP server ${server}'s call failed: ${String(error?.message ?? error).slice(0, 300)}` }], isError: true };
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
clearInterval(timer);
|
|
111
|
+
running.delete(key);
|
|
112
|
+
}
|
|
113
|
+
if (res.destroyed)
|
|
114
|
+
return;
|
|
115
|
+
const body = { jsonrpc: "2.0", id, result };
|
|
116
|
+
if (!stream)
|
|
117
|
+
return json(res, 200, body);
|
|
118
|
+
send(body);
|
|
119
|
+
res.end();
|
|
120
|
+
}
|
|
121
|
+
}
|
package/dist/host/proxy.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type IncomingHttpHeaders } from "node:http";
|
|
2
|
+
import type { McpHandler } from "./mcp-route.js";
|
|
2
3
|
import { type Ended } from "../booking.js";
|
|
3
4
|
/** A route a policy admits: its method, its exact path, and the request headers forwarded with it. */
|
|
4
5
|
export interface ProxyRoute {
|
|
@@ -161,6 +162,8 @@ export interface ModelProxyOptions {
|
|
|
161
162
|
* after the client's, so the client can neither set nor override them, and before the credential's. Checked when the
|
|
162
163
|
* proxy starts (`hostHeaderProblems`). */
|
|
163
164
|
headers?: Readonly<Record<string, string>>;
|
|
165
|
+
/** The session's remote MCP servers (mcp-route.ts), each at `/mcp/<server>` behind the session's key. */
|
|
166
|
+
mcp?: McpHandler;
|
|
164
167
|
/** For tests: the fetch the proxy forwards with. */
|
|
165
168
|
fetch?: typeof fetch;
|
|
166
169
|
/** Told, with CONTENT_FILTERED, once an answer the provider stopped on its content filter has been passed and booked
|
package/dist/host/proxy.js
CHANGED
|
@@ -78,6 +78,10 @@ export function hostHeaderProblems(headers) {
|
|
|
78
78
|
}
|
|
79
79
|
return problems;
|
|
80
80
|
}
|
|
81
|
+
/** A server's endpoint, `/mcp/<server>`, with no query. */
|
|
82
|
+
const MCP_PATH = /^\/mcp\/([a-z][a-z0-9-]{0,31})$/;
|
|
83
|
+
/** Where an MCP client looks for OAuth after a 401: never served here, so it never starts a sign-in against the proxy. */
|
|
84
|
+
const OAUTH_PATH = /^\/(?:\.well-known\/|register(?:$|[/?]))/;
|
|
81
85
|
const UNSAFE_PATH = /[^A-Za-z0-9/_.\-?=&]|\.\.|\/\/|^[^/]|\?.*\?/;
|
|
82
86
|
const MAX_BODY = 32 * 1024 * 1024;
|
|
83
87
|
/** How much of a response's start and end the proxy keeps to read its usage from. */
|
|
@@ -324,6 +328,19 @@ export async function startModelProxy(options, key) {
|
|
|
324
328
|
// A request refused before its body is read is drained, so its client gets the answer instead of a reset.
|
|
325
329
|
res.once("finish", () => { if (!req.complete)
|
|
326
330
|
req.resume(); });
|
|
331
|
+
const path = req.url ?? "";
|
|
332
|
+
if (options.mcp && OAUTH_PATH.test(path)) {
|
|
333
|
+
res.writeHead(404, { "content-length": "0" });
|
|
334
|
+
return res.end();
|
|
335
|
+
}
|
|
336
|
+
const mcp = options.mcp ? MCP_PATH.exec(path) : null;
|
|
337
|
+
if (mcp) {
|
|
338
|
+
if (!sameKey(policy.keyOf(req.headers), key))
|
|
339
|
+
return refuse(res, 401, "this session's key is required", req);
|
|
340
|
+
if (closing)
|
|
341
|
+
return refuse(res, 503, "the model proxy is closing", req);
|
|
342
|
+
return options.mcp(mcp[1], req, res);
|
|
343
|
+
}
|
|
327
344
|
let route;
|
|
328
345
|
let query;
|
|
329
346
|
try {
|
package/dist/index.d.ts
CHANGED
|
@@ -10,4 +10,4 @@ export type { HarnessRunReason } from "./errors.js";
|
|
|
10
10
|
export { CONTENT_FILTERED, HOST_RESULT_MAX_CHARS } from "./types.js";
|
|
11
11
|
export { bookRequest, isConnectCode, isGatewayTimeout, rejectionCode } from "./booking.js";
|
|
12
12
|
export type { Booked, Ended, Priced } from "./booking.js";
|
|
13
|
-
export type { AdapterCapabilities, AdapterEvent, AdapterSession, AgentAdapter, HarnessEvent, HostTool, ToolImage, JsonValue, ModelPort, ModelTicket, ModelRequest, ModelUsage, NodeRequest, SessionIO, SessionRecord, SessionSpec, SessionState, SessionStore, SessionWriter, TurnStatus, } from "./types.js";
|
|
13
|
+
export type { AdapterCapabilities, AdapterEvent, AdapterSession, AgentAdapter, HarnessEvent, HostTool, McpCallResult, McpTool, ToolImage, JsonValue, ModelPort, ModelTicket, ModelRequest, ModelUsage, NodeRequest, SessionIO, SessionMcp, SessionRecord, SessionSpec, SessionState, SessionStore, SessionWriter, TurnStatus, } from "./types.js";
|
package/dist/types.d.ts
CHANGED
|
@@ -56,6 +56,35 @@ export interface HostTool {
|
|
|
56
56
|
images?: readonly ToolImage[];
|
|
57
57
|
}>;
|
|
58
58
|
}
|
|
59
|
+
/** A tool of a remote MCP server, as the server listed it when its owner saved it (the run's pinned copy). */
|
|
60
|
+
export interface McpTool {
|
|
61
|
+
name: string;
|
|
62
|
+
title?: string;
|
|
63
|
+
description?: string;
|
|
64
|
+
inputSchema: Record<string, unknown>;
|
|
65
|
+
annotations?: Record<string, unknown>;
|
|
66
|
+
}
|
|
67
|
+
/** What an MCP tool call answers the model: its text, and whether the tool said it failed. */
|
|
68
|
+
export interface McpCallResult {
|
|
69
|
+
content: {
|
|
70
|
+
type: "text";
|
|
71
|
+
text: string;
|
|
72
|
+
}[];
|
|
73
|
+
isError?: boolean;
|
|
74
|
+
}
|
|
75
|
+
/** A session's remote MCP servers: each one's granted tools, and the call that reaches them through the host, which holds
|
|
76
|
+
* their credentials. A harness with its own MCP client points it at the session's model proxy (`/mcp/<server>`). */
|
|
77
|
+
export interface SessionMcp {
|
|
78
|
+
servers: readonly {
|
|
79
|
+
name: string;
|
|
80
|
+
tools: readonly McpTool[];
|
|
81
|
+
}[];
|
|
82
|
+
/** Never throws: a failure is an `isError` result the model reads. `callId` is the harness's id for the call. */
|
|
83
|
+
call(server: string, tool: string, args: Record<string, unknown>, context: {
|
|
84
|
+
signal: AbortSignal;
|
|
85
|
+
callId?: string;
|
|
86
|
+
}): Promise<McpCallResult>;
|
|
87
|
+
}
|
|
59
88
|
/** One durable record of a session, in the order it was appended. */
|
|
60
89
|
export interface SessionRecord {
|
|
61
90
|
kind: string;
|
|
@@ -88,6 +117,8 @@ export interface SessionSpec {
|
|
|
88
117
|
system: string[];
|
|
89
118
|
/** Exactly these tools; the harness's own stay off. */
|
|
90
119
|
tools: HostTool[];
|
|
120
|
+
/** The remote MCP servers it may call, for a harness that has its own MCP client. */
|
|
121
|
+
mcp?: SessionMcp;
|
|
91
122
|
context: "fresh" | "fork";
|
|
92
123
|
tier?: NodeRequest["tier"];
|
|
93
124
|
effort?: NodeRequest["effort"];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-kernel",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.31",
|
|
4
4
|
"description": "Agent harnesses other than Pi, starting with Codex, behind one seam: a DSL node's runNode and an AgentRun run on a harness adapter, durable in AgentRun's own store.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentrun",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"test": "node --test --test-concurrency=8 'test/*.test.mjs'"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@parcha/agentrun-dsl": "0.1.0-beta.
|
|
59
|
+
"@parcha/agentrun-dsl": "0.1.0-beta.31",
|
|
60
60
|
"ajv": "8.18.0",
|
|
61
61
|
"typebox": "1.3.7"
|
|
62
62
|
},
|