@goodtiming/baton-sdk 0.3.5 → 0.3.6

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
@@ -14,7 +14,7 @@ Most production MCP servers are TypeScript, not Python — see the [design note]
14
14
  - `StdoutSink` — JSONL to a writable stream (default `process.stderr` — MCP stdio transport reserves stdout for JSON-RPC framing).
15
15
  - `HttpSink` — `POST {url}/v0/events` with bearer auth, bounded buffer, retry with backoff, circuit breaker. The contract any Baton-compatible collector consumes.
16
16
  - `withBaton(server, config)` — takes a packed `dsn` (the one string from /account) or the parts named individually, and wraps every tool call on a high-level `McpServer` from either SDK major (both optional peer dependencies) and emits `tool_call_start` / `tool_call_end` / `tool_call_error`. Works regardless of whether tools are registered before or after `withBaton` runs, and survives a tool's `.update()`/`.remove()` after the fact too (see the module docstring in `src/integrations/mcp/withBaton.ts` for why that ordering/mutation independence is load-bearing, not incidental). Also injects server `instructions` and registers the `<vendor>_annotate` tool so the calling agent can supply structured intent/outcome/friction signal — same behavioral contract as Python's adapters, ported byte-for-byte (verified against the Python SDK's actual rendered output, not just eyeballed).
17
- - Intent-param injection — `user_goal`/`expected_result`/`overall_task` string params are spliced onto every wrapped tool's advertised schema (`intentParamMode: "optional"` by default, `"required"` — which promotes only `user_goal` — or `"off"`), stripped before the vendor handler runs, and surfaced as `tool_call_start.payload.call_intent`/`call_expected`/`call_workflow`/`intent_source` plus a synthesised proactive `annotation` (at most one per session). `call_intent`/`call_expected` are call-scoped diagnostics that reword freely; `call_workflow` is the task-label grouping key the Console segments sessions on, which is why its injected param description carries an explicit repeat-the-exact-string contract. This is the capture path that survives runtimes which drop `instructions` entirely (notably Claude Desktop). A tool that already declares one of these names itself is left alone for that field (`"native"` disposition) — its value is forwarded to the vendor untouched, not captured as intent. A tool registered with no `inputSchema` at all is left alone regardless of mode, so a zero-arg handler's calling convention never changes underneath it.
17
+ - Intent-param injection: `user_goal`/`expected_result`/`overall_task` string params are spliced onto every wrapped tool's advertised schema (`intentParamMode: "required"` by default, which advertises only `user_goal` as required in `tools/list` and never enforces it, so a call without it is still served; `"optional"`, which advertises none as required; or `"off"`), stripped before the vendor handler runs, and surfaced as `tool_call_start.payload.call_intent`/`call_expected`/`call_workflow`/`intent_source` plus a synthesised proactive `annotation` (at most one per session). `call_intent`/`call_expected` are call-scoped diagnostics that reword freely; `call_workflow` is the task-label grouping key the Console segments sessions on, which is why its injected param description carries an explicit repeat-the-exact-string contract. This is the capture path that survives runtimes which drop `instructions` entirely (notably Claude Desktop). A tool that already declares one of these names itself is left alone for that field (`"native"` disposition): its value is forwarded to the vendor untouched, not captured as intent. A tool registered with no `inputSchema` at all is left alone regardless of mode, so a zero-arg handler's calling convention never changes underneath it.
18
18
  - `surface_snapshot` capture — the vendor-true (pre-injection) `server_info`/`capabilities`/`instructions`/tool list, hashed and emitted at most once per observed hash (lazily, on the first tool call — the high-level `McpServer` exposes no `tools/list` hook to capture on eagerly). Baton's own additions (the annotate tool, the injected params) are reported separately under `seam_augmentations`, never folded into the vendor-true snapshot.
19
19
  - PII scrubbing — every payload (tool params/results, `_meta`, intent strings, error bodies) runs through `Scrubber` before it reaches any sink, **on by default**. The ruleset is a rule-for-rule port of Python's `baton.scrub` (email, `Bearer` values, `sk-*` keys, `AKIA*` keys, JWTs, phone numbers, Luhn-checked card numbers, plus force-redaction on sensitive field names) — parity verified by mirroring Python's test matrix case-for-case *and* by diffing both implementations' output over a shared corpus. Pass `identityScrub` to opt out explicitly, or supply your own `(value: unknown) => unknown`.
20
20
 
package/dist/index.cjs CHANGED
@@ -58,7 +58,7 @@ __export(index_exports, {
58
58
  module.exports = __toCommonJS(index_exports);
59
59
 
60
60
  // src/version.ts
61
- var SDK_VERSION = "ts-0.3.5";
61
+ var SDK_VERSION = "ts-0.3.6";
62
62
 
63
63
  // src/events.ts
64
64
  var import_zod = require("zod");
@@ -561,11 +561,13 @@ var USER_GOAL_PARAM_NAME = "user_goal";
561
561
  var EXPECTED_RESULT_PARAM_NAME = "expected_result";
562
562
  var OVERALL_TASK_PARAM_NAME = "overall_task";
563
563
  var INTENT_SOURCE_PARAM = "injected_param";
564
- var USER_GOAL_PARAM_DESCRIPTION = "OPTIONAL. One sentence: what the user is actually trying to accomplish with this call (their goal, not a restatement of the arguments).";
564
+ var USER_GOAL_PARAM_BODY = "One sentence: what the user is actually trying to accomplish with this call (their goal, not a restatement of the arguments).";
565
+ var USER_GOAL_PARAM_DESCRIPTION = "OPTIONAL. " + USER_GOAL_PARAM_BODY;
566
+ var USER_GOAL_PARAM_DESCRIPTION_REQUIRED = "REQUIRED. " + USER_GOAL_PARAM_BODY;
565
567
  var EXPECTED_RESULT_PARAM_DESCRIPTION = "OPTIONAL. One sentence: what a successful result should look like, so a silent/thin failure can be told apart from success.";
566
568
  var OVERALL_TASK_PARAM_DESCRIPTION = "OPTIONAL. Short stable label for the broader task this call serves (e.g. 'prepare campaign approval'). REPEAT the exact same string on every call serving the same task; change it only when the user starts a different task.";
567
- function buildUserGoalParamDescription() {
568
- return USER_GOAL_PARAM_DESCRIPTION;
569
+ function buildUserGoalParamDescription(options = {}) {
570
+ return options.intentParamMode === "required" ? USER_GOAL_PARAM_DESCRIPTION_REQUIRED : USER_GOAL_PARAM_DESCRIPTION;
569
571
  }
570
572
  function buildOverallTaskParamDescription() {
571
573
  return OVERALL_TASK_PARAM_DESCRIPTION;
@@ -1387,28 +1389,28 @@ function toJsonSchemaCompat(schema, opts) {
1387
1389
  var import_zod3 = require("zod");
1388
1390
  var import_v3 = require("zod/v3");
1389
1391
  var EMPTY_OBJECT_JSON_SCHEMA = { type: "object", properties: {} };
1390
- function injectGoalParams(inputSchema) {
1392
+ function injectGoalParams(inputSchema, intentParamMode) {
1391
1393
  const objSchema = normalizeObjectSchema(inputSchema);
1392
1394
  if (!objSchema) return { schema: inputSchema, dispositions: {} };
1393
1395
  const existingShape = getObjectShape(objSchema) ?? {};
1394
1396
  const values = Object.values(existingShape);
1395
1397
  const useV4 = values.length === 0 || isZ4Schema(values[0]);
1396
1398
  const zodImpl = useV4 ? import_zod3.z : import_v3.z;
1397
- const { dispositions, toInject } = buildIntentFields(existingShape, zodImpl);
1399
+ const { dispositions, toInject } = buildIntentFields(existingShape, zodImpl, intentParamMode);
1398
1400
  if (Object.keys(toInject).length === 0)
1399
1401
  return { schema: inputSchema, dispositions };
1400
1402
  const mergedShape = { ...existingShape, ...toInject };
1401
1403
  const newSchema = objectFromShape(mergedShape);
1402
1404
  return { schema: newSchema, dispositions };
1403
1405
  }
1404
- function buildIntentFields(existingShape, zodImpl) {
1406
+ function buildIntentFields(existingShape, zodImpl, intentParamMode) {
1405
1407
  const dispositions = {};
1406
1408
  const toInject = {};
1407
1409
  if (USER_GOAL_PARAM_NAME in existingShape) {
1408
1410
  dispositions[USER_GOAL_PARAM_NAME] = "native";
1409
1411
  } else {
1410
1412
  dispositions[USER_GOAL_PARAM_NAME] = "injected";
1411
- toInject[USER_GOAL_PARAM_NAME] = zodImpl.string().optional().describe(buildUserGoalParamDescription());
1413
+ toInject[USER_GOAL_PARAM_NAME] = zodImpl.string().optional().describe(buildUserGoalParamDescription({ intentParamMode }));
1412
1414
  }
1413
1415
  for (const [name, buildDescription] of [
1414
1416
  [EXPECTED_RESULT_PARAM_NAME, buildExpectedResultParamDescription],
@@ -1423,17 +1425,35 @@ function buildIntentFields(existingShape, zodImpl) {
1423
1425
  }
1424
1426
  return { dispositions, toInject };
1425
1427
  }
1426
- function injectGoalParamsV2(inputSchema) {
1428
+ function injectGoalParamsV2(inputSchema, intentParamMode) {
1427
1429
  const candidate = inputSchema;
1428
1430
  const shape = candidate?.shape;
1429
1431
  if (!shape || typeof shape !== "object" || typeof candidate?.extend !== "function") {
1430
1432
  return { schema: inputSchema, dispositions: {} };
1431
1433
  }
1432
- const { dispositions, toInject } = buildIntentFields(shape, import_zod3.z);
1434
+ const { dispositions, toInject } = buildIntentFields(shape, import_zod3.z, intentParamMode);
1433
1435
  if (Object.keys(toInject).length === 0)
1434
1436
  return { schema: inputSchema, dispositions };
1435
1437
  return { schema: candidate.extend(toInject), dispositions };
1436
1438
  }
1439
+ function advertiseUserGoalRequired(result, isInjected) {
1440
+ const tools = result?.tools;
1441
+ if (!Array.isArray(tools)) return result;
1442
+ let changed = false;
1443
+ const advertised = tools.map((tool) => {
1444
+ const entry = tool;
1445
+ if (typeof entry?.name !== "string" || !isInjected(entry.name)) return tool;
1446
+ const schema = entry.inputSchema;
1447
+ if (!schema || typeof schema !== "object") return tool;
1448
+ const declared = schema.required ?? [];
1449
+ if (!Array.isArray(declared)) return tool;
1450
+ const required = declared;
1451
+ if (required.includes(USER_GOAL_PARAM_NAME)) return tool;
1452
+ changed = true;
1453
+ return { ...entry, inputSchema: { ...schema, required: [...required, USER_GOAL_PARAM_NAME] } };
1454
+ });
1455
+ return changed ? { ...result, tools: advertised } : result;
1456
+ }
1437
1457
  function toolInputJsonSchema(inputSchema) {
1438
1458
  const objSchema = normalizeObjectSchema(inputSchema);
1439
1459
  if (!objSchema) return EMPTY_OBJECT_JSON_SCHEMA;
@@ -1710,7 +1730,7 @@ function captureAndInject(name, entry, ctx) {
1710
1730
  if (ctx.intentParamMode === "off") return;
1711
1731
  const isV2 = typeof mutable.executor === "function";
1712
1732
  try {
1713
- const { schema, dispositions } = isV2 ? injectGoalParamsV2(mutable.inputSchema) : injectGoalParams(mutable.inputSchema);
1733
+ const { schema, dispositions } = isV2 ? injectGoalParamsV2(mutable.inputSchema, ctx.intentParamMode) : injectGoalParams(mutable.inputSchema, ctx.intentParamMode);
1714
1734
  if (Object.keys(dispositions).length > 0) {
1715
1735
  mutable.inputSchema = schema;
1716
1736
  ctx.bustSchemaMemo(name);
@@ -1721,6 +1741,46 @@ function captureAndInject(name, entry, ctx) {
1721
1741
  } catch {
1722
1742
  }
1723
1743
  }
1744
+ var TOOLS_LIST_METHOD = "tools/list";
1745
+ var TOOLS_LIST_SEAM = /* @__PURE__ */ Symbol("batonToolsListSeam");
1746
+ function installToolsListSeam(lowLevel, ctx) {
1747
+ const table = lowLevel._requestHandlers;
1748
+ const originalSet = lowLevel.setRequestHandler;
1749
+ if (!(table instanceof Map) || typeof originalSet !== "function") return;
1750
+ const handlers = table;
1751
+ const setRequestHandler = originalSet;
1752
+ const isInjected = (toolName) => ctx.paramRegistry.get(toolName)?.[USER_GOAL_PARAM_NAME] === "injected";
1753
+ const advertise = (result) => {
1754
+ try {
1755
+ return advertiseUserGoalRequired(result, isInjected);
1756
+ } catch (err) {
1757
+ process.stderr.write(
1758
+ `baton: tools/list advertisement failed; serving the SDK's own result: ${String(err)}
1759
+ `
1760
+ );
1761
+ return result;
1762
+ }
1763
+ };
1764
+ const wrapCurrent = () => {
1765
+ const handler = handlers.get(TOOLS_LIST_METHOD);
1766
+ if (typeof handler !== "function" || handler[TOOLS_LIST_SEAM]) return;
1767
+ const seamed = (...args) => {
1768
+ const out = handler(...args);
1769
+ return isThenable(out) ? out.then(advertise) : advertise(out);
1770
+ };
1771
+ seamed[TOOLS_LIST_SEAM] = true;
1772
+ handlers.set(TOOLS_LIST_METHOD, seamed);
1773
+ };
1774
+ wrapCurrent();
1775
+ lowLevel.setRequestHandler = (...args) => {
1776
+ const registered = setRequestHandler.apply(lowLevel, args);
1777
+ wrapCurrent();
1778
+ return registered;
1779
+ };
1780
+ }
1781
+ function isThenable(value) {
1782
+ return typeof value?.then === "function";
1783
+ }
1724
1784
  function wrapIfNeeded(nameRef, entry, ctx) {
1725
1785
  if (!entry || typeof entry !== "object") return;
1726
1786
  const mutable = entry;
@@ -1811,7 +1871,7 @@ function withBaton(server, supplied = {}) {
1811
1871
  const config = resolveBatonConfig(supplied, serverName);
1812
1872
  const sink = config.sink ?? new StdoutSink();
1813
1873
  const annotationToolName = resolveAnnotationToolName(serverName, config);
1814
- const intentParamMode = config.intentParamMode ?? "optional";
1874
+ const intentParamMode = config.intentParamMode ?? "required";
1815
1875
  const counter = new SessionCounter();
1816
1876
  const fallbackSessionId = `sdk-${(0, import_uuid2.v7)()}`;
1817
1877
  const tenantId = resolveTenantId(config.tenantId, config.vendorId);
@@ -1908,6 +1968,7 @@ function withBaton(server, supplied = {}) {
1908
1968
  annotationToolName
1909
1969
  });
1910
1970
  internals.server._instructions = instructions;
1971
+ if (intentParamMode === "required") installToolsListSeam(internals.server, ctx);
1911
1972
  const resolvedAnnotationToolName = registerAnnotationTool(server, {
1912
1973
  sink,
1913
1974
  counter: ctx.counter,