@goodtiming/baton-sdk 1.3.0 → 1.3.2

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 CHANGED
@@ -57,7 +57,7 @@ Sending to your own collector instead, or trying the package before you have a k
57
57
  withBaton(server, { dsn: process.env.BATON_DSN, tools: TOOLS }); // or tools: () => TOOLS
58
58
  ```
59
59
 
60
- Baton reads `tools` once per `withBaton` or `createBaton`, and never calls your `tools/list` handler itself. It also needs `tools` to leave its subagent sentence out of the instructions when a tool declares its own `user_goal`.
60
+ Baton reads `tools` once per `withBaton` or `createBaton`, and never calls your `tools/list` handler itself. It also needs `tools` to leave its subagent sentence out of the instructions when a tool declares its own `user_goal`. The snapshot of your tools comes from the lists you serve, never from `tools`, so a server sends none until it has served its whole list in one page.
61
61
  - If your list has a tool with the report tool's name, yours is kept, Baton's report tool is not added, and the instructions stop naming it; the handle's `annotationToolName` still does. Until Baton has seen that list, a call to that name reaches Baton's report tool, so pass `tools` or set `annotationToolName` to another name.
62
62
  - Baton has no tool registry to check a call against here, so `failure_kind` is never `unknown_tool`, `invalid_argument` or `output_schema_mismatch`. Such a failure is recorded as an error with no `failure_kind`.
63
63
 
package/dist/index.cjs CHANGED
@@ -53,7 +53,7 @@ __export(index_exports, {
53
53
  module.exports = __toCommonJS(index_exports);
54
54
 
55
55
  // src/version.ts
56
- var SDK_VERSION = "ts-1.3.0";
56
+ var SDK_VERSION = "ts-1.3.2";
57
57
 
58
58
  // src/events.ts
59
59
  var import_zod = require("zod");
@@ -287,7 +287,8 @@ var PromptListErrorPayloadSchema = import_zod.z.object({
287
287
  var ToolListStartPayloadSchema = import_zod.z.object({}).strict();
288
288
  var ToolListEndPayloadSchema = import_zod.z.object({
289
289
  count: import_zod.z.number().int(),
290
- ...timingShape
290
+ ...timingShape,
291
+ surface_hash: import_zod.z.string().nullable().optional()
291
292
  }).strict();
292
293
  var ToolListErrorPayloadSchema = import_zod.z.object({
293
294
  ...failureShape
@@ -507,6 +508,9 @@ function isPlainObject(value) {
507
508
  function isRecord(value) {
508
509
  return typeof value === "object" && value !== null && !Array.isArray(value);
509
510
  }
511
+ function jsonObjectCopy(value) {
512
+ return isRecord(value) ? JSON.parse(JSON.stringify(value)) : void 0;
513
+ }
510
514
 
511
515
  // src/scrub.ts
512
516
  var DEPTH_LIMIT = 10;
@@ -3085,12 +3089,14 @@ function standardInputJsonSchema(schema) {
3085
3089
  }
3086
3090
  }
3087
3091
  function toolInputJsonSchema(inputSchema2) {
3088
- const objSchema = normalizeObjectSchema(inputSchema2);
3089
- if (!objSchema) return EMPTY_OBJECT_JSON_SCHEMA;
3090
- return toJsonSchemaCompat(objSchema, {
3091
- strictUnions: true,
3092
- pipeStrategy: "input"
3093
- });
3092
+ return toolJsonSchema(inputSchema2, "input") ?? EMPTY_OBJECT_JSON_SCHEMA;
3093
+ }
3094
+ function toolOutputJsonSchema(outputSchema) {
3095
+ return outputSchema ? toolJsonSchema(outputSchema, "output") : void 0;
3096
+ }
3097
+ function toolJsonSchema(schema, pipeStrategy) {
3098
+ const objSchema = normalizeObjectSchema(schema);
3099
+ return objSchema ? toJsonSchemaCompat(objSchema, { strictUnions: true, pipeStrategy }) : void 0;
3094
3100
  }
3095
3101
 
3096
3102
  // src/integrations/mcp/safeScrub.ts
@@ -3241,19 +3247,29 @@ var ANNOTATION_INPUT_SHAPE = {
3241
3247
  // zod renders "any value" as `{}`, which some MCP clients refuse (reportToolSchema.test.ts).
3242
3248
  context: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()).meta({ additionalProperties: true }).nullable().optional()
3243
3249
  };
3250
+ var ANNOTATION_ARGS = import_zod3.z.object(ANNOTATION_INPUT_SHAPE);
3244
3251
  function isReport(whatHappened) {
3245
3252
  return typeof whatHappened === "string" && !isBlank(whatHappened);
3246
3253
  }
3247
- function registerAnnotationTool(server, options) {
3254
+ var INVALID_PARAMS = -32602;
3255
+ function registerAnnotationTool(server, options, callArguments) {
3248
3256
  const tool = buildAnnotationTool(server, options);
3249
- server.registerTool(
3250
- tool.name,
3251
- { description: tool.description, inputSchema: ANNOTATION_INPUT_SHAPE },
3252
- tool.handler
3253
- );
3254
- return tool.name;
3257
+ if (callArguments === void 0) {
3258
+ server.registerTool(tool.name, { description: tool.description, inputSchema: ANNOTATION_INPUT_SHAPE }, tool.handler);
3259
+ return;
3260
+ }
3261
+ server.registerTool(tool.name, { description: tool.description }, async (extra) => {
3262
+ const args = callArguments();
3263
+ const { validateToolInput } = server;
3264
+ let parsed;
3265
+ try {
3266
+ parsed = await validateToolInput?.call(server, { inputSchema: ANNOTATION_ARGS }, args, tool.name);
3267
+ } catch (err) {
3268
+ if (err?.code === INVALID_PARAMS) throw err;
3269
+ }
3270
+ return isRecord(parsed) ? tool.handler(parsed, extra) : callAnnotationTool(tool, args, extra);
3271
+ });
3255
3272
  }
3256
- var ANNOTATION_ARGS = import_zod3.z.object(ANNOTATION_INPUT_SHAPE);
3257
3273
  function annotationInputJsonSchema(major) {
3258
3274
  return annotationJsonSchemas[major] ??= (major === 2 ? standardInputJsonSchema(ANNOTATION_ARGS) : void 0) ?? toolInputJsonSchema(ANNOTATION_INPUT_SHAPE);
3259
3275
  }
@@ -4041,6 +4057,9 @@ function assembleSurface(serverMeta, tools) {
4041
4057
  tools
4042
4058
  };
4043
4059
  }
4060
+ function isWholeList(cursor, nextCursor) {
4061
+ return (cursor === void 0 || cursor === null) && (nextCursor === void 0 || nextCursor === null);
4062
+ }
4044
4063
  var SENT_SURFACES_CAP = 256;
4045
4064
  var SentSurfaces = class {
4046
4065
  hashes = /* @__PURE__ */ new Set();
@@ -4084,12 +4103,14 @@ var CLEAN_OUTCOME = { failed: false, raised: false, result: void 0 };
4084
4103
  var INTENT_PARAM_NAMES = [USER_GOAL_PARAM_NAME, ...RETIRED_PARAM_NAMES];
4085
4104
  var callSlots = new asyncHooks.AsyncLocalStorage();
4086
4105
  var SurfaceState = class {
4087
- constructor(serverMeta, sentSurfaces) {
4106
+ constructor(serverMeta, sentSurfaces, fromRegistry) {
4088
4107
  this.serverMeta = serverMeta;
4089
4108
  this.sentSurfaces = sentSurfaces;
4109
+ this.fromRegistry = fromRegistry;
4090
4110
  }
4091
4111
  serverMeta;
4092
4112
  sentSurfaces;
4113
+ fromRegistry;
4093
4114
  /** Every tool this install has seen, keyed by its CURRENT name. `disabled`
4094
4115
  * is tracked alongside rather than pruned, so re-enabling costs nothing and
4095
4116
  * — crucially — never needs a re-capture, which would re-read Baton's own
@@ -4097,20 +4118,74 @@ var SurfaceState = class {
4097
4118
  rawTools = /* @__PURE__ */ new Map();
4098
4119
  dirty = false;
4099
4120
  servedOverlayId = null;
4121
+ listed = false;
4122
+ /** `null` when this version of the surface could not be captured. */
4123
+ captured;
4124
+ /** The surface each low-level request was served, keyed by the request,
4125
+ * which every seam passes on as it came. Kept apart from the current
4126
+ * surface, which another listing can change before this one's end is sent:
4127
+ * `surfaceAtList.test.ts`, "gives two listings that finish together each its own hash". */
4128
+ listedSurfaces = /* @__PURE__ */ new WeakMap();
4129
+ get hasSurface() {
4130
+ return this.fromRegistry || this.listed;
4131
+ }
4132
+ /** A low-level Server served `request` its whole list, which then is the
4133
+ * surface: `surfaceAtList.test.ts`, "snapshots each list as served". */
4134
+ servedList(request, names) {
4135
+ this.listed = true;
4136
+ for (const name of [...this.rawTools.keys()]) {
4137
+ if (!names.has(name)) this.pruneTool(name);
4138
+ }
4139
+ const surface = this.capture();
4140
+ if (surface !== null) this.listedSurfaces.set(request, surface);
4141
+ }
4142
+ /** The surface a `tools/list` request was served, when it got the whole list. */
4143
+ surfaceListed(request, served) {
4144
+ const listed = this.listedSurfaces.get(request);
4145
+ if (listed !== void 0) return listed;
4146
+ const cursor = isRecord(request.params) ? request.params.cursor : void 0;
4147
+ const whole = isWholeList(cursor, isRecord(served) ? served.nextCursor : void 0);
4148
+ return this.fromRegistry && whole ? this.capture() : null;
4149
+ }
4150
+ /** The surface as it stands, and its hash; built once per change, so a
4151
+ * surface that cannot be hashed is reported once, not on every request. */
4152
+ capture() {
4153
+ if (this.captured !== void 0) return this.captured;
4154
+ try {
4155
+ const snapshot = this.buildSnapshot();
4156
+ this.captured = { snapshot, digest: surfaceHash(snapshot) };
4157
+ } catch (err) {
4158
+ stderrLine(`baton: surface snapshot capture failed: ${printable(err)}`);
4159
+ this.captured = null;
4160
+ }
4161
+ return this.captured;
4162
+ }
4163
+ changed() {
4164
+ this.dirty = true;
4165
+ this.captured = void 0;
4166
+ }
4100
4167
  /** `inputSchemaJson` is already converted — by `ctx.vendorToolJsonSchema`,
4101
4168
  * which picks the spelling THIS SDK major puts on the wire. */
4102
- noteTool(name, inputSchemaJson, description) {
4169
+ noteTool(name, inputSchemaJson, description, outputSchemaJson) {
4103
4170
  const before = this.rawTools.get(name);
4104
- const tool = { name, description: description ?? null, inputSchema: inputSchemaJson };
4105
- if (before !== void 0 && before.tool.description === tool.description) {
4106
- const same = before.tool.inputSchema === inputSchemaJson || JSON.stringify(before.tool.inputSchema) === JSON.stringify(inputSchemaJson);
4107
- if (same) return;
4171
+ if (before !== void 0 && before.tool.description === (description ?? null)) {
4172
+ const sameInput = before.tool.inputSchema === inputSchemaJson || JSON.stringify(before.tool.inputSchema) === JSON.stringify(inputSchemaJson);
4173
+ if (sameInput && JSON.stringify(before.tool.outputSchema) === JSON.stringify(outputSchemaJson)) return;
4108
4174
  }
4175
+ const tool = { name, description: description ?? null, inputSchema: inputSchemaJson };
4176
+ const outputSchema = jsonObjectCopy(outputSchemaJson);
4177
+ if (outputSchema !== void 0) tool.outputSchema = outputSchema;
4109
4178
  this.rawTools.set(name, { disabled: before?.disabled ?? false, tool });
4110
- this.dirty = true;
4179
+ this.changed();
4180
+ }
4181
+ setOutputSchema(name, outputSchemaJson) {
4182
+ const tool = this.rawTools.get(name)?.tool;
4183
+ if (tool === void 0) return;
4184
+ const description = typeof tool.description === "string" ? tool.description : void 0;
4185
+ this.noteTool(name, tool.inputSchema, description, outputSchemaJson);
4111
4186
  }
4112
4187
  pruneTool(name) {
4113
- if (this.rawTools.delete(name)) this.dirty = true;
4188
+ if (this.rawTools.delete(name)) this.changed();
4114
4189
  }
4115
4190
  /** Follow a `update({name})` rename, keeping the snapshot keyed the way
4116
4191
  * `tools/list` now renders it. */
@@ -4119,7 +4194,7 @@ var SurfaceState = class {
4119
4194
  if (!entry) return;
4120
4195
  this.rawTools.delete(from);
4121
4196
  this.rawTools.set(to, { ...entry, tool: { ...entry.tool, name: to } });
4122
- this.dirty = true;
4197
+ this.changed();
4123
4198
  }
4124
4199
  /** Both majors filter `tools/list` on `tool.enabled`, so a disabled tool is
4125
4200
  * not part of the surface a client sees and must not be part of the one we
@@ -4129,7 +4204,7 @@ var SurfaceState = class {
4129
4204
  const entry = this.rawTools.get(name);
4130
4205
  if (!entry || entry.disabled === !enabled) return;
4131
4206
  entry.disabled = !enabled;
4132
- this.dirty = true;
4207
+ this.changed();
4133
4208
  }
4134
4209
  buildSnapshot() {
4135
4210
  const names = [...this.rawTools.keys()].sort().filter((n) => !this.rawTools.get(n).disabled);
@@ -4404,6 +4479,7 @@ function stripIntentParams(params, ctx) {
4404
4479
  return stripped;
4405
4480
  }
4406
4481
  async function maybeEmitSurfaceSnapshot(ctx) {
4482
+ if (!ctx.surfaceState.hasSurface) return;
4407
4483
  const overlayId = ctx.perCaller?.serverOverlay?.id ?? null;
4408
4484
  if (overlayId !== ctx.surfaceState.servedOverlayId) {
4409
4485
  ctx.surfaceState.servedOverlayId = overlayId;
@@ -4411,17 +4487,13 @@ async function maybeEmitSurfaceSnapshot(ctx) {
4411
4487
  }
4412
4488
  if (!ctx.surfaceState.dirty) return;
4413
4489
  ctx.surfaceState.dirty = false;
4414
- let snapshot;
4415
- let digest;
4416
- try {
4417
- snapshot = ctx.surfaceState.buildSnapshot();
4418
- digest = surfaceHash(snapshot);
4419
- } catch (err) {
4420
- stderrLine(`baton: surface snapshot capture failed: ${String(err)}`);
4421
- return;
4422
- }
4490
+ const surface = ctx.surfaceState.capture();
4491
+ if (surface !== null) await sendSurface(ctx, surface);
4492
+ }
4493
+ async function sendSurface(ctx, { snapshot, digest }) {
4494
+ const overlayId = ctx.perCaller?.serverOverlay?.id ?? null;
4423
4495
  const sent = ctx.surfaceState.sentSurfaces;
4424
- if (sent.wasSent(digest, overlayId)) return;
4496
+ if (sent.wasSent(digest, overlayId)) return digest;
4425
4497
  sent.mark(digest, overlayId);
4426
4498
  const seam = buildSeamAugmentations({
4427
4499
  injectedToolNames: ctx.annotationToolName ? [ctx.annotationToolName] : [],
@@ -4438,6 +4510,16 @@ async function maybeEmitSurfaceSnapshot(ctx) {
4438
4510
  });
4439
4511
  if (ctx.captureSettings !== void 0) seam.server_overlay = overlayId;
4440
4512
  await ctx.emitSurface(ctx.fallbackSessionId, digest, { ...snapshot, seam_augmentations: seam });
4513
+ return digest;
4514
+ }
4515
+ async function listedSurfaceFields(ctx, request, served) {
4516
+ try {
4517
+ const surface = isRecord(request) ? ctx.surfaceState.surfaceListed(request, served) : null;
4518
+ return surface === null ? {} : { surface_hash: await sendSurface(ctx, surface) };
4519
+ } catch (err) {
4520
+ stderrLine(`baton: surface snapshot capture failed: ${printable(err)}`);
4521
+ return {};
4522
+ }
4441
4523
  }
4442
4524
  function declaredIntentParams(vendorSchema) {
4443
4525
  const declared2 = {};
@@ -4450,7 +4532,7 @@ function captureAndInject(name, entry, ctx) {
4450
4532
  if (!entry || typeof entry !== "object") return;
4451
4533
  const mutable = entry;
4452
4534
  const vendorSchema = ctx.vendorToolJsonSchema(name, mutable.inputSchema);
4453
- ctx.surfaceState.noteTool(name, vendorSchema, mutable.description);
4535
+ ctx.surfaceState.noteTool(name, vendorSchema, mutable.description, ctx.vendorOutputJsonSchema(entry));
4454
4536
  if (declaresParam(vendorSchema, USER_GOAL_PARAM_NAME)) ctx.dropSubagentClause(name);
4455
4537
  const isV2 = typeof mutable.executor === "function";
4456
4538
  try {
@@ -4472,11 +4554,9 @@ var TOOLS_LIST_SEAM = /* @__PURE__ */ Symbol("batonToolsListSeam");
4472
4554
  var TOOLS_CALL_METHOD = "tools/call";
4473
4555
  var TOOLS_CALL_SEAM = /* @__PURE__ */ Symbol("batonToolsCallSeam");
4474
4556
  function installRequestSeam(lowLevel, method, seamTag, wrap) {
4475
- const table = lowLevel._requestHandlers;
4476
- const originalSet = lowLevel.setRequestHandler;
4477
- if (!(table instanceof Map) || typeof originalSet !== "function") return;
4478
- const handlers = table;
4479
- const setRequestHandler = originalSet;
4557
+ if (!takesRequestSeams(lowLevel)) return;
4558
+ const handlers = lowLevel._requestHandlers;
4559
+ const setRequestHandler = lowLevel.setRequestHandler;
4480
4560
  const wrapCurrent = () => {
4481
4561
  const handler = handlers.get(method);
4482
4562
  if (typeof handler !== "function" || handler[seamTag]) return;
@@ -4492,18 +4572,24 @@ function installRequestSeam(lowLevel, method, seamTag, wrap) {
4492
4572
  return registered;
4493
4573
  };
4494
4574
  }
4575
+ function takesRequestSeams(lowLevel) {
4576
+ return lowLevel._requestHandlers instanceof Map && typeof lowLevel.setRequestHandler === "function";
4577
+ }
4495
4578
  function installToolsListSeam(lowLevel, ctx, listsOwnReportTool) {
4496
4579
  const names = [...requiredParamNames(ctx.intentParamMode), TOOL_NAME_PARAM_NAME];
4497
4580
  const isInjected = (toolName, paramName) => toolName === ctx.annotationToolName ? paramName === TOOL_NAME_PARAM_NAME : ctx.paramRegistry.get(toolName)?.[paramName] === "injected";
4498
4581
  const advertise = (result) => {
4582
+ let served = result;
4499
4583
  try {
4500
- const served = listsOwnReportTool || ctx.annotationToolName === "" ? result : withToolInputSchema(result, ctx.annotationToolName, annotationInputJsonSchema(lowLevelMajor(lowLevel)));
4584
+ if (!listsOwnReportTool && ctx.annotationToolName !== "") {
4585
+ served = withToolInputSchema(result, ctx.annotationToolName, annotationInputJsonSchema(lowLevelMajor(lowLevel)));
4586
+ }
4501
4587
  return advertiseIntentParamsRequired(served, names, isInjected);
4502
4588
  } catch (err) {
4503
4589
  stderrLine(
4504
- `baton: tools/list advertisement failed; serving the SDK's own result: ${String(err)}`
4590
+ `baton: tools/list advertisement failed; serving the list without it: ${String(err)}`
4505
4591
  );
4506
- return result;
4592
+ return served;
4507
4593
  }
4508
4594
  };
4509
4595
  installRequestSeam(lowLevel, TOOLS_LIST_METHOD, TOOLS_LIST_SEAM, (handler) => (...args) => {
@@ -4560,10 +4646,6 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4560
4646
  const name = reportTool2?.name;
4561
4647
  if (name !== void 0 && tools.some((tool) => tool.name === name)) leaveReportNameToVendor(name);
4562
4648
  };
4563
- const read = (tools, { surface }) => {
4564
- if (surface) takeReportNameIfListed(tools);
4565
- readToolList(tools, ctx, surface, skipNote);
4566
- };
4567
4649
  const learn = (tools) => {
4568
4650
  keepGoalIfDeclared(tools);
4569
4651
  for (const tool of tools) {
@@ -4591,8 +4673,8 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4591
4673
  );
4592
4674
  }
4593
4675
  }
4594
- read(known.givenTools, { surface: true });
4595
- read([...known.served.values()], { surface: false });
4676
+ takeReportNameIfListed(known.givenTools);
4677
+ readToolList([...known.givenTools, ...known.served.values()], ctx, false, skipNote);
4596
4678
  };
4597
4679
  installRequestSeam(lowLevel, TOOLS_LIST_METHOD, LOW_LEVEL_LIST_SEAM, (handler) => async (...args) => {
4598
4680
  catchUp();
@@ -4602,8 +4684,14 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4602
4684
  if (listed === null) return result;
4603
4685
  const tools = toolInfos(listed, skipTool);
4604
4686
  learn(tools);
4605
- read(tools, { surface: true });
4606
- const firstPage = args[0]?.params?.cursor === void 0;
4687
+ takeReportNameIfListed(tools);
4688
+ readToolList(tools, ctx, true, skipNote);
4689
+ const request = args[0];
4690
+ const cursor = isRecord(request) && isRecord(request.params) ? request.params.cursor : void 0;
4691
+ if (isRecord(request) && isWholeList(cursor, result.nextCursor)) {
4692
+ ctx.surfaceState.servedList(request, new Set(tools.map((tool) => tool.name)));
4693
+ }
4694
+ const firstPage = cursor === void 0 || cursor === null;
4607
4695
  return servedToolList(result, listed, ctx, firstPage && reportTool2 ? listedReportTool : void 0);
4608
4696
  } catch (err) {
4609
4697
  stderrLine(`baton: tools/list capture failed; serving the vendor's own result: ${printable(err)}`);
@@ -4667,7 +4755,8 @@ function toolInfos(tools, skip) {
4667
4755
  try {
4668
4756
  name = isRecord(tool) ? tool.name : void 0;
4669
4757
  if (typeof name !== "string") continue;
4670
- infos.push({ name, description: tool.description, inputSchema: tool.inputSchema });
4758
+ const { description, inputSchema: inputSchema2, outputSchema } = tool;
4759
+ infos.push({ name, description, inputSchema: inputSchema2, outputSchema });
4671
4760
  } catch (err) {
4672
4761
  if (typeof name === "string") skip(name, err);
4673
4762
  }
@@ -4675,11 +4764,16 @@ function toolInfos(tools, skip) {
4675
4764
  return infos;
4676
4765
  }
4677
4766
  function readToolList(tools, ctx, surface, skip) {
4678
- for (const { name, inputSchema: inputSchema2, description } of tools) {
4767
+ for (const { name, inputSchema: inputSchema2, description, outputSchema } of tools) {
4679
4768
  try {
4680
4769
  const schema = isRecord(inputSchema2) ? inputSchema2 : null;
4681
4770
  if (surface) {
4682
- ctx.surfaceState.noteTool(name, schema ?? EMPTY_OBJECT_JSON_SCHEMA, typeof description === "string" ? description : void 0);
4771
+ ctx.surfaceState.noteTool(
4772
+ name,
4773
+ schema ?? EMPTY_OBJECT_JSON_SCHEMA,
4774
+ typeof description === "string" ? description : void 0,
4775
+ isRecord(outputSchema) ? outputSchema : void 0
4776
+ );
4683
4777
  }
4684
4778
  const dispositions = declaredIntentParams(schema);
4685
4779
  if (dispositions[USER_GOAL_PARAM_NAME] === "native") ctx.dropSubagentClause(name);
@@ -4715,7 +4809,7 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
4715
4809
  const requestParams = request?.params ?? {};
4716
4810
  const extra = args[1];
4717
4811
  const opened = await openLifecycleCall(ctx, spec, requestParams, extra);
4718
- const fire = async (schema, payload) => {
4812
+ const fire = async (schema, payload, unscrubbed = {}) => {
4719
4813
  if (opened === null) return;
4720
4814
  await emit(
4721
4815
  ctx.sink,
@@ -4723,7 +4817,7 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
4723
4817
  ...opened.common,
4724
4818
  sequence_number: ctx.counter.nextFor(opened.session),
4725
4819
  captured_at: (/* @__PURE__ */ new Date()).toISOString(),
4726
- payload: scrubLifecyclePayload(ctx, { ...opened.subject, ...payload })
4820
+ payload: { ...scrubLifecyclePayload(ctx, { ...opened.subject, ...payload }), ...unscrubbed }
4727
4821
  })
4728
4822
  );
4729
4823
  };
@@ -4734,10 +4828,15 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
4734
4828
  const served = await handler(...args);
4735
4829
  if (isInputRequired(served)) return served;
4736
4830
  const result = opened !== null && spec.rewriteResult !== void 0 ? spec.rewriteResult(ctx, served, opened.caller) : served;
4737
- await fire(spec.schemas.end, {
4738
- ...spec.countKey === void 0 ? {} : { count: countOf(result, spec.countKey) },
4739
- duration_ms: elapsed()
4740
- });
4831
+ const duration = elapsed();
4832
+ await fire(
4833
+ spec.schemas.end,
4834
+ {
4835
+ ...spec.countKey === void 0 ? {} : { count: countOf(result, spec.countKey) },
4836
+ duration_ms: duration
4837
+ },
4838
+ await spec.endFields?.(ctx, request, served)
4839
+ );
4741
4840
  return result;
4742
4841
  } catch (err) {
4743
4842
  await fire(spec.schemas.error, {
@@ -4857,7 +4956,8 @@ var LIFECYCLE_SPECS = [
4857
4956
  },
4858
4957
  countKey: "tools",
4859
4958
  startParams: NO_PARAMS,
4860
- rewriteResult: (ctx, result, caller) => ctx.perCaller?.toolsFor(result, caller) ?? result
4959
+ rewriteResult: (ctx, result, caller) => ctx.perCaller?.toolsFor(result, caller) ?? result,
4960
+ endFields: listedSurfaceFields
4861
4961
  }
4862
4962
  ];
4863
4963
  function installLifecycleSeams(lowLevel, ctx) {
@@ -4901,14 +5001,15 @@ function installToolsCallSeam(lowLevel, toolEntry, ctx) {
4901
5001
  (handler) => async (...args) => {
4902
5002
  const request = args[0];
4903
5003
  const name = request?.params?.name;
5004
+ const toolArgs = request?.params?.arguments;
4904
5005
  const slot = {
4905
5006
  innerFired: false,
4906
5007
  flushTerminal: null,
4907
5008
  stripped: stripIntentParams(request?.params, ctx),
4908
5009
  strippedFor: typeof name === "string" ? name : null,
4909
- validation: "unseen"
5010
+ validation: "unseen",
5011
+ arguments: name === ctx.annotationToolName ? toolArgs : void 0
4910
5012
  };
4911
- const toolArgs = request?.params?.arguments;
4912
5013
  if (typeof name === "string" && toolEntry(name) != null && await hiddenFrom(slot, name, isRecord(toolArgs) ? toolArgs : {}, args[1], ctx)) {
4913
5014
  return answerHidden(lowLevel, name, (outcome2) => settleCall(slot, outcome2, args, toolEntry, ctx, "tool_hidden"));
4914
5015
  }
@@ -5100,6 +5201,8 @@ function wireEntry(name, entry, ctx) {
5100
5201
  }
5101
5202
  if (u.paramsSchema !== void 0) {
5102
5203
  captureAndInject(nameRef.current, entry, ctx);
5204
+ } else if (u.outputSchema !== void 0) {
5205
+ ctx.surfaceState.setOutputSchema(nameRef.current, ctx.vendorOutputJsonSchema(entry));
5103
5206
  }
5104
5207
  if (typeof u.enabled === "boolean") {
5105
5208
  ctx.surfaceState.setToolEnabled(nameRef.current, u.enabled);
@@ -5225,7 +5328,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5225
5328
  const counter2 = new ServerCounter(sharedCounter, fallbackSessionId);
5226
5329
  const tenantId = resolveTenantId(config.tenantId, config.vendorId);
5227
5330
  const serverMeta = buildServerMeta(lowLevel);
5228
- const surfaceState = new SurfaceState(serverMeta, sentSurfaces);
5331
+ const surfaceState = new SurfaceState(serverMeta, sentSurfaces, shape === "mcpServer");
5229
5332
  const emitSurface = async (sessionId, digest, snapshot) => {
5230
5333
  await emit(
5231
5334
  sink,
@@ -5285,6 +5388,15 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5285
5388
  return EMPTY_OBJECT_JSON_SCHEMA;
5286
5389
  }
5287
5390
  };
5391
+ const vendorOutputJsonSchema = (entry) => {
5392
+ try {
5393
+ const tool = entry;
5394
+ if (!isV2Server) return toolOutputJsonSchema(tool.outputSchema);
5395
+ return isRecord(tool.outputSchemaJson) ? tool.outputSchemaJson : void 0;
5396
+ } catch {
5397
+ return void 0;
5398
+ }
5399
+ };
5288
5400
  installValidationProbe(server);
5289
5401
  const ctx = {
5290
5402
  sink,
@@ -5304,6 +5416,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5304
5416
  captureSettings: config.captureSettings,
5305
5417
  perCaller: void 0,
5306
5418
  vendorToolJsonSchema,
5419
+ vendorOutputJsonSchema,
5307
5420
  bustSchemaMemo,
5308
5421
  dropSubagentClause,
5309
5422
  servedInstructions: () => lowLevel._instructions,
@@ -5403,7 +5516,13 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5403
5516
  checkGivenTools?.();
5404
5517
  return handle(annotationToolName);
5405
5518
  }
5406
- const resolvedAnnotationToolName = annotationOptions ? registerAnnotationTool(server, annotationOptions) : "";
5519
+ if (annotationOptions) {
5520
+ registerAnnotationTool(
5521
+ server,
5522
+ annotationOptions,
5523
+ takesRequestSeams(lowLevel) ? () => callSlots.getStore()?.arguments : void 0
5524
+ );
5525
+ }
5407
5526
  const existing = internals._registeredTools;
5408
5527
  if (existing) {
5409
5528
  for (const [name, entry] of Object.entries(existing)) {
@@ -5416,7 +5535,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5416
5535
  wireEntry(String(args[0]), registered, ctx);
5417
5536
  return registered;
5418
5537
  };
5419
- return handle(resolvedAnnotationToolName);
5538
+ return handle(annotationToolName);
5420
5539
  }
5421
5540
  function reportsFor(config) {
5422
5541
  if (config.reportMode !== "on") return void 0;
@@ -5786,7 +5905,7 @@ var Serving = class {
5786
5905
  annotationToolName: report?.tool.name ?? "",
5787
5906
  injectsGoal: (name) => known.dispositions.get(name)?.[USER_GOAL_PARAM_NAME] === "injected"
5788
5907
  });
5789
- const whole = firstPage && (result.nextCursor === void 0 || result.nextCursor === null) ? surface : null;
5908
+ const whole = isWholeList(request.params.cursor, result.nextCursor) ? surface : null;
5790
5909
  return {
5791
5910
  answer: { ...answer, result: listed },
5792
5911
  surface: whole,