@goodtiming/baton-sdk 0.5.0 → 0.5.1

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
@@ -49,6 +49,25 @@ Sending to your own collector instead, or trying the package before you have a k
49
49
 
50
50
  **The scrubber is not the only lever.** If some tool results cannot be recorded at all, `resultCaptureMode` below withholds them outright rather than transforming them.
51
51
 
52
+ ## Who is calling
53
+
54
+ **Nothing is captured about the person behind a call unless you say how to find them.** Pass `resolvePrincipal`: it receives the call's headers, `_meta`, tool name, arguments and validated `authInfo`, and returns a `Principal` or `null`. Two ready-made hooks cover OAuth:
55
+
56
+ ```typescript
57
+ import { withBaton, principalFromOAuthEmail } from "@goodtiming/baton-sdk";
58
+
59
+ withBaton(server, {
60
+ dsn: "https://baton_pk_...@baton.goodtiming.ai/ten_.../your-vendor",
61
+ resolvePrincipal: principalFromOAuthEmail, // or principalFromOAuthSub
62
+ });
63
+ ```
64
+
65
+ `principalFromOAuthEmail` keys on the token's `email` claim (the whole address); `principalFromOAuthSub` on its subject and issuer. Each returns `null` when the claim is missing, so they compose: `(ctx) => principalFromOAuthEmail(ctx) ?? principalFromOAuthSub(ctx)`. Set `displayName` on the returned `Principal` to choose what your dashboard shows for a person; it is sent as-is, even when ids are hashed. The email hook sets it to the part before `@`.
66
+
67
+ ⚠ **They read the claims from `authInfo.extra`.** The MCP SDK's `AuthInfo` has no `claims` field, so these hooks expect your token verifier to put the decoded JWT claims (`sub`, `iss`, `email`) at the top level of `extra`. If yours keeps them elsewhere, write the three-line hook that reads them from there. A token exists only on HTTP with auth configured; on stdio, write a hook that names the user from whatever you authenticated them with.
68
+
69
+ The value is HMAC-hashed in your process before it is sent, keyed with `BATON_PRINCIPAL_ID_HMAC_KEY`; without a key, nothing is attached.
70
+
52
71
  ## Not capturing responses at all
53
72
 
54
73
  `resultCaptureMode: "off"` and nothing **derived from what your tool returns** leaves your process — not the result, and not the reason text on a failure your handler returns rather than throws. Requests are unaffected: `params` are captured either way.
package/dist/index.cjs CHANGED
@@ -79,13 +79,15 @@ __export(index_exports, {
79
79
  ToolCallStartPayloadSchema: () => ToolCallStartPayloadSchema,
80
80
  hashPrincipalId: () => hashPrincipalId,
81
81
  identityScrub: () => identityScrub,
82
+ principalFromOAuthEmail: () => principalFromOAuthEmail,
83
+ principalFromOAuthSub: () => principalFromOAuthSub,
82
84
  safeWrite: () => safeWrite,
83
85
  withBaton: () => withBaton
84
86
  });
85
87
  module.exports = __toCommonJS(index_exports);
86
88
 
87
89
  // src/version.ts
88
- var SDK_VERSION = "ts-0.5.0";
90
+ var SDK_VERSION = "ts-0.5.1";
89
91
 
90
92
  // src/events.ts
91
93
  var import_zod = require("zod");
@@ -185,7 +187,8 @@ var DEFAULT_CONSENT_TOKEN = "customer-consented";
185
187
  var PrincipalWireSchema = import_zod.z.object({
186
188
  id: import_zod.z.string(),
187
189
  source: import_zod.z.string(),
188
- form: import_zod.z.string()
190
+ form: import_zod.z.string(),
191
+ display_name: import_zod.z.string().nullable().optional()
189
192
  }).strict();
190
193
  var envelopeShape = {
191
194
  event_id: import_zod.z.uuid().default(() => (0, import_uuid.v7)()),
@@ -728,6 +731,11 @@ function roundMetaCoordinates(meta) {
728
731
  var import_node_crypto = require("crypto");
729
732
 
730
733
  // src/_text.ts
734
+ function exceedsCodePoints(value, max) {
735
+ if (value.length <= max) return false;
736
+ if (value.length > 2 * max) return true;
737
+ return [...value].length > max;
738
+ }
731
739
  function capCodePoints(value, max) {
732
740
  if (value.length <= max) return value;
733
741
  return [...value].slice(0, max).join("");
@@ -738,6 +746,13 @@ var PRINCIPAL_SOURCE_ASSERTED = "asserted";
738
746
  var PRINCIPAL_FORM_HASHED = "hashed";
739
747
  var PRINCIPAL_FORM_RAW = "raw";
740
748
  var RAW_PRINCIPAL_ID_MAX_LEN = 128;
749
+ var DISPLAY_NAME_MAX_LEN = 128;
750
+ var ALL_WHITE_SPACE = /^\p{White_Space}*$/u;
751
+ function wireDisplayName(value) {
752
+ if (typeof value !== "string" || exceedsCodePoints(value, DISPLAY_NAME_MAX_LEN)) return null;
753
+ if (LONE_SURROGATE.test(value) || value.includes("\0") || ALL_WHITE_SPACE.test(value)) return null;
754
+ return value;
755
+ }
741
756
  var PRINCIPAL_ID_MODE_HASHED = "hashed";
742
757
  var PRINCIPAL_ID_MODE_RAW = "raw";
743
758
  var FORM_BY_MODE = {
@@ -773,7 +788,7 @@ function normalizePrincipal(result) {
773
788
  if (canonicalize(candidate.principalId) === "") return null;
774
789
  const issuerRaw = typeof candidate.issuer === "string" && candidate.issuer ? candidate.issuer : null;
775
790
  const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
776
- return { principalId: candidate.principalId, issuer };
791
+ return typeof candidate.displayName === "string" ? { principalId: candidate.principalId, issuer, displayName: candidate.displayName } : { principalId: candidate.principalId, issuer };
777
792
  }
778
793
  function principalFor(principal, options) {
779
794
  let id;
@@ -787,7 +802,14 @@ function principalFor(principal, options) {
787
802
  issuer: principal.issuer ?? null
788
803
  });
789
804
  }
790
- return { id, source: PRINCIPAL_SOURCE_ASSERTED, form: FORM_BY_MODE[options.mode] };
805
+ const wire = {
806
+ id,
807
+ source: PRINCIPAL_SOURCE_ASSERTED,
808
+ form: FORM_BY_MODE[options.mode]
809
+ };
810
+ const displayName = wireDisplayName(principal.displayName);
811
+ if (displayName !== null) wire.display_name = displayName;
812
+ return wire;
791
813
  }
792
814
 
793
815
  // src/integrations/mcp/llmText.ts
@@ -948,6 +970,10 @@ async function emit(sink, build) {
948
970
  }
949
971
 
950
972
  // src/integrations/mcp/mcpTypes.ts
973
+ function extraAuthInfo(extra) {
974
+ const found = extra.authInfo ?? extra.http?.authInfo;
975
+ return found !== null && typeof found === "object" ? found : null;
976
+ }
951
977
  function extraMeta(extra) {
952
978
  const direct = extra._meta;
953
979
  if (direct) return direct;
@@ -992,7 +1018,8 @@ function buildPrincipalResolutionContext(extra, toolName, args) {
992
1018
  headers: extraHeaders(extra),
993
1019
  meta: extraMeta(extra),
994
1020
  toolName,
995
- arguments: args
1021
+ arguments: args,
1022
+ authInfo: extraAuthInfo(extra)
996
1023
  };
997
1024
  }
998
1025
  function warnIfIdentityCannotResolve(config) {
@@ -2822,6 +2849,38 @@ function withBaton(server, supplied = {}) {
2822
2849
  annotationToolName: resolvedAnnotationToolName
2823
2850
  });
2824
2851
  }
2852
+
2853
+ // src/integrations/mcp/oauthHooks.ts
2854
+ function claimsOf(context) {
2855
+ try {
2856
+ const extra = context.authInfo?.extra;
2857
+ return extra !== null && typeof extra === "object" ? extra : null;
2858
+ } catch {
2859
+ return null;
2860
+ }
2861
+ }
2862
+ function stringClaim(claims, name) {
2863
+ const value = claims[name];
2864
+ return typeof value === "string" && pythonStrip(value) !== "" ? value : null;
2865
+ }
2866
+ function principalFromOAuthSub(context) {
2867
+ const claims = claimsOf(context);
2868
+ if (claims === null) return null;
2869
+ const sub = stringClaim(claims, "sub");
2870
+ const iss = claims.iss;
2871
+ return sub === null ? null : { principalId: sub, issuer: typeof iss === "string" && iss !== "" ? iss : null };
2872
+ }
2873
+ function principalFromOAuthEmail(context) {
2874
+ const claims = claimsOf(context);
2875
+ if (claims === null) return null;
2876
+ const email = stringClaim(claims, "email");
2877
+ if (email === null) return null;
2878
+ const at = email.lastIndexOf("@");
2879
+ return {
2880
+ principalId: email,
2881
+ displayName: at > 0 ? email.slice(0, at) : null
2882
+ };
2883
+ }
2825
2884
  // Annotate the CommonJS export names for ESM import in node:
2826
2885
  0 && (module.exports = {
2827
2886
  AnnotationEventSchema,
@@ -2873,6 +2932,8 @@ function withBaton(server, supplied = {}) {
2873
2932
  ToolCallStartPayloadSchema,
2874
2933
  hashPrincipalId,
2875
2934
  identityScrub,
2935
+ principalFromOAuthEmail,
2936
+ principalFromOAuthSub,
2876
2937
  safeWrite,
2877
2938
  withBaton
2878
2939
  });