@goodtiming/baton-sdk 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  *The TypeScript counterpart to [`baton-sdk`](https://github.com/good-timing/baton) (Python) — structured signal capture for agent-mediated tool use over MCP.*
4
4
 
5
- **Status: published — [`@goodtiming/baton-sdk@0.1.0`](https://www.npmjs.com/package/@goodtiming/baton-sdk) on npm.** `withBaton` instruments a high-level `McpServer` — **either** official SDK major, 1.x (`@modelcontextprotocol/sdk`) or v2 (`@modelcontextprotocol/server`): wraps every tool call, injects server `instructions`, registers the `<vendor>_annotate` tool (SPEC §5.1.1–§5.1.2), injects `user_goal`/`expected_result`/`overall_task` intent params on every wrapped tool's schema, captures a `surface_snapshot` of the vendor-true surface, and PII-scrubs every payload with the same default ruleset the Python SDK ships. What's still not here: intent-param injection for **non-zod** schemas on v2, MRTR (Python's `_is_mrtr_pause`/`_is_mrtr_continuation`), and the low-level `Server` adapter — see [What's deferred](#whats-deferred).
5
+ **Status: published — [`@goodtiming/baton-sdk@0.3.0`](https://www.npmjs.com/package/@goodtiming/baton-sdk) on npm.** `withBaton` instruments a high-level `McpServer` — **either** official SDK major, 1.x (`@modelcontextprotocol/sdk`) or v2 (`@modelcontextprotocol/server`): wraps every tool call, injects server `instructions`, registers the `<vendor>_annotate` tool (SPEC §5.1.1–§5.1.2), injects `user_goal`/`expected_result`/`overall_task` intent params on every wrapped tool's schema, captures a `surface_snapshot` of the vendor-true surface, and PII-scrubs every payload with the same default ruleset the Python SDK ships. What's still not here: intent-param injection for **non-zod** schemas on v2, MRTR (Python's `_is_mrtr_pause`/`_is_mrtr_continuation`), and the low-level `Server` adapter — see [What's deferred](#whats-deferred).
6
6
 
7
7
  ## Why this exists
8
8
 
@@ -13,7 +13,7 @@ Most production MCP servers are TypeScript, not Python — see the [design note]
13
13
  - `Event` — a Zod-validated discriminated union over the five event types (`tool_call_start`, `tool_call_end`, `tool_call_error`, `annotation`, `surface_snapshot`), field-for-field with the Python SDK's Pydantic models.
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
- - `withBaton(server, config)` — 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).
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
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.
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`.
@@ -38,37 +38,137 @@ npm install @goodtiming/baton-sdk
38
38
 
39
39
  ```typescript
40
40
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
41
- import { withBaton, StdoutSink } from "@goodtiming/baton-sdk";
41
+ import { withBaton } from "@goodtiming/baton-sdk";
42
42
 
43
43
  const server = new McpServer({ name: "your-vendor-mcp", version: "1.0.0" });
44
44
  const handle = withBaton(server, {
45
- vendorId: "your-vendor", // the SERVER
46
- tenantId: "ten_7cd4c8cf", // the ACCOUNT — not the same thing
47
- vendorDisplayName: "Your Vendor",
48
- consentToken: process.env.BATON_CONSENT_TOKEN!,
49
- sink: new StdoutSink(), // swap in HttpSink(...) to ship to a collector
45
+ dsn: "https://baton_pk_...@ingest.goodtiming.ai/ten_7cd4c8cf.../your-vendor",
50
46
  });
51
47
  // handle.annotationToolName === "your-vendor_annotate"
52
48
 
53
- // `tenantId` names the ACCOUNT the collector authenticates; `vendorId` names
54
- // the server whose surface is captured. One account wraps many servers, so
55
- // sending the account id in both slots collapses them into one. Resolved
56
- // explicit → `BATON_TENANT_ID` → `vendorId`; that last fallback is a
57
- // migration shim, not a supported configuration.
58
- //
59
- // Stated on its own line on purpose — it is the diff your reviewer reads. If
60
- // you would rather keep it out of source, leave `tenantId` off entirely and
61
- // set `BATON_TENANT_ID` in the environment; the SDK reads it. Do NOT write
62
- // `process.env.BATON_TENANT_ID!` — `tenantId` is optional, so the `!` buys no
63
- // check and an unset variable silently falls back to `vendorId`, which is the
64
- // collapse described above wearing the look of a configured account.
65
-
66
49
  // register tools before or after withBaton — both are captured
67
50
  server.registerTool("lookup", { inputSchema: { name: z.string() } }, async ({ name }) => {
68
51
  /* ... */
69
52
  });
70
53
  ```
71
54
 
55
+ That one string is the whole configuration. Copy it from **/account**, where
56
+ it is labelled DSN. It packs four values — the collector to send to, your
57
+ workspace, this server, and the key that binds them — and the SDK unpacks them
58
+ and builds the sink itself.
59
+
60
+ **If you distribute your server, put the DSN in your source.** A stdio server
61
+ runs on your user's machine, spawned by their MCP client — and the official
62
+ client SDKs pass it a fixed six-variable allowlist (`HOME`, `LOGNAME`, `PATH`,
63
+ `SHELL`, `TERM`, `USER`), plus whatever that user wrote in their own client
64
+ config. Nothing from your `.env` is in either list, so a server configured that
65
+ way captures nothing while appearing to work.
66
+
67
+ If you would rather not put it in source — a hosted server, where the process
68
+ starts from your own environment — set `BATON_DSN` instead and pass nothing:
69
+
70
+ ```typescript
71
+ const handle = withBaton(server); // reads BATON_DSN
72
+ ```
73
+
74
+ An explicit `dsn` wins over `BATON_DSN`, and both win over every other
75
+ `BATON_*` variable. That last rule matters when you **re-onboard** a server:
76
+ the new DSN beats the old install's leftover `.env`, rather than the stale file
77
+ quietly filing your events under the previous server's name.
78
+
79
+ ### Without a DSN
80
+
81
+ Two cases do not have one: you are sending to **your own collector** rather than
82
+ a hosted one, or you are trying the package out before you have a key. Name the
83
+ parts instead.
84
+
85
+ ```typescript
86
+ import { withBaton, HttpSink } from "@goodtiming/baton-sdk";
87
+
88
+ const handle = withBaton(server, {
89
+ vendorId: "your-vendor", // the SERVER whose surface is captured
90
+ tenantId: "ten_7cd4c8cf", // the ACCOUNT the collector authenticates
91
+ vendorDisplayName: "Your Vendor",
92
+ sink: new HttpSink("https://your-collector.example.com", { apiKey: "..." }),
93
+ });
94
+ ```
95
+
96
+ Leave `sink` off and events go to stderr as JSON Lines — the quickest way to
97
+ see that capture works at all, and the fastest way to see exactly what is
98
+ collected. One real line from that stream, pretty-printed:
99
+
100
+ ```json
101
+ {
102
+ "event_id": "01a091a9-24d0-732e-9e92-8165c2018a93",
103
+ "tenant_id": "ten_7cd4c8cf9a0e4b1d8f2a6c3e5b7d9f10",
104
+ "vendor_id": "your-vendor",
105
+ "session_id": "sdk-01a091a9-24c8-72c8-a64e-e49164e1f568",
106
+ "sequence_number": 3,
107
+ "captured_at": "2026-09-11T18:09:36.463Z",
108
+ "consent_token": "customer-consented",
109
+ "sdk_version": "ts-0.3.0",
110
+ "agent_runtime": "unknown",
111
+ "user_id": null,
112
+ "runtime_meta": null,
113
+ "event_type": "tool_call_start",
114
+ "payload": {
115
+ "tool_name": "lookup",
116
+ "params": { "name": "ACME Corp" },
117
+ "call_intent": "check the ACME account",
118
+ "call_expected": null,
119
+ "call_workflow": null,
120
+ "intent_source": "injected_param"
121
+ }
122
+ }
123
+ ```
124
+
125
+ `params` is the tool's own arguments, scrubbed. `call_intent` is what the
126
+ calling agent said it was trying to do, captured through the injected
127
+ `user_goal` param. The matching `tool_call_end` carries the result and a
128
+ duration; a `surface_snapshot` of your tool list goes out once per session.
129
+
130
+ `vendorId` and `tenantId` are different things, and one account wraps many
131
+ servers — put the account id in both and two servers render as one, whose label
132
+ flips to whichever deployed last. A DSN carries both, so this form is the only
133
+ one that can get it wrong. `tenantId` resolves explicit → `BATON_TENANT_ID` →
134
+ `vendorId`, and that last fallback is a migration shim: an unset
135
+ `BATON_TENANT_ID` produces the collapse rather than an error.
136
+
137
+ Every other option — the intent-param mode, a session-id resolver, your own
138
+ scrubber — is set the same way with or without a DSN. Passing a `dsn` **and** an
139
+ explicit `vendorId`, `tenantId` or `sink` throws: two sources for one value
140
+ cannot be reconciled without guessing, and a wrong guess routes a server's
141
+ traffic under someone else's identity. `consentToken` defaults to a value the
142
+ SDK supplies, so you never carry one.
143
+
144
+ ### Turning capture off
145
+
146
+ `BATON_DISABLED=1` in the environment of the process running the server, and
147
+ this package installs nothing at all: no tool wrapping, no annotation tool, no
148
+ instructions rewrite, no collector connection. (`handle.sink` is still an
149
+ object — a `DisabledSink` that holds no key, opens no socket and discards what
150
+ it is given — so finding one there is not a sign the switch failed.) Your server starts and behaves exactly as it
151
+ would with the `withBaton` line deleted. It is read once at startup, it never
152
+ writes to stdout (which is the JSON-RPC stream), and it cannot make your server
153
+ fail to boot — a config this package would otherwise refuse is accepted and
154
+ ignored while the switch is on.
155
+
156
+ The switch belongs to whoever RUNS the server. For a server you distribute,
157
+ that is your user. For one you host, it is you — your users cannot set an
158
+ environment variable on your machine, and there is no per-user opt-out today.
159
+
160
+ **If you pass this on to your users, say where the variable goes.** For a
161
+ server started by an MCP client, it belongs in the `env` block for your server
162
+ in that client's config file — **not in their shell**. MCP clients spawn
163
+ servers with a fixed, short allowlist of environment variables — six names on
164
+ macOS and Linux, a different dozen on Windows — and `BATON_DISABLED` is on
165
+ neither, so an exported one never reaches your process.
166
+ Someone who follows "set the environment variable" gets a server that is still
167
+ capturing and believes it is not.
168
+
169
+ What you tell your users about what is captured is yours to write, on your own
170
+ surface. This package does not put words in your README beyond the switch.
171
+
72
172
  Both `@modelcontextprotocol/sdk` (1.x) and `@modelcontextprotocol/server` (v2) are declared as **optional** peer dependencies: install whichever your server is built on, and nothing here resolves the other. This package imports neither — not at runtime, and not as a type, so a v2-only tree typechecks as well as it runs. `withBaton` takes the exported structural `SupportedMcpServer` type that both majors' `McpServer` classes satisfy, which is also how you annotate the parameter in your own code.
73
173
 
74
174
  Same shape as Python's `mcp`/`fastmcp` peer relationship: the vendor's own copy, already in their tree.