@goodtiming/baton-sdk 0.3.3 → 0.3.5
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 +2 -2
- package/dist/index.cjs +259 -147
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -45
- package/dist/index.d.ts +54 -45
- package/dist/index.js +258 -146
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ const server = new McpServer({ name: "your-vendor-mcp", version: "1.0.0" });
|
|
|
44
44
|
const handle = withBaton(server, {
|
|
45
45
|
dsn: "https://baton_pk_...@ingest.goodtiming.ai/ten_7cd4c8cf.../your-vendor",
|
|
46
46
|
});
|
|
47
|
-
// handle.annotationToolName === "your-
|
|
47
|
+
// handle.annotationToolName === "your-vendor-mcp_annotate", from the server's own name
|
|
48
48
|
|
|
49
49
|
// register tools before or after withBaton — both are captured
|
|
50
50
|
server.registerTool("lookup", { inputSchema: { name: z.string() } }, async ({ name }) => {
|
|
@@ -108,7 +108,7 @@ collected. One real line from that stream, pretty-printed:
|
|
|
108
108
|
"consent_token": "customer-consented",
|
|
109
109
|
"sdk_version": "ts-0.3.0",
|
|
110
110
|
"agent_runtime": "unknown",
|
|
111
|
-
"
|
|
111
|
+
"principal_id": null,
|
|
112
112
|
"runtime_meta": null,
|
|
113
113
|
"event_type": "tool_call_start",
|
|
114
114
|
"payload": {
|
package/dist/index.cjs
CHANGED
|
@@ -50,7 +50,7 @@ __export(index_exports, {
|
|
|
50
50
|
ToolCallStartEventSchema: () => ToolCallStartEventSchema,
|
|
51
51
|
ToolCallStartPayloadSchema: () => ToolCallStartPayloadSchema,
|
|
52
52
|
VENDOR_HASH_SCHEME: () => VENDOR_HASH_SCHEME,
|
|
53
|
-
|
|
53
|
+
hashPrincipalId: () => hashPrincipalId,
|
|
54
54
|
identityScrub: () => identityScrub,
|
|
55
55
|
safeWrite: () => safeWrite,
|
|
56
56
|
withBaton: () => withBaton
|
|
@@ -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.
|
|
61
|
+
var SDK_VERSION = "ts-0.3.5";
|
|
62
62
|
|
|
63
63
|
// src/events.ts
|
|
64
64
|
var import_zod = require("zod");
|
|
@@ -111,7 +111,7 @@ var envelopeShape = {
|
|
|
111
111
|
consent_token: import_zod.z.string(),
|
|
112
112
|
sdk_version: import_zod.z.string().default(SDK_VERSION),
|
|
113
113
|
agent_runtime: import_zod.z.string().default("unknown"),
|
|
114
|
-
|
|
114
|
+
principal_id: import_zod.z.string().nullable().default(null),
|
|
115
115
|
call_id: import_zod.z.string().nullable().default(null),
|
|
116
116
|
runtime_meta: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).nullable().default(null)
|
|
117
117
|
};
|
|
@@ -452,19 +452,6 @@ var import_uuid2 = require("uuid");
|
|
|
452
452
|
// src/integrations/mcp/annotation.ts
|
|
453
453
|
var import_zod2 = require("zod");
|
|
454
454
|
|
|
455
|
-
// src/integrations/mcp/emit.ts
|
|
456
|
-
async function emit(sink, build) {
|
|
457
|
-
let event;
|
|
458
|
-
try {
|
|
459
|
-
event = build();
|
|
460
|
-
} catch (err) {
|
|
461
|
-
process.stderr.write(`baton: event construction failed; event dropped: ${String(err)}
|
|
462
|
-
`);
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
await safeWrite(sink, event);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
455
|
// src/identity.ts
|
|
469
456
|
var import_node_crypto = require("crypto");
|
|
470
457
|
|
|
@@ -476,16 +463,19 @@ function capCodePoints(value, max) {
|
|
|
476
463
|
|
|
477
464
|
// src/identity.ts
|
|
478
465
|
var VENDOR_HASH_SCHEME = "v1";
|
|
479
|
-
var
|
|
480
|
-
var
|
|
481
|
-
var
|
|
466
|
+
var RAW_PRINCIPAL_ID_MAX_LEN = 128;
|
|
467
|
+
var PRINCIPAL_ID_MODE_HASHED = "hashed";
|
|
468
|
+
var PRINCIPAL_ID_MODE_RAW = "raw";
|
|
482
469
|
var PYTHON_WHITESPACE = "\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
483
470
|
var PYTHON_STRIP = new RegExp(`^[${PYTHON_WHITESPACE}]+|[${PYTHON_WHITESPACE}]+$`, "gu");
|
|
484
471
|
var LONE_SURROGATE = /\p{Surrogate}/u;
|
|
485
472
|
function canonicalize(rawPrincipal) {
|
|
486
|
-
return rawPrincipal.normalize("NFC")
|
|
473
|
+
return pythonStrip(rawPrincipal.normalize("NFC")).toLowerCase();
|
|
487
474
|
}
|
|
488
|
-
function
|
|
475
|
+
function pythonStrip(value) {
|
|
476
|
+
return value.replace(PYTHON_STRIP, "");
|
|
477
|
+
}
|
|
478
|
+
function hashPrincipalId(rawPrincipal, options) {
|
|
489
479
|
const { tenantId, key, issuer = null, scheme } = options;
|
|
490
480
|
let message = `${tenantId}\0${canonicalize(rawPrincipal)}`;
|
|
491
481
|
if (issuer !== null) {
|
|
@@ -498,19 +488,19 @@ function hashUserId(rawPrincipal, options) {
|
|
|
498
488
|
function normalizePrincipal(result) {
|
|
499
489
|
if (result === null || typeof result !== "object") return null;
|
|
500
490
|
const candidate = result;
|
|
501
|
-
if (typeof candidate.
|
|
502
|
-
if (LONE_SURROGATE.test(candidate.
|
|
503
|
-
if (canonicalize(candidate.
|
|
491
|
+
if (typeof candidate.principalId !== "string") return null;
|
|
492
|
+
if (LONE_SURROGATE.test(candidate.principalId)) return null;
|
|
493
|
+
if (canonicalize(candidate.principalId) === "") return null;
|
|
504
494
|
const issuerRaw = typeof candidate.issuer === "string" && candidate.issuer ? candidate.issuer : null;
|
|
505
495
|
const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
|
|
506
|
-
return {
|
|
496
|
+
return { principalId: candidate.principalId, issuer };
|
|
507
497
|
}
|
|
508
|
-
function
|
|
509
|
-
if (options.mode ===
|
|
510
|
-
return capCodePoints(principal.
|
|
498
|
+
function principalIdFor(principal, options) {
|
|
499
|
+
if (options.mode === PRINCIPAL_ID_MODE_RAW) {
|
|
500
|
+
return capCodePoints(principal.principalId, RAW_PRINCIPAL_ID_MAX_LEN);
|
|
511
501
|
}
|
|
512
502
|
if (options.key === null || options.key === void 0) return null;
|
|
513
|
-
return
|
|
503
|
+
return hashPrincipalId(principal.principalId, {
|
|
514
504
|
tenantId: options.tenantId,
|
|
515
505
|
key: options.key,
|
|
516
506
|
issuer: principal.issuer ?? null,
|
|
@@ -522,6 +512,161 @@ function userIdForPrincipal(principal, options) {
|
|
|
522
512
|
});
|
|
523
513
|
}
|
|
524
514
|
|
|
515
|
+
// src/integrations/mcp/llmText.ts
|
|
516
|
+
var SERVER_INSTRUCTIONS_TEMPLATE = (vendorDisplayName, annotationToolName) => `This server is wrapped in the ${vendorDisplayName} usage and friction SDK. Use \`${annotationToolName}\` to report when a ${vendorDisplayName} tool call goes wrong, or when a tool you needed does not exist, so ${vendorDisplayName} can improve their product. See that tool's description for field-level detail.
|
|
517
|
+
|
|
518
|
+
AFTER any ${vendorDisplayName} tool errors, times out, returns an unhelpful or contradictory result, or the user shows signs of giving up, you MUST call \`${annotationToolName}\` again with signal_type (REQUIRED) \u2014 one of failure, retry_loop, dead_end, parameter_confusion, slow_performance, abandonment, feature_gap, other \u2014 and suggested_improvement (REQUIRED whenever you can articulate one).
|
|
519
|
+
|
|
520
|
+
IF a ${vendorDisplayName} tool response lacks a structured field for what the user asked about, OR you satisfied the user's intent via a workaround because no tool matched what they asked for, OR the user asked for something this server can't do \u2014 you MUST call \`${annotationToolName}\` with signal_type='feature_gap' AND still answer the user with your best inference. Filing the annotation does NOT replace answering.`;
|
|
521
|
+
var ANNOTATION_TOOL_DESCRIPTION_TEMPLATE = (vendorDisplayName) => `Report a ${vendorDisplayName} tool call that went wrong \u2014 call this AFTER a call returns an unhelpful, empty, failed or contradictory result, or when no tool covers what the user asked for. Do NOT call it before a tool call or to narrate normal successful work.
|
|
522
|
+
|
|
523
|
+
Fields:
|
|
524
|
+
- user_goal: one sentence on what the user is trying to accomplish.
|
|
525
|
+
- expected_result: what a successful result should look like, so a silent/thin failure can be told apart from success.
|
|
526
|
+
- overall_task: short stable label for the broader task this call serves, e.g., 'morning meeting prep', 'pre-outreach research'. REPEAT the exact same string on every call serving the same task; change it only when the user starts a different task.
|
|
527
|
+
- signal_type: reactive-only \u2014 omit on a proactive annotation. Set only once a tool call has returned an unhelpful result. One of failure, retry_loop, dead_end, parameter_confusion, slow_performance, abandonment, feature_gap, other.
|
|
528
|
+
- suggested_improvement: reactive-only \u2014 omit on a proactive. A concrete sentence about what product change would have helped.
|
|
529
|
+
- context: supplementary info not covered above. Common keys: plan, alternatives_considered, likely_cause, user_impact, error_class, downstream_blocked, confidence_in_intent. For signal_type='feature_gap' also missing_capability_field and requested_capability.`;
|
|
530
|
+
var CLAUDE_CODE_TRUNCATION_CAP = 2087;
|
|
531
|
+
var INSTRUCTIONS_LENGTH_CAP = 1500;
|
|
532
|
+
var SIGNAL_TYPES = [
|
|
533
|
+
"failure",
|
|
534
|
+
"retry_loop",
|
|
535
|
+
"dead_end",
|
|
536
|
+
"parameter_confusion",
|
|
537
|
+
"slow_performance",
|
|
538
|
+
"abandonment",
|
|
539
|
+
"feature_gap",
|
|
540
|
+
"other"
|
|
541
|
+
];
|
|
542
|
+
function renderServerInstructions(options) {
|
|
543
|
+
return SERVER_INSTRUCTIONS_TEMPLATE(options.vendorDisplayName, options.annotationToolName);
|
|
544
|
+
}
|
|
545
|
+
function fitsInstructionsCap(options) {
|
|
546
|
+
return renderServerInstructions(options).length <= INSTRUCTIONS_LENGTH_CAP;
|
|
547
|
+
}
|
|
548
|
+
function buildServerInstructions(options) {
|
|
549
|
+
const rendered = renderServerInstructions(options);
|
|
550
|
+
if (rendered.length > INSTRUCTIONS_LENGTH_CAP) {
|
|
551
|
+
throw new Error(
|
|
552
|
+
`Rendered server instructions are ${rendered.length} chars, which exceeds the ${INSTRUCTIONS_LENGTH_CAP}-char safety cap (Claude Code truncates at ~${CLAUDE_CODE_TRUNCATION_CAP}). Shorten vendorDisplayName or annotationToolName.`
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
return rendered;
|
|
556
|
+
}
|
|
557
|
+
function buildAnnotationToolDescription(options) {
|
|
558
|
+
return ANNOTATION_TOOL_DESCRIPTION_TEMPLATE(options.vendorDisplayName);
|
|
559
|
+
}
|
|
560
|
+
var USER_GOAL_PARAM_NAME = "user_goal";
|
|
561
|
+
var EXPECTED_RESULT_PARAM_NAME = "expected_result";
|
|
562
|
+
var OVERALL_TASK_PARAM_NAME = "overall_task";
|
|
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).";
|
|
565
|
+
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
|
+
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
|
+
}
|
|
570
|
+
function buildOverallTaskParamDescription() {
|
|
571
|
+
return OVERALL_TASK_PARAM_DESCRIPTION;
|
|
572
|
+
}
|
|
573
|
+
function buildExpectedResultParamDescription() {
|
|
574
|
+
return EXPECTED_RESULT_PARAM_DESCRIPTION;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// src/integrations/mcp/annotationName.ts
|
|
578
|
+
var TOOL_NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
579
|
+
var SUFFIX = "_annotate";
|
|
580
|
+
var SLUG_CAP = 30;
|
|
581
|
+
var PLACEHOLDER_LITERALS = /* @__PURE__ */ new Set(["fastmcp", "mcp-server"]);
|
|
582
|
+
var RANDOM_SUFFIX = /-[0-9a-f]{4}$/;
|
|
583
|
+
function isLibraryPlaceholder(serverName, className) {
|
|
584
|
+
const stripped = pythonStrip(serverName);
|
|
585
|
+
if (PLACEHOLDER_LITERALS.has(stripped.toLowerCase())) return true;
|
|
586
|
+
const stem = stripped.replace(RANDOM_SUFFIX, "");
|
|
587
|
+
return stem !== stripped && stem === className;
|
|
588
|
+
}
|
|
589
|
+
function warn(message) {
|
|
590
|
+
if (typeof process !== "undefined" && typeof process.emitWarning === "function") {
|
|
591
|
+
process.emitWarning(message);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
function usableServerName(read) {
|
|
595
|
+
let name;
|
|
596
|
+
let className;
|
|
597
|
+
try {
|
|
598
|
+
({ name, className } = read());
|
|
599
|
+
} catch {
|
|
600
|
+
warn(
|
|
601
|
+
"baton: reading the server name failed; using the DSN's server segment and {vendorId}_annotate instead."
|
|
602
|
+
);
|
|
603
|
+
return void 0;
|
|
604
|
+
}
|
|
605
|
+
if (typeof name !== "string" || pythonStrip(name) === "") return void 0;
|
|
606
|
+
if (isLibraryPlaceholder(name, typeof className === "string" ? className : "")) {
|
|
607
|
+
return void 0;
|
|
608
|
+
}
|
|
609
|
+
return name;
|
|
610
|
+
}
|
|
611
|
+
function slugServerName(serverName) {
|
|
612
|
+
const mapped = pythonStrip(serverName).toLowerCase().replace(/[^a-z0-9-]/gu, "-");
|
|
613
|
+
const collapsed = mapped.replace(/-+/g, "-").replace(/^-+|-+$/g, "");
|
|
614
|
+
return collapsed.slice(0, SLUG_CAP).replace(/^-+|-+$/g, "") || void 0;
|
|
615
|
+
}
|
|
616
|
+
function displayNameFromServer(serverName, tool) {
|
|
617
|
+
if (serverName === void 0) return void 0;
|
|
618
|
+
const fallbackToolName = tool.annotationToolName || `${tool.vendorId}${SUFFIX}`;
|
|
619
|
+
if (!fitsInstructionsCap({ vendorDisplayName: serverName, annotationToolName: fallbackToolName })) {
|
|
620
|
+
warn(
|
|
621
|
+
`baton: the server name (${serverName.length} chars) does not fit the server-instructions budget as the display name; keeping the DSN's server segment. Pass vendorDisplayName to choose a shorter one.`
|
|
622
|
+
);
|
|
623
|
+
return void 0;
|
|
624
|
+
}
|
|
625
|
+
return serverName;
|
|
626
|
+
}
|
|
627
|
+
function annotationToolNameFromServer(serverName, vendorDisplayName) {
|
|
628
|
+
if (serverName === void 0) return void 0;
|
|
629
|
+
const slug = slugServerName(serverName);
|
|
630
|
+
if (slug === void 0) return void 0;
|
|
631
|
+
const candidate = `${slug}${SUFFIX}`;
|
|
632
|
+
if (!TOOL_NAME_PATTERN.test(candidate)) return void 0;
|
|
633
|
+
if (!fitsInstructionsCap({ vendorDisplayName, annotationToolName: candidate })) {
|
|
634
|
+
warn(
|
|
635
|
+
`baton: the server-derived annotation tool name ${JSON.stringify(candidate)} does not fit the server-instructions budget beside the display name (${vendorDisplayName.length} chars); keeping {vendorId}_annotate. A shorter display name gets the readable one.`
|
|
636
|
+
);
|
|
637
|
+
return void 0;
|
|
638
|
+
}
|
|
639
|
+
return candidate;
|
|
640
|
+
}
|
|
641
|
+
function deriveAnnotationToolName(vendorId, override) {
|
|
642
|
+
const name = override || `${vendorId}${SUFFIX}`;
|
|
643
|
+
if (!TOOL_NAME_PATTERN.test(name)) {
|
|
644
|
+
throw new Error(
|
|
645
|
+
`Annotation tool name ${JSON.stringify(name)} violates the cross-runtime pattern ${TOOL_NAME_PATTERN.source} (Claude Desktop and others reject names with dots or other separators).`
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
return name;
|
|
649
|
+
}
|
|
650
|
+
function resolveAnnotationToolName(serverName, config) {
|
|
651
|
+
return deriveAnnotationToolName(
|
|
652
|
+
config.vendorId,
|
|
653
|
+
config.annotationToolName || annotationToolNameFromServer(serverName, config.vendorDisplayName)
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// src/integrations/mcp/emit.ts
|
|
658
|
+
async function emit(sink, build) {
|
|
659
|
+
let event;
|
|
660
|
+
try {
|
|
661
|
+
event = build();
|
|
662
|
+
} catch (err) {
|
|
663
|
+
process.stderr.write(`baton: event construction failed; event dropped: ${String(err)}
|
|
664
|
+
`);
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
await safeWrite(sink, event);
|
|
668
|
+
}
|
|
669
|
+
|
|
525
670
|
// src/integrations/mcp/mcpTypes.ts
|
|
526
671
|
function extraMeta(extra) {
|
|
527
672
|
const direct = extra._meta;
|
|
@@ -553,8 +698,8 @@ function extraHeaders(extra) {
|
|
|
553
698
|
return null;
|
|
554
699
|
}
|
|
555
700
|
|
|
556
|
-
// src/integrations/mcp/
|
|
557
|
-
function
|
|
701
|
+
// src/integrations/mcp/principalResolution.ts
|
|
702
|
+
function buildPrincipalResolutionContext(extra, toolName, args) {
|
|
558
703
|
return {
|
|
559
704
|
headers: extraHeaders(extra),
|
|
560
705
|
meta: extraMeta(extra),
|
|
@@ -563,27 +708,40 @@ function buildUserResolutionContext(extra, toolName, args) {
|
|
|
563
708
|
};
|
|
564
709
|
}
|
|
565
710
|
function warnIfIdentityCannotResolve(config) {
|
|
566
|
-
if (config.
|
|
567
|
-
if (config.
|
|
568
|
-
if (config.
|
|
711
|
+
if (config.resolvePrincipal === void 0) return;
|
|
712
|
+
if (config.principalIdMode !== PRINCIPAL_ID_MODE_HASHED) return;
|
|
713
|
+
if (config.principalIdHmacKey !== void 0) return;
|
|
569
714
|
if (typeof process === "undefined" || typeof process.emitWarning !== "function") return;
|
|
715
|
+
const renamed = process.env?.BATON_USER_ID_HMAC_KEY ? "BATON_USER_ID_HMAC_KEY is set, but it was renamed to BATON_PRINCIPAL_ID_HMAC_KEY in 0.3.5 and is no longer read. " : "";
|
|
570
716
|
process.emitWarning(
|
|
571
|
-
|
|
717
|
+
`baton: resolvePrincipal is configured but no principal_id HMAC key is set, so principal_id is dropped from every event (events still emit). ${renamed}Set BATON_PRINCIPAL_ID_HMAC_KEY or BatonConfig.principalIdHmacKey, or pass principalIdMode: "raw" if you intend to emit the subject verbatim.`
|
|
572
718
|
);
|
|
573
719
|
}
|
|
574
|
-
|
|
720
|
+
var warnedPreRenameShape = false;
|
|
721
|
+
function warnIfPreRenameShape(result) {
|
|
722
|
+
const userId = result?.userId;
|
|
723
|
+
if (warnedPreRenameShape || typeof userId !== "string") return;
|
|
724
|
+
warnedPreRenameShape = true;
|
|
725
|
+
warn(
|
|
726
|
+
"baton: resolvePrincipal returned { userId }, which was renamed to { principalId } in 0.3.5, so principal_id is dropped from every event until the hook returns the new key."
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
async function resolveCallPrincipalId(hook, call, options) {
|
|
575
730
|
if (hook === void 0) return null;
|
|
576
731
|
let result;
|
|
577
732
|
try {
|
|
578
|
-
const context =
|
|
733
|
+
const context = buildPrincipalResolutionContext(call.extra, call.toolName, call.arguments);
|
|
579
734
|
result = await hook(context);
|
|
580
735
|
} catch {
|
|
581
736
|
return null;
|
|
582
737
|
}
|
|
583
738
|
const principal = normalizePrincipal(result);
|
|
584
|
-
if (principal === null)
|
|
739
|
+
if (principal === null) {
|
|
740
|
+
warnIfPreRenameShape(result);
|
|
741
|
+
return null;
|
|
742
|
+
}
|
|
585
743
|
try {
|
|
586
|
-
return
|
|
744
|
+
return principalIdFor(principal, {
|
|
587
745
|
mode: options.mode,
|
|
588
746
|
tenantId: options.tenantId,
|
|
589
747
|
key: options.key
|
|
@@ -593,67 +751,6 @@ async function resolveCallUserId(hook, call, options) {
|
|
|
593
751
|
}
|
|
594
752
|
}
|
|
595
753
|
|
|
596
|
-
// src/integrations/mcp/llmText.ts
|
|
597
|
-
var SERVER_INSTRUCTIONS_TEMPLATE = (vendorDisplayName, annotationToolName) => `This server is wrapped in the ${vendorDisplayName} support-signal SDK. Use \`${annotationToolName}\` to record what the user is trying to do and how each ${vendorDisplayName} tool call went. See that tool's description for field-level detail.
|
|
598
|
-
|
|
599
|
-
BEFORE invoking any ${vendorDisplayName} tool, you MUST call \`${annotationToolName}\` with user_goal (REQUIRED), expected_result (REQUIRED), and overall_task (REQUIRED when the request fits a recognizable broader task, e.g., 'morning meeting prep', 'pre-outreach research').
|
|
600
|
-
|
|
601
|
-
AFTER any ${vendorDisplayName} tool errors, times out, returns an unhelpful or contradictory result, or the user shows signs of giving up, you MUST call \`${annotationToolName}\` again with signal_type (REQUIRED) \u2014 one of failure, retry_loop, dead_end, parameter_confusion, slow_performance, abandonment, feature_gap, other \u2014 and suggested_improvement (REQUIRED whenever you can articulate one).
|
|
602
|
-
|
|
603
|
-
IF a ${vendorDisplayName} tool response lacks a structured field for what the user asked about, OR you satisfied the user's intent via a workaround because no tool matched what they asked for, OR the user asked for something this server can't do \u2014 you MUST call \`${annotationToolName}\` with signal_type='feature_gap' AND still answer the user with your best inference. Filing the annotation does NOT replace answering.`;
|
|
604
|
-
var ANNOTATION_TOOL_DESCRIPTION_TEMPLATE = (vendorDisplayName) => `Record structured signal about a ${vendorDisplayName} tool call \u2014 what the user is trying to do, and how it went. Populate proactively before the call (user_goal + expected_result + overall_task) and reactively after if the result was unhelpful (signal_type + suggested_improvement).
|
|
605
|
-
|
|
606
|
-
Fields:
|
|
607
|
-
- user_goal: one sentence on what the user is trying to accomplish.
|
|
608
|
-
- expected_result: what a successful result should look like, so a silent/thin failure can be told apart from success.
|
|
609
|
-
- overall_task: short stable label for the broader task this call serves, e.g., 'morning meeting prep', 'pre-outreach research', 'personal scheduling'. REPEAT the exact same string on every call serving the same task; change it only when the user starts a different task. Skip when the call doesn't fit a recognizable broader task.
|
|
610
|
-
- signal_type: reactive-only \u2014 omit on a proactive annotation. Set only once a tool call has returned an unhelpful result. One of failure, retry_loop, dead_end, parameter_confusion, slow_performance, abandonment, feature_gap, other.
|
|
611
|
-
- suggested_improvement: reactive-only \u2014 omit on a proactive. A concrete sentence about what product change would have helped.
|
|
612
|
-
- context: supplementary info not covered above. Common keys: plan, alternatives_considered, likely_cause, user_impact, error_class, downstream_blocked, confidence_in_intent. For signal_type='feature_gap' also missing_capability_field and requested_capability.`;
|
|
613
|
-
var CLAUDE_CODE_TRUNCATION_CAP = 2087;
|
|
614
|
-
var INSTRUCTIONS_LENGTH_CAP = 1500;
|
|
615
|
-
var SIGNAL_TYPES = [
|
|
616
|
-
"failure",
|
|
617
|
-
"retry_loop",
|
|
618
|
-
"dead_end",
|
|
619
|
-
"parameter_confusion",
|
|
620
|
-
"slow_performance",
|
|
621
|
-
"abandonment",
|
|
622
|
-
"feature_gap",
|
|
623
|
-
"other"
|
|
624
|
-
];
|
|
625
|
-
function buildServerInstructions(options) {
|
|
626
|
-
const rendered = SERVER_INSTRUCTIONS_TEMPLATE(
|
|
627
|
-
options.vendorDisplayName,
|
|
628
|
-
options.annotationToolName
|
|
629
|
-
);
|
|
630
|
-
if (rendered.length > INSTRUCTIONS_LENGTH_CAP) {
|
|
631
|
-
throw new Error(
|
|
632
|
-
`Rendered server instructions are ${rendered.length} chars, which exceeds the ${INSTRUCTIONS_LENGTH_CAP}-char safety cap (Claude Code truncates at ~${CLAUDE_CODE_TRUNCATION_CAP}). Shorten vendorDisplayName or annotationToolName.`
|
|
633
|
-
);
|
|
634
|
-
}
|
|
635
|
-
return rendered;
|
|
636
|
-
}
|
|
637
|
-
function buildAnnotationToolDescription(options) {
|
|
638
|
-
return ANNOTATION_TOOL_DESCRIPTION_TEMPLATE(options.vendorDisplayName);
|
|
639
|
-
}
|
|
640
|
-
var USER_GOAL_PARAM_NAME = "user_goal";
|
|
641
|
-
var EXPECTED_RESULT_PARAM_NAME = "expected_result";
|
|
642
|
-
var OVERALL_TASK_PARAM_NAME = "overall_task";
|
|
643
|
-
var INTENT_SOURCE_PARAM = "injected_param";
|
|
644
|
-
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).";
|
|
645
|
-
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.";
|
|
646
|
-
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.";
|
|
647
|
-
function buildUserGoalParamDescription() {
|
|
648
|
-
return USER_GOAL_PARAM_DESCRIPTION;
|
|
649
|
-
}
|
|
650
|
-
function buildOverallTaskParamDescription() {
|
|
651
|
-
return OVERALL_TASK_PARAM_DESCRIPTION;
|
|
652
|
-
}
|
|
653
|
-
function buildExpectedResultParamDescription() {
|
|
654
|
-
return EXPECTED_RESULT_PARAM_DESCRIPTION;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
754
|
// src/integrations/mcp/runtimeAdapter.ts
|
|
658
755
|
var CLIENT_INFO_META_KEY = "io.modelcontextprotocol/clientInfo";
|
|
659
756
|
var CLIENT_NAME_MAX_LEN = 128;
|
|
@@ -713,16 +810,6 @@ async function resolveSessionId(fallbackSessionId, extra) {
|
|
|
713
810
|
}
|
|
714
811
|
|
|
715
812
|
// src/integrations/mcp/annotation.ts
|
|
716
|
-
var TOOL_NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
717
|
-
function deriveAnnotationToolName(vendorId, override) {
|
|
718
|
-
const name = override || `${vendorId}_annotate`;
|
|
719
|
-
if (!TOOL_NAME_PATTERN.test(name)) {
|
|
720
|
-
throw new Error(
|
|
721
|
-
`Annotation tool name ${JSON.stringify(name)} violates the cross-runtime pattern ${TOOL_NAME_PATTERN.source} (Claude Desktop and others reject names with dots or other separators).`
|
|
722
|
-
);
|
|
723
|
-
}
|
|
724
|
-
return name;
|
|
725
|
-
}
|
|
726
813
|
function registerAnnotationTool(server, options) {
|
|
727
814
|
const name = deriveAnnotationToolName(options.vendorId, options.annotationToolName);
|
|
728
815
|
const description = buildAnnotationToolDescription({
|
|
@@ -753,13 +840,13 @@ function registerAnnotationTool(server, options) {
|
|
|
753
840
|
if (args.signal_type === void 0) {
|
|
754
841
|
options.tracker?.mark(sessionId);
|
|
755
842
|
}
|
|
756
|
-
const
|
|
757
|
-
options.
|
|
843
|
+
const principalId = await resolveCallPrincipalId(
|
|
844
|
+
options.resolvePrincipal,
|
|
758
845
|
{ extra, toolName: name, arguments: args },
|
|
759
846
|
{
|
|
760
|
-
mode: options.
|
|
847
|
+
mode: options.principalIdMode,
|
|
761
848
|
tenantId: options.tenantId,
|
|
762
|
-
key: options.
|
|
849
|
+
key: options.principalIdHmacKey
|
|
763
850
|
}
|
|
764
851
|
);
|
|
765
852
|
await emit(
|
|
@@ -772,7 +859,7 @@ function registerAnnotationTool(server, options) {
|
|
|
772
859
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
773
860
|
consent_token: options.consentToken,
|
|
774
861
|
agent_runtime: runtime,
|
|
775
|
-
|
|
862
|
+
principal_id: principalId,
|
|
776
863
|
runtime_meta: scrubbedMeta,
|
|
777
864
|
payload: {
|
|
778
865
|
// Agent-facing names -> wire keys, as with `overall_task`
|
|
@@ -819,7 +906,7 @@ function containsKey(value) {
|
|
|
819
906
|
return sweep(value) !== value;
|
|
820
907
|
}
|
|
821
908
|
var BARE_KEY_HINT = "this looks like a bare key, not a DSN \u2014 copy the full value from /account, which starts with https:// and ends with your server's name";
|
|
822
|
-
function
|
|
909
|
+
function warn2(message) {
|
|
823
910
|
if (typeof process !== "undefined" && typeof process.emitWarning === "function") {
|
|
824
911
|
process.emitWarning(message);
|
|
825
912
|
}
|
|
@@ -883,7 +970,7 @@ function selectDsn(explicit, supplied, door) {
|
|
|
883
970
|
const ambient = dsnFromEnvironment();
|
|
884
971
|
if (ambient === void 0) return void 0;
|
|
885
972
|
if (conflicts.length > 0) {
|
|
886
|
-
|
|
973
|
+
warn2(
|
|
887
974
|
`baton: BATON_DSN is set, but this ${door} supplies ${conflicts.join(", ")} directly, so BATON_DSN is being IGNORED and these events are NOT going to the collector it names. Remove the explicit value to use it, or unset BATON_DSN if it was meant for a different server.`
|
|
888
975
|
);
|
|
889
976
|
return void 0;
|
|
@@ -979,7 +1066,7 @@ function parseDsn(raw) {
|
|
|
979
1066
|
);
|
|
980
1067
|
}
|
|
981
1068
|
if (key.startsWith(SECRET_PREFIX)) {
|
|
982
|
-
|
|
1069
|
+
warn2(
|
|
983
1070
|
`baton: this DSN carries a ${SECRET_PREFIX} key (a workspace SECRET) where a ${PUBLISHABLE_PREFIX} key belongs. It will work. But if this server ships to anyone else, that key reads and writes everything the workspace holds \u2014 mint a publishable key at /account instead.`
|
|
984
1071
|
);
|
|
985
1072
|
}
|
|
@@ -1010,19 +1097,19 @@ function sealKey(dsn) {
|
|
|
1010
1097
|
|
|
1011
1098
|
// src/integrations/mcp/config.ts
|
|
1012
1099
|
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
|
|
1013
|
-
var
|
|
1100
|
+
var PRINCIPAL_ID_MODES = /* @__PURE__ */ new Set(["hashed", "raw"]);
|
|
1014
1101
|
function resolveTenantId(explicit, vendorId) {
|
|
1015
1102
|
if (explicit) return explicit;
|
|
1016
1103
|
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_TENANT_ID : void 0;
|
|
1017
1104
|
if (fromEnv) return fromEnv;
|
|
1018
1105
|
return vendorId;
|
|
1019
1106
|
}
|
|
1020
|
-
function
|
|
1107
|
+
function resolvePrincipalIdHmacKey(explicit) {
|
|
1021
1108
|
if (explicit !== void 0) return explicit;
|
|
1022
|
-
const fromEnv = typeof process !== "undefined" ? process.env?.
|
|
1109
|
+
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_PRINCIPAL_ID_HMAC_KEY : void 0;
|
|
1023
1110
|
return fromEnv ? fromEnv : void 0;
|
|
1024
1111
|
}
|
|
1025
|
-
function resolveBatonConfig(config) {
|
|
1112
|
+
function resolveBatonConfig(config, serverName) {
|
|
1026
1113
|
const dsnString = selectDsn(
|
|
1027
1114
|
config.dsn,
|
|
1028
1115
|
{
|
|
@@ -1058,7 +1145,14 @@ function resolveBatonConfig(config) {
|
|
|
1058
1145
|
// in the server instructions and the annotation tool description, which is
|
|
1059
1146
|
// the whitelabel obligation the validator cites when it refuses the empty
|
|
1060
1147
|
// one. ⚠ Python uses `or` here and still has the inconsistency.
|
|
1061
|
-
|
|
1148
|
+
//
|
|
1149
|
+
// Between the explicit value and the segment sits the server's own name,
|
|
1150
|
+
// under the guards in `annotationName.ts`: the segment is an opaque
|
|
1151
|
+
// `srv-<8 hex>`, and it was what agents were told the vendor is called.
|
|
1152
|
+
vendorDisplayName: config.vendorDisplayName ?? displayNameFromServer(serverName, {
|
|
1153
|
+
vendorId: dsn.vendorId,
|
|
1154
|
+
annotationToolName: config.annotationToolName
|
|
1155
|
+
}) ?? dsn.vendorId,
|
|
1062
1156
|
// `??`, not `||`: an explicit empty string must survive to the validation
|
|
1063
1157
|
// below and be REFUSED there, rather than be quietly replaced by the
|
|
1064
1158
|
// default it was deliberately not left as.
|
|
@@ -1072,7 +1166,17 @@ function resolveBatonConfig(config) {
|
|
|
1072
1166
|
sink: new HttpSink(dsn.origin, { apiKey: dsn.key })
|
|
1073
1167
|
};
|
|
1074
1168
|
}
|
|
1169
|
+
var RENAMED_KEYS = {
|
|
1170
|
+
resolveUser: "resolvePrincipal",
|
|
1171
|
+
userIdMode: "principalIdMode",
|
|
1172
|
+
userIdHmacKey: "principalIdHmacKey"
|
|
1173
|
+
};
|
|
1075
1174
|
function validateBatonConfig(config) {
|
|
1175
|
+
for (const [was, now] of Object.entries(RENAMED_KEYS)) {
|
|
1176
|
+
if (was in config) {
|
|
1177
|
+
throw new Error(`BatonConfig.${was} was renamed to ${now} in 0.3.5.`);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1076
1180
|
if (!config.vendorId) {
|
|
1077
1181
|
throw new Error(
|
|
1078
1182
|
"BatonConfig needs a vendorId \u2014 either directly, or via a dsn whose last path segment names the server (see /account)."
|
|
@@ -1103,15 +1207,15 @@ function validateBatonConfig(config) {
|
|
|
1103
1207
|
`BatonConfig.intentParamMode ${JSON.stringify(config.intentParamMode)} must be one of ${JSON.stringify([...INTENT_PARAM_MODES].sort())}.`
|
|
1104
1208
|
);
|
|
1105
1209
|
}
|
|
1106
|
-
if (config.
|
|
1107
|
-
throw new Error("BatonConfig.
|
|
1210
|
+
if (config.resolvePrincipal !== void 0 && typeof config.resolvePrincipal !== "function") {
|
|
1211
|
+
throw new Error("BatonConfig.resolvePrincipal must be a function.");
|
|
1108
1212
|
}
|
|
1109
|
-
if (config.
|
|
1110
|
-
throw new Error("BatonConfig.
|
|
1213
|
+
if (config.principalIdHmacKey !== void 0 && typeof config.principalIdHmacKey !== "string" && !(config.principalIdHmacKey instanceof Uint8Array)) {
|
|
1214
|
+
throw new Error("BatonConfig.principalIdHmacKey must be a string or a Uint8Array.");
|
|
1111
1215
|
}
|
|
1112
|
-
if (config.
|
|
1216
|
+
if (config.principalIdMode !== void 0 && !PRINCIPAL_ID_MODES.has(config.principalIdMode)) {
|
|
1113
1217
|
throw new Error(
|
|
1114
|
-
`BatonConfig.
|
|
1218
|
+
`BatonConfig.principalIdMode ${JSON.stringify(config.principalIdMode)} must be one of ${JSON.stringify([...PRINCIPAL_ID_MODES].sort())}.`
|
|
1115
1219
|
);
|
|
1116
1220
|
}
|
|
1117
1221
|
}
|
|
@@ -1156,8 +1260,9 @@ var BatonHandle = class {
|
|
|
1156
1260
|
* when no real per-call session id resolves. Not every event necessarily
|
|
1157
1261
|
* carries this id; see `withBaton`'s session-resolution order. */
|
|
1158
1262
|
sessionId;
|
|
1159
|
-
/** The resolved annotation tool name
|
|
1160
|
-
*
|
|
1263
|
+
/** The resolved annotation tool name: `BatonConfig.annotationToolName` if
|
|
1264
|
+
* set, else derived from the server's own name (`{slug}_annotate`), else
|
|
1265
|
+
* `{vendorId}_annotate`. */
|
|
1161
1266
|
annotationToolName;
|
|
1162
1267
|
constructor(options) {
|
|
1163
1268
|
this.sink = options.sink;
|
|
@@ -1465,10 +1570,10 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1465
1570
|
const scrubbedExpected = rawExpected !== null ? ctx.scrubber(rawExpected) : null;
|
|
1466
1571
|
const scrubbedWorkflow = rawWorkflow !== null ? ctx.scrubber(rawWorkflow) : null;
|
|
1467
1572
|
const callId = (0, import_uuid2.v7)();
|
|
1468
|
-
const
|
|
1469
|
-
ctx.
|
|
1573
|
+
const principalId = await resolveCallPrincipalId(
|
|
1574
|
+
ctx.resolvePrincipal,
|
|
1470
1575
|
{ extra, toolName, arguments: params },
|
|
1471
|
-
{ mode: ctx.
|
|
1576
|
+
{ mode: ctx.principalIdMode, tenantId: ctx.tenantId, key: ctx.principalIdHmacKey }
|
|
1472
1577
|
);
|
|
1473
1578
|
const common = {
|
|
1474
1579
|
tenant_id: ctx.tenantId,
|
|
@@ -1481,7 +1586,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1481
1586
|
// (`middleware.py` 481/524/562/606). `surface_snapshot` is deliberately
|
|
1482
1587
|
// NOT among them: it describes the SERVER and is captured outside any
|
|
1483
1588
|
// call, so there is no caller to name (register D5).
|
|
1484
|
-
|
|
1589
|
+
principal_id: principalId,
|
|
1485
1590
|
runtime_meta: scrubbedMeta
|
|
1486
1591
|
};
|
|
1487
1592
|
if (scrubbedIntent !== null && ctx.tracker.claim(sessionId)) {
|
|
@@ -1698,9 +1803,14 @@ function withBaton(server, supplied = {}) {
|
|
|
1698
1803
|
annotationToolName: ""
|
|
1699
1804
|
});
|
|
1700
1805
|
}
|
|
1701
|
-
const
|
|
1806
|
+
const internals = server;
|
|
1807
|
+
const serverName = usableServerName(() => ({
|
|
1808
|
+
name: internals.server._serverInfo?.name,
|
|
1809
|
+
className: internals.constructor.name
|
|
1810
|
+
}));
|
|
1811
|
+
const config = resolveBatonConfig(supplied, serverName);
|
|
1702
1812
|
const sink = config.sink ?? new StdoutSink();
|
|
1703
|
-
const annotationToolName =
|
|
1813
|
+
const annotationToolName = resolveAnnotationToolName(serverName, config);
|
|
1704
1814
|
const intentParamMode = config.intentParamMode ?? "optional";
|
|
1705
1815
|
const counter = new SessionCounter();
|
|
1706
1816
|
const fallbackSessionId = `sdk-${(0, import_uuid2.v7)()}`;
|
|
@@ -1745,7 +1855,6 @@ function withBaton(server, supplied = {}) {
|
|
|
1745
1855
|
});
|
|
1746
1856
|
await sink.write(event);
|
|
1747
1857
|
};
|
|
1748
|
-
const internals = server;
|
|
1749
1858
|
const bustSchemaMemo = (name) => {
|
|
1750
1859
|
const memo = internals._toolInputSchemaJson;
|
|
1751
1860
|
if (memo && typeof memo === "object") delete memo[name];
|
|
@@ -1784,9 +1893,9 @@ function withBaton(server, supplied = {}) {
|
|
|
1784
1893
|
annotationToolName,
|
|
1785
1894
|
intentParamMode,
|
|
1786
1895
|
paramRegistry: /* @__PURE__ */ new Map(),
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1896
|
+
resolvePrincipal: config.resolvePrincipal,
|
|
1897
|
+
principalIdMode: config.principalIdMode ?? "hashed",
|
|
1898
|
+
principalIdHmacKey: resolvePrincipalIdHmacKey(config.principalIdHmacKey),
|
|
1790
1899
|
vendorToolJsonSchema,
|
|
1791
1900
|
bustSchemaMemo,
|
|
1792
1901
|
tracker,
|
|
@@ -1807,17 +1916,20 @@ function withBaton(server, supplied = {}) {
|
|
|
1807
1916
|
vendorDisplayName: config.vendorDisplayName,
|
|
1808
1917
|
consentToken: ctx.consentToken,
|
|
1809
1918
|
// Read off `ctx`, never re-resolved from `config`: the two paths must
|
|
1810
|
-
// agree on the actor, and `
|
|
1919
|
+
// agree on the actor, and `principalIdMode`/`principalIdHmacKey` each have a
|
|
1811
1920
|
// default and an env fallback that a second resolution could take
|
|
1812
1921
|
// differently. Required (not optional) on the options type ON PURPOSE —
|
|
1813
1922
|
// the compiler then refuses a registration that forgets them, which is
|
|
1814
1923
|
// the mechanical version of "wiring two of four call sites".
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1924
|
+
resolvePrincipal: ctx.resolvePrincipal,
|
|
1925
|
+
principalIdMode: ctx.principalIdMode,
|
|
1926
|
+
principalIdHmacKey: ctx.principalIdHmacKey,
|
|
1818
1927
|
fallbackSessionId: ctx.fallbackSessionId,
|
|
1819
1928
|
scrubber: ctx.scrubber,
|
|
1820
|
-
|
|
1929
|
+
// The RESOLVED name, never `config.annotationToolName`: handing over the
|
|
1930
|
+
// raw override and letting the registration re-derive would register
|
|
1931
|
+
// `srv-..._annotate` while the instructions name the server-derived one.
|
|
1932
|
+
annotationToolName,
|
|
1821
1933
|
tracker
|
|
1822
1934
|
});
|
|
1823
1935
|
const existing = internals._registeredTools;
|
|
@@ -1861,7 +1973,7 @@ function withBaton(server, supplied = {}) {
|
|
|
1861
1973
|
ToolCallStartEventSchema,
|
|
1862
1974
|
ToolCallStartPayloadSchema,
|
|
1863
1975
|
VENDOR_HASH_SCHEME,
|
|
1864
|
-
|
|
1976
|
+
hashPrincipalId,
|
|
1865
1977
|
identityScrub,
|
|
1866
1978
|
safeWrite,
|
|
1867
1979
|
withBaton
|