fascicle 0.8.1 → 0.8.10
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 +91 -0
- package/README.md +34 -4
- package/dist/{tee-B-MeNi7d.js → adapters-BS8cBOjL.js} +79 -7
- package/dist/adapters-BS8cBOjL.js.map +1 -0
- package/dist/adapters.d.ts +8 -8
- package/dist/adapters.js +1 -77
- package/dist/core-dRSAN1Q2.js +1846 -0
- package/dist/core-dRSAN1Q2.js.map +1 -0
- package/dist/index-CEj6hY9z.d.ts +856 -0
- package/dist/index-Cy7EUeiP.d.ts +430 -0
- package/dist/index.d.ts +24 -452
- package/dist/index.js +116 -1918
- package/dist/index.js.map +1 -1
- package/dist/mcp.d.ts +78 -0
- package/dist/mcp.js +315 -0
- package/dist/mcp.js.map +1 -0
- package/dist/static/viewer.html +1 -1
- package/package.json +54 -23
- package/dist/adapters.js.map +0 -1
- package/dist/index-BjOnbuc9.d.ts +0 -710
- package/dist/tee-B-MeNi7d.js.map +0 -1
package/dist/mcp.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { G as Step, J as TrajectoryLogger } from "./index-CEj6hY9z.js";
|
|
2
|
+
import { P as Tool } from "./index-Cy7EUeiP.js";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
|
+
|
|
7
|
+
//#region src/mcp/client.d.ts
|
|
8
|
+
type McpStdioConfig = {
|
|
9
|
+
transport: 'stdio';
|
|
10
|
+
command: string;
|
|
11
|
+
args?: string[];
|
|
12
|
+
env?: Record<string, string>;
|
|
13
|
+
cwd?: string;
|
|
14
|
+
};
|
|
15
|
+
type McpStreamableHttpConfig = {
|
|
16
|
+
transport: 'http';
|
|
17
|
+
url: string;
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
};
|
|
20
|
+
type McpExistingClientConfig = {
|
|
21
|
+
transport: 'client';
|
|
22
|
+
client: Client;
|
|
23
|
+
};
|
|
24
|
+
type McpClientConfig = McpStdioConfig | McpStreamableHttpConfig | McpExistingClientConfig;
|
|
25
|
+
type McpClientOptions = {
|
|
26
|
+
client_info?: {
|
|
27
|
+
name: string;
|
|
28
|
+
version: string;
|
|
29
|
+
};
|
|
30
|
+
include?: ReadonlyArray<string>;
|
|
31
|
+
default_description?: (tool_name: string) => string;
|
|
32
|
+
};
|
|
33
|
+
type McpClientHandle = {
|
|
34
|
+
readonly tools: ReadonlyArray<Tool>;
|
|
35
|
+
readonly close: () => Promise<void>;
|
|
36
|
+
};
|
|
37
|
+
declare function mcp_client(config: McpClientConfig, options?: McpClientOptions): Promise<McpClientHandle>;
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/mcp/serve.d.ts
|
|
40
|
+
type ServeFlowOptions<i, o> = {
|
|
41
|
+
server: McpServer;
|
|
42
|
+
flow: Step<i, o>;
|
|
43
|
+
name: string;
|
|
44
|
+
description: string;
|
|
45
|
+
input_schema: z.ZodType<i>;
|
|
46
|
+
trajectory?: TrajectoryLogger;
|
|
47
|
+
to_result?: (output: o) => {
|
|
48
|
+
text: string;
|
|
49
|
+
structured?: Record<string, unknown>;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
declare function serve_flow<i, o>(options: ServeFlowOptions<i, o>): void;
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/mcp/schema_bridge.d.ts
|
|
55
|
+
declare function json_schema_to_zod(schema: unknown): z.ZodType;
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/mcp/errors.d.ts
|
|
58
|
+
/**
|
|
59
|
+
* Typed errors for the MCP adapter.
|
|
60
|
+
*
|
|
61
|
+
* Listed alongside src/core/errors.ts and src/engine/errors.ts in the
|
|
62
|
+
* `no-class` rule ignores: `instanceof` branching is how callers distinguish a
|
|
63
|
+
* missing optional peer from a tool-level failure surfaced over the wire.
|
|
64
|
+
*/
|
|
65
|
+
declare class mcp_sdk_missing_error extends Error {
|
|
66
|
+
readonly kind: "mcp_sdk_missing_error";
|
|
67
|
+
constructor(message?: string);
|
|
68
|
+
}
|
|
69
|
+
declare class mcp_error extends Error {
|
|
70
|
+
readonly kind: "mcp_error";
|
|
71
|
+
readonly tool_name: string | undefined;
|
|
72
|
+
constructor(message: string, metadata?: {
|
|
73
|
+
tool_name?: string;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { type McpClientConfig, type McpClientHandle, type McpClientOptions, type McpExistingClientConfig, type McpStdioConfig, type McpStreamableHttpConfig, type ServeFlowOptions, json_schema_to_zod, mcp_client, mcp_error, mcp_sdk_missing_error, serve_flow };
|
|
78
|
+
//# sourceMappingURL=mcp.d.ts.map
|
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { T as run } from "./core-dRSAN1Q2.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
//#region src/mcp/errors.ts
|
|
4
|
+
/**
|
|
5
|
+
* Typed errors for the MCP adapter.
|
|
6
|
+
*
|
|
7
|
+
* Listed alongside src/core/errors.ts and src/engine/errors.ts in the
|
|
8
|
+
* `no-class` rule ignores: `instanceof` branching is how callers distinguish a
|
|
9
|
+
* missing optional peer from a tool-level failure surfaced over the wire.
|
|
10
|
+
*/
|
|
11
|
+
var mcp_sdk_missing_error = class extends Error {
|
|
12
|
+
kind = "mcp_sdk_missing_error";
|
|
13
|
+
constructor(message = "@modelcontextprotocol/sdk is required for fascicle/mcp; install it: pnpm add @modelcontextprotocol/sdk") {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "mcp_sdk_missing_error";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var mcp_error = class extends Error {
|
|
19
|
+
kind = "mcp_error";
|
|
20
|
+
tool_name;
|
|
21
|
+
constructor(message, metadata = {}) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = "mcp_error";
|
|
24
|
+
this.tool_name = metadata.tool_name;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/mcp/internal.ts
|
|
29
|
+
/**
|
|
30
|
+
* Small shared guards for the MCP adapter. Kept in one place so the client,
|
|
31
|
+
* schema bridge, and result mapping do not each carry a copy.
|
|
32
|
+
*/
|
|
33
|
+
function is_record(value) {
|
|
34
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
|
+
}
|
|
36
|
+
function as_record(value) {
|
|
37
|
+
return is_record(value) ? value : void 0;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/mcp/result_mapping.ts
|
|
41
|
+
/**
|
|
42
|
+
* Mapping between MCP `CallToolResult` payloads and fascicle tool values.
|
|
43
|
+
*
|
|
44
|
+
* Inbound (`call_result_to_output`): a remote tool result becomes the value a
|
|
45
|
+
* fascicle `Tool.execute` returns. `isError: true` is a tool-level failure, so
|
|
46
|
+
* it throws rather than returns, letting the tool loop's error policy feed it
|
|
47
|
+
* back to the model. Structured output is preferred over text when present.
|
|
48
|
+
*
|
|
49
|
+
* Outbound (`output_to_call_result`): a fascicle flow's output becomes a
|
|
50
|
+
* `CallToolResult` for `serve_flow`. Objects are emitted as both a JSON text
|
|
51
|
+
* part (every MCP host can read it) and `structuredContent` (typed hosts get
|
|
52
|
+
* the object).
|
|
53
|
+
*/
|
|
54
|
+
function call_result_to_output(result, tool_name) {
|
|
55
|
+
const record = as_record(result) ?? {};
|
|
56
|
+
if (record["isError"] === true) throw new mcp_error(join_text(record["content"]) ?? "MCP tool returned an error", tool_name !== void 0 ? { tool_name } : {});
|
|
57
|
+
if (is_record(record["structuredContent"])) return record["structuredContent"];
|
|
58
|
+
const text = join_text(record["content"]);
|
|
59
|
+
if (text !== void 0) return text;
|
|
60
|
+
return record["content"] ?? null;
|
|
61
|
+
}
|
|
62
|
+
function output_to_call_result(output, to_result) {
|
|
63
|
+
if (to_result !== void 0) {
|
|
64
|
+
const shaped = to_result(output);
|
|
65
|
+
return build_result(shaped.text, shaped.structured);
|
|
66
|
+
}
|
|
67
|
+
if (typeof output === "string") return build_result(output);
|
|
68
|
+
if (is_record(output)) return build_result(stringify(output), output);
|
|
69
|
+
if (Array.isArray(output)) return build_result(stringify(output));
|
|
70
|
+
return build_result(String(output));
|
|
71
|
+
}
|
|
72
|
+
function build_result(text, structured) {
|
|
73
|
+
return structured !== void 0 ? {
|
|
74
|
+
content: [{
|
|
75
|
+
type: "text",
|
|
76
|
+
text
|
|
77
|
+
}],
|
|
78
|
+
structuredContent: structured
|
|
79
|
+
} : { content: [{
|
|
80
|
+
type: "text",
|
|
81
|
+
text
|
|
82
|
+
}] };
|
|
83
|
+
}
|
|
84
|
+
function join_text(content) {
|
|
85
|
+
if (!Array.isArray(content)) return void 0;
|
|
86
|
+
const parts = [];
|
|
87
|
+
for (const part of content) if (is_record(part) && part["type"] === "text" && typeof part["text"] === "string") parts.push(part["text"]);
|
|
88
|
+
return parts.length > 0 ? parts.join("\n") : void 0;
|
|
89
|
+
}
|
|
90
|
+
function stringify(value) {
|
|
91
|
+
try {
|
|
92
|
+
return JSON.stringify(value);
|
|
93
|
+
} catch {
|
|
94
|
+
return String(value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/mcp/schema_bridge.ts
|
|
99
|
+
/**
|
|
100
|
+
* JSON Schema to Zod conversion for inbound MCP tools.
|
|
101
|
+
*
|
|
102
|
+
* An MCP tool advertises its input as JSON Schema, but a fascicle `Tool` carries
|
|
103
|
+
* a Zod `input_schema` that the tool loop `safeParse`s before `execute` and the
|
|
104
|
+
* engine serializes back to JSON Schema for the provider. So the converted Zod
|
|
105
|
+
* must do two things at once: stay permissive enough never to reject an arg the
|
|
106
|
+
* remote server would accept, and round-trip through `z.toJSONSchema` to a shape
|
|
107
|
+
* the provider can fill.
|
|
108
|
+
*
|
|
109
|
+
* Two deliberate choices serve those goals. Objects are `looseObject` so extra
|
|
110
|
+
* keys survive to the server (which re-validates and is the real authority), and
|
|
111
|
+
* a freeform object maps to `z.looseObject({})` (emits `{type:"object"}`) rather
|
|
112
|
+
* than `z.unknown()` (emits `{}`), preserving the "takes an object" signal. Value
|
|
113
|
+
* constraints (min/max, length, pattern, format) are intentionally not modeled:
|
|
114
|
+
* they add rejection risk for marginal provider benefit, and the server enforces
|
|
115
|
+
* them anyway. Structure, types, enums, const, unions, and descriptions are
|
|
116
|
+
* preserved; anything unrecognized degrades to `z.unknown()`.
|
|
117
|
+
*/
|
|
118
|
+
function json_schema_to_zod(schema) {
|
|
119
|
+
try {
|
|
120
|
+
return convert(schema);
|
|
121
|
+
} catch {
|
|
122
|
+
return z.unknown();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function convert(node) {
|
|
126
|
+
const schema = as_record(node);
|
|
127
|
+
if (schema === void 0) return z.unknown();
|
|
128
|
+
const described = (inner) => {
|
|
129
|
+
const description = schema["description"];
|
|
130
|
+
return typeof description === "string" ? inner.describe(description) : inner;
|
|
131
|
+
};
|
|
132
|
+
if ("const" in schema) return described(literal_of(schema["const"]));
|
|
133
|
+
if (Array.isArray(schema["enum"])) return described(enum_of(schema["enum"]));
|
|
134
|
+
if (Array.isArray(schema["anyOf"])) return described(union_of(schema["anyOf"]));
|
|
135
|
+
if (Array.isArray(schema["oneOf"])) return described(union_of(schema["oneOf"]));
|
|
136
|
+
if (Array.isArray(schema["allOf"])) return described(intersection_of(schema["allOf"]));
|
|
137
|
+
const type = schema["type"];
|
|
138
|
+
if (Array.isArray(type)) return described(union_of_types(type, schema));
|
|
139
|
+
if (typeof type === "string") return described(for_type(type, schema));
|
|
140
|
+
if (is_record(schema["properties"])) return described(build_object(schema));
|
|
141
|
+
return described(z.unknown());
|
|
142
|
+
}
|
|
143
|
+
function for_type(type, schema) {
|
|
144
|
+
switch (type) {
|
|
145
|
+
case "object": return build_object(schema);
|
|
146
|
+
case "array": return build_array(schema);
|
|
147
|
+
case "string": return z.string();
|
|
148
|
+
case "number": return z.number();
|
|
149
|
+
case "integer": return z.number().int();
|
|
150
|
+
case "boolean": return z.boolean();
|
|
151
|
+
case "null": return z.null();
|
|
152
|
+
default: return z.unknown();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function build_object(schema) {
|
|
156
|
+
const properties = as_record(schema["properties"]) ?? {};
|
|
157
|
+
const required = new Set(string_array(schema["required"]));
|
|
158
|
+
const shape = {};
|
|
159
|
+
for (const [key, prop] of Object.entries(properties)) {
|
|
160
|
+
const child = convert(prop);
|
|
161
|
+
shape[key] = required.has(key) ? child : child.optional();
|
|
162
|
+
}
|
|
163
|
+
return z.looseObject(shape);
|
|
164
|
+
}
|
|
165
|
+
function build_array(schema) {
|
|
166
|
+
return z.array(convert(schema["items"]));
|
|
167
|
+
}
|
|
168
|
+
function enum_of(values) {
|
|
169
|
+
if (values.length === 0) return z.unknown();
|
|
170
|
+
if (values.every((v) => typeof v === "string")) return z.enum(values);
|
|
171
|
+
return combine_union(values.map(literal_of));
|
|
172
|
+
}
|
|
173
|
+
function union_of(nodes) {
|
|
174
|
+
return combine_union(nodes.map(convert));
|
|
175
|
+
}
|
|
176
|
+
function union_of_types(types, schema) {
|
|
177
|
+
return combine_union(types.map((t) => for_type(String(t), schema)));
|
|
178
|
+
}
|
|
179
|
+
function intersection_of(nodes) {
|
|
180
|
+
const parts = nodes.map(convert);
|
|
181
|
+
if (parts.length === 0) return z.unknown();
|
|
182
|
+
return parts.reduce((acc, part) => z.intersection(acc, part));
|
|
183
|
+
}
|
|
184
|
+
function combine_union(variants) {
|
|
185
|
+
const [first, second, ...rest] = variants;
|
|
186
|
+
if (first === void 0) return z.unknown();
|
|
187
|
+
if (second === void 0) return first;
|
|
188
|
+
return z.union([
|
|
189
|
+
first,
|
|
190
|
+
second,
|
|
191
|
+
...rest
|
|
192
|
+
]);
|
|
193
|
+
}
|
|
194
|
+
function literal_of(value) {
|
|
195
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return z.literal(value);
|
|
196
|
+
if (value === null) return z.null();
|
|
197
|
+
return z.unknown();
|
|
198
|
+
}
|
|
199
|
+
function string_array(value) {
|
|
200
|
+
return Array.isArray(value) ? value.filter((v) => typeof v === "string") : [];
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region src/mcp/sdk_loader.ts
|
|
204
|
+
/**
|
|
205
|
+
* Lazy loader for the optional `@modelcontextprotocol/sdk` peer.
|
|
206
|
+
*
|
|
207
|
+
* `mcp_client` needs SDK values (the Client and transport constructors), so it
|
|
208
|
+
* imports them dynamically rather than statically: a consumer can import
|
|
209
|
+
* `fascicle/mcp` without the SDK installed and only pays the cost when they
|
|
210
|
+
* actually connect. A missing peer surfaces as a typed `mcp_sdk_missing_error`
|
|
211
|
+
* with install guidance, mirroring the engine's `load_optional_peer`.
|
|
212
|
+
*
|
|
213
|
+
* `serve_flow` is not covered here: it operates on a caller-constructed
|
|
214
|
+
* `McpServer`, so it needs only a type-only import and no runtime SDK load.
|
|
215
|
+
*/
|
|
216
|
+
async function load_client_sdk() {
|
|
217
|
+
try {
|
|
218
|
+
const [client_mod, stdio_mod, http_mod] = await Promise.all([
|
|
219
|
+
import("@modelcontextprotocol/sdk/client/index.js"),
|
|
220
|
+
import("@modelcontextprotocol/sdk/client/stdio.js"),
|
|
221
|
+
import("@modelcontextprotocol/sdk/client/streamableHttp.js")
|
|
222
|
+
]);
|
|
223
|
+
return {
|
|
224
|
+
Client: client_mod.Client,
|
|
225
|
+
StdioClientTransport: stdio_mod.StdioClientTransport,
|
|
226
|
+
StreamableHTTPClientTransport: http_mod.StreamableHTTPClientTransport
|
|
227
|
+
};
|
|
228
|
+
} catch (err) {
|
|
229
|
+
throw new mcp_sdk_missing_error(`@modelcontextprotocol/sdk is required for fascicle/mcp; install it: pnpm add @modelcontextprotocol/sdk (cause: ${err instanceof Error ? err.message : String(err)})`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
//#endregion
|
|
233
|
+
//#region src/mcp/client.ts
|
|
234
|
+
const DEFAULT_CLIENT_INFO = {
|
|
235
|
+
name: "fascicle",
|
|
236
|
+
version: "0.0.0"
|
|
237
|
+
};
|
|
238
|
+
async function mcp_client(config, options = {}) {
|
|
239
|
+
const owns_client = config.transport !== "client";
|
|
240
|
+
const client = await connect_client(config, options);
|
|
241
|
+
const include = options.include !== void 0 ? new Set(options.include) : void 0;
|
|
242
|
+
const listed = await client.listTools();
|
|
243
|
+
const tools = [];
|
|
244
|
+
for (const advertised of listed.tools) {
|
|
245
|
+
if (include !== void 0 && !include.has(advertised.name)) continue;
|
|
246
|
+
tools.push(to_fascicle_tool(client, advertised, options));
|
|
247
|
+
}
|
|
248
|
+
return {
|
|
249
|
+
tools,
|
|
250
|
+
close: async () => {
|
|
251
|
+
if (owns_client) await client.close();
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
async function connect_client(config, options) {
|
|
256
|
+
if (config.transport === "client") return config.client;
|
|
257
|
+
const sdk = await load_client_sdk();
|
|
258
|
+
const client = new sdk.Client(options.client_info ?? DEFAULT_CLIENT_INFO);
|
|
259
|
+
await client.connect(build_transport(sdk, config));
|
|
260
|
+
return client;
|
|
261
|
+
}
|
|
262
|
+
function build_transport(sdk, config) {
|
|
263
|
+
if (config.transport === "stdio") return new sdk.StdioClientTransport({
|
|
264
|
+
command: config.command,
|
|
265
|
+
...config.args !== void 0 ? { args: config.args } : {},
|
|
266
|
+
...config.env !== void 0 ? { env: config.env } : {},
|
|
267
|
+
...config.cwd !== void 0 ? { cwd: config.cwd } : {}
|
|
268
|
+
});
|
|
269
|
+
const opts = config.headers !== void 0 ? { requestInit: { headers: config.headers } } : {};
|
|
270
|
+
return new sdk.StreamableHTTPClientTransport(new URL(config.url), opts);
|
|
271
|
+
}
|
|
272
|
+
function to_fascicle_tool(client, advertised, options) {
|
|
273
|
+
const name = advertised.name;
|
|
274
|
+
return {
|
|
275
|
+
name,
|
|
276
|
+
description: advertised.description ?? options.default_description?.(name) ?? `MCP tool ${name}`,
|
|
277
|
+
input_schema: json_schema_to_zod(advertised.inputSchema),
|
|
278
|
+
execute: async (input, ctx) => {
|
|
279
|
+
if (ctx.abort.aborted) throw ctx.abort.reason instanceof Error ? ctx.abort.reason : new mcp_error(`MCP tool ${name} aborted before invocation`, { tool_name: name });
|
|
280
|
+
return call_result_to_output(await client.callTool({
|
|
281
|
+
name,
|
|
282
|
+
arguments: is_record(input) ? input : void 0
|
|
283
|
+
}, void 0, { signal: ctx.abort }), name);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
//#endregion
|
|
288
|
+
//#region src/mcp/serve.ts
|
|
289
|
+
function serve_flow(options) {
|
|
290
|
+
const { server, flow, name, description, input_schema, trajectory, to_result } = options;
|
|
291
|
+
server.registerTool(name, {
|
|
292
|
+
description,
|
|
293
|
+
inputSchema: input_schema
|
|
294
|
+
}, async (input, extra) => {
|
|
295
|
+
try {
|
|
296
|
+
return output_to_call_result(await run(flow, input, {
|
|
297
|
+
install_signal_handlers: false,
|
|
298
|
+
abort: extra.signal,
|
|
299
|
+
...trajectory !== void 0 ? { trajectory } : {}
|
|
300
|
+
}), to_result);
|
|
301
|
+
} catch (err) {
|
|
302
|
+
return {
|
|
303
|
+
isError: true,
|
|
304
|
+
content: [{
|
|
305
|
+
type: "text",
|
|
306
|
+
text: err instanceof Error ? err.message : String(err)
|
|
307
|
+
}]
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
//#endregion
|
|
313
|
+
export { json_schema_to_zod, mcp_client, mcp_error, mcp_sdk_missing_error, serve_flow };
|
|
314
|
+
|
|
315
|
+
//# sourceMappingURL=mcp.js.map
|
package/dist/mcp.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp.js","names":[],"sources":["../src/mcp/errors.ts","../src/mcp/internal.ts","../src/mcp/result_mapping.ts","../src/mcp/schema_bridge.ts","../src/mcp/sdk_loader.ts","../src/mcp/client.ts","../src/mcp/serve.ts"],"sourcesContent":["/**\n * Typed errors for the MCP adapter.\n *\n * Listed alongside src/core/errors.ts and src/engine/errors.ts in the\n * `no-class` rule ignores: `instanceof` branching is how callers distinguish a\n * missing optional peer from a tool-level failure surfaced over the wire.\n */\n\nexport class mcp_sdk_missing_error extends Error {\n readonly kind = 'mcp_sdk_missing_error' as const;\n constructor(\n message = '@modelcontextprotocol/sdk is required for fascicle/mcp; install it: pnpm add @modelcontextprotocol/sdk',\n ) {\n super(message)\n this.name = 'mcp_sdk_missing_error'\n }\n}\n\nexport class mcp_error extends Error {\n readonly kind = 'mcp_error' as const;\n readonly tool_name: string | undefined;\n constructor(message: string, metadata: { tool_name?: string } = {}) {\n super(message)\n this.name = 'mcp_error'\n this.tool_name = metadata.tool_name\n }\n}\n","/**\n * Small shared guards for the MCP adapter. Kept in one place so the client,\n * schema bridge, and result mapping do not each carry a copy.\n */\n\nexport function is_record(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n\nexport function as_record(value: unknown): Record<string, unknown> | undefined {\n return is_record(value) ? value : undefined\n}\n","/**\n * Mapping between MCP `CallToolResult` payloads and fascicle tool values.\n *\n * Inbound (`call_result_to_output`): a remote tool result becomes the value a\n * fascicle `Tool.execute` returns. `isError: true` is a tool-level failure, so\n * it throws rather than returns, letting the tool loop's error policy feed it\n * back to the model. Structured output is preferred over text when present.\n *\n * Outbound (`output_to_call_result`): a fascicle flow's output becomes a\n * `CallToolResult` for `serve_flow`. Objects are emitted as both a JSON text\n * part (every MCP host can read it) and `structuredContent` (typed hosts get\n * the object).\n */\n\nimport { mcp_error } from './errors.js'\nimport { as_record, is_record } from './internal.js'\n\nexport type McpToolResult = {\n content: Array<{ type: 'text'; text: string }>\n structuredContent?: Record<string, unknown>\n}\n\nexport function call_result_to_output(result: unknown, tool_name?: string): unknown {\n const record = as_record(result) ?? {}\n\n if (record['isError'] === true) {\n const detail = join_text(record['content']) ?? 'MCP tool returned an error'\n throw new mcp_error(detail, tool_name !== undefined ? { tool_name } : {})\n }\n\n if (is_record(record['structuredContent'])) return record['structuredContent']\n\n const text = join_text(record['content'])\n if (text !== undefined) return text\n\n return record['content'] ?? null\n}\n\nexport function output_to_call_result<o>(\n output: o,\n to_result?: (output: o) => { text: string; structured?: Record<string, unknown> },\n): McpToolResult {\n if (to_result !== undefined) {\n const shaped = to_result(output)\n return build_result(shaped.text, shaped.structured)\n }\n if (typeof output === 'string') return build_result(output)\n if (is_record(output)) return build_result(stringify(output), output)\n if (Array.isArray(output)) return build_result(stringify(output))\n return build_result(String(output))\n}\n\nfunction build_result(text: string, structured?: Record<string, unknown>): McpToolResult {\n return structured !== undefined\n ? { content: [{ type: 'text', text }], structuredContent: structured }\n : { content: [{ type: 'text', text }] }\n}\n\nfunction join_text(content: unknown): string | undefined {\n if (!Array.isArray(content)) return undefined\n const parts: string[] = []\n for (const part of content) {\n if (is_record(part) && part['type'] === 'text' && typeof part['text'] === 'string') {\n parts.push(part['text'])\n }\n }\n return parts.length > 0 ? parts.join('\\n') : undefined\n}\n\nfunction stringify(value: unknown): string {\n try {\n return JSON.stringify(value)\n } catch {\n return String(value)\n }\n}\n","/**\n * JSON Schema to Zod conversion for inbound MCP tools.\n *\n * An MCP tool advertises its input as JSON Schema, but a fascicle `Tool` carries\n * a Zod `input_schema` that the tool loop `safeParse`s before `execute` and the\n * engine serializes back to JSON Schema for the provider. So the converted Zod\n * must do two things at once: stay permissive enough never to reject an arg the\n * remote server would accept, and round-trip through `z.toJSONSchema` to a shape\n * the provider can fill.\n *\n * Two deliberate choices serve those goals. Objects are `looseObject` so extra\n * keys survive to the server (which re-validates and is the real authority), and\n * a freeform object maps to `z.looseObject({})` (emits `{type:\"object\"}`) rather\n * than `z.unknown()` (emits `{}`), preserving the \"takes an object\" signal. Value\n * constraints (min/max, length, pattern, format) are intentionally not modeled:\n * they add rejection risk for marginal provider benefit, and the server enforces\n * them anyway. Structure, types, enums, const, unions, and descriptions are\n * preserved; anything unrecognized degrades to `z.unknown()`.\n */\n\nimport { z } from 'zod'\nimport { as_record, is_record } from './internal.js'\n\nexport function json_schema_to_zod(schema: unknown): z.ZodType {\n try {\n return convert(schema)\n } catch {\n // An arbitrary remote schema must never crash tool discovery.\n return z.unknown()\n }\n}\n\nfunction convert(node: unknown): z.ZodType {\n const schema = as_record(node)\n if (schema === undefined) return z.unknown()\n\n const described = (inner: z.ZodType): z.ZodType => {\n const description = schema['description']\n return typeof description === 'string' ? inner.describe(description) : inner\n }\n\n if ('const' in schema) return described(literal_of(schema['const']))\n if (Array.isArray(schema['enum'])) return described(enum_of(schema['enum']))\n if (Array.isArray(schema['anyOf'])) return described(union_of(schema['anyOf']))\n if (Array.isArray(schema['oneOf'])) return described(union_of(schema['oneOf']))\n if (Array.isArray(schema['allOf'])) return described(intersection_of(schema['allOf']))\n\n const type = schema['type']\n if (Array.isArray(type)) {\n return described(union_of_types(type, schema))\n }\n if (typeof type === 'string') {\n return described(for_type(type, schema))\n }\n // No explicit type: an object with declared properties is still an object;\n // otherwise nothing structural is known, so stay permissive.\n if (is_record(schema['properties'])) return described(build_object(schema))\n return described(z.unknown())\n}\n\nfunction for_type(type: string, schema: Record<string, unknown>): z.ZodType {\n switch (type) {\n case 'object':\n return build_object(schema)\n case 'array':\n return build_array(schema)\n case 'string':\n return z.string()\n case 'number':\n return z.number()\n case 'integer':\n return z.number().int()\n case 'boolean':\n return z.boolean()\n case 'null':\n return z.null()\n default:\n return z.unknown()\n }\n}\n\nfunction build_object(schema: Record<string, unknown>): z.ZodType {\n const properties = as_record(schema['properties']) ?? {}\n const required = new Set(string_array(schema['required']))\n const shape: Record<string, z.ZodType> = {}\n for (const [key, prop] of Object.entries(properties)) {\n const child = convert(prop)\n shape[key] = required.has(key) ? child : child.optional()\n }\n return z.looseObject(shape)\n}\n\nfunction build_array(schema: Record<string, unknown>): z.ZodType {\n // A positional-tuple `items` array and a missing `items` both convert to\n // `z.unknown()`, so one permissive element type covers every case. Tuple\n // schemas are rare in MCP tools and the server re-validates them anyway.\n return z.array(convert(schema['items']))\n}\n\nfunction enum_of(values: unknown[]): z.ZodType {\n if (values.length === 0) return z.unknown()\n if (values.every((v): v is string => typeof v === 'string')) {\n return z.enum(values)\n }\n return combine_union(values.map(literal_of))\n}\n\nfunction union_of(nodes: unknown[]): z.ZodType {\n return combine_union(nodes.map(convert))\n}\n\nfunction union_of_types(types: unknown[], schema: Record<string, unknown>): z.ZodType {\n return combine_union(types.map((t) => for_type(String(t), schema)))\n}\n\nfunction intersection_of(nodes: unknown[]): z.ZodType {\n const parts = nodes.map(convert)\n if (parts.length === 0) return z.unknown()\n return parts.reduce((acc, part) => z.intersection(acc, part))\n}\n\nfunction combine_union(variants: z.ZodType[]): z.ZodType {\n const [first, second, ...rest] = variants\n if (first === undefined) return z.unknown()\n if (second === undefined) return first\n return z.union([first, second, ...rest])\n}\n\nfunction literal_of(value: unknown): z.ZodType {\n if (\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'boolean'\n ) {\n return z.literal(value)\n }\n if (value === null) return z.null()\n // Object and array literals are not expressible as z.literal; accept anything.\n return z.unknown()\n}\n\nfunction string_array(value: unknown): string[] {\n return Array.isArray(value) ? value.filter((v): v is string => typeof v === 'string') : []\n}\n","/**\n * Lazy loader for the optional `@modelcontextprotocol/sdk` peer.\n *\n * `mcp_client` needs SDK values (the Client and transport constructors), so it\n * imports them dynamically rather than statically: a consumer can import\n * `fascicle/mcp` without the SDK installed and only pays the cost when they\n * actually connect. A missing peer surfaces as a typed `mcp_sdk_missing_error`\n * with install guidance, mirroring the engine's `load_optional_peer`.\n *\n * `serve_flow` is not covered here: it operates on a caller-constructed\n * `McpServer`, so it needs only a type-only import and no runtime SDK load.\n */\n\nimport { mcp_sdk_missing_error } from './errors.js'\n\nexport type McpClientSdk = Awaited<ReturnType<typeof load_client_sdk>>\n\nexport async function load_client_sdk() {\n try {\n const [client_mod, stdio_mod, http_mod] = await Promise.all([\n import('@modelcontextprotocol/sdk/client/index.js'),\n import('@modelcontextprotocol/sdk/client/stdio.js'),\n import('@modelcontextprotocol/sdk/client/streamableHttp.js'),\n ])\n return {\n Client: client_mod.Client,\n StdioClientTransport: stdio_mod.StdioClientTransport,\n StreamableHTTPClientTransport: http_mod.StreamableHTTPClientTransport,\n }\n } catch (err: unknown) {\n const detail = err instanceof Error ? err.message : String(err)\n throw new mcp_sdk_missing_error(\n `@modelcontextprotocol/sdk is required for fascicle/mcp; install it: pnpm add @modelcontextprotocol/sdk (cause: ${detail})`,\n )\n }\n}\n","/**\n * Inbound MCP: connect to an external MCP server and surface its tools as plain\n * fascicle `Tool[]`.\n *\n * `mcp_client` owns the connection lifecycle (config in, `{ tools, close }`\n * out) so callers do not have to import SDK transport classes themselves. Each\n * advertised MCP tool becomes an ordinary `Tool`: its JSON Schema is bridged to\n * Zod for the loop's `safeParse` and the provider, and `execute` forwards to\n * `callTool`, propagating the run's abort signal.\n */\n\nimport type { Client } from '@modelcontextprotocol/sdk/client/index.js'\nimport type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'\nimport type { Tool, ToolExecContext } from '#engine'\nimport { mcp_error } from './errors.js'\nimport { is_record } from './internal.js'\nimport { call_result_to_output } from './result_mapping.js'\nimport { json_schema_to_zod } from './schema_bridge.js'\nimport { load_client_sdk, type McpClientSdk } from './sdk_loader.js'\n\nconst DEFAULT_CLIENT_INFO = { name: 'fascicle', version: '0.0.0' }\n\nexport type McpStdioConfig = {\n transport: 'stdio'\n command: string\n args?: string[]\n env?: Record<string, string>\n cwd?: string\n}\n\nexport type McpStreamableHttpConfig = {\n transport: 'http'\n url: string\n headers?: Record<string, string>\n}\n\n// Escape hatch: hand in an already-connected Client (a transport the adapter\n// does not build, a test harness, a shared connection). Its lifecycle stays\n// with the caller, so the returned `close` is a no-op for this variant.\nexport type McpExistingClientConfig = {\n transport: 'client'\n client: Client\n}\n\nexport type McpClientConfig =\n | McpStdioConfig\n | McpStreamableHttpConfig\n | McpExistingClientConfig\n\nexport type McpClientOptions = {\n // Identity sent in the MCP handshake; defaults to fascicle.\n client_info?: { name: string; version: string }\n // When set, only tools whose name is listed are surfaced.\n include?: ReadonlyArray<string>\n // Supplies a description for tools that advertise none.\n default_description?: (tool_name: string) => string\n}\n\nexport type McpClientHandle = {\n readonly tools: ReadonlyArray<Tool>\n readonly close: () => Promise<void>\n}\n\nexport async function mcp_client(\n config: McpClientConfig,\n options: McpClientOptions = {},\n): Promise<McpClientHandle> {\n const owns_client = config.transport !== 'client'\n const client = await connect_client(config, options)\n\n const include = options.include !== undefined ? new Set(options.include) : undefined\n const listed = await client.listTools()\n const tools: Tool[] = []\n for (const advertised of listed.tools) {\n if (include !== undefined && !include.has(advertised.name)) continue\n tools.push(to_fascicle_tool(client, advertised, options))\n }\n\n return {\n tools,\n close: async () => {\n if (owns_client) await client.close()\n },\n }\n}\n\nasync function connect_client(\n config: McpClientConfig,\n options: McpClientOptions,\n): Promise<Client> {\n if (config.transport === 'client') return config.client\n const sdk = await load_client_sdk()\n const client = new sdk.Client(options.client_info ?? DEFAULT_CLIENT_INFO)\n await client.connect(build_transport(sdk, config))\n return client\n}\n\nfunction build_transport(\n sdk: McpClientSdk,\n config: McpStdioConfig | McpStreamableHttpConfig,\n): Transport {\n if (config.transport === 'stdio') {\n return new sdk.StdioClientTransport({\n command: config.command,\n ...(config.args !== undefined ? { args: config.args } : {}),\n ...(config.env !== undefined ? { env: config.env } : {}),\n ...(config.cwd !== undefined ? { cwd: config.cwd } : {}),\n })\n }\n const opts = config.headers !== undefined ? { requestInit: { headers: config.headers } } : {}\n // The SDK declares this class `implements Transport`, but its `onmessage`\n // overload is narrower than the interface's, which only strict mode flags.\n // eslint-disable-next-line typescript-eslint/no-unsafe-type-assertion\n return new sdk.StreamableHTTPClientTransport(new URL(config.url), opts) as Transport\n}\n\nfunction to_fascicle_tool(\n client: Client,\n advertised: { name: string; description?: string | undefined; inputSchema: unknown },\n options: McpClientOptions,\n): Tool {\n const name = advertised.name\n const description =\n advertised.description ?? options.default_description?.(name) ?? `MCP tool ${name}`\n return {\n name,\n description,\n input_schema: json_schema_to_zod(advertised.inputSchema),\n execute: async (input: unknown, ctx: ToolExecContext) => {\n if (ctx.abort.aborted) {\n throw ctx.abort.reason instanceof Error\n ? ctx.abort.reason\n : new mcp_error(`MCP tool ${name} aborted before invocation`, { tool_name: name })\n }\n const result = await client.callTool(\n { name, arguments: is_record(input) ? input : undefined },\n undefined,\n { signal: ctx.abort },\n )\n return call_result_to_output(result, name)\n },\n }\n}\n","/**\n * Outbound MCP: expose a composed fascicle flow as an MCP tool.\n *\n * `serve_flow` registers onto a caller-constructed `McpServer` (the caller owns\n * the transport, identity, and lifecycle, and can host many flows on one\n * server), so it needs only a type-only SDK import. The registered handler\n * drives the existing `run`, threading the per-request abort signal so an MCP\n * client cancellation aborts the in-flight flow, and maps the result into a\n * `CallToolResult`. A thrown flow becomes an `isError` result rather than a\n * JSON-RPC protocol error.\n */\n\nimport type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { z } from 'zod'\nimport { run } from '#core'\nimport type { Step, TrajectoryLogger } from '#core'\nimport { output_to_call_result } from './result_mapping.js'\n\nexport type ServeFlowOptions<i, o> = {\n server: McpServer\n flow: Step<i, o>\n name: string\n description: string\n input_schema: z.ZodType<i>\n trajectory?: TrajectoryLogger\n // Overrides the default value-to-CallToolResult mapping.\n to_result?: (output: o) => { text: string; structured?: Record<string, unknown> }\n}\n\nexport function serve_flow<i, o>(options: ServeFlowOptions<i, o>): void {\n const { server, flow, name, description, input_schema, trajectory, to_result } = options\n server.registerTool(\n name,\n { description, inputSchema: input_schema },\n async (input: i, extra) => {\n try {\n const output = await run(flow, input, {\n install_signal_handlers: false,\n abort: extra.signal,\n ...(trajectory !== undefined ? { trajectory } : {}),\n })\n return output_to_call_result(output, to_result)\n } catch (err: unknown) {\n return {\n isError: true,\n content: [{ type: 'text', text: err instanceof Error ? err.message : String(err) }],\n }\n }\n },\n )\n}\n"],"mappings":";;;;;;;;;;AAQA,IAAa,wBAAb,cAA2C,MAAM;CAC/C,OAAgB;CAChB,YACE,UAAU,0GACV;AACA,QAAM,QAAQ;AACd,OAAK,OAAO;;;AAIhB,IAAa,YAAb,cAA+B,MAAM;CACnC,OAAgB;CAChB;CACA,YAAY,SAAiB,WAAmC,EAAE,EAAE;AAClE,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,YAAY,SAAS;;;;;;;;;ACnB9B,SAAgB,UAAU,OAAkD;AAC1E,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM;;AAG7E,SAAgB,UAAU,OAAqD;AAC7E,QAAO,UAAU,MAAM,GAAG,QAAQ,KAAA;;;;;;;;;;;;;;;;;ACYpC,SAAgB,sBAAsB,QAAiB,WAA6B;CAClF,MAAM,SAAS,UAAU,OAAO,IAAI,EAAE;AAEtC,KAAI,OAAO,eAAe,KAExB,OAAM,IAAI,UADK,UAAU,OAAO,WAAW,IAAI,8BACnB,cAAc,KAAA,IAAY,EAAE,WAAW,GAAG,EAAE,CAAC;AAG3E,KAAI,UAAU,OAAO,qBAAqB,CAAE,QAAO,OAAO;CAE1D,MAAM,OAAO,UAAU,OAAO,WAAW;AACzC,KAAI,SAAS,KAAA,EAAW,QAAO;AAE/B,QAAO,OAAO,cAAc;;AAG9B,SAAgB,sBACd,QACA,WACe;AACf,KAAI,cAAc,KAAA,GAAW;EAC3B,MAAM,SAAS,UAAU,OAAO;AAChC,SAAO,aAAa,OAAO,MAAM,OAAO,WAAW;;AAErD,KAAI,OAAO,WAAW,SAAU,QAAO,aAAa,OAAO;AAC3D,KAAI,UAAU,OAAO,CAAE,QAAO,aAAa,UAAU,OAAO,EAAE,OAAO;AACrE,KAAI,MAAM,QAAQ,OAAO,CAAE,QAAO,aAAa,UAAU,OAA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|
169
169
|
function format_cost(usd) {
|
|
170
|
-
// < 1¢ shown to 4 decimals, otherwise to 2 decimals (per
|
|
170
|
+
// < 1¢ shown to 4 decimals, otherwise to 2 decimals (per research/explorations/2026-04-eval-surface.md §5)
|
|
171
171
|
if (usd < 0.01) return '$' + usd.toFixed(4)
|
|
172
172
|
return '$' + usd.toFixed(2)
|
|
173
173
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fascicle",
|
|
3
|
-
"version": "0.8.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.8.10",
|
|
4
|
+
"description": "Compose agents like plain values. A TypeScript toolkit that wires LLM calls, tools, and functions into typed steps over eight providers. No framework, no ambient state.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agents",
|
|
7
|
+
"agentic",
|
|
8
|
+
"multi-agent",
|
|
9
|
+
"ai",
|
|
10
|
+
"llm",
|
|
11
|
+
"workflow",
|
|
12
|
+
"pipeline",
|
|
13
|
+
"orchestration",
|
|
14
|
+
"tool-calling",
|
|
15
|
+
"harness",
|
|
16
|
+
"streaming",
|
|
17
|
+
"observability",
|
|
18
|
+
"typescript",
|
|
19
|
+
"composable",
|
|
20
|
+
"ai-sdk",
|
|
21
|
+
"vercel-ai-sdk",
|
|
22
|
+
"anthropic",
|
|
23
|
+
"openai",
|
|
24
|
+
"gemini",
|
|
25
|
+
"bedrock",
|
|
26
|
+
"openrouter",
|
|
27
|
+
"ollama",
|
|
28
|
+
"lmstudio"
|
|
29
|
+
],
|
|
5
30
|
"license": "Apache-2.0",
|
|
6
31
|
"author": {
|
|
7
32
|
"name": "Rob McLarty",
|
|
@@ -13,7 +38,6 @@
|
|
|
13
38
|
"node": ">=24.0.0",
|
|
14
39
|
"pnpm": ">=10.13.0"
|
|
15
40
|
},
|
|
16
|
-
"packageManager": "pnpm@11.1.2",
|
|
17
41
|
"main": "./dist/index.js",
|
|
18
42
|
"module": "./dist/index.js",
|
|
19
43
|
"types": "./dist/index.d.ts",
|
|
@@ -25,14 +49,17 @@
|
|
|
25
49
|
"./adapters": {
|
|
26
50
|
"types": "./dist/adapters.d.ts",
|
|
27
51
|
"import": "./dist/adapters.js"
|
|
52
|
+
},
|
|
53
|
+
"./mcp": {
|
|
54
|
+
"types": "./dist/mcp.d.ts",
|
|
55
|
+
"import": "./dist/mcp.js"
|
|
28
56
|
}
|
|
29
57
|
},
|
|
30
58
|
"imports": {
|
|
31
59
|
"#core": "./src/core/index.ts",
|
|
32
60
|
"#engine": "./src/engine/index.ts",
|
|
33
61
|
"#composites": "./src/composites/index.ts",
|
|
34
|
-
"#
|
|
35
|
-
"#stores": "./src/stores/index.ts",
|
|
62
|
+
"#adapters": "./src/adapters/index.ts",
|
|
36
63
|
"#viewer": "./src/viewer/index.ts",
|
|
37
64
|
"#agents": "./src/agents/index.ts"
|
|
38
65
|
},
|
|
@@ -49,35 +76,20 @@
|
|
|
49
76
|
"type": "git",
|
|
50
77
|
"url": "git+https://github.com/robmclarty/fascicle.git"
|
|
51
78
|
},
|
|
52
|
-
"homepage": "https://github.
|
|
79
|
+
"homepage": "https://robmclarty.github.io/fascicle/",
|
|
53
80
|
"bugs": {
|
|
54
81
|
"url": "https://github.com/robmclarty/fascicle/issues"
|
|
55
82
|
},
|
|
56
83
|
"publishConfig": {
|
|
57
84
|
"access": "public"
|
|
58
85
|
},
|
|
59
|
-
"scripts": {
|
|
60
|
-
"build": "node scripts/build.mjs",
|
|
61
|
-
"check": "node scripts/check.mjs",
|
|
62
|
-
"check:all": "node scripts/check.mjs --all",
|
|
63
|
-
"check:json": "node scripts/check.mjs --json",
|
|
64
|
-
"check:bail": "node scripts/check.mjs --bail",
|
|
65
|
-
"check:fix": "pnpm exec oxlint --type-aware --fix && node scripts/strip-semicolons.mjs && pnpm exec fallow fix",
|
|
66
|
-
"check:mutation": "stryker run",
|
|
67
|
-
"check:publish": "node scripts/check-publish.mjs",
|
|
68
|
-
"check:security": "pnpm audit --audit-level=high",
|
|
69
|
-
"prepublishOnly": "pnpm check && pnpm build && pnpm check:publish",
|
|
70
|
-
"test": "vitest run",
|
|
71
|
-
"test:watch": "vitest",
|
|
72
|
-
"types": "tsc --noEmit",
|
|
73
|
-
"fascicle-viewer": "tsx src/viewer/cli.ts"
|
|
74
|
-
},
|
|
75
86
|
"peerDependencies": {
|
|
76
87
|
"@ai-sdk/amazon-bedrock": "^3.0.0",
|
|
77
88
|
"@ai-sdk/anthropic": "^3.0.0",
|
|
78
89
|
"@ai-sdk/google": "^3.0.0",
|
|
79
90
|
"@ai-sdk/openai": "^3.0.0",
|
|
80
91
|
"@ai-sdk/openai-compatible": "^2.0.0",
|
|
92
|
+
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
81
93
|
"@openrouter/ai-sdk-provider": "^2.0.0",
|
|
82
94
|
"ai": "^6.0.0",
|
|
83
95
|
"ai-sdk-ollama": "^3.0.0",
|
|
@@ -99,6 +111,9 @@
|
|
|
99
111
|
"@ai-sdk/openai-compatible": {
|
|
100
112
|
"optional": true
|
|
101
113
|
},
|
|
114
|
+
"@modelcontextprotocol/sdk": {
|
|
115
|
+
"optional": true
|
|
116
|
+
},
|
|
102
117
|
"@openrouter/ai-sdk-provider": {
|
|
103
118
|
"optional": true
|
|
104
119
|
},
|
|
@@ -109,6 +124,7 @@
|
|
|
109
124
|
"devDependencies": {
|
|
110
125
|
"@arethetypeswrong/cli": "0.18.2",
|
|
111
126
|
"@ast-grep/cli": "0.42.1",
|
|
127
|
+
"@modelcontextprotocol/sdk": "1.29.0",
|
|
112
128
|
"@stryker-mutator/core": "9.6.1",
|
|
113
129
|
"@stryker-mutator/vitest-runner": "9.6.1",
|
|
114
130
|
"@types/node": "24.12.2",
|
|
@@ -123,5 +139,20 @@
|
|
|
123
139
|
"typescript": "6.0.3",
|
|
124
140
|
"vitest": "4.1.4",
|
|
125
141
|
"zod": "4.3.6"
|
|
142
|
+
},
|
|
143
|
+
"scripts": {
|
|
144
|
+
"build": "node scripts/build.mjs",
|
|
145
|
+
"check": "node scripts/check.mjs",
|
|
146
|
+
"check:all": "node scripts/check.mjs --all",
|
|
147
|
+
"check:json": "node scripts/check.mjs --json",
|
|
148
|
+
"check:bail": "node scripts/check.mjs --bail",
|
|
149
|
+
"check:fix": "pnpm exec oxlint --type-aware --fix && node scripts/strip-semicolons.mjs && pnpm exec fallow fix",
|
|
150
|
+
"check:mutation": "stryker run",
|
|
151
|
+
"check:publish": "node scripts/check-publish.mjs",
|
|
152
|
+
"check:security": "pnpm audit --audit-level=high",
|
|
153
|
+
"test": "vitest run",
|
|
154
|
+
"test:watch": "vitest",
|
|
155
|
+
"types": "tsc --noEmit",
|
|
156
|
+
"fascicle-viewer": "tsx src/viewer/cli.ts"
|
|
126
157
|
}
|
|
127
|
-
}
|
|
158
|
+
}
|
package/dist/adapters.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adapters.js","names":[],"sources":["../src/observability/noop.ts","../src/stores/filesystem.ts"],"sourcesContent":["/**\n * No-op trajectory logger.\n *\n * Satisfies `TrajectoryLogger` from `core` without emitting any\n * output. Used as the default when no logger is supplied to `run(...)` and as\n * a cheap fallback in tests that do not care about trajectory output.\n */\n\nimport { randomUUID } from 'node:crypto'\nimport type { TrajectoryLogger } from '#core'\n\nexport function noop_logger(): TrajectoryLogger {\n return {\n record: () => {},\n start_span: (name) => `${name}:${randomUUID().slice(0, 8)}`,\n end_span: () => {},\n }\n}\n","/**\n * Filesystem-backed checkpoint store.\n *\n * Satisfies `CheckpointStore` from `core`. Each key is stored as\n * a JSON file under the configured root directory. Writes are all-or-nothing:\n * values are written to a temporary sibling file and then atomically renamed\n * into place, so an interrupted write never leaves a partially-written file\n * at the target key (spec.md §6.3, §6.8). On `get`, a missing file, a partial\n * temp file that is not atomically renamed, or a JSON parse failure each read\n * as a cache miss (returning `null`) rather than an error.\n *\n * Paths are accepted at construction; the store never reads `process.env`.\n */\n\nimport { createHash, randomUUID } from 'node:crypto'\nimport { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'\nimport { join } from 'node:path'\nimport type { CheckpointStore } from '#core'\n\nexport type FilesystemStoreOptions = {\n readonly root_dir: string\n}\n\nfunction safe_filename(key: string): string {\n const hash = createHash('sha256').update(key).digest('hex').slice(0, 12)\n const slug = key.replace(/[^a-zA-Z0-9._-]/g, '_').slice(0, 64)\n return `${slug}.${hash}.json`\n}\n\nexport function filesystem_store(options: FilesystemStoreOptions): CheckpointStore {\n const { root_dir } = options\n\n const ensure_root = async (): Promise<void> => {\n await mkdir(root_dir, { recursive: true })\n }\n\n const path_for = (key: string): string => join(root_dir, safe_filename(key))\n\n const get = async (key: string): Promise<unknown> => {\n const path = path_for(key)\n let raw: string\n try {\n raw = await readFile(path, 'utf8')\n } catch {\n return null\n }\n try {\n return JSON.parse(raw) as unknown\n } catch {\n return null\n }\n }\n\n const set = async (key: string, value: unknown): Promise<void> => {\n await ensure_root()\n const target = path_for(key)\n const tmp = `${target}.${randomUUID().slice(0, 8)}.tmp`\n await writeFile(tmp, JSON.stringify(value), 'utf8')\n await rename(tmp, target)\n }\n\n const delete_key = async (key: string): Promise<void> => {\n const target = path_for(key)\n await rm(target, { force: true })\n }\n\n return { get, set, delete: delete_key }\n}\n"],"mappings":";;;;;;;;;;;;AAWA,SAAgB,cAAgC;AAC9C,QAAO;EACL,cAAc;EACd,aAAa,SAAS,GAAG,KAAK,GAAG,YAAY,CAAC,MAAM,GAAG,EAAE;EACzD,gBAAgB;EACjB;;;;;;;;;;;;;;;;;ACOH,SAAS,cAAc,KAAqB;CAC1C,MAAM,OAAO,WAAW,SAAS,CAAC,OAAO,IAAI,CAAC,OAAO,MAAM,CAAC,MAAM,GAAG,GAAG;AAExE,QAAO,GADM,IAAI,QAAQ,oBAAoB,IAAI,CAAC,MAAM,GAAG,GAAG,CAC/C,GAAG,KAAK;;AAGzB,SAAgB,iBAAiB,SAAkD;CACjF,MAAM,EAAE,aAAa;CAErB,MAAM,cAAc,YAA2B;AAC7C,QAAM,MAAM,UAAU,EAAE,WAAW,MAAM,CAAC;;CAG5C,MAAM,YAAY,QAAwB,KAAK,UAAU,cAAc,IAAI,CAAC;CAE5E,MAAM,MAAM,OAAO,QAAkC;EACnD,MAAM,OAAO,SAAS,IAAI;EAC1B,IAAI;AACJ,MAAI;AACF,SAAM,MAAM,SAAS,MAAM,OAAO;UAC5B;AACN,UAAO;;AAET,MAAI;AACF,UAAO,KAAK,MAAM,IAAI;UAChB;AACN,UAAO;;;CAIX,MAAM,MAAM,OAAO,KAAa,UAAkC;AAChE,QAAM,aAAa;EACnB,MAAM,SAAS,SAAS,IAAI;EAC5B,MAAM,MAAM,GAAG,OAAO,GAAG,YAAY,CAAC,MAAM,GAAG,EAAE,CAAC;AAClD,QAAM,UAAU,KAAK,KAAK,UAAU,MAAM,EAAE,OAAO;AACnD,QAAM,OAAO,KAAK,OAAO;;CAG3B,MAAM,aAAa,OAAO,QAA+B;AAEvD,QAAM,GADS,SAAS,IAAI,EACX,EAAE,OAAO,MAAM,CAAC;;AAGnC,QAAO;EAAE;EAAK;EAAK,QAAQ;EAAY"}
|