@goodtiming/baton-sdk 0.5.6 → 0.5.8

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
@@ -25,7 +25,6 @@ const server = new McpServer({ name: "your-vendor-mcp", version: "1.0.0" });
25
25
  const handle = withBaton(server, {
26
26
  dsn: "https://baton_pk_...@baton.goodtiming.ai/ten_.../your-vendor",
27
27
  });
28
- // handle.annotationToolName === "your-vendor-mcp_annotate", from the server's own name
29
28
 
30
29
  // register tools before or after withBaton; both are captured
31
30
  server.registerTool("lookup", { inputSchema: { name: z.string() } }, async ({ name }) => {
@@ -35,6 +34,8 @@ server.registerTool("lookup", { inputSchema: { name: z.string() } }, async ({ na
35
34
 
36
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.
37
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"`, a `<vendor>_annotate` tool is also registered (named from your server's own name, 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
+
38
39
  **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.
39
40
 
40
41
  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.
@@ -85,6 +86,34 @@ What a stateless server costs, with either function:
85
86
  Signals that need several calls in one session, such as a retry loop, do not fire across requests.
86
87
  - **The client is named from its `User-Agent`.** A client states its name in the `initialize` handshake, and a per-request server never sees the handshake of the call it is serving. Each event still carries the request's `User-Agent` header in `client_observed`, and your dashboard names the client from that.
87
88
 
89
+ ## Without MCP: the library API
90
+
91
+ Use `Client` when agents reach you through something other than an MCP server: a REST API, a skill, or a tool loop in your own agent. It sends the same events `withBaton` sends.
92
+
93
+ Put it in the one place every tool call passes through, such as a middleware or a dispatcher. Then every tool is covered by one `trace()`:
94
+
95
+ ```ts
96
+ import { Client } from "@goodtiming/baton-sdk";
97
+
98
+ const baton = new Client({ dsn: process.env.BATON_DSN });
99
+
100
+ app.use((req, res, next) =>
101
+ baton.session(
102
+ { sessionId: req.get("x-conversation-id"), principal: { principalId: req.user.id }, headers: req.headers },
103
+ () => baton.trace({ toolName: req.path, params: req.body }, () => next()),
104
+ ),
105
+ );
106
+ ```
107
+
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 `turnId`, all optional. Without a `sessionId` the block makes one up. Pass the same `turnId` to every block that serves one user request.
109
+ - **`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
+ - **`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
+ - **`annotate(...)`** and `trace.annotate(...)` send a report your code relays from the agent.
112
+
113
+ `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
+
115
+ Capture never throws into your code. Only the constructor throws, on a bad config. Call `await baton.aclose()` at shutdown.
116
+
88
117
  ## PII scrubbing
89
118
 
90
119
  **On by default**, a rule-for-rule port of the Python SDK's ruleset: email, `Bearer` values, `sk-*` and `AKIA*` keys, JWTs, phone numbers, Luhn-checked card numbers, plus force-redaction on sensitive field names. Pass `identityScrub` to opt out, or supply your own `(value: unknown) => unknown`.
@@ -216,7 +245,7 @@ None of it blocks the quickstart above.
216
245
 
217
246
  - Only `StdoutSink` and `HttpSink`. `FileSink` and `MultiSink` are deferred; the Python package has all four.
218
247
  - Only the high-level `McpServer`. The low-level `Server` is not wrapped.
219
- - **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`, `expected_result` and `overall_task` 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`.
248
+ - **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`.
220
249
  - 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.
221
250
  - v2's `createMcpHandler` is untested. A server built per request by your own code is supported: see [A server per request](#a-server-per-request).
222
251