@goodtiming/baton-sdk 0.5.1 → 0.5.3

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
@@ -41,6 +41,50 @@ For a hosted server, where the process starts from your own environment, set `BA
41
41
 
42
42
  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, and there is no equivalent of `shutdown_flush_timeout_seconds`.
43
43
 
44
+ ## A server per request
45
+
46
+ A stateless HTTP server builds a new `McpServer` for every request. Create one Baton at startup and wrap each server with it:
47
+
48
+ ```ts
49
+ import { createBaton } from "@goodtiming/baton-sdk";
50
+
51
+ const baton = createBaton(); // reads BATON_DSN, like withBaton
52
+
53
+ app.post("/mcp", async (req, res) => {
54
+ const server = buildServer();
55
+ baton.wrap(server);
56
+ // connect a transport and handle the request as before
57
+ });
58
+ ```
59
+
60
+ Call `await baton.aclose()` in your own shutdown path, after the HTTP server stops taking requests. It sends what is still buffered.
61
+
62
+ `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.
63
+
64
+ What a stateless server costs, with either function:
65
+
66
+ - **Every request is its own session.** The protocol gives a stateless server no session id, so the SDK does not invent a join between two requests. To see one person's requests together, return a principal from `resolvePrincipal` ([Who is calling](#who-is-calling)): the Console lists sessions by person. If your HTTP handler has already authenticated the request, hand the user to the hook as `authInfo`. On `@modelcontextprotocol/sdk` 1.x that is `req.auth`, shown here; on `@modelcontextprotocol/server` 2.x pass `{ authInfo }` when you hand the request to the transport:
67
+
68
+ ```js
69
+ const baton = createBaton({
70
+ resolvePrincipal: (ctx) => {
71
+ const user = ctx.authInfo?.extra;
72
+ return typeof user?.userId === "string" ? { principalId: user.userId } : null;
73
+ },
74
+ });
75
+
76
+ app.post("/mcp", async (req, res) => {
77
+ const user = await authenticate(req); // your own check
78
+ req.auth = { token: "", clientId: "my-server", scopes: [], extra: { userId: user.id } };
79
+ const server = buildServer();
80
+ baton.wrap(server);
81
+ // connect a transport and handle the request as before
82
+ });
83
+ ```
84
+
85
+ Signals that need several calls in one session, such as a retry loop, do not fire across requests.
86
+ - **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
+
44
88
  ## PII scrubbing
45
89
 
46
90
  **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`.
@@ -51,7 +95,7 @@ Sending to your own collector instead, or trying the package before you have a k
51
95
 
52
96
  ## Who is calling
53
97
 
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:
98
+ **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:
55
99
 
56
100
  ```typescript
57
101
  import { withBaton, principalFromOAuthEmail } from "@goodtiming/baton-sdk";
@@ -62,11 +106,13 @@ withBaton(server, {
62
106
  });
63
107
  ```
64
108
 
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 `@`.
109
+ `principalFromOAuthEmail` keys on the token's `email` claim (the whole address); `principalFromOAuthSub` on its subject. 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. The email hook sets it to the part before `@`.
66
110
 
67
111
  ⚠ **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
112
 
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.
113
+ The hook runs on every tool call and every `tools/list` request, before your handler. On a `tools/list` request `toolName` is `null` and `arguments` is empty, so a hook that names the person from the headers or `authInfo` needs no change. If it has not answered after 5 seconds, the SDK stops waiting and that request is sent without a principal. That covers an async hook waiting on a slow lookup. A hook that blocks synchronously cannot be interrupted, so keep the work in it async.
114
+
115
+ 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.
70
116
 
71
117
  ## Not capturing responses at all
72
118
 
@@ -144,6 +190,15 @@ with `could not parse ${body.slice(0, 200)}`, that content leaves your process
144
190
  under a mode you set to stop exactly that. Throw a message that names the
145
191
  resource, not its contents.
146
192
 
193
+ ## Tool listings
194
+
195
+ Every `tools/list` request emits a `tool_list_start` and then a
196
+ `tool_list_end` or a `tool_list_error`. A client that connects, lists your
197
+ tools and calls none is still recorded. The end event carries how many tools
198
+ that response held and how long it took. No tool name, description or schema
199
+ is on these events. They carry the principal your `resolvePrincipal` hook
200
+ returns ([Who is calling](#who-is-calling)).
201
+
147
202
  ## Turning capture off entirely
148
203
 
149
204
  Not the same thing as **Not capturing responses at all**: that one keeps the signal and drops the response bodies, this one emits nothing at all. It also belongs to a different person — `resultCaptureMode` is set by whoever WRAPS the server, in code; this is set by whoever RUNS it, in the environment.
@@ -160,7 +215,7 @@ None of it blocks the quickstart above.
160
215
  - Only the high-level `McpServer`. The low-level `Server` is not wrapped.
161
216
  - **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`.
162
217
  - 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.
163
- - The per-request `createMcpHandler` deployment shape is unscoped.
218
+ - 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).
164
219
 
165
220
  ## Wire compatibility
166
221