@theokit/sdk 4.37.0 → 4.37.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.
@@ -3294,7 +3294,7 @@ var HISTOGRAM_NAMES = {
3294
3294
  };
3295
3295
  function safeRequire(moduleName) {
3296
3296
  try {
3297
- const r = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('chunk-LMWXF4LL.cjs', document.baseURI).href)));
3297
+ const r = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('chunk-W52DKZBR.cjs', document.baseURI).href)));
3298
3298
  return r(moduleName);
3299
3299
  } catch {
3300
3300
  return void 0;
@@ -3519,7 +3519,7 @@ var cachedOtel;
3519
3519
  function loadOtel() {
3520
3520
  if (cachedOtel === void 0) {
3521
3521
  try {
3522
- const r = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('chunk-LMWXF4LL.cjs', document.baseURI).href)));
3522
+ const r = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('chunk-W52DKZBR.cjs', document.baseURI).href)));
3523
3523
  const mod = r("@opentelemetry/api");
3524
3524
  cachedOtel = mod;
3525
3525
  } catch {
@@ -6864,18 +6864,80 @@ var HttpMcpClient = class extends BaseMcpClient {
6864
6864
  config;
6865
6865
  name;
6866
6866
  nextId = 1;
6867
+ /**
6868
+ * The `mcp-session-id` a STATEFUL server issues on `initialize`.
6869
+ *
6870
+ * The comment on `request` used to say "the http transport is stateless". That is true of the
6871
+ * *connection* — each POST opens a fresh one — and false of the *session*: a server that follows
6872
+ * Streamable HTTP answers 400 to every call that omits this header, so a stateful server served
6873
+ * exactly zero tools to this client. Captured once, replayed after.
6874
+ */
6875
+ sessionId;
6867
6876
  fetchImpl;
6868
6877
  close() {
6869
6878
  return Promise.resolve();
6870
6879
  }
6871
- async request(method, params) {
6872
- const id = this.nextId++;
6873
- const payload = { jsonrpc: "2.0", id, method, params };
6874
- const headers = {
6880
+ /**
6881
+ * The wire headers for one request.
6882
+ *
6883
+ * Extracted from `request` rather than inlined: the session/accept handling pushed that method
6884
+ * past the cognitive-complexity ceiling, and a header policy is a different concern from the
6885
+ * fetch/timeout/error handling around it (SRP). The user-supplied spread stays LAST — that
6886
+ * override contract predates this change and is the only escape a user has while a server
6887
+ * misbehaves.
6888
+ */
6889
+ buildHeaders() {
6890
+ return {
6875
6891
  "content-type": "application/json",
6876
- accept: "application/json",
6892
+ // Streamable HTTP asks for BOTH media types. Declaring only `application/json` makes a
6893
+ // spec-enforcing server answer 406 before any RPC is even reachable.
6894
+ accept: "application/json, text/event-stream",
6895
+ // Present only for a STATEFUL server. For a stateless one nothing is sent — inventing a
6896
+ // header would trade one broken transport for another.
6897
+ ...this.sessionId !== void 0 ? { "mcp-session-id": this.sessionId } : {},
6877
6898
  ...this.config.headers ?? {}
6878
6899
  };
6900
+ }
6901
+ /**
6902
+ * Capture the session on the FIRST response that carries it (the `initialize`).
6903
+ *
6904
+ * Never overwritten by a later one: a server that re-issues mid-session would otherwise split
6905
+ * the session in two, and the second half would not see the first half's state.
6906
+ */
6907
+ /**
6908
+ * Read one JSON-RPC response, in either encoding the server may choose.
6909
+ *
6910
+ * Streamable HTTP lets the server answer a POST with `application/json` OR `text/event-stream`,
6911
+ * and the client advertises both. Reading the body with `response.json()` unconditionally is what
6912
+ * turned a working server into `Unexpected token 'e', "event: mes"... is not valid JSON` the
6913
+ * moment the Accept header started asking for SSE — measured against a real server, not imagined.
6914
+ *
6915
+ * Only the `data:` payload is JSON; `event:` / `id:` / `retry:` lines and comments are framing.
6916
+ * A single JSON-RPC reply arrives as one event, so the FIRST parseable `data:` is the answer.
6917
+ */
6918
+ async readBody(response) {
6919
+ const tipo = response.headers.get("content-type") ?? "";
6920
+ if (!tipo.includes("text/event-stream")) return await response.json();
6921
+ const texto = await response.text();
6922
+ for (const linha of texto.split(/\r?\n/)) {
6923
+ if (!linha.startsWith("data:")) continue;
6924
+ const carga = linha.slice(5).trim();
6925
+ if (carga === "") continue;
6926
+ return JSON.parse(carga);
6927
+ }
6928
+ throw new chunkMHCKWQR3_cjs.NetworkError(`MCP ${this.name} returned an event stream with no data payload`, {
6929
+ code: "mcp_http_error"
6930
+ });
6931
+ }
6932
+ captureSession(response) {
6933
+ if (this.sessionId !== void 0) return;
6934
+ const issued = response.headers.get("mcp-session-id");
6935
+ if (issued !== null && issued !== "") this.sessionId = issued;
6936
+ }
6937
+ async request(method, params) {
6938
+ const id = this.nextId++;
6939
+ const payload = { jsonrpc: "2.0", id, method, params };
6940
+ const headers = this.buildHeaders();
6879
6941
  const timeoutMs = this.config.requestTimeoutMs ?? DEFAULT_MCP_TIMEOUT_MS;
6880
6942
  let response;
6881
6943
  try {
@@ -6895,8 +6957,9 @@ var HttpMcpClient = class extends BaseMcpClient {
6895
6957
  code: "mcp_http_error"
6896
6958
  });
6897
6959
  }
6960
+ this.captureSession(response);
6898
6961
  try {
6899
- return await response.json();
6962
+ return await this.readBody(response);
6900
6963
  } catch (cause) {
6901
6964
  if (isAbortLike(cause)) throw mcpTimeoutError(this.name, timeoutMs);
6902
6965
  throw cause;
@@ -10442,5 +10505,5 @@ exports.generateCronId = generateCronId;
10442
10505
  exports.getPricingEntry = getPricingEntry;
10443
10506
  exports.openRouterMemoryEmbeddingProviderAdapter = openRouterMemoryEmbeddingProviderAdapter;
10444
10507
  exports.resolveApiKey = resolveApiKey;
10445
- //# sourceMappingURL=chunk-LMWXF4LL.cjs.map
10446
- //# sourceMappingURL=chunk-LMWXF4LL.cjs.map
10508
+ //# sourceMappingURL=chunk-W52DKZBR.cjs.map
10509
+ //# sourceMappingURL=chunk-W52DKZBR.cjs.map