@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/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
- declare const SDK_VERSION = "ts-1.3.0";
3
+ declare const SDK_VERSION = "ts-1.3.2";
4
4
 
5
5
  /**
6
6
  * Zod schemas + TS types for the Baton event stream per SPEC §11.4.
@@ -251,8 +251,10 @@ type PromptListErrorPayload = z.infer<typeof PromptListErrorPayloadSchema>;
251
251
  declare const ToolListStartPayloadSchema: z.ZodObject<{}, z.core.$strict>;
252
252
  type ToolListStartPayload = z.infer<typeof ToolListStartPayloadSchema>;
253
253
  /** `tools/list` returned. `count` is the length of the `tools` array in that
254
- * one response, this SDK's own tool included. */
254
+ * one response, this SDK's own tool included. `surface_hash` is that of the
255
+ * `surface_snapshot` describing the response, when one was captured. */
255
256
  declare const ToolListEndPayloadSchema: z.ZodObject<{
257
+ surface_hash: z.ZodOptional<z.ZodNullable<z.ZodString>>;
256
258
  duration_ms: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
257
259
  count: z.ZodNumber;
258
260
  }, z.core.$strict>;
@@ -1286,6 +1288,7 @@ type ToolListStartEvent = z.infer<typeof ToolListStartEventSchema>;
1286
1288
  declare const ToolListEndEventSchema: z.ZodObject<{
1287
1289
  event_type: z.ZodDefault<z.ZodLiteral<"tool_list_end">>;
1288
1290
  payload: z.ZodObject<{
1291
+ surface_hash: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1289
1292
  duration_ms: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
1290
1293
  count: z.ZodNumber;
1291
1294
  }, z.core.$strict>;
@@ -2340,6 +2343,7 @@ declare const EventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
2340
2343
  }, z.core.$strict>, z.ZodObject<{
2341
2344
  event_type: z.ZodDefault<z.ZodLiteral<"tool_list_end">>;
2342
2345
  payload: z.ZodObject<{
2346
+ surface_hash: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2343
2347
  duration_ms: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
2344
2348
  count: z.ZodNumber;
2345
2349
  }, z.core.$strict>;
@@ -3096,8 +3100,9 @@ interface BatonConfig {
3096
3100
  * function that returns them. Read once per `withBaton` or `createBaton`.
3097
3101
  * Without it Baton learns the tools from the lists its servers serve, so a
3098
3102
  * process that has served none yet, such as a fresh worker, takes
3099
- * `user_goal` off every call, even from a tool that declares its own. An
3100
- * `McpServer` does not read it. */
3103
+ * `user_goal` off every call, even from a tool that declares its own. The
3104
+ * surface snapshot comes only from the lists a server serves, never from
3105
+ * this. An `McpServer` does not read it. */
3101
3106
  tools?: readonly unknown[] | (() => readonly unknown[]);
3102
3107
  }
3103
3108
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
- declare const SDK_VERSION = "ts-1.3.0";
3
+ declare const SDK_VERSION = "ts-1.3.2";
4
4
 
5
5
  /**
6
6
  * Zod schemas + TS types for the Baton event stream per SPEC §11.4.
@@ -251,8 +251,10 @@ type PromptListErrorPayload = z.infer<typeof PromptListErrorPayloadSchema>;
251
251
  declare const ToolListStartPayloadSchema: z.ZodObject<{}, z.core.$strict>;
252
252
  type ToolListStartPayload = z.infer<typeof ToolListStartPayloadSchema>;
253
253
  /** `tools/list` returned. `count` is the length of the `tools` array in that
254
- * one response, this SDK's own tool included. */
254
+ * one response, this SDK's own tool included. `surface_hash` is that of the
255
+ * `surface_snapshot` describing the response, when one was captured. */
255
256
  declare const ToolListEndPayloadSchema: z.ZodObject<{
257
+ surface_hash: z.ZodOptional<z.ZodNullable<z.ZodString>>;
256
258
  duration_ms: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
257
259
  count: z.ZodNumber;
258
260
  }, z.core.$strict>;
@@ -1286,6 +1288,7 @@ type ToolListStartEvent = z.infer<typeof ToolListStartEventSchema>;
1286
1288
  declare const ToolListEndEventSchema: z.ZodObject<{
1287
1289
  event_type: z.ZodDefault<z.ZodLiteral<"tool_list_end">>;
1288
1290
  payload: z.ZodObject<{
1291
+ surface_hash: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1289
1292
  duration_ms: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
1290
1293
  count: z.ZodNumber;
1291
1294
  }, z.core.$strict>;
@@ -2340,6 +2343,7 @@ declare const EventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
2340
2343
  }, z.core.$strict>, z.ZodObject<{
2341
2344
  event_type: z.ZodDefault<z.ZodLiteral<"tool_list_end">>;
2342
2345
  payload: z.ZodObject<{
2346
+ surface_hash: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2343
2347
  duration_ms: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
2344
2348
  count: z.ZodNumber;
2345
2349
  }, z.core.$strict>;
@@ -3096,8 +3100,9 @@ interface BatonConfig {
3096
3100
  * function that returns them. Read once per `withBaton` or `createBaton`.
3097
3101
  * Without it Baton learns the tools from the lists its servers serve, so a
3098
3102
  * process that has served none yet, such as a fresh worker, takes
3099
- * `user_goal` off every call, even from a tool that declares its own. An
3100
- * `McpServer` does not read it. */
3103
+ * `user_goal` off every call, even from a tool that declares its own. The
3104
+ * surface snapshot comes only from the lists a server serves, never from
3105
+ * this. An `McpServer` does not read it. */
3101
3106
  tools?: readonly unknown[] | (() => readonly unknown[]);
3102
3107
  }
3103
3108
 
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var SDK_VERSION = "ts-1.3.0";
2
+ var SDK_VERSION = "ts-1.3.2";
3
3
 
4
4
  // src/events.ts
5
5
  import { z } from "zod";
@@ -233,7 +233,8 @@ var PromptListErrorPayloadSchema = z.object({
233
233
  var ToolListStartPayloadSchema = z.object({}).strict();
234
234
  var ToolListEndPayloadSchema = z.object({
235
235
  count: z.number().int(),
236
- ...timingShape
236
+ ...timingShape,
237
+ surface_hash: z.string().nullable().optional()
237
238
  }).strict();
238
239
  var ToolListErrorPayloadSchema = z.object({
239
240
  ...failureShape
@@ -453,6 +454,9 @@ function isPlainObject(value) {
453
454
  function isRecord(value) {
454
455
  return typeof value === "object" && value !== null && !Array.isArray(value);
455
456
  }
457
+ function jsonObjectCopy(value) {
458
+ return isRecord(value) ? JSON.parse(JSON.stringify(value)) : void 0;
459
+ }
456
460
 
457
461
  // src/scrub.ts
458
462
  var DEPTH_LIMIT = 10;
@@ -3031,12 +3035,14 @@ function standardInputJsonSchema(schema) {
3031
3035
  }
3032
3036
  }
3033
3037
  function toolInputJsonSchema(inputSchema2) {
3034
- const objSchema = normalizeObjectSchema(inputSchema2);
3035
- if (!objSchema) return EMPTY_OBJECT_JSON_SCHEMA;
3036
- return toJsonSchemaCompat(objSchema, {
3037
- strictUnions: true,
3038
- pipeStrategy: "input"
3039
- });
3038
+ return toolJsonSchema(inputSchema2, "input") ?? EMPTY_OBJECT_JSON_SCHEMA;
3039
+ }
3040
+ function toolOutputJsonSchema(outputSchema) {
3041
+ return outputSchema ? toolJsonSchema(outputSchema, "output") : void 0;
3042
+ }
3043
+ function toolJsonSchema(schema, pipeStrategy) {
3044
+ const objSchema = normalizeObjectSchema(schema);
3045
+ return objSchema ? toJsonSchemaCompat(objSchema, { strictUnions: true, pipeStrategy }) : void 0;
3040
3046
  }
3041
3047
 
3042
3048
  // src/integrations/mcp/safeScrub.ts
@@ -3187,19 +3193,29 @@ var ANNOTATION_INPUT_SHAPE = {
3187
3193
  // zod renders "any value" as `{}`, which some MCP clients refuse (reportToolSchema.test.ts).
3188
3194
  context: z2.record(z2.string(), z2.unknown()).meta({ additionalProperties: true }).nullable().optional()
3189
3195
  };
3196
+ var ANNOTATION_ARGS = z2.object(ANNOTATION_INPUT_SHAPE);
3190
3197
  function isReport(whatHappened) {
3191
3198
  return typeof whatHappened === "string" && !isBlank(whatHappened);
3192
3199
  }
3193
- function registerAnnotationTool(server, options) {
3200
+ var INVALID_PARAMS = -32602;
3201
+ function registerAnnotationTool(server, options, callArguments) {
3194
3202
  const tool = buildAnnotationTool(server, options);
3195
- server.registerTool(
3196
- tool.name,
3197
- { description: tool.description, inputSchema: ANNOTATION_INPUT_SHAPE },
3198
- tool.handler
3199
- );
3200
- return tool.name;
3203
+ if (callArguments === void 0) {
3204
+ server.registerTool(tool.name, { description: tool.description, inputSchema: ANNOTATION_INPUT_SHAPE }, tool.handler);
3205
+ return;
3206
+ }
3207
+ server.registerTool(tool.name, { description: tool.description }, async (extra) => {
3208
+ const args = callArguments();
3209
+ const { validateToolInput } = server;
3210
+ let parsed;
3211
+ try {
3212
+ parsed = await validateToolInput?.call(server, { inputSchema: ANNOTATION_ARGS }, args, tool.name);
3213
+ } catch (err) {
3214
+ if (err?.code === INVALID_PARAMS) throw err;
3215
+ }
3216
+ return isRecord(parsed) ? tool.handler(parsed, extra) : callAnnotationTool(tool, args, extra);
3217
+ });
3201
3218
  }
3202
- var ANNOTATION_ARGS = z2.object(ANNOTATION_INPUT_SHAPE);
3203
3219
  function annotationInputJsonSchema(major) {
3204
3220
  return annotationJsonSchemas[major] ??= (major === 2 ? standardInputJsonSchema(ANNOTATION_ARGS) : void 0) ?? toolInputJsonSchema(ANNOTATION_INPUT_SHAPE);
3205
3221
  }
@@ -3987,6 +4003,9 @@ function assembleSurface(serverMeta, tools) {
3987
4003
  tools
3988
4004
  };
3989
4005
  }
4006
+ function isWholeList(cursor, nextCursor) {
4007
+ return (cursor === void 0 || cursor === null) && (nextCursor === void 0 || nextCursor === null);
4008
+ }
3990
4009
  var SENT_SURFACES_CAP = 256;
3991
4010
  var SentSurfaces = class {
3992
4011
  hashes = /* @__PURE__ */ new Set();
@@ -4030,12 +4049,14 @@ var CLEAN_OUTCOME = { failed: false, raised: false, result: void 0 };
4030
4049
  var INTENT_PARAM_NAMES = [USER_GOAL_PARAM_NAME, ...RETIRED_PARAM_NAMES];
4031
4050
  var callSlots = new asyncHooks.AsyncLocalStorage();
4032
4051
  var SurfaceState = class {
4033
- constructor(serverMeta, sentSurfaces) {
4052
+ constructor(serverMeta, sentSurfaces, fromRegistry) {
4034
4053
  this.serverMeta = serverMeta;
4035
4054
  this.sentSurfaces = sentSurfaces;
4055
+ this.fromRegistry = fromRegistry;
4036
4056
  }
4037
4057
  serverMeta;
4038
4058
  sentSurfaces;
4059
+ fromRegistry;
4039
4060
  /** Every tool this install has seen, keyed by its CURRENT name. `disabled`
4040
4061
  * is tracked alongside rather than pruned, so re-enabling costs nothing and
4041
4062
  * — crucially — never needs a re-capture, which would re-read Baton's own
@@ -4043,20 +4064,74 @@ var SurfaceState = class {
4043
4064
  rawTools = /* @__PURE__ */ new Map();
4044
4065
  dirty = false;
4045
4066
  servedOverlayId = null;
4067
+ listed = false;
4068
+ /** `null` when this version of the surface could not be captured. */
4069
+ captured;
4070
+ /** The surface each low-level request was served, keyed by the request,
4071
+ * which every seam passes on as it came. Kept apart from the current
4072
+ * surface, which another listing can change before this one's end is sent:
4073
+ * `surfaceAtList.test.ts`, "gives two listings that finish together each its own hash". */
4074
+ listedSurfaces = /* @__PURE__ */ new WeakMap();
4075
+ get hasSurface() {
4076
+ return this.fromRegistry || this.listed;
4077
+ }
4078
+ /** A low-level Server served `request` its whole list, which then is the
4079
+ * surface: `surfaceAtList.test.ts`, "snapshots each list as served". */
4080
+ servedList(request, names) {
4081
+ this.listed = true;
4082
+ for (const name of [...this.rawTools.keys()]) {
4083
+ if (!names.has(name)) this.pruneTool(name);
4084
+ }
4085
+ const surface = this.capture();
4086
+ if (surface !== null) this.listedSurfaces.set(request, surface);
4087
+ }
4088
+ /** The surface a `tools/list` request was served, when it got the whole list. */
4089
+ surfaceListed(request, served) {
4090
+ const listed = this.listedSurfaces.get(request);
4091
+ if (listed !== void 0) return listed;
4092
+ const cursor = isRecord(request.params) ? request.params.cursor : void 0;
4093
+ const whole = isWholeList(cursor, isRecord(served) ? served.nextCursor : void 0);
4094
+ return this.fromRegistry && whole ? this.capture() : null;
4095
+ }
4096
+ /** The surface as it stands, and its hash; built once per change, so a
4097
+ * surface that cannot be hashed is reported once, not on every request. */
4098
+ capture() {
4099
+ if (this.captured !== void 0) return this.captured;
4100
+ try {
4101
+ const snapshot = this.buildSnapshot();
4102
+ this.captured = { snapshot, digest: surfaceHash(snapshot) };
4103
+ } catch (err) {
4104
+ stderrLine(`baton: surface snapshot capture failed: ${printable(err)}`);
4105
+ this.captured = null;
4106
+ }
4107
+ return this.captured;
4108
+ }
4109
+ changed() {
4110
+ this.dirty = true;
4111
+ this.captured = void 0;
4112
+ }
4046
4113
  /** `inputSchemaJson` is already converted — by `ctx.vendorToolJsonSchema`,
4047
4114
  * which picks the spelling THIS SDK major puts on the wire. */
4048
- noteTool(name, inputSchemaJson, description) {
4115
+ noteTool(name, inputSchemaJson, description, outputSchemaJson) {
4049
4116
  const before = this.rawTools.get(name);
4050
- const tool = { name, description: description ?? null, inputSchema: inputSchemaJson };
4051
- if (before !== void 0 && before.tool.description === tool.description) {
4052
- const same = before.tool.inputSchema === inputSchemaJson || JSON.stringify(before.tool.inputSchema) === JSON.stringify(inputSchemaJson);
4053
- if (same) return;
4117
+ if (before !== void 0 && before.tool.description === (description ?? null)) {
4118
+ const sameInput = before.tool.inputSchema === inputSchemaJson || JSON.stringify(before.tool.inputSchema) === JSON.stringify(inputSchemaJson);
4119
+ if (sameInput && JSON.stringify(before.tool.outputSchema) === JSON.stringify(outputSchemaJson)) return;
4054
4120
  }
4121
+ const tool = { name, description: description ?? null, inputSchema: inputSchemaJson };
4122
+ const outputSchema = jsonObjectCopy(outputSchemaJson);
4123
+ if (outputSchema !== void 0) tool.outputSchema = outputSchema;
4055
4124
  this.rawTools.set(name, { disabled: before?.disabled ?? false, tool });
4056
- this.dirty = true;
4125
+ this.changed();
4126
+ }
4127
+ setOutputSchema(name, outputSchemaJson) {
4128
+ const tool = this.rawTools.get(name)?.tool;
4129
+ if (tool === void 0) return;
4130
+ const description = typeof tool.description === "string" ? tool.description : void 0;
4131
+ this.noteTool(name, tool.inputSchema, description, outputSchemaJson);
4057
4132
  }
4058
4133
  pruneTool(name) {
4059
- if (this.rawTools.delete(name)) this.dirty = true;
4134
+ if (this.rawTools.delete(name)) this.changed();
4060
4135
  }
4061
4136
  /** Follow a `update({name})` rename, keeping the snapshot keyed the way
4062
4137
  * `tools/list` now renders it. */
@@ -4065,7 +4140,7 @@ var SurfaceState = class {
4065
4140
  if (!entry) return;
4066
4141
  this.rawTools.delete(from);
4067
4142
  this.rawTools.set(to, { ...entry, tool: { ...entry.tool, name: to } });
4068
- this.dirty = true;
4143
+ this.changed();
4069
4144
  }
4070
4145
  /** Both majors filter `tools/list` on `tool.enabled`, so a disabled tool is
4071
4146
  * not part of the surface a client sees and must not be part of the one we
@@ -4075,7 +4150,7 @@ var SurfaceState = class {
4075
4150
  const entry = this.rawTools.get(name);
4076
4151
  if (!entry || entry.disabled === !enabled) return;
4077
4152
  entry.disabled = !enabled;
4078
- this.dirty = true;
4153
+ this.changed();
4079
4154
  }
4080
4155
  buildSnapshot() {
4081
4156
  const names = [...this.rawTools.keys()].sort().filter((n) => !this.rawTools.get(n).disabled);
@@ -4350,6 +4425,7 @@ function stripIntentParams(params, ctx) {
4350
4425
  return stripped;
4351
4426
  }
4352
4427
  async function maybeEmitSurfaceSnapshot(ctx) {
4428
+ if (!ctx.surfaceState.hasSurface) return;
4353
4429
  const overlayId = ctx.perCaller?.serverOverlay?.id ?? null;
4354
4430
  if (overlayId !== ctx.surfaceState.servedOverlayId) {
4355
4431
  ctx.surfaceState.servedOverlayId = overlayId;
@@ -4357,17 +4433,13 @@ async function maybeEmitSurfaceSnapshot(ctx) {
4357
4433
  }
4358
4434
  if (!ctx.surfaceState.dirty) return;
4359
4435
  ctx.surfaceState.dirty = false;
4360
- let snapshot;
4361
- let digest;
4362
- try {
4363
- snapshot = ctx.surfaceState.buildSnapshot();
4364
- digest = surfaceHash(snapshot);
4365
- } catch (err) {
4366
- stderrLine(`baton: surface snapshot capture failed: ${String(err)}`);
4367
- return;
4368
- }
4436
+ const surface = ctx.surfaceState.capture();
4437
+ if (surface !== null) await sendSurface(ctx, surface);
4438
+ }
4439
+ async function sendSurface(ctx, { snapshot, digest }) {
4440
+ const overlayId = ctx.perCaller?.serverOverlay?.id ?? null;
4369
4441
  const sent = ctx.surfaceState.sentSurfaces;
4370
- if (sent.wasSent(digest, overlayId)) return;
4442
+ if (sent.wasSent(digest, overlayId)) return digest;
4371
4443
  sent.mark(digest, overlayId);
4372
4444
  const seam = buildSeamAugmentations({
4373
4445
  injectedToolNames: ctx.annotationToolName ? [ctx.annotationToolName] : [],
@@ -4384,6 +4456,16 @@ async function maybeEmitSurfaceSnapshot(ctx) {
4384
4456
  });
4385
4457
  if (ctx.captureSettings !== void 0) seam.server_overlay = overlayId;
4386
4458
  await ctx.emitSurface(ctx.fallbackSessionId, digest, { ...snapshot, seam_augmentations: seam });
4459
+ return digest;
4460
+ }
4461
+ async function listedSurfaceFields(ctx, request, served) {
4462
+ try {
4463
+ const surface = isRecord(request) ? ctx.surfaceState.surfaceListed(request, served) : null;
4464
+ return surface === null ? {} : { surface_hash: await sendSurface(ctx, surface) };
4465
+ } catch (err) {
4466
+ stderrLine(`baton: surface snapshot capture failed: ${printable(err)}`);
4467
+ return {};
4468
+ }
4387
4469
  }
4388
4470
  function declaredIntentParams(vendorSchema) {
4389
4471
  const declared2 = {};
@@ -4396,7 +4478,7 @@ function captureAndInject(name, entry, ctx) {
4396
4478
  if (!entry || typeof entry !== "object") return;
4397
4479
  const mutable = entry;
4398
4480
  const vendorSchema = ctx.vendorToolJsonSchema(name, mutable.inputSchema);
4399
- ctx.surfaceState.noteTool(name, vendorSchema, mutable.description);
4481
+ ctx.surfaceState.noteTool(name, vendorSchema, mutable.description, ctx.vendorOutputJsonSchema(entry));
4400
4482
  if (declaresParam(vendorSchema, USER_GOAL_PARAM_NAME)) ctx.dropSubagentClause(name);
4401
4483
  const isV2 = typeof mutable.executor === "function";
4402
4484
  try {
@@ -4418,11 +4500,9 @@ var TOOLS_LIST_SEAM = /* @__PURE__ */ Symbol("batonToolsListSeam");
4418
4500
  var TOOLS_CALL_METHOD = "tools/call";
4419
4501
  var TOOLS_CALL_SEAM = /* @__PURE__ */ Symbol("batonToolsCallSeam");
4420
4502
  function installRequestSeam(lowLevel, method, seamTag, wrap) {
4421
- const table = lowLevel._requestHandlers;
4422
- const originalSet = lowLevel.setRequestHandler;
4423
- if (!(table instanceof Map) || typeof originalSet !== "function") return;
4424
- const handlers = table;
4425
- const setRequestHandler = originalSet;
4503
+ if (!takesRequestSeams(lowLevel)) return;
4504
+ const handlers = lowLevel._requestHandlers;
4505
+ const setRequestHandler = lowLevel.setRequestHandler;
4426
4506
  const wrapCurrent = () => {
4427
4507
  const handler = handlers.get(method);
4428
4508
  if (typeof handler !== "function" || handler[seamTag]) return;
@@ -4438,18 +4518,24 @@ function installRequestSeam(lowLevel, method, seamTag, wrap) {
4438
4518
  return registered;
4439
4519
  };
4440
4520
  }
4521
+ function takesRequestSeams(lowLevel) {
4522
+ return lowLevel._requestHandlers instanceof Map && typeof lowLevel.setRequestHandler === "function";
4523
+ }
4441
4524
  function installToolsListSeam(lowLevel, ctx, listsOwnReportTool) {
4442
4525
  const names = [...requiredParamNames(ctx.intentParamMode), TOOL_NAME_PARAM_NAME];
4443
4526
  const isInjected = (toolName, paramName) => toolName === ctx.annotationToolName ? paramName === TOOL_NAME_PARAM_NAME : ctx.paramRegistry.get(toolName)?.[paramName] === "injected";
4444
4527
  const advertise = (result) => {
4528
+ let served = result;
4445
4529
  try {
4446
- const served = listsOwnReportTool || ctx.annotationToolName === "" ? result : withToolInputSchema(result, ctx.annotationToolName, annotationInputJsonSchema(lowLevelMajor(lowLevel)));
4530
+ if (!listsOwnReportTool && ctx.annotationToolName !== "") {
4531
+ served = withToolInputSchema(result, ctx.annotationToolName, annotationInputJsonSchema(lowLevelMajor(lowLevel)));
4532
+ }
4447
4533
  return advertiseIntentParamsRequired(served, names, isInjected);
4448
4534
  } catch (err) {
4449
4535
  stderrLine(
4450
- `baton: tools/list advertisement failed; serving the SDK's own result: ${String(err)}`
4536
+ `baton: tools/list advertisement failed; serving the list without it: ${String(err)}`
4451
4537
  );
4452
- return result;
4538
+ return served;
4453
4539
  }
4454
4540
  };
4455
4541
  installRequestSeam(lowLevel, TOOLS_LIST_METHOD, TOOLS_LIST_SEAM, (handler) => (...args) => {
@@ -4506,10 +4592,6 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4506
4592
  const name = reportTool2?.name;
4507
4593
  if (name !== void 0 && tools.some((tool) => tool.name === name)) leaveReportNameToVendor(name);
4508
4594
  };
4509
- const read = (tools, { surface }) => {
4510
- if (surface) takeReportNameIfListed(tools);
4511
- readToolList(tools, ctx, surface, skipNote);
4512
- };
4513
4595
  const learn = (tools) => {
4514
4596
  keepGoalIfDeclared(tools);
4515
4597
  for (const tool of tools) {
@@ -4537,8 +4619,8 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4537
4619
  );
4538
4620
  }
4539
4621
  }
4540
- read(known.givenTools, { surface: true });
4541
- read([...known.served.values()], { surface: false });
4622
+ takeReportNameIfListed(known.givenTools);
4623
+ readToolList([...known.givenTools, ...known.served.values()], ctx, false, skipNote);
4542
4624
  };
4543
4625
  installRequestSeam(lowLevel, TOOLS_LIST_METHOD, LOW_LEVEL_LIST_SEAM, (handler) => async (...args) => {
4544
4626
  catchUp();
@@ -4548,8 +4630,14 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4548
4630
  if (listed === null) return result;
4549
4631
  const tools = toolInfos(listed, skipTool);
4550
4632
  learn(tools);
4551
- read(tools, { surface: true });
4552
- const firstPage = args[0]?.params?.cursor === void 0;
4633
+ takeReportNameIfListed(tools);
4634
+ readToolList(tools, ctx, true, skipNote);
4635
+ const request = args[0];
4636
+ const cursor = isRecord(request) && isRecord(request.params) ? request.params.cursor : void 0;
4637
+ if (isRecord(request) && isWholeList(cursor, result.nextCursor)) {
4638
+ ctx.surfaceState.servedList(request, new Set(tools.map((tool) => tool.name)));
4639
+ }
4640
+ const firstPage = cursor === void 0 || cursor === null;
4553
4641
  return servedToolList(result, listed, ctx, firstPage && reportTool2 ? listedReportTool : void 0);
4554
4642
  } catch (err) {
4555
4643
  stderrLine(`baton: tools/list capture failed; serving the vendor's own result: ${printable(err)}`);
@@ -4613,7 +4701,8 @@ function toolInfos(tools, skip) {
4613
4701
  try {
4614
4702
  name = isRecord(tool) ? tool.name : void 0;
4615
4703
  if (typeof name !== "string") continue;
4616
- infos.push({ name, description: tool.description, inputSchema: tool.inputSchema });
4704
+ const { description, inputSchema: inputSchema2, outputSchema } = tool;
4705
+ infos.push({ name, description, inputSchema: inputSchema2, outputSchema });
4617
4706
  } catch (err) {
4618
4707
  if (typeof name === "string") skip(name, err);
4619
4708
  }
@@ -4621,11 +4710,16 @@ function toolInfos(tools, skip) {
4621
4710
  return infos;
4622
4711
  }
4623
4712
  function readToolList(tools, ctx, surface, skip) {
4624
- for (const { name, inputSchema: inputSchema2, description } of tools) {
4713
+ for (const { name, inputSchema: inputSchema2, description, outputSchema } of tools) {
4625
4714
  try {
4626
4715
  const schema = isRecord(inputSchema2) ? inputSchema2 : null;
4627
4716
  if (surface) {
4628
- ctx.surfaceState.noteTool(name, schema ?? EMPTY_OBJECT_JSON_SCHEMA, typeof description === "string" ? description : void 0);
4717
+ ctx.surfaceState.noteTool(
4718
+ name,
4719
+ schema ?? EMPTY_OBJECT_JSON_SCHEMA,
4720
+ typeof description === "string" ? description : void 0,
4721
+ isRecord(outputSchema) ? outputSchema : void 0
4722
+ );
4629
4723
  }
4630
4724
  const dispositions = declaredIntentParams(schema);
4631
4725
  if (dispositions[USER_GOAL_PARAM_NAME] === "native") ctx.dropSubagentClause(name);
@@ -4661,7 +4755,7 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
4661
4755
  const requestParams = request?.params ?? {};
4662
4756
  const extra = args[1];
4663
4757
  const opened = await openLifecycleCall(ctx, spec, requestParams, extra);
4664
- const fire = async (schema, payload) => {
4758
+ const fire = async (schema, payload, unscrubbed = {}) => {
4665
4759
  if (opened === null) return;
4666
4760
  await emit(
4667
4761
  ctx.sink,
@@ -4669,7 +4763,7 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
4669
4763
  ...opened.common,
4670
4764
  sequence_number: ctx.counter.nextFor(opened.session),
4671
4765
  captured_at: (/* @__PURE__ */ new Date()).toISOString(),
4672
- payload: scrubLifecyclePayload(ctx, { ...opened.subject, ...payload })
4766
+ payload: { ...scrubLifecyclePayload(ctx, { ...opened.subject, ...payload }), ...unscrubbed }
4673
4767
  })
4674
4768
  );
4675
4769
  };
@@ -4680,10 +4774,15 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
4680
4774
  const served = await handler(...args);
4681
4775
  if (isInputRequired(served)) return served;
4682
4776
  const result = opened !== null && spec.rewriteResult !== void 0 ? spec.rewriteResult(ctx, served, opened.caller) : served;
4683
- await fire(spec.schemas.end, {
4684
- ...spec.countKey === void 0 ? {} : { count: countOf(result, spec.countKey) },
4685
- duration_ms: elapsed()
4686
- });
4777
+ const duration = elapsed();
4778
+ await fire(
4779
+ spec.schemas.end,
4780
+ {
4781
+ ...spec.countKey === void 0 ? {} : { count: countOf(result, spec.countKey) },
4782
+ duration_ms: duration
4783
+ },
4784
+ await spec.endFields?.(ctx, request, served)
4785
+ );
4687
4786
  return result;
4688
4787
  } catch (err) {
4689
4788
  await fire(spec.schemas.error, {
@@ -4803,7 +4902,8 @@ var LIFECYCLE_SPECS = [
4803
4902
  },
4804
4903
  countKey: "tools",
4805
4904
  startParams: NO_PARAMS,
4806
- rewriteResult: (ctx, result, caller) => ctx.perCaller?.toolsFor(result, caller) ?? result
4905
+ rewriteResult: (ctx, result, caller) => ctx.perCaller?.toolsFor(result, caller) ?? result,
4906
+ endFields: listedSurfaceFields
4807
4907
  }
4808
4908
  ];
4809
4909
  function installLifecycleSeams(lowLevel, ctx) {
@@ -4847,14 +4947,15 @@ function installToolsCallSeam(lowLevel, toolEntry, ctx) {
4847
4947
  (handler) => async (...args) => {
4848
4948
  const request = args[0];
4849
4949
  const name = request?.params?.name;
4950
+ const toolArgs = request?.params?.arguments;
4850
4951
  const slot = {
4851
4952
  innerFired: false,
4852
4953
  flushTerminal: null,
4853
4954
  stripped: stripIntentParams(request?.params, ctx),
4854
4955
  strippedFor: typeof name === "string" ? name : null,
4855
- validation: "unseen"
4956
+ validation: "unseen",
4957
+ arguments: name === ctx.annotationToolName ? toolArgs : void 0
4856
4958
  };
4857
- const toolArgs = request?.params?.arguments;
4858
4959
  if (typeof name === "string" && toolEntry(name) != null && await hiddenFrom(slot, name, isRecord(toolArgs) ? toolArgs : {}, args[1], ctx)) {
4859
4960
  return answerHidden(lowLevel, name, (outcome2) => settleCall(slot, outcome2, args, toolEntry, ctx, "tool_hidden"));
4860
4961
  }
@@ -5046,6 +5147,8 @@ function wireEntry(name, entry, ctx) {
5046
5147
  }
5047
5148
  if (u.paramsSchema !== void 0) {
5048
5149
  captureAndInject(nameRef.current, entry, ctx);
5150
+ } else if (u.outputSchema !== void 0) {
5151
+ ctx.surfaceState.setOutputSchema(nameRef.current, ctx.vendorOutputJsonSchema(entry));
5049
5152
  }
5050
5153
  if (typeof u.enabled === "boolean") {
5051
5154
  ctx.surfaceState.setToolEnabled(nameRef.current, u.enabled);
@@ -5171,7 +5274,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5171
5274
  const counter2 = new ServerCounter(sharedCounter, fallbackSessionId);
5172
5275
  const tenantId = resolveTenantId(config.tenantId, config.vendorId);
5173
5276
  const serverMeta = buildServerMeta(lowLevel);
5174
- const surfaceState = new SurfaceState(serverMeta, sentSurfaces);
5277
+ const surfaceState = new SurfaceState(serverMeta, sentSurfaces, shape === "mcpServer");
5175
5278
  const emitSurface = async (sessionId, digest, snapshot) => {
5176
5279
  await emit(
5177
5280
  sink,
@@ -5231,6 +5334,15 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5231
5334
  return EMPTY_OBJECT_JSON_SCHEMA;
5232
5335
  }
5233
5336
  };
5337
+ const vendorOutputJsonSchema = (entry) => {
5338
+ try {
5339
+ const tool = entry;
5340
+ if (!isV2Server) return toolOutputJsonSchema(tool.outputSchema);
5341
+ return isRecord(tool.outputSchemaJson) ? tool.outputSchemaJson : void 0;
5342
+ } catch {
5343
+ return void 0;
5344
+ }
5345
+ };
5234
5346
  installValidationProbe(server);
5235
5347
  const ctx = {
5236
5348
  sink,
@@ -5250,6 +5362,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5250
5362
  captureSettings: config.captureSettings,
5251
5363
  perCaller: void 0,
5252
5364
  vendorToolJsonSchema,
5365
+ vendorOutputJsonSchema,
5253
5366
  bustSchemaMemo,
5254
5367
  dropSubagentClause,
5255
5368
  servedInstructions: () => lowLevel._instructions,
@@ -5349,7 +5462,13 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5349
5462
  checkGivenTools?.();
5350
5463
  return handle(annotationToolName);
5351
5464
  }
5352
- const resolvedAnnotationToolName = annotationOptions ? registerAnnotationTool(server, annotationOptions) : "";
5465
+ if (annotationOptions) {
5466
+ registerAnnotationTool(
5467
+ server,
5468
+ annotationOptions,
5469
+ takesRequestSeams(lowLevel) ? () => callSlots.getStore()?.arguments : void 0
5470
+ );
5471
+ }
5353
5472
  const existing = internals._registeredTools;
5354
5473
  if (existing) {
5355
5474
  for (const [name, entry] of Object.entries(existing)) {
@@ -5362,7 +5481,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5362
5481
  wireEntry(String(args[0]), registered, ctx);
5363
5482
  return registered;
5364
5483
  };
5365
- return handle(resolvedAnnotationToolName);
5484
+ return handle(annotationToolName);
5366
5485
  }
5367
5486
  function reportsFor(config) {
5368
5487
  if (config.reportMode !== "on") return void 0;
@@ -5732,7 +5851,7 @@ var Serving = class {
5732
5851
  annotationToolName: report?.tool.name ?? "",
5733
5852
  injectsGoal: (name) => known.dispositions.get(name)?.[USER_GOAL_PARAM_NAME] === "injected"
5734
5853
  });
5735
- const whole = firstPage && (result.nextCursor === void 0 || result.nextCursor === null) ? surface : null;
5854
+ const whole = isWholeList(request.params.cursor, result.nextCursor) ? surface : null;
5736
5855
  return {
5737
5856
  answer: { ...answer, result: listed },
5738
5857
  surface: whole,