@modelprofile.com/flexharness 10.20.0 → 10.22.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/ts/interfaces.ts CHANGED
@@ -73,6 +73,16 @@ export interface IFlexSessionMetricsState {
73
73
  pendingCompactionId?: string;
74
74
  lastObservedInputTokens?: number;
75
75
  lastObservedInputTokensAt?: string;
76
+ /**
77
+ * Sums of the settled per-call reports' input (cache reads included), cache-read and cache-write
78
+ * tokens. All four breakdown keys are present together; metrics written before they were counted
79
+ * have none of them.
80
+ */
81
+ reportedLifetimeInputTokens?: number;
82
+ reportedLifetimeCacheReadTokens?: number;
83
+ reportedLifetimeCacheWriteTokens?: number;
84
+ /** The breakdown counts every call since the session began, without saturation. */
85
+ lifetimeTokenBreakdownComplete?: boolean;
76
86
  }
77
87
 
78
88
  /** No member estimates current context size or includes child-session / tool-owned model calls. */
@@ -90,6 +100,17 @@ export interface IFlexSessionMetrics {
90
100
  lastObservedInputTokensAt?: string;
91
101
  /** Always historical: output, steering, compaction and reversion can change context. */
92
102
  lastObservedInputTokensFreshness?: 'historical';
103
+ /** Sum of the settled per-call reports' input tokens, cache reads included; a lower bound when incomplete. */
104
+ reportedLifetimeInputTokens: number;
105
+ /** Sum of the settled per-call reports' input tokens read from the prompt cache; a lower bound when incomplete. */
106
+ reportedLifetimeCacheReadTokens: number;
107
+ /** Sum of the settled per-call reports' input tokens written to the prompt cache; a lower bound when incomplete. */
108
+ reportedLifetimeCacheWriteTokens: number;
109
+ /**
110
+ * The three sums cover every call `lifetimeUsedTokens` covers: lifetime usage is complete and
111
+ * the session's calls were counted by input and cache since it began.
112
+ */
113
+ lifetimeTokenBreakdownComplete: boolean;
93
114
  }
94
115
 
95
116
  export interface IFlexModelIdentity {
package/ts/readme.md CHANGED
@@ -65,7 +65,7 @@ or provider SDKs.
65
65
  | `/chat/harness` | `FlexHarnessTranscript` and intents for the `@design.estate/dees-catalog` harness components, browser-safe | `@design.estate/dees-catalog` `^17.0.0 \|\| ^19.5.0 \|\| ^20.0.0 \|\| ^21.0.0` (types only) |
66
66
  | `/kvm` | Browser control, terminal framing and OCR observation | `puppeteer` |
67
67
  | `/documents` | PDF and image documents, `extractTextFromPdf` | `@push.rocks/smartpdf`; for image documents `sharp` |
68
- | `/mcp` | MCP clients and AI SDK tool conversion | `@modelcontextprotocol/sdk` |
68
+ | `/mcp` | MCP clients, AI SDK tool conversion and `createMcpToolCatalog`, the tool catalog of MCP tools | `@modelcontextprotocol/sdk` |
69
69
  | `/migration` | Existing versioned harness migrations | None |
70
70
  | `/stores/nosqldb` | `NoSqlFlexHarnessStores`: cross-process CAS stores on `@lossless.org/client/nosqldb` | `@lossless.org/client` |
71
71
 
@@ -108,6 +108,9 @@ A warm read joins outstanding projection writes and surfaces accounting failures
108
108
  | `inFlight` | A generation or actual compactor invocation has not settled its accounting yet. |
109
109
  | `lastObservedInputTokens`, `lastObservedInputTokensAt` | Input count and observation time of the last settled generation call with complete input/output totals. |
110
110
  | `lastObservedInputTokensFreshness` | Always `historical`. This is **not** current context size: output, steering, compaction and reversion can change context. |
111
+ | `reportedLifetimeInputTokens` | Sum of the settled per-call reports' input tokens, prompt-cache reads included. |
112
+ | `reportedLifetimeCacheReadTokens`, `reportedLifetimeCacheWriteTokens` | Sums of the settled per-call reports' input tokens read from and written to the prompt cache. |
113
+ | `lifetimeTokenBreakdownComplete` | The three sums cover every call `lifetimeUsedTokens` covers: lifetime usage is complete and the session's calls were counted by input and cache since it began. Otherwise they are lower bounds. |
111
114
 
112
115
  Accounting covers this session's generation calls and reported compaction calls,
113
116
  including failure, cancellation and retries. Child sessions own their own spend;
@@ -122,6 +125,10 @@ conservative: unresolved operations are cleared without certifying their unknown
122
125
  spend. Undo, redo, compaction and transcript archiving never subtract lifetime
123
126
  spend. Importing visible history does not reconstruct historical billing.
124
127
 
128
+ Sessions whose metrics were written before input and cache tokens were counted count
129
+ them from their next settled call on and never report the breakdown complete; their
130
+ lifetime total keeps its completeness.
131
+
125
132
  Conversational runs that exhaust `maxSteps` with tool work or unseen input remaining
126
133
  fail with `FlexHarnessStepLimitError` (`FLEX_STEP_LIMIT`), rather than being marked
127
134
  completed with an empty answer. Standalone Agent callers retain tool-only results
@@ -1137,7 +1144,7 @@ Each domain serializes its own mutations. A successful run first persists a hidd
1137
1144
 
1138
1145
  Projection stores accept schema version 1, 2, or 3 from `load()`, but every `save()` receives the current schema-3 shape. Schema 3 records explicit reversion protocol, transcript/workspace provenance, and V2 disposition. A terminal V2 segment must be conclusively `revertible`, `no-change`, or `nonrevertible`; a pending V2 capture remains owned by its capture WAL and is never treated as transcript history.
1139
1146
 
1140
- Current schema-3 projections also persist optional `metrics` state. Older strict readers that do not recognize this key cannot reopen a projection after metrics have been written; downgrade or mixed-version access to that session is unsupported.
1147
+ Current schema-3 projections also persist optional `metrics` state. Older strict readers that do not recognize this key, or the token breakdown keys inside it (`reportedLifetimeInputTokens`, `reportedLifetimeCacheReadTokens`, `reportedLifetimeCacheWriteTokens`, `lifetimeTokenBreakdownComplete`), cannot reopen a projection after they have been written; downgrade or mixed-version access to that session is unsupported.
1141
1148
 
1142
1149
  A loaded schema-1 projection has no reversion state. Schema-2 `workspaceCaptured` segments migrate as protocol-1 workspace/revertible history without discarding references; transcript-only segments migrate as protocol-1 transcript provenance. Under `workspace-required`, that legacy transcript history is a barrier. The next projection mutation writes schema 3. Custom stores must preserve strict compare-and-swap revisions across schema-1 and schema-2 read-upgrade-write cycles, including uncertain-save reconciliation.
1143
1150
 
package/ts/utils.json.ts CHANGED
@@ -1516,6 +1516,7 @@ function assertSessionMetrics(value: unknown): void {
1516
1516
  requireOnlyKeys(metrics, [
1517
1517
  'reportedLifetimeUsedTokens', 'reportedLifetimeUsedTokensExact', 'lifetimeUsageComplete',
1518
1518
  'pendingGenerationId', 'pendingCompactionId', 'lastObservedInputTokens', 'lastObservedInputTokensAt',
1519
+ ...sessionMetricsBreakdownKeys,
1519
1520
  ], path);
1520
1521
  requireNonNegativeInteger(metrics.reportedLifetimeUsedTokens, `${path}.reportedLifetimeUsedTokens`);
1521
1522
  for (const key of ['reportedLifetimeUsedTokensExact', 'lifetimeUsageComplete']) {
@@ -1537,8 +1538,26 @@ function assertSessionMetrics(value: unknown): void {
1537
1538
  throw new FlexHarnessStoreFormatError(`${path}.lastObservedInputTokensAt must be an ISO timestamp.`);
1538
1539
  }
1539
1540
  }
1541
+ // Metrics written before the breakdown was counted have none of its keys.
1542
+ const breakdownKeys = sessionMetricsBreakdownKeys.filter((breakdownKey) => Object.hasOwn(metrics, breakdownKey));
1543
+ if (breakdownKeys.length > 0) {
1544
+ if (breakdownKeys.length !== sessionMetricsBreakdownKeys.length) {
1545
+ throw new FlexHarnessStoreFormatError(`${path} has a partial token breakdown.`);
1546
+ }
1547
+ for (const counter of ['reportedLifetimeInputTokens', 'reportedLifetimeCacheReadTokens', 'reportedLifetimeCacheWriteTokens']) {
1548
+ requireNonNegativeInteger(metrics[counter], `${path}.${counter}`);
1549
+ }
1550
+ if (typeof metrics.lifetimeTokenBreakdownComplete !== 'boolean') {
1551
+ throw new FlexHarnessStoreFormatError(`${path}.lifetimeTokenBreakdownComplete must be boolean.`);
1552
+ }
1553
+ }
1540
1554
  }
1541
1555
 
1556
+ const sessionMetricsBreakdownKeys = [
1557
+ 'reportedLifetimeInputTokens', 'reportedLifetimeCacheReadTokens', 'reportedLifetimeCacheWriteTokens',
1558
+ 'lifetimeTokenBreakdownComplete',
1559
+ ];
1560
+
1542
1561
  export function assertFlexProjectionSnapshotV3(
1543
1562
  value: unknown,
1544
1563
  ): asserts value is IFlexProjectionSnapshotV3 {
@@ -12,6 +12,11 @@ export interface ISessionMetricsOperation {
12
12
  reportedTokensExact: boolean;
13
13
  complete: boolean;
14
14
  sawCall: boolean;
15
+ inputTokens: number;
16
+ cacheReadTokens: number;
17
+ cacheWriteTokens: number;
18
+ /** No breakdown sum saturated. */
19
+ breakdownExact: boolean;
15
20
  lastObservedInputTokens?: number;
16
21
  lastObservedInputTokensAt?: string;
17
22
  }
@@ -20,6 +25,10 @@ export const createSessionMetricsState = (complete: boolean): IFlexSessionMetric
20
25
  reportedLifetimeUsedTokens: 0,
21
26
  reportedLifetimeUsedTokensExact: true,
22
27
  lifetimeUsageComplete: complete,
28
+ reportedLifetimeInputTokens: 0,
29
+ reportedLifetimeCacheReadTokens: 0,
30
+ reportedLifetimeCacheWriteTokens: 0,
31
+ lifetimeTokenBreakdownComplete: complete,
23
32
  });
24
33
 
25
34
  export const createSessionMetricsOperation = (
@@ -27,6 +36,7 @@ export const createSessionMetricsOperation = (
27
36
  source: ISessionMetricsOperation['source'],
28
37
  ): ISessionMetricsOperation => ({
29
38
  id, source, reportedTokens: 0, reportedTokensExact: true, complete: true, sawCall: false,
39
+ inputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, breakdownExact: true,
30
40
  });
31
41
 
32
42
  const isTokenCount = (value: unknown): value is number =>
@@ -60,6 +70,13 @@ export function observeSessionMetricsCall(
60
70
  const sum = addTokens(operation.reportedTokens, usage.totalTokens);
61
71
  operation.reportedTokens = sum.tokens;
62
72
  operation.reportedTokensExact &&= sum.exact;
73
+ const input = addTokens(operation.inputTokens, usage.inputTokens);
74
+ const cacheRead = addTokens(operation.cacheReadTokens, usage.cacheReadTokens);
75
+ const cacheWrite = addTokens(operation.cacheWriteTokens, usage.cacheWriteTokens);
76
+ operation.inputTokens = input.tokens;
77
+ operation.cacheReadTokens = cacheRead.tokens;
78
+ operation.cacheWriteTokens = cacheWrite.tokens;
79
+ operation.breakdownExact &&= input.exact && cacheRead.exact && cacheWrite.exact;
63
80
  if (operation.source === 'generation' && call.usageComplete !== false) {
64
81
  operation.lastObservedInputTokens = usage.inputTokens;
65
82
  operation.lastObservedInputTokensAt = new Date().toISOString();
@@ -79,6 +96,15 @@ export function settleSessionMetricsOperation(
79
96
  && metrics.reportedLifetimeUsedTokensExact
80
97
  // An arbitrary compactor may make uninstrumented calls. No report is not proof of zero spend.
81
98
  && (operation.source === 'generation' || operation.sawCall);
99
+ // Metrics written before the breakdown was counted start it now, short of the calls before.
100
+ const input = addTokens(metrics.reportedLifetimeInputTokens ?? 0, operation.inputTokens);
101
+ const cacheRead = addTokens(metrics.reportedLifetimeCacheReadTokens ?? 0, operation.cacheReadTokens);
102
+ const cacheWrite = addTokens(metrics.reportedLifetimeCacheWriteTokens ?? 0, operation.cacheWriteTokens);
103
+ metrics.lifetimeTokenBreakdownComplete = (metrics.lifetimeTokenBreakdownComplete ?? false)
104
+ && operation.breakdownExact && input.exact && cacheRead.exact && cacheWrite.exact;
105
+ metrics.reportedLifetimeInputTokens = input.tokens;
106
+ metrics.reportedLifetimeCacheReadTokens = cacheRead.tokens;
107
+ metrics.reportedLifetimeCacheWriteTokens = cacheWrite.tokens;
82
108
  if (operation.lastObservedInputTokens !== undefined) {
83
109
  metrics.lastObservedInputTokens = operation.lastObservedInputTokens;
84
110
  metrics.lastObservedInputTokensAt = operation.lastObservedInputTokensAt;
@@ -95,6 +121,10 @@ export function sessionMetricsView(metrics: IFlexSessionMetricsState): IFlexSess
95
121
  lifetimeUsageComplete,
96
122
  inFlight,
97
123
  ...(lifetimeUsageComplete ? { lifetimeUsedTokens: metrics.reportedLifetimeUsedTokens } : {}),
124
+ reportedLifetimeInputTokens: metrics.reportedLifetimeInputTokens ?? 0,
125
+ reportedLifetimeCacheReadTokens: metrics.reportedLifetimeCacheReadTokens ?? 0,
126
+ reportedLifetimeCacheWriteTokens: metrics.reportedLifetimeCacheWriteTokens ?? 0,
127
+ lifetimeTokenBreakdownComplete: lifetimeUsageComplete && metrics.lifetimeTokenBreakdownComplete === true,
98
128
  ...(metrics.lastObservedInputTokens === undefined ? {} : {
99
129
  lastObservedInputTokens: metrics.lastObservedInputTokens,
100
130
  lastObservedInputTokensAt: metrics.lastObservedInputTokensAt,
package/ts_mcp/index.ts CHANGED
@@ -27,22 +27,32 @@ export interface IMcpServerClientOptions {
27
27
  requestOptions?: plugins.RequestOptions;
28
28
  }
29
29
 
30
- export interface IMcpStdioServerConfig extends plugins.StdioServerParameters, IMcpServerClientOptions {
30
+ export interface IMcpServerTrustOptions {
31
+ /**
32
+ * Trusts the server's `readOnlyHint` annotations: `isReadOnlyToolCall` classifies a call of a tool
33
+ * the server annotates `readOnlyHint: true` as read-only. An annotation is the server's own claim,
34
+ * so no server's is trusted unless its configuration says so. Default: false.
35
+ */
36
+ trustReadOnlyHints?: boolean;
37
+ }
38
+
39
+ export interface IMcpStdioServerConfig
40
+ extends plugins.StdioServerParameters, IMcpServerClientOptions, IMcpServerTrustOptions {
31
41
  type: 'stdio';
32
42
  }
33
43
 
34
- export interface IMcpStreamableHttpServerConfig extends IMcpServerClientOptions {
44
+ export interface IMcpStreamableHttpServerConfig extends IMcpServerClientOptions, IMcpServerTrustOptions {
35
45
  type: 'streamableHttp' | 'http';
36
46
  url: string | URL;
37
47
  transportOptions?: plugins.StreamableHTTPClientTransportOptions;
38
48
  }
39
49
 
40
- export interface IMcpTransportServerConfig extends IMcpServerClientOptions {
50
+ export interface IMcpTransportServerConfig extends IMcpServerClientOptions, IMcpServerTrustOptions {
41
51
  type: 'transport';
42
52
  transport: plugins.Transport;
43
53
  }
44
54
 
45
- export interface IMcpClientServerConfig {
55
+ export interface IMcpClientServerConfig extends IMcpServerTrustOptions {
46
56
  type: 'client';
47
57
  client: IMcpClientLike;
48
58
  }
@@ -86,6 +96,8 @@ export interface IMcpConnectedServer {
86
96
  name: string;
87
97
  client: IMcpClientLike;
88
98
  tools: TMcpToolMetadata[];
99
+ /** Whether its configuration trusts the server's `readOnlyHint` annotations. */
100
+ trustReadOnlyHints: boolean;
89
101
  }
90
102
 
91
103
  export interface IMcpToolNameMapping {
@@ -97,6 +109,12 @@ export interface ICreateMcpToolsResult {
97
109
  tools: plugins.ToolSet;
98
110
  servers: IMcpConnectedServer[];
99
111
  toolNameMap: Record<string, IMcpToolNameMapping>;
112
+ /**
113
+ * Whether a call of `tools` only reads: its server's configuration trusts `readOnlyHint`, and the
114
+ * server annotates the tool `readOnlyHint: true`. Every other call is not read-only. Pass it as the
115
+ * tool handle's `isReadOnlyToolCall`, or ask it from that classifier.
116
+ */
117
+ isReadOnlyToolCall: (call: { toolName: string }) => boolean;
100
118
  close: () => Promise<void>;
101
119
  }
102
120
 
@@ -113,7 +131,12 @@ export const createMcpTools = async (
113
131
  try {
114
132
  for (const [serverName, config] of serverEntries) {
115
133
  const client = await connectMcpServer(serverName, config, options);
116
- const connectedServer: IMcpConnectedServer = { name: serverName, client, tools: [] };
134
+ const connectedServer: IMcpConnectedServer = {
135
+ name: serverName,
136
+ client,
137
+ tools: [],
138
+ trustReadOnlyHints: config.trustReadOnlyHints === true,
139
+ };
117
140
  connectedServers.push(connectedServer);
118
141
  connectedServer.tools = await listAllMcpTools(
119
142
  client,
@@ -124,68 +147,41 @@ export const createMcpTools = async (
124
147
 
125
148
  const exposedTools: plugins.ToolSet = {};
126
149
  const toolNameMap: Record<string, IMcpToolNameMapping> = {};
127
- const usedToolNames = new Set<string>(['invalid']);
128
- const formatResult = options.formatResult ?? formatMcpToolResult;
150
+ const readOnlyToolNames = new Set<string>();
129
151
  const shouldPrefixToolNames = options.prefixToolNames ?? connectedServers.length > 1;
130
152
 
131
- for (const server of connectedServers) {
132
- for (const mcpTool of server.tools) {
133
- const baseToolName = createExposedToolName(server.name, mcpTool.name, shouldPrefixToolNames);
134
- const exposedToolName = createUniqueToolName(baseToolName, usedToolNames);
135
- usedToolNames.add(exposedToolName);
136
- toolNameMap[exposedToolName] = {
153
+ for (const { server, tool: mcpTool, exposedToolName } of nameMcpTools(connectedServers, shouldPrefixToolNames)) {
154
+ toolNameMap[exposedToolName] = {
155
+ serverName: server.name,
156
+ toolName: mcpTool.name,
157
+ };
158
+ if (server.trustReadOnlyHints && mcpTool.annotations?.readOnlyHint === true) {
159
+ readOnlyToolNames.add(exposedToolName);
160
+ }
161
+
162
+ exposedTools[exposedToolName] = plugins.tool({
163
+ description: mcpTool.description ?? `MCP tool ${mcpTool.name} from ${server.name}`,
164
+ inputSchema: plugins.jsonSchema(mcpTool.inputSchema as never),
165
+ execute: async (input: unknown, invocationOptions?: plugins.ToolExecutionOptions<unknown>) => callMcpTool({
137
166
  serverName: server.name,
138
167
  toolName: mcpTool.name,
139
- };
140
-
141
- exposedTools[exposedToolName] = plugins.tool({
142
- description: mcpTool.description ?? `MCP tool ${mcpTool.name} from ${server.name}`,
143
- inputSchema: plugins.jsonSchema(mcpTool.inputSchema as never),
144
- execute: async (input: unknown, invocationOptions?: plugins.ToolExecutionOptions<unknown>) => {
145
- const normalizedArguments = normalizeMcpArguments(input);
146
- await options.authorizeToolCall?.({
147
- serverName: server.name,
148
- toolName: mcpTool.name,
149
- exposedToolName,
150
- arguments: normalizedArguments,
151
- tool: mcpTool,
152
- toolCallId: invocationOptions?.toolCallId ?? '',
153
- abortSignal: invocationOptions?.abortSignal,
154
- });
155
- const configuredCallToolOptions: plugins.RequestOptions = {
156
- ...options.requestOptions,
157
- ...options.callToolRequestOptions,
158
- };
159
- const result = await withMcpRequestOptions(
160
- configuredCallToolOptions,
161
- invocationOptions?.abortSignal,
162
- (requestOptions) => server.client.callTool(
163
- {
164
- name: mcpTool.name,
165
- arguments: normalizedArguments,
166
- },
167
- undefined,
168
- requestOptions,
169
- ),
170
- );
171
- const formattedResult = formatResult(result, {
172
- serverName: server.name,
173
- toolName: mcpTool.name,
174
- exposedToolName,
175
- });
176
- if ((result as Record<string, unknown>).isError === true) {
177
- throw new Error(formatMcpError(formattedResult));
178
- }
179
- return formattedResult;
180
- },
181
- });
182
- }
168
+ tool: mcpTool,
169
+ exposedToolName,
170
+ arguments: normalizeMcpArguments(input),
171
+ toolCallId: invocationOptions?.toolCallId ?? '',
172
+ abortSignal: invocationOptions?.abortSignal,
173
+ authorize: (context) => options.authorizeToolCall?.(context),
174
+ client: () => server.client,
175
+ options,
176
+ }),
177
+ });
183
178
  }
184
179
 
185
180
  return {
186
181
  tools: exposedTools,
187
182
  servers: connectedServers,
188
183
  toolNameMap,
184
+ isReadOnlyToolCall: ({ toolName }) => readOnlyToolNames.has(toolName),
189
185
  close: async () => closeMcpClients(connectedServers),
190
186
  };
191
187
  } catch (error) {
@@ -197,6 +193,114 @@ export const createMcpTools = async (
197
193
  }
198
194
  };
199
195
 
196
+ /** The tools one MCP server lists, as `listTools` answered them. `IMcpConnectedServer` is one. */
197
+ export interface IMcpToolCatalogServer {
198
+ name: string;
199
+ tools: readonly TMcpToolMetadata[];
200
+ }
201
+
202
+ export interface IMcpToolCatalogToolContext extends IMcpToolFormatContext {
203
+ tool: TMcpToolMetadata;
204
+ }
205
+
206
+ /** A catalog call of an MCP tool: the run's `call` context names the scope, session, run and `requestPermission`. */
207
+ export interface IMcpToolCatalogCallContext<TScope = unknown> extends IMcpToolFormatContext {
208
+ call: plugins.IFlexCatalogRunContext<TScope>;
209
+ }
210
+
211
+ export interface IMcpToolCatalogAuthorizeToolCallContext<TScope = unknown> extends IMcpAuthorizeToolCallContext {
212
+ abortSignal: AbortSignal;
213
+ call: plugins.IFlexCatalogRunContext<TScope>;
214
+ }
215
+
216
+ export interface ICreateMcpToolCatalogOptions<TScope = unknown> {
217
+ /** The servers and the tools they list, in the order `createMcpTools` was given them. */
218
+ servers: readonly IMcpToolCatalogServer[];
219
+ /**
220
+ * The client that runs a call: the one connected for the call's run, which `call.provider.runId`
221
+ * names. The catalog stays the same object across runs; only the clients change.
222
+ */
223
+ resolveClient(context: IMcpToolCatalogCallContext<TScope>): IMcpClientLike | PromiseLike<IMcpClientLike>;
224
+ /** As for `createMcpTools`: `<server>__<tool>`, by default when there is more than one server. */
225
+ prefixToolNames?: boolean;
226
+ requestOptions?: plugins.RequestOptions;
227
+ callToolRequestOptions?: plugins.RequestOptions;
228
+ formatResult?: (result: TMcpCallToolResult, context: IMcpToolFormatContext) => unknown;
229
+ /**
230
+ * Resolve to allow the call, or throw to deny it, before the client is resolved. It sees `execute`
231
+ * and `run_code` calls as `createMcpTools`' hook sees tool calls. Omission allows all calls.
232
+ */
233
+ authorizeToolCall?: (context: IMcpToolCatalogAuthorizeToolCallContext<TScope>) => PromiseLike<void> | void;
234
+ /**
235
+ * Declares a tool's method read-only: its calls run concurrently with other reads and a turn
236
+ * answers identical ones once. A tool whose calls ask for permission must not be. Omission
237
+ * declares none; a server's `readOnlyHint` annotation is not trusted for it.
238
+ */
239
+ isReadOnly?: (context: IMcpToolCatalogToolContext) => boolean;
240
+ }
241
+
242
+ export interface ICreateMcpToolCatalogResult<TScope = unknown> {
243
+ catalog: plugins.IFlexToolCatalog<TScope>;
244
+ /** The server and tool of each method path. */
245
+ toolNameMap: Record<string, IMcpToolNameMapping>;
246
+ }
247
+
248
+ /**
249
+ * The MCP tools of `servers` as a tool catalog, which the model searches, describes and calls through
250
+ * `execute` and `run_code`. A method's path is the name `createMcpTools` gives the tool; a name that
251
+ * is no identifier (with `-`, or a reserved word) is declared as `FlexToolCatalog.identifierOf()`
252
+ * makes it. Create one catalog per set of tool definitions and return the same object on every run.
253
+ */
254
+ export const createMcpToolCatalog = <TScope = unknown>(
255
+ options: ICreateMcpToolCatalogOptions<TScope>,
256
+ ): ICreateMcpToolCatalogResult<TScope> => {
257
+ if (typeof options?.resolveClient !== 'function') {
258
+ throw new Error('createMcpToolCatalog requires resolveClient().');
259
+ }
260
+ const shouldPrefixToolNames = options.prefixToolNames ?? options.servers.length > 1;
261
+ const named = nameMcpTools(options.servers, shouldPrefixToolNames);
262
+ // A name that is an identifier is its path; a mapped one gets a path no such name has.
263
+ const usedPaths = new Set(named.map(({ exposedToolName }) => exposedToolName)
264
+ .filter((exposedToolName) => catalogPathOf(exposedToolName) === exposedToolName));
265
+ const toolNameMap: Record<string, IMcpToolNameMapping> = {};
266
+ const methods: plugins.IFlexCatalogMethod<TScope, Record<string, unknown>>[] = [];
267
+ for (const { server, tool: mcpTool, exposedToolName } of named) {
268
+ const identifier = catalogPathOf(exposedToolName);
269
+ const path = identifier === exposedToolName ? identifier : createUniqueToolName(identifier, usedPaths);
270
+ usedPaths.add(path);
271
+ toolNameMap[path] = { serverName: server.name, toolName: mcpTool.name };
272
+ const toolContext: IMcpToolCatalogToolContext = {
273
+ serverName: server.name,
274
+ toolName: mcpTool.name,
275
+ exposedToolName: path,
276
+ tool: mcpTool,
277
+ };
278
+ const text = mcpTool.description?.trim() || `MCP tool ${mcpTool.name} from ${server.name}`;
279
+ const summary = summaryOf(text);
280
+ const description = text === summary ? undefined : truncateUtf8(text, plugins.FLEX_TOOL_CATALOG_LIMITS.maxDescriptionBytes);
281
+ methods.push({
282
+ method: path,
283
+ summary,
284
+ ...(description ? { description } : {}),
285
+ inputSchema: mcpTool.inputSchema,
286
+ ...(options.isReadOnly?.(toolContext) ? { readOnly: true } : {}),
287
+ run: (params, call) => callMcpTool({
288
+ serverName: server.name,
289
+ toolName: mcpTool.name,
290
+ tool: mcpTool,
291
+ exposedToolName: path,
292
+ arguments: normalizeMcpArguments(params),
293
+ toolCallId: call.toolCallId,
294
+ abortSignal: call.abortSignal,
295
+ authorize: (context) => options.authorizeToolCall?.({ ...context, abortSignal: call.abortSignal, call }),
296
+ client: () => options.resolveClient({ serverName: server.name, toolName: mcpTool.name, exposedToolName: path, call }),
297
+ options,
298
+ }),
299
+ });
300
+ }
301
+ return { catalog: { methods }, toolNameMap };
302
+ };
303
+
200
304
  export const sanitizeMcpToolName = (name: string, fallback = 'mcp_tool'): string => {
201
305
  const sanitized = name
202
306
  .trim()
@@ -283,7 +387,15 @@ const createMcpTransport = (
283
387
  config: Exclude<TMcpServerConfig, IMcpClientServerConfig>,
284
388
  ): plugins.Transport => {
285
389
  if (config.type === 'stdio') {
286
- const { type, clientName, clientVersion, clientOptions, requestOptions, ...serverParameters } = config;
390
+ const {
391
+ type,
392
+ clientName,
393
+ clientVersion,
394
+ clientOptions,
395
+ requestOptions,
396
+ trustReadOnlyHints,
397
+ ...serverParameters
398
+ } = config;
287
399
  return new plugins.StdioClientTransport(serverParameters);
288
400
  }
289
401
 
@@ -340,6 +452,100 @@ const normalizeMaxListToolsPages = (maxPages = DEFAULT_MAX_LIST_TOOLS_PAGES): nu
340
452
  return maxPages;
341
453
  };
342
454
 
455
+ /** Every tool of `servers` with the name it is exposed under, unique across them, in order. */
456
+ const nameMcpTools = <TServer extends IMcpToolCatalogServer>(
457
+ servers: readonly TServer[],
458
+ prefixToolNames: boolean,
459
+ ): Array<{ server: TServer; tool: TMcpToolMetadata; exposedToolName: string }> => {
460
+ const usedToolNames = new Set<string>(['invalid']);
461
+ const named: Array<{ server: TServer; tool: TMcpToolMetadata; exposedToolName: string }> = [];
462
+ for (const server of servers) {
463
+ for (const mcpTool of server.tools) {
464
+ const exposedToolName = createUniqueToolName(
465
+ createExposedToolName(server.name, mcpTool.name, prefixToolNames),
466
+ usedToolNames,
467
+ );
468
+ usedToolNames.add(exposedToolName);
469
+ named.push({ server, tool: mcpTool, exposedToolName });
470
+ }
471
+ }
472
+ return named;
473
+ };
474
+
475
+ interface IMcpToolCall extends IMcpToolFormatContext {
476
+ tool: TMcpToolMetadata;
477
+ arguments: Record<string, unknown>;
478
+ toolCallId: string;
479
+ abortSignal?: AbortSignal;
480
+ authorize: (context: IMcpAuthorizeToolCallContext) => PromiseLike<void> | void;
481
+ client: () => IMcpClientLike | PromiseLike<IMcpClientLike>;
482
+ options: Pick<ICreateMcpToolsOptions, 'requestOptions' | 'callToolRequestOptions' | 'formatResult'>;
483
+ }
484
+
485
+ /** Authorizes a call, runs it on the client and formats its result; an MCP error result throws. */
486
+ const callMcpTool = async (call: IMcpToolCall): Promise<unknown> => {
487
+ await call.authorize({
488
+ serverName: call.serverName,
489
+ toolName: call.toolName,
490
+ exposedToolName: call.exposedToolName,
491
+ arguments: call.arguments,
492
+ tool: call.tool,
493
+ toolCallId: call.toolCallId,
494
+ abortSignal: call.abortSignal,
495
+ });
496
+ const client = await call.client();
497
+ const configuredCallToolOptions: plugins.RequestOptions = {
498
+ ...call.options.requestOptions,
499
+ ...call.options.callToolRequestOptions,
500
+ };
501
+ const result = await withMcpRequestOptions(
502
+ configuredCallToolOptions,
503
+ call.abortSignal,
504
+ (requestOptions) => client.callTool(
505
+ {
506
+ name: call.toolName,
507
+ arguments: call.arguments,
508
+ },
509
+ undefined,
510
+ requestOptions,
511
+ ),
512
+ );
513
+ const formattedResult = (call.options.formatResult ?? formatMcpToolResult)(result, {
514
+ serverName: call.serverName,
515
+ toolName: call.toolName,
516
+ exposedToolName: call.exposedToolName,
517
+ });
518
+ if ((result as Record<string, unknown>).isError === true) {
519
+ throw new Error(formatMcpError(formattedResult));
520
+ }
521
+ return formattedResult;
522
+ };
523
+
524
+ /** A catalog method path: the exposed name where it is an identifier, else the identifier made of it. */
525
+ const catalogPathOf = (exposedToolName: string): string => plugins.FlexToolCatalog.identifierOf(exposedToolName);
526
+
527
+ /** The first line of a description, as one line of at most the catalog's summary length. */
528
+ const summaryOf = (text: string): string => {
529
+ const line = text.split(/\r?\n/).find((candidate) => candidate.trim())!.replace(/[\s\u0000-\u001f\u007f]+/g, ' ').trim();
530
+ const characters = [...line];
531
+ const max = plugins.FLEX_TOOL_CATALOG_LIMITS.maxSummaryCharacters;
532
+ return characters.length <= max ? line : `${characters.slice(0, max - 1).join('').trimEnd()}…`;
533
+ };
534
+
535
+ /** The text cut to at most `maxBytes` UTF-8 bytes on a character boundary, marked with `…` when cut. */
536
+ const truncateUtf8 = (text: string, maxBytes: number): string => {
537
+ const encoder = new TextEncoder();
538
+ if (encoder.encode(text).length <= maxBytes) return text;
539
+ let bytes = encoder.encode('…').length;
540
+ let kept = '';
541
+ for (const character of text) {
542
+ bytes += encoder.encode(character).length;
543
+ if (bytes > maxBytes) break;
544
+ kept += character;
545
+ }
546
+ return `${kept}…`;
547
+ };
548
+
343
549
  const createExposedToolName = (
344
550
  serverName: string,
345
551
  toolName: string,
package/ts_mcp/plugins.ts CHANGED
@@ -10,6 +10,12 @@ export type { StreamableHTTPClientTransportOptions } from '@modelcontextprotocol
10
10
  export type { RequestOptions } from '@modelcontextprotocol/sdk/shared/protocol.js';
11
11
  export type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
12
12
 
13
+ // @modelprofile.com scope
14
+ import { FLEX_TOOL_CATALOG_LIMITS, FlexToolCatalog } from '@modelprofile.com/flexharness';
15
+
16
+ export { FLEX_TOOL_CATALOG_LIMITS, FlexToolCatalog };
17
+ export type { IFlexCatalogMethod, IFlexCatalogRunContext, IFlexToolCatalog } from '@modelprofile.com/flexharness';
18
+
13
19
  // ai-sdk core
14
20
  import { jsonSchema, tool } from 'ai';
15
21
 
package/ts_mcp/readme.md CHANGED
@@ -8,6 +8,18 @@ the agent and harness do not install or register vendor SDKs.
8
8
  pnpm add @modelprofile.com/flexharness @modelcontextprotocol/sdk
9
9
  ```
10
10
 
11
+ `createMcpTools()` connects MCP servers and exposes their tools as AI SDK tools.
12
+ Its `isReadOnlyToolCall` is the tool handle's classifier for them: a call is read-only when the
13
+ server annotates its tool `readOnlyHint: true` and the server's configuration sets
14
+ `trustReadOnlyHints: true`, so those reads run concurrently. No server's annotations are trusted
15
+ by default; `servers` carries each server's `trustReadOnlyHints`.
16
+ `createMcpToolCatalog()` makes the tools servers list into a FlexHarness tool catalog
17
+ (`search_tools`, `describe_tools`, `execute`, `run_code`): one catalog object for every
18
+ run, each call on the client `resolveClient` names for its run, method paths equal to
19
+ `createMcpTools()`' tool names, `authorizeToolCall` deciding catalog calls as it decides
20
+ tool calls, and read-only methods only where `isReadOnly` declares them. The root readme's
21
+ Tool Catalog section shows a tool provider that connects clients per run.
22
+
11
23
  See the [toolbox usage and migration guide](https://code.foss.global/modelprofile.com/flexharness#readme)
12
24
  for composition examples and the SmartAI/SmartAgent migration map.
13
25
 
package/ts_tools/index.ts CHANGED
@@ -9,7 +9,7 @@ export type {
9
9
  IToolExecutionContextLease,
10
10
  } from './plugins.js';
11
11
  export type { ICreateBrowserToolsOptions } from './tool.browser.js';
12
- export { createFilesystemTools } from './tool.filesystem.js';
12
+ export { createFilesystemTools, isReadOnlyFilesystemToolCall } from './tool.filesystem.js';
13
13
  export type { ICreateFilesystemToolsOptions } from './tool.filesystem.js';
14
14
  export { createShellTools } from './tool.shell.js';
15
15
  export type { ICreateShellToolsOptions } from './tool.shell.js';
@@ -15,6 +15,8 @@ for composition examples and the SmartAI/SmartAgent migration map.
15
15
 
16
16
  `createFilesystemTools(context, options)` exposes `read_file`, `write_file`, `list_directory` and, unless `includeDelete: false`, `delete_file` over the context's `fs`. `write_file` and `delete_file` ask `context.requestPermission` (`write`, `delete`) before they change anything.
17
17
 
18
+ `isReadOnlyFilesystemToolCall({ toolName })` is true for `read_file` and `list_directory`, which change nothing and ask for no permission. Pass it as the FlexHarness tool handle's `isReadOnlyToolCall`, or ask it from that classifier, and their calls run concurrently; leave it out for a context whose `fs.readFile` or `fs.listDirectory` changes something or asks for permission.
19
+
18
20
  `includeEdit: true` adds `edit_file { path, find, replace, replaceAll? }`. It replaces one exact occurrence of `find`, or every occurrence with `replaceAll`, and refuses a `find` that does not occur or, without `replaceAll`, occurs more than once. The replacement text is literal. It asks for `write` permission before it reads the file, so an edit made while the request waits is kept. It is off by default, so the tools an existing agent sees do not change.
19
21
 
20
22
  ```typescript
@@ -15,6 +15,18 @@ export interface ICreateFilesystemToolsOptions {
15
15
  maxBytes?: number;
16
16
  }
17
17
 
18
+ /** The tools of `createFilesystemTools()` that only read. */
19
+ const readOnlyFilesystemToolNames: ReadonlySet<string> = new Set(['read_file', 'list_directory']);
20
+
21
+ /**
22
+ * Whether a call of `createFilesystemTools()`' tools only reads: `read_file` and `list_directory`
23
+ * change nothing and ask for no permission. Pass it as the tool handle's `isReadOnlyToolCall`, or ask
24
+ * it from that classifier, when the context's `fs.readFile` and `fs.listDirectory` do neither either.
25
+ */
26
+ export function isReadOnlyFilesystemToolCall(call: { toolName: string }): boolean {
27
+ return readOnlyFilesystemToolNames.has(call.toolName);
28
+ }
29
+
18
30
  export function createFilesystemTools(context: IToolExecutionContext, options: ICreateFilesystemToolsOptions = {}): plugins.ToolSet {
19
31
  const truncate = (output: unknown) => plugins.truncateOutput(formatToolOutput(output), {
20
32
  maxLines: options.maxLines,