@tangle-network/agent-runtime 0.104.0 → 0.106.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 +176 -42
- package/dist/activation-CRjVfOiy.js +203 -0
- package/dist/activation-CRjVfOiy.js.map +1 -0
- package/dist/activation-DyWB0K6E.d.ts +98 -0
- package/dist/agent.d.ts +216 -234
- package/dist/agent.js +615 -547
- package/dist/agent.js.map +1 -1
- package/dist/analyst-loop-DGdtVENg.js +517 -0
- package/dist/analyst-loop-DGdtVENg.js.map +1 -0
- package/dist/analyst-loop.d.ts +10 -43
- package/dist/analyst-loop.js +2 -11
- package/dist/backends-Dy8NujB4.js +713 -0
- package/dist/backends-Dy8NujB4.js.map +1 -0
- package/dist/candidate-execution/index.d.ts +4 -192
- package/dist/candidate-execution/index.js +5 -66
- package/dist/candidate-execution-DqMbv1rU.js +488 -0
- package/dist/candidate-execution-DqMbv1rU.js.map +1 -0
- package/dist/coder-yhVWbdWc.js +40 -0
- package/dist/coder-yhVWbdWc.js.map +1 -0
- package/dist/conversation-XucG_K3l.js +1265 -0
- package/dist/conversation-XucG_K3l.js.map +1 -0
- package/dist/conversation.d.ts +2 -804
- package/dist/conversation.js +2 -55
- package/dist/environment-provider-CfLUmKmf.js +1168 -0
- package/dist/environment-provider-CfLUmKmf.js.map +1 -0
- package/dist/environment-provider-IUGU3epE.d.ts +964 -0
- package/dist/environment-provider.d.ts +2 -78
- package/dist/environment-provider.js +2 -18
- package/dist/errors-DEAvWQPy.js +111 -0
- package/dist/errors-DEAvWQPy.js.map +1 -0
- package/dist/improvement-adapter-Cr5TBSji.d.ts +166 -0
- package/dist/improvement-cycle-C095kS_g.js +2624 -0
- package/dist/improvement-cycle-C095kS_g.js.map +1 -0
- package/dist/index-BGdxAwfO.d.ts +838 -0
- package/dist/index-C-FYUuFG.d.ts +193 -0
- package/dist/index-CbNM40xG.d.ts +140 -0
- package/dist/index-DruSKs3Z.d.ts +7224 -0
- package/dist/index-Efjb3nrQ.d.ts +671 -0
- package/dist/index.d.ts +14 -1134
- package/dist/index.js +2134 -1972
- package/dist/index.js.map +1 -1
- package/dist/intelligence.d.ts +776 -888
- package/dist/intelligence.js +1466 -1410
- package/dist/intelligence.js.map +1 -1
- package/dist/{kb-gate-CwHO0vz6.d.ts → kb-gate-C8z2juK8.d.ts} +33 -31
- package/dist/kb-gate-DpaSwXVx.js +80 -0
- package/dist/kb-gate-DpaSwXVx.js.map +1 -0
- package/dist/knowledge-Ku-mPP4f.js +433 -0
- package/dist/knowledge-Ku-mPP4f.js.map +1 -0
- package/dist/knowledge.d.ts +2 -150
- package/dist/knowledge.js +2 -40
- package/dist/local-harness-Dh8PJ0ot.d.ts +455 -0
- package/dist/loop-runner-bin-BXsFEUcf.d.ts +162 -0
- package/dist/loop-runner-bin-BwjQ1NfH.js +268 -0
- package/dist/loop-runner-bin-BwjQ1NfH.js.map +1 -0
- package/dist/loop-runner-bin.d.ts +2 -17
- package/dist/loop-runner-bin.js +2 -24
- package/dist/loops.d.ts +5 -4164
- package/dist/loops.js +8 -419
- package/dist/mcp/bin.d.ts +1 -1
- package/dist/mcp/bin.js +116 -146
- package/dist/mcp/bin.js.map +1 -1
- package/dist/mcp/index.d.ts +295 -437
- package/dist/mcp/index.js +1242 -1177
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/memory-bin.d.ts +1 -1
- package/dist/mcp/memory-bin.js +37 -16
- package/dist/mcp/memory-bin.js.map +1 -1
- package/dist/memory-server-DL6cE2Ag.js +234 -0
- package/dist/memory-server-DL6cE2Ag.js.map +1 -0
- package/dist/model-resolution-Btd9iIKV.js +98 -0
- package/dist/model-resolution-Btd9iIKV.js.map +1 -0
- package/dist/openai-tools-B68JaOCx.d.ts +24 -0
- package/dist/openai-tools-CKLy1C7M.js +44 -0
- package/dist/openai-tools-CKLy1C7M.js.map +1 -0
- package/dist/otel-export-D34IQW1C.js +843 -0
- package/dist/otel-export-D34IQW1C.js.map +1 -0
- package/dist/platform.d.ts +188 -185
- package/dist/platform.js +185 -218
- package/dist/platform.js.map +1 -1
- package/dist/prepare-B53sPV7M.js +4292 -0
- package/dist/prepare-B53sPV7M.js.map +1 -0
- package/dist/primeintellect/index.d.ts +150 -147
- package/dist/primeintellect/index.js +626 -1004
- package/dist/primeintellect/index.js.map +1 -1
- package/dist/profiles.d.ts +296 -490
- package/dist/profiles.js +722 -749
- package/dist/profiles.js.map +1 -1
- package/dist/protected-model-port-BLPH5VVv.js +722 -0
- package/dist/protected-model-port-BLPH5VVv.js.map +1 -0
- package/dist/protected-model-port-BP6Z4eau.d.ts +283 -0
- package/dist/protected-redaction--F3v1oo8.js +200 -0
- package/dist/protected-redaction--F3v1oo8.js.map +1 -0
- package/dist/redact-D-u-rrcn.js +82 -0
- package/dist/redact-D-u-rrcn.js.map +1 -0
- package/dist/redact-kbIOjfjM.d.ts +452 -0
- package/dist/researcher-CoVqNhfI.js +433 -0
- package/dist/researcher-CoVqNhfI.js.map +1 -0
- package/dist/runtime-BG0BziaP.js +5408 -0
- package/dist/runtime-BG0BziaP.js.map +1 -0
- package/dist/sandbox-events-DeI5xX8P.js +316 -0
- package/dist/sandbox-events-DeI5xX8P.js.map +1 -0
- package/dist/spawn-journal-CwPvKUTa.js +456 -0
- package/dist/spawn-journal-CwPvKUTa.js.map +1 -0
- package/dist/structural-rollout-IXUEplky.js +1445 -0
- package/dist/structural-rollout-IXUEplky.js.map +1 -0
- package/dist/substrate-B0TYNrXn.js +27 -0
- package/dist/substrate-B0TYNrXn.js.map +1 -0
- package/dist/{substrate-Dyiqmd3o.d.ts → substrate-BcnuSHXm.d.ts} +57 -54
- package/dist/supervise-BiRutHS9.js +7321 -0
- package/dist/supervise-BiRutHS9.js.map +1 -0
- package/dist/supervisor-DTKhF-RV.js +3826 -0
- package/dist/supervisor-DTKhF-RV.js.map +1 -0
- package/dist/testing.d.ts +4 -3
- package/dist/testing.js +2478 -2637
- package/dist/testing.js.map +1 -1
- package/dist/tool-server-RcWgLIsL.js +141 -0
- package/dist/tool-server-RcWgLIsL.js.map +1 -0
- package/dist/trace-propagation-CJJC7SVB.js +57 -0
- package/dist/trace-propagation-CJJC7SVB.js.map +1 -0
- package/dist/types-BaIpb0XT.d.ts +492 -0
- package/dist/types-BevOjfTY.d.ts +765 -0
- package/dist/types-C9j4qg6l.d.ts +500 -0
- package/dist/types-Cja5OTGh.d.ts +190 -0
- package/dist/util-MVgdwuIS.js +145 -0
- package/dist/util-MVgdwuIS.js.map +1 -0
- package/dist/workspace-archive-BosQiotA.js +575 -0
- package/dist/workspace-archive-BosQiotA.js.map +1 -0
- package/package.json +43 -55
- package/skills/build-with-agent-runtime/SKILL.md +16 -9
- package/skills/loop-writer/SKILL.md +1 -1
- package/dist/activation-Cm-9MMX1.d.ts +0 -69
- package/dist/analyst-loop.js.map +0 -1
- package/dist/backends-BO4Jqoe2.d.ts +0 -134
- package/dist/candidate-execution/index.js.map +0 -1
- package/dist/chunk-2KGAN2HM.js +0 -1256
- package/dist/chunk-2KGAN2HM.js.map +0 -1
- package/dist/chunk-2QJSNOGK.js +0 -48
- package/dist/chunk-2QJSNOGK.js.map +0 -1
- package/dist/chunk-2UABGGUB.js +0 -474
- package/dist/chunk-2UABGGUB.js.map +0 -1
- package/dist/chunk-2VZYRB55.js +0 -834
- package/dist/chunk-2VZYRB55.js.map +0 -1
- package/dist/chunk-2X65HYMI.js +0 -3526
- package/dist/chunk-2X65HYMI.js.map +0 -1
- package/dist/chunk-3MDZX7YU.js +0 -125
- package/dist/chunk-3MDZX7YU.js.map +0 -1
- package/dist/chunk-3PPVCS75.js +0 -199
- package/dist/chunk-3PPVCS75.js.map +0 -1
- package/dist/chunk-5HNDYWUT.js +0 -6424
- package/dist/chunk-5HNDYWUT.js.map +0 -1
- package/dist/chunk-5IBQAPVB.js +0 -29
- package/dist/chunk-5IBQAPVB.js.map +0 -1
- package/dist/chunk-5QOB7H74.js +0 -387
- package/dist/chunk-5QOB7H74.js.map +0 -1
- package/dist/chunk-ATDFUJYI.js +0 -4728
- package/dist/chunk-ATDFUJYI.js.map +0 -1
- package/dist/chunk-B7K7V22Y.js +0 -699
- package/dist/chunk-B7K7V22Y.js.map +0 -1
- package/dist/chunk-DPEUKJRO.js +0 -27
- package/dist/chunk-DPEUKJRO.js.map +0 -1
- package/dist/chunk-FD2MBMOH.js +0 -494
- package/dist/chunk-FD2MBMOH.js.map +0 -1
- package/dist/chunk-FJI3EXU3.js +0 -2402
- package/dist/chunk-FJI3EXU3.js.map +0 -1
- package/dist/chunk-HAO4CXHP.js +0 -200
- package/dist/chunk-HAO4CXHP.js.map +0 -1
- package/dist/chunk-ISPWRSEC.js +0 -408
- package/dist/chunk-ISPWRSEC.js.map +0 -1
- package/dist/chunk-KRBFHMV6.js +0 -1228
- package/dist/chunk-KRBFHMV6.js.map +0 -1
- package/dist/chunk-L5DST3QC.js +0 -351
- package/dist/chunk-L5DST3QC.js.map +0 -1
- package/dist/chunk-M22PL4GH.js +0 -171
- package/dist/chunk-M22PL4GH.js.map +0 -1
- package/dist/chunk-NURIQBJQ.js +0 -72
- package/dist/chunk-NURIQBJQ.js.map +0 -1
- package/dist/chunk-PZZKQVQV.js +0 -52
- package/dist/chunk-PZZKQVQV.js.map +0 -1
- package/dist/chunk-Q2JSAVQ3.js +0 -671
- package/dist/chunk-Q2JSAVQ3.js.map +0 -1
- package/dist/chunk-SBTWKPVR.js +0 -5576
- package/dist/chunk-SBTWKPVR.js.map +0 -1
- package/dist/chunk-SGKPNBXE.js +0 -60
- package/dist/chunk-SGKPNBXE.js.map +0 -1
- package/dist/chunk-SNSMRT6H.js +0 -680
- package/dist/chunk-SNSMRT6H.js.map +0 -1
- package/dist/chunk-STJEHM5F.js +0 -1338
- package/dist/chunk-STJEHM5F.js.map +0 -1
- package/dist/chunk-UPWGXKXB.js +0 -254
- package/dist/chunk-UPWGXKXB.js.map +0 -1
- package/dist/chunk-YEJR7IXO.js +0 -68
- package/dist/chunk-YEJR7IXO.js.map +0 -1
- package/dist/chunk-ZXICDSAK.js +0 -907
- package/dist/chunk-ZXICDSAK.js.map +0 -1
- package/dist/completion-gate-Bv47cOnn.d.ts +0 -256
- package/dist/conversation.js.map +0 -1
- package/dist/coordination-CxtzMxSM.d.ts +0 -2570
- package/dist/environment-provider.js.map +0 -1
- package/dist/improve--2A0gK1-.d.ts +0 -440
- package/dist/improvement-adapter-HAZz-7vK.d.ts +0 -203
- package/dist/knowledge.js.map +0 -1
- package/dist/local-harness-CtK9dSny.d.ts +0 -175
- package/dist/loop-runner-bin-DUOGtvOl.d.ts +0 -207
- package/dist/loop-runner-bin.js.map +0 -1
- package/dist/loops.js.map +0 -1
- package/dist/openai-tools-fnj6SRVg.d.ts +0 -46
- package/dist/protected-model-port-DzkUY3HQ.d.ts +0 -271
- package/dist/sanitize-DEbPNtyI.d.ts +0 -328
- package/dist/structural-rollout-C4mabf87.d.ts +0 -446
- package/dist/supervise-7q9F5brx.d.ts +0 -379
- package/dist/types-BCoemcXU.d.ts +0 -802
- package/dist/types-BwoZWq-i.d.ts +0 -510
- package/dist/types-Cbp72qyu.d.ts +0 -491
- package/dist/types-CmYCMbFT.d.ts +0 -203
- package/dist/types-CvNwMzQt.d.ts +0 -957
- package/dist/worktree-fanout-WsOzt8qu.d.ts +0 -999
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { u as ValidationError } from "./errors-DEAvWQPy.js";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
//#region src/mcp/tool-server.ts
|
|
4
|
+
/**
|
|
5
|
+
* `createStdioToolServer` — the generic newline-delimited JSON-RPC 2.0 MCP
|
|
6
|
+
* server core: `initialize` / `notifications/initialized` / `tools/list` /
|
|
7
|
+
* `tools/call` over a stdio-shaped transport, protocol 2024-11-05.
|
|
8
|
+
*
|
|
9
|
+
* Extracted from `createMcpServer` (server.ts) so every in-repo MCP server —
|
|
10
|
+
* the delegation server, the memory server — serves the ONE wire protocol the
|
|
11
|
+
* same-host client (`connectStdioMcp`, runtime/stdio-mcp-client.ts) speaks.
|
|
12
|
+
* A second hand-rolled serve loop could drift from what the client expects;
|
|
13
|
+
* this core makes that impossible by construction.
|
|
14
|
+
*
|
|
15
|
+
* @experimental
|
|
16
|
+
*/
|
|
17
|
+
const PROTOCOL_VERSION = "2024-11-05";
|
|
18
|
+
/** Build the generic stdio JSON-RPC tool server. */
|
|
19
|
+
function createStdioToolServer(options) {
|
|
20
|
+
const tools = /* @__PURE__ */ new Map();
|
|
21
|
+
for (const tool of options.tools) {
|
|
22
|
+
if (tools.has(tool.name)) throw new ValidationError(`createStdioToolServer: duplicate tool name "${tool.name}"`);
|
|
23
|
+
tools.set(tool.name, tool);
|
|
24
|
+
}
|
|
25
|
+
let stopped = false;
|
|
26
|
+
let activeReadline;
|
|
27
|
+
async function handle(message) {
|
|
28
|
+
if (stopped) return rpcError(message.id ?? null, -32099, "server stopped");
|
|
29
|
+
if (message.method === "initialize") return rpcResult(message.id ?? null, {
|
|
30
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
31
|
+
capabilities: { tools: {} },
|
|
32
|
+
serverInfo: {
|
|
33
|
+
name: options.serverName,
|
|
34
|
+
version: options.serverVersion
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
if (message.method === "notifications/initialized") return null;
|
|
38
|
+
if (message.method === "tools/list") return rpcResult(message.id ?? null, { tools: [...tools.values()].map((tool) => ({
|
|
39
|
+
name: tool.name,
|
|
40
|
+
description: tool.description,
|
|
41
|
+
inputSchema: tool.inputSchema
|
|
42
|
+
})) });
|
|
43
|
+
if (message.method === "tools/call") {
|
|
44
|
+
const params = message.params ?? {};
|
|
45
|
+
const name = typeof params.name === "string" ? params.name : "";
|
|
46
|
+
const tool = tools.get(name);
|
|
47
|
+
if (!tool) return rpcError(message.id ?? null, -32601, `unknown tool: ${name}`);
|
|
48
|
+
try {
|
|
49
|
+
const output = await tool.handler(params.arguments ?? {});
|
|
50
|
+
return rpcResult(message.id ?? null, {
|
|
51
|
+
content: [{
|
|
52
|
+
type: "text",
|
|
53
|
+
text: JSON.stringify(output)
|
|
54
|
+
}],
|
|
55
|
+
structuredContent: output,
|
|
56
|
+
isError: false
|
|
57
|
+
});
|
|
58
|
+
} catch (err) {
|
|
59
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
60
|
+
const code = err instanceof TypeError || err instanceof RangeError ? -32602 : -32e3;
|
|
61
|
+
return rpcError(message.id ?? null, code, reason);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (message.id === void 0 || message.id === null) return null;
|
|
65
|
+
return rpcError(message.id, -32601, `unknown method: ${message.method}`);
|
|
66
|
+
}
|
|
67
|
+
async function serve(transport) {
|
|
68
|
+
const input = transport?.input ?? process.stdin;
|
|
69
|
+
const output = transport?.output ?? process.stdout;
|
|
70
|
+
const rl = createInterface({
|
|
71
|
+
input,
|
|
72
|
+
crlfDelay: Number.POSITIVE_INFINITY
|
|
73
|
+
});
|
|
74
|
+
activeReadline = rl;
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
rl.on("line", (line) => {
|
|
77
|
+
const trimmed = line.trim();
|
|
78
|
+
if (!trimmed) return;
|
|
79
|
+
let parsed;
|
|
80
|
+
try {
|
|
81
|
+
parsed = JSON.parse(trimmed);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
writeResponse(output, rpcError(null, -32700, `parse error: ${err.message}`));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (parsed?.jsonrpc !== "2.0" || typeof parsed.method !== "string") {
|
|
87
|
+
writeResponse(output, rpcError(parsed?.id ?? null, -32600, "invalid request"));
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
handle(parsed).then((response) => {
|
|
91
|
+
if (response) writeResponse(output, response);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
rl.on("close", () => resolve());
|
|
95
|
+
rl.on("error", (err) => reject(err));
|
|
96
|
+
if (stopped) {
|
|
97
|
+
rl.close();
|
|
98
|
+
resolve();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
function stop() {
|
|
103
|
+
stopped = true;
|
|
104
|
+
activeReadline?.close();
|
|
105
|
+
activeReadline = void 0;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
tools,
|
|
109
|
+
handle,
|
|
110
|
+
serve,
|
|
111
|
+
stop
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function rpcResult(id, result) {
|
|
115
|
+
return {
|
|
116
|
+
jsonrpc: "2.0",
|
|
117
|
+
id,
|
|
118
|
+
result
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function rpcError(id, code, message, data) {
|
|
122
|
+
return {
|
|
123
|
+
jsonrpc: "2.0",
|
|
124
|
+
id,
|
|
125
|
+
error: data === void 0 ? {
|
|
126
|
+
code,
|
|
127
|
+
message
|
|
128
|
+
} : {
|
|
129
|
+
code,
|
|
130
|
+
message,
|
|
131
|
+
data
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function writeResponse(output, response) {
|
|
136
|
+
output.write(`${JSON.stringify(response)}\n`);
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
export { createStdioToolServer as t };
|
|
140
|
+
|
|
141
|
+
//# sourceMappingURL=tool-server-RcWgLIsL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-server-RcWgLIsL.js","names":[],"sources":["../src/mcp/tool-server.ts"],"sourcesContent":["/**\n * `createStdioToolServer` — the generic newline-delimited JSON-RPC 2.0 MCP\n * server core: `initialize` / `notifications/initialized` / `tools/list` /\n * `tools/call` over a stdio-shaped transport, protocol 2024-11-05.\n *\n * Extracted from `createMcpServer` (server.ts) so every in-repo MCP server —\n * the delegation server, the memory server — serves the ONE wire protocol the\n * same-host client (`connectStdioMcp`, runtime/stdio-mcp-client.ts) speaks.\n * A second hand-rolled serve loop could drift from what the client expects;\n * this core makes that impossible by construction.\n *\n * @experimental\n */\n\nimport { createInterface, type Interface as ReadlineInterface } from 'node:readline'\nimport { ValidationError } from '../errors'\nimport type { JsonRpcMessage, JsonRpcResponse, McpToolDescriptor, McpTransport } from './protocol'\n\nexport type { JsonRpcMessage, JsonRpcResponse, McpToolDescriptor, McpTransport } from './protocol'\n\nconst PROTOCOL_VERSION = '2024-11-05'\n\n/** @experimental */\nexport interface StdioToolServerOptions {\n /** Server display name surfaced via `initialize`. */\n serverName: string\n /** Server version surfaced via `initialize`. */\n serverVersion: string\n /** The tools to serve. Duplicate names throw — a silent shadow would hide a tool. */\n tools: readonly McpToolDescriptor[]\n}\n\n/** @experimental */\nexport interface StdioToolServer {\n /** Tools currently registered, keyed by name. */\n readonly tools: ReadonlyMap<string, McpToolDescriptor>\n /** Handle a single parsed JSON-RPC message. Returns the response object (or `null` for notifications). */\n handle(message: JsonRpcMessage): Promise<JsonRpcResponse | null>\n /** Drive the server on a stdio-shaped transport until `stop()` is called. */\n serve(transport?: McpTransport): Promise<void>\n /** Stop a `serve` call. Subsequent requests are rejected. */\n stop(): void\n}\n\n/** Build the generic stdio JSON-RPC tool server. */\nexport function createStdioToolServer(options: StdioToolServerOptions): StdioToolServer {\n const tools = new Map<string, McpToolDescriptor>()\n for (const tool of options.tools) {\n if (tools.has(tool.name)) {\n throw new ValidationError(`createStdioToolServer: duplicate tool name \"${tool.name}\"`)\n }\n tools.set(tool.name, tool)\n }\n\n let stopped = false\n let activeReadline: ReadlineInterface | undefined\n\n async function handle(message: JsonRpcMessage): Promise<JsonRpcResponse | null> {\n if (stopped) {\n return rpcError(message.id ?? null, -32099, 'server stopped')\n }\n if (message.method === 'initialize') {\n return rpcResult(message.id ?? null, {\n protocolVersion: PROTOCOL_VERSION,\n capabilities: { tools: {} },\n serverInfo: { name: options.serverName, version: options.serverVersion },\n })\n }\n if (message.method === 'notifications/initialized') {\n // MCP clients send this after the handshake; it has no id and expects\n // no response.\n return null\n }\n if (message.method === 'tools/list') {\n return rpcResult(message.id ?? null, {\n tools: [...tools.values()].map((tool) => ({\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema,\n })),\n })\n }\n if (message.method === 'tools/call') {\n const params = (message.params ?? {}) as { name?: unknown; arguments?: unknown }\n const name = typeof params.name === 'string' ? params.name : ''\n const tool = tools.get(name)\n if (!tool) {\n return rpcError(message.id ?? null, -32601, `unknown tool: ${name}`)\n }\n try {\n const output = await tool.handler(params.arguments ?? {})\n return rpcResult(message.id ?? null, {\n content: [{ type: 'text', text: JSON.stringify(output) }],\n structuredContent: output,\n isError: false,\n })\n } catch (err) {\n const reason = err instanceof Error ? err.message : String(err)\n const code = err instanceof TypeError || err instanceof RangeError ? -32602 : -32000\n return rpcError(message.id ?? null, code, reason)\n }\n }\n if (message.id === undefined || message.id === null) return null\n return rpcError(message.id, -32601, `unknown method: ${message.method}`)\n }\n\n async function serve(transport?: McpTransport): Promise<void> {\n const input = transport?.input ?? process.stdin\n const output = transport?.output ?? process.stdout\n const rl = createInterface({ input, crlfDelay: Number.POSITIVE_INFINITY })\n activeReadline = rl\n return new Promise<void>((resolve, reject) => {\n rl.on('line', (line) => {\n const trimmed = line.trim()\n if (!trimmed) return\n let parsed: JsonRpcMessage | undefined\n try {\n parsed = JSON.parse(trimmed) as JsonRpcMessage\n } catch (err) {\n writeResponse(output, rpcError(null, -32700, `parse error: ${(err as Error).message}`))\n return\n }\n if (parsed?.jsonrpc !== '2.0' || typeof parsed.method !== 'string') {\n writeResponse(output, rpcError(parsed?.id ?? null, -32600, 'invalid request'))\n return\n }\n void handle(parsed).then((response) => {\n if (response) writeResponse(output, response)\n })\n })\n rl.on('close', () => resolve())\n rl.on('error', (err) => reject(err))\n if (stopped) {\n rl.close()\n resolve()\n }\n })\n }\n\n function stop(): void {\n stopped = true\n activeReadline?.close()\n activeReadline = undefined\n }\n\n return { tools, handle, serve, stop }\n}\n\nfunction rpcResult(id: number | string | null, result: unknown): JsonRpcResponse {\n return { jsonrpc: '2.0', id, result }\n}\n\nfunction rpcError(\n id: number | string | null,\n code: number,\n message: string,\n data?: unknown,\n): JsonRpcResponse {\n return {\n jsonrpc: '2.0',\n id,\n error: data === undefined ? { code, message } : { code, message, data },\n }\n}\n\nfunction writeResponse(output: NodeJS.WritableStream, response: JsonRpcResponse): void {\n output.write(`${JSON.stringify(response)}\\n`)\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { a as createOtelExporter, n as buildLoopOtelSpans } from "./otel-export-D34IQW1C.js";
|
|
2
|
+
//#region src/mcp/trace-propagation.ts
|
|
3
|
+
/**
|
|
4
|
+
* Read trace context from the process environment.
|
|
5
|
+
* Returns a context with inherited ids or a freshly generated root.
|
|
6
|
+
*/
|
|
7
|
+
function readTraceContextFromEnv() {
|
|
8
|
+
return {
|
|
9
|
+
traceId: process.env.TRACE_ID || generateTraceId(),
|
|
10
|
+
parentSpanId: process.env.PARENT_SPAN_ID || void 0
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create a LoopTraceEmitter that:
|
|
15
|
+
* 1. Parents all spans under the inherited PARENT_SPAN_ID.
|
|
16
|
+
* 2. Exports spans to OTEL when OTEL_EXPORTER_OTLP_ENDPOINT is set.
|
|
17
|
+
*
|
|
18
|
+
* Returns both the emitter and the optional exporter handle for shutdown.
|
|
19
|
+
*/
|
|
20
|
+
function createPropagatingTraceEmitter(ctx) {
|
|
21
|
+
const exporter = createOtelExporter();
|
|
22
|
+
const buffers = /* @__PURE__ */ new Map();
|
|
23
|
+
return {
|
|
24
|
+
emitter: { emit(event) {
|
|
25
|
+
if (!exporter) return;
|
|
26
|
+
const buf = buffers.get(event.runId);
|
|
27
|
+
if (buf) buf.push(event);
|
|
28
|
+
else buffers.set(event.runId, [event]);
|
|
29
|
+
if (event.kind === "loop.ended") {
|
|
30
|
+
const events = buffers.get(event.runId) ?? [event];
|
|
31
|
+
buffers.delete(event.runId);
|
|
32
|
+
for (const span of buildLoopOtelSpans(events, ctx.traceId, ctx.parentSpanId)) exporter.exportSpan(span);
|
|
33
|
+
}
|
|
34
|
+
} },
|
|
35
|
+
exporter,
|
|
36
|
+
context: ctx
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Build env vars to pass to a child MCP subprocess so it inherits the
|
|
41
|
+
* current trace context.
|
|
42
|
+
*/
|
|
43
|
+
function traceContextToEnv(ctx) {
|
|
44
|
+
const env = { TRACE_ID: ctx.traceId };
|
|
45
|
+
if (ctx.parentSpanId) env.PARENT_SPAN_ID = ctx.parentSpanId;
|
|
46
|
+
return env;
|
|
47
|
+
}
|
|
48
|
+
function generateTraceId() {
|
|
49
|
+
const bytes = /* @__PURE__ */ new Uint8Array(16);
|
|
50
|
+
if (typeof globalThis.crypto?.getRandomValues === "function") globalThis.crypto.getRandomValues(bytes);
|
|
51
|
+
else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
52
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { readTraceContextFromEnv as n, traceContextToEnv as r, createPropagatingTraceEmitter as t };
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=trace-propagation-CJJC7SVB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace-propagation-CJJC7SVB.js","names":[],"sources":["../src/mcp/trace-propagation.ts"],"sourcesContent":["/**\n *\n * Trace context propagation for MCP subprocess.\n *\n * When the MCP server is launched as a child process by a sandbox harness,\n * the parent passes trace context via environment variables:\n *\n * TRACE_ID=<current-run-trace-id>\n * PARENT_SPAN_ID=<span-that-dispatched-the-delegation>\n *\n * The MCP server reads these at startup and uses them as the root of its\n * internal trace tree. All spans emitted by `runAgentRounds` invocations inside\n * the MCP are children of the parent's delegation span.\n *\n * When these env vars are absent, the MCP generates a fresh trace root —\n * the server operates standalone without trace joining.\n *\n * @experimental\n */\n\nimport type { OtelExporter } from '../otel-export'\nimport { buildLoopOtelSpans, createOtelExporter } from '../otel-export'\nimport type { LoopTraceEmitter, LoopTraceEvent } from '../runtime/types'\n\nexport interface TraceContext {\n /** Trace id inherited from the parent process, or a fresh one. */\n traceId: string\n /** Parent span id from the delegation that launched this MCP server. */\n parentSpanId?: string\n}\n\n/**\n * Read trace context from the process environment.\n * Returns a context with inherited ids or a freshly generated root.\n */\nexport function readTraceContextFromEnv(): TraceContext {\n const traceId = process.env.TRACE_ID || generateTraceId()\n const parentSpanId = process.env.PARENT_SPAN_ID || undefined\n return { traceId, parentSpanId }\n}\n\n/**\n * Create a LoopTraceEmitter that:\n * 1. Parents all spans under the inherited PARENT_SPAN_ID.\n * 2. Exports spans to OTEL when OTEL_EXPORTER_OTLP_ENDPOINT is set.\n *\n * Returns both the emitter and the optional exporter handle for shutdown.\n */\nexport function createPropagatingTraceEmitter(ctx: TraceContext): {\n emitter: LoopTraceEmitter\n exporter: OtelExporter | undefined\n context: TraceContext\n} {\n const exporter = createOtelExporter()\n\n // Buffer events per loop run, then emit the full nested span tree on\n // `loop.ended` so the topology hierarchy (loop → round → branch) reaches the\n // OTLP collector — not a flat list of zero-duration point spans. A run that\n // never reaches `loop.ended` (hard abort) drops its buffer; acceptable for\n // the short-lived MCP subprocess.\n const buffers = new Map<string, LoopTraceEvent[]>()\n\n const emitter: LoopTraceEmitter = {\n emit(event: LoopTraceEvent) {\n if (!exporter) return\n const buf = buffers.get(event.runId)\n if (buf) buf.push(event)\n else buffers.set(event.runId, [event])\n if (event.kind === 'loop.ended') {\n const events = buffers.get(event.runId) ?? [event]\n buffers.delete(event.runId)\n for (const span of buildLoopOtelSpans(events, ctx.traceId, ctx.parentSpanId)) {\n exporter.exportSpan(span)\n }\n }\n },\n }\n\n return { emitter, exporter, context: ctx }\n}\n\n/**\n * Build env vars to pass to a child MCP subprocess so it inherits the\n * current trace context.\n */\nexport function traceContextToEnv(ctx: TraceContext): Record<string, string> {\n const env: Record<string, string> = { TRACE_ID: ctx.traceId }\n if (ctx.parentSpanId) env.PARENT_SPAN_ID = ctx.parentSpanId\n return env\n}\n\nfunction generateTraceId(): string {\n const bytes = new Uint8Array(16)\n if (typeof globalThis.crypto?.getRandomValues === 'function') {\n globalThis.crypto.getRandomValues(bytes)\n } else {\n for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256)\n }\n return Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, '0'))\n .join('')\n}\n"],"mappings":";;;;;;AAmCA,SAAgB,0BAAwC;CAGtD,OAAO;EAAE,SAFO,QAAQ,IAAI,YAAY,gBAAgB;EAEtC,cADG,QAAQ,IAAI,kBAAkB,KAAA;CACpB;AACjC;;;;;;;;AASA,SAAgB,8BAA8B,KAI5C;CACA,MAAM,WAAW,mBAAmB;CAOpC,MAAM,0BAAU,IAAI,IAA8B;CAkBlD,OAAO;EAAE,SAAA,EAfP,KAAK,OAAuB;GAC1B,IAAI,CAAC,UAAU;GACf,MAAM,MAAM,QAAQ,IAAI,MAAM,KAAK;GACnC,IAAI,KAAK,IAAI,KAAK,KAAK;QAClB,QAAQ,IAAI,MAAM,OAAO,CAAC,KAAK,CAAC;GACrC,IAAI,MAAM,SAAS,cAAc;IAC/B,MAAM,SAAS,QAAQ,IAAI,MAAM,KAAK,KAAK,CAAC,KAAK;IACjD,QAAQ,OAAO,MAAM,KAAK;IAC1B,KAAK,MAAM,QAAQ,mBAAmB,QAAQ,IAAI,SAAS,IAAI,YAAY,GACzE,SAAS,WAAW,IAAI;GAE5B;EACF,EAGa;EAAG;EAAU,SAAS;CAAI;AAC3C;;;;;AAMA,SAAgB,kBAAkB,KAA2C;CAC3E,MAAM,MAA8B,EAAE,UAAU,IAAI,QAAQ;CAC5D,IAAI,IAAI,cAAc,IAAI,iBAAiB,IAAI;CAC/C,OAAO;AACT;AAEA,SAAS,kBAA0B;CACjC,MAAM,wBAAQ,IAAI,WAAW,EAAE;CAC/B,IAAI,OAAO,WAAW,QAAQ,oBAAoB,YAChD,WAAW,OAAO,gBAAgB,KAAK;MAEvC,KAAK,IAAI,IAAI,GAAG,IAAI,IAAI,KAAK,MAAM,KAAK,KAAK,MAAM,KAAK,OAAO,IAAI,GAAG;CAExE,OAAO,MAAM,KAAK,KAAK,CAAC,CACrB,KAAK,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,CAC3C,KAAK,EAAE;AACZ"}
|