@goodtiming/baton-sdk 0.5.8 → 0.6.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 +25 -11
- package/dist/index.cjs +1071 -342
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +490 -34
- package/dist/index.d.ts +490 -34
- package/dist/index.js +1066 -342
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,13 +34,15 @@ server.registerTool("lookup", { inputSchema: { name: z.string() } }, async ({ na
|
|
|
34
34
|
|
|
35
35
|
Copy the DSN from **/account**. It packs four values: the collector to send to, your workspace, this server, and the key that binds them. The SDK unpacks them and builds the sink itself.
|
|
36
36
|
|
|
37
|
-
**Installing changes what your server advertises.** Each tool handler is wrapped, one sentence is added below your server's own `instructions`, and a `user_goal` parameter is added to every tool's schema and stripped again before your handler sees it. With `reportMode: "on"`,
|
|
37
|
+
**Installing changes what your server advertises.** Each tool handler is wrapped, one sentence is added below your server's own `instructions`, and a `user_goal` parameter is added to every tool's schema and stripped again before your handler sees it. With `reportMode: "on"`, an annotation tool is also registered, named from your server's own name (slugged, `<server-name>_annotate`, so `your-vendor-mcp_annotate` here), and the added text tells the agent when to report a problem with it. [What gets injected](https://goodtiming.ai/docs.html#injected-tools).
|
|
38
|
+
|
|
39
|
+
With no DSN and no sink, events go to `StdoutSink`, which writes to stderr, so a stdio server's stdout stays clean and nothing goes over the network.
|
|
38
40
|
|
|
39
41
|
**If you distribute your server, put the DSN in your source.** A stdio server runs on your user's machine, spawned by their MCP client. The official client SDKs pass it a fixed six-variable allowlist (`HOME`, `LOGNAME`, `PATH`, `SHELL`, `TERM`, `USER`), plus whatever that user wrote in their own client config. Nothing from your `.env` is in either list, so a server configured that way captures nothing while appearing to work.
|
|
40
42
|
|
|
41
43
|
For a hosted server, where the process starts from your own environment, set `BATON_DSN` and call `withBaton(server)` with no config. An explicit `dsn` wins over `BATON_DSN`, and both win over every other `BATON_*` variable. That ordering matters when you **re-onboard** a server: the new DSN beats the old install's leftover `.env`, rather than filing your events under the previous server's name.
|
|
42
44
|
|
|
43
|
-
Sending to your own collector instead, or trying the package before you have a key, means naming the parts rather than passing a DSN: [Without a DSN](https://goodtiming.ai/docs.html#without-dsn). That section's examples are Python. The config field names carry over as camelCase, and `HttpSink` takes its options as an object: `new HttpSink(url, { apiKey })`. Its timeouts do **not** carry over. Python's `request_timeout_seconds` / `backoff_base_seconds` / `backoff_max_seconds` / `circuit_breaker_reset_seconds` are `requestTimeoutMs` / `backoffBaseMs` / `backoffMaxMs` / `circuitBreakerResetMs` here, in milliseconds rather than seconds
|
|
45
|
+
Sending to your own collector instead, or trying the package before you have a key, means naming the parts rather than passing a DSN: [Without a DSN](https://goodtiming.ai/docs.html#without-dsn). That section's examples are Python. The config field names carry over as camelCase, and `HttpSink` takes its options as an object: `new HttpSink(url, { apiKey })`. Its timeouts do **not** carry over. Python's `request_timeout_seconds` / `backoff_base_seconds` / `backoff_max_seconds` / `circuit_breaker_reset_seconds` / `shutdown_flush_timeout_seconds` are `requestTimeoutMs` / `backoffBaseMs` / `backoffMaxMs` / `circuitBreakerResetMs` / `shutdownFlushTimeoutMs` here, in milliseconds rather than seconds.
|
|
44
46
|
|
|
45
47
|
## A server per request
|
|
46
48
|
|
|
@@ -58,7 +60,7 @@ app.post("/mcp", async (req, res) => {
|
|
|
58
60
|
});
|
|
59
61
|
```
|
|
60
62
|
|
|
61
|
-
Call `await baton.aclose()` in your own shutdown path, after the HTTP server stops taking requests. It sends what is still buffered.
|
|
63
|
+
Call `await baton.aclose()` in your own shutdown path, after the HTTP server stops taking requests. It sends what is still buffered, and returns soon after `shutdownFlushTimeoutMs` (3 seconds by default) even if the collector does not answer.
|
|
62
64
|
|
|
63
65
|
`createBaton` takes the same config as `withBaton`. The Baton owns the sink, so there is nothing to close per request. Calling `withBaton` per request instead builds a new sink each time and re-sends the tool surface on every request.
|
|
64
66
|
|
|
@@ -105,13 +107,19 @@ app.use((req, res, next) =>
|
|
|
105
107
|
);
|
|
106
108
|
```
|
|
107
109
|
|
|
108
|
-
- **`session(options, fn)`** gives every `trace()` and `annotate()` inside `fn` the same session, principal, client and turn, including after `await`. Its options are `sessionId`, `principal`, `clientInfo`, `headers` and `
|
|
110
|
+
- **`session(options, fn)`** gives every `trace()` and `annotate()` inside `fn` the same session, principal, client and turn, including after `await`. Its options are `sessionId`, `principal`, `clientInfo`, `headers`, `turnId` and `meta`, all optional. Without a `sessionId` the block makes one up. Pass the same `turnId` to every block that serves one user request.
|
|
109
111
|
- **`trace(options, fn)`** runs `fn` as one tool call and returns what it returns. If `fn` returns a promise, the call ends when the promise settles, and `trace()` returns a new native Promise of the same value. Its `then` is called once, so a lazy query builder is not run twice, but its own methods are not on what comes back. A throw or rejection is recorded and passed on unchanged. The options are `toolName`, `intent`, `expectedOutcome`, `workflow`, `params` and `consentToken`. The callback's return value is the recorded result unless `fn` calls `trace.observed(...)`.
|
|
110
112
|
- **`wrap(toolName, fn)`** returns `fn` traced on every call, for code with no single place to put `trace()`. A single plain-object argument is recorded as `params`.
|
|
111
113
|
- **`annotate(...)`** and `trace.annotate(...)` send a report your code relays from the agent.
|
|
112
114
|
|
|
113
115
|
`INTENT_PARAMS` holds the name and wording of the one param `withBaton` adds to MCP tools. Use them to ask for the same values in your own API, then pass the answers to `trace()`.
|
|
114
116
|
|
|
117
|
+
**Serving MCP yourself**, with a server `withBaton` does not wrap:
|
|
118
|
+
|
|
119
|
+
- **`await listTools(tools, { serverInfo, capabilities, instructions })`** returns your `tools/list` entries with `user_goal` added to each `inputSchema`, exactly as `withBaton` adds it. It also sends a `surface_snapshot` of the tools you passed, once for each distinct surface. Pass your tools before anything else is added to them, and your own `initialize` values. Your list is not changed.
|
|
120
|
+
- **`session({ meta })`** takes the request's `_meta`. It is sent as `runtime_meta`, and a client declaration in it wins over `clientInfo`.
|
|
121
|
+
- **`INTENT_INSTRUCTIONS`** is the sentence to add to your server's instructions.
|
|
122
|
+
|
|
115
123
|
Capture never throws into your code. Only the constructor throws, on a bad config. Call `await baton.aclose()` at shutdown.
|
|
116
124
|
|
|
117
125
|
## PII scrubbing
|
|
@@ -124,7 +132,7 @@ Capture never throws into your code. Only the constructor throws, on a bad confi
|
|
|
124
132
|
|
|
125
133
|
## Who is calling
|
|
126
134
|
|
|
127
|
-
**Nothing is captured about the person behind a call unless you say how to find them.** Pass `resolvePrincipal`: it receives the request's headers, `_meta`, tool name, arguments and validated `authInfo`, and returns a `Principal` or `null`. Two ready-made hooks cover OAuth:
|
|
135
|
+
**Nothing is captured about the person behind a call unless you say how to find them.** Pass `resolvePrincipal`: it receives the request's headers, `_meta`, tool name, arguments and validated `authInfo`, and returns a `Principal`, an `Organization` or `null`. Two ready-made hooks cover OAuth:
|
|
128
136
|
|
|
129
137
|
```typescript
|
|
130
138
|
import { withBaton, principalFromOAuthEmail } from "@goodtiming/baton-sdk";
|
|
@@ -143,9 +151,17 @@ The hook runs on every tool call, every `tools/list` request and every resource
|
|
|
143
151
|
|
|
144
152
|
The id is sent exactly as your hook returns it, so the hook decides what is safe to send. To send a pseudonym instead, hash the id inside your hook and return `form: "hashed"` with it.
|
|
145
153
|
|
|
154
|
+
If your users belong to your customers' companies, return the company too, so you can filter by it:
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
{ principalId: "u_123", organization: { organizationId: "org_acme", displayName: "Acme" } }
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
If you know only the company, for example from an org-wide API key, return `{ organizationId: ... }` alone. A company is never counted as a person. It has its own `form`, so you can hash one id and not the other. The library API takes the same values in `client.session({ principal })`. If you pass only a company there, also pass your own `sessionId`: without a person, Baton cannot join that block's calls to the next one.
|
|
161
|
+
|
|
146
162
|
## Not capturing responses at all
|
|
147
163
|
|
|
148
|
-
`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
|
|
164
|
+
`resultCaptureMode: "off"` and nothing **derived from what your tool returns** leaves your process — not the result, and not the reason text on a failure, whether your handler returns it or throws it. Requests are unaffected: `params` are captured either way.
|
|
149
165
|
|
|
150
166
|
```ts
|
|
151
167
|
withBaton(server, { dsn, resultCaptureMode: "off" });
|
|
@@ -153,7 +169,7 @@ withBaton(server, { dsn, resultCaptureMode: "off" });
|
|
|
153
169
|
|
|
154
170
|
Each affected event says so, in its own field (`result_capture: "off"`), rather than leaving a reader to guess from what is missing. That is the difference between the two levers: a scrubber TRANSFORMS a value that still crosses the network, this DECLARES that nothing crosses.
|
|
155
171
|
|
|
156
|
-
**What is still captured, and it is deliberate:** the tool name, the duration, and whether the call failed — so failure detection, pairing and timing all keep working.
|
|
172
|
+
**What is still captured, and it is deliberate:** the tool name, the duration, and whether the call failed — so failure detection, pairing and timing all keep working. The message of an error your handler **throws** is withheld too: thrown messages often echo the data a call was handling.
|
|
157
173
|
|
|
158
174
|
**What it costs you:** anything that needs to read a response body. A call that returns `200` with a useless body can no longer be detected, and those calls sit outside the denominator of body-level analysis rather than counting as passes or failures.
|
|
159
175
|
|
|
@@ -212,9 +228,8 @@ can carry paths, query strings and account identifiers.
|
|
|
212
228
|
`resultCaptureMode: "off"` does not withhold it.** These twelve events never
|
|
213
229
|
carry the `result_capture` marker — the rule is that nothing on them is derived
|
|
214
230
|
from a result — so a failing read's `error_body` is kept in both modes and the
|
|
215
|
-
wire says nothing either way.
|
|
216
|
-
|
|
217
|
-
tells a consumer what happened, and here there is no marker. If your read fails
|
|
231
|
+
wire says nothing either way. A tool's thrown message is withheld under
|
|
232
|
+
`"off"`; a resource or prompt read's is not, and no marker says so. If your read fails
|
|
218
233
|
with `could not parse ${body.slice(0, 200)}`, that content leaves your process
|
|
219
234
|
under a mode you set to stop exactly that. Throw a message that names the
|
|
220
235
|
resource, not its contents.
|
|
@@ -243,7 +258,6 @@ Checking it worked: `handle.sink` is still an object, so finding one there is no
|
|
|
243
258
|
|
|
244
259
|
None of it blocks the quickstart above.
|
|
245
260
|
|
|
246
|
-
- Only `StdoutSink` and `HttpSink`. `FileSink` and `MultiSink` are deferred; the Python package has all four.
|
|
247
261
|
- Only the high-level `McpServer`. The low-level `Server` is not wrapped.
|
|
248
262
|
- **Task-based tools are not wrapped, and are not left alone either.** A 1.x tool registered with an object at `.handler` rather than a function produces no `tool_call_*` events at all, and nothing reports that. It still gets the intent parameters added to its advertised schema, because injection runs before the wrap is skipped and the strip only happens inside the wrapper. So `user_goal` can arrive in your own handler's arguments. Tools on the same server registered the ordinary way are unaffected. ⚠ **The request-level seam above does not rescue this one**: it reports only on tools this package wrapped, so a task-based tool emits nothing even for a rejected argument, where an unknown tool now emits a `tool_call_error`.
|
|
249
263
|
- Intent parameters need a Zod schema. On the 2.x SDK a tool registered with a non-Zod standard schema is still wrapped and still emits `tool_call_*`; it just advertises no intent parameters.
|