@corbat-tech/coco 2.25.12 → 2.25.14

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/dist/index.js CHANGED
@@ -27532,6 +27532,8 @@ var HTTPTransport = class {
27532
27532
  pendingRequests = /* @__PURE__ */ new Map();
27533
27533
  oauthToken;
27534
27534
  oauthInFlight = null;
27535
+ sessionId;
27536
+ protocolVersion = "2024-11-05";
27535
27537
  /**
27536
27538
  * Get authentication token
27537
27539
  */
@@ -27551,12 +27553,20 @@ var HTTPTransport = class {
27551
27553
  /**
27552
27554
  * Build request headers
27553
27555
  */
27554
- buildHeaders() {
27556
+ buildHeaders(method) {
27555
27557
  const headers = {
27556
- "Content-Type": "application/json",
27557
- Accept: "application/json",
27558
+ ...method === "POST" ? {
27559
+ "Content-Type": "application/json",
27560
+ Accept: "application/json, text/event-stream"
27561
+ } : {
27562
+ Accept: "text/event-stream"
27563
+ },
27564
+ ...this.protocolVersion ? { "MCP-Protocol-Version": this.protocolVersion } : {},
27558
27565
  ...this.config.headers
27559
27566
  };
27567
+ if (this.sessionId) {
27568
+ headers["Mcp-Session-Id"] = this.sessionId;
27569
+ }
27560
27570
  if (this.oauthToken) {
27561
27571
  headers["Authorization"] = `Bearer ${this.oauthToken}`;
27562
27572
  return headers;
@@ -27607,7 +27617,7 @@ var HTTPTransport = class {
27607
27617
  async sendRequestWithOAuthRetry(method, body, signal) {
27608
27618
  const doFetch = async () => fetch(this.config.url, {
27609
27619
  method,
27610
- headers: this.buildHeaders(),
27620
+ headers: this.buildHeaders(method),
27611
27621
  ...body ? { body } : {},
27612
27622
  signal
27613
27623
  });
@@ -27636,6 +27646,53 @@ var HTTPTransport = class {
27636
27646
  if (!payload.error) return false;
27637
27647
  return this.looksLikeAuthErrorMessage(payload.error.message);
27638
27648
  }
27649
+ captureResponseSession(response) {
27650
+ const sessionId = response.headers.get("mcp-session-id");
27651
+ if (sessionId) {
27652
+ this.sessionId = sessionId;
27653
+ }
27654
+ }
27655
+ async parseSseResponse(response) {
27656
+ if (!response.body) {
27657
+ throw new MCPTransportError("SSE response has no body");
27658
+ }
27659
+ const reader = response.body.getReader();
27660
+ const decoder = new TextDecoder();
27661
+ let buffer = "";
27662
+ let eventData = "";
27663
+ const flushEvent = () => {
27664
+ if (!eventData) return;
27665
+ const payload = eventData.trim();
27666
+ eventData = "";
27667
+ if (!payload) return;
27668
+ const parsed = JSON.parse(payload);
27669
+ if (parsed.result && typeof parsed.result === "object" && parsed.result !== null && "protocolVersion" in parsed.result && typeof parsed.result.protocolVersion === "string") {
27670
+ this.protocolVersion = parsed.result.protocolVersion;
27671
+ }
27672
+ this.messageCallback?.(parsed);
27673
+ };
27674
+ while (true) {
27675
+ const { done, value } = await reader.read();
27676
+ if (done) break;
27677
+ buffer += decoder.decode(value, { stream: true });
27678
+ const lines = buffer.split(/\r?\n/);
27679
+ buffer = lines.pop() ?? "";
27680
+ for (const line of lines) {
27681
+ if (line === "") {
27682
+ flushEvent();
27683
+ continue;
27684
+ }
27685
+ if (line.startsWith(":")) continue;
27686
+ if (line.startsWith("data:")) {
27687
+ eventData += (eventData ? "\n" : "") + line.slice(5).trimStart();
27688
+ }
27689
+ }
27690
+ }
27691
+ if (buffer.length > 0 && buffer.startsWith("data:")) {
27692
+ eventData += (eventData ? "\n" : "") + buffer.slice(5).trimStart();
27693
+ }
27694
+ flushEvent();
27695
+ }
27639
27696
  /**
27640
27697
  * Connect to the HTTP transport
27641
27698
  */
@@ -27687,10 +27744,22 @@ var HTTPTransport = class {
27687
27744
  abortController.signal
27688
27745
  );
27689
27746
  clearTimeout(timeoutId);
27747
+ this.captureResponseSession(response);
27690
27748
  if (!response.ok) {
27691
27749
  throw new MCPTransportError(`HTTP error ${response.status}: ${response.statusText}`);
27692
27750
  }
27751
+ if (response.status === 202) {
27752
+ return;
27753
+ }
27754
+ const contentType = response.headers.get("content-type")?.toLowerCase() ?? "";
27755
+ if (contentType.includes("text/event-stream")) {
27756
+ await this.parseSseResponse(response);
27757
+ return;
27758
+ }
27693
27759
  const data = await response.json();
27760
+ if (data.result && typeof data.result === "object" && data.result !== null && "protocolVersion" in data.result && typeof data.result.protocolVersion === "string") {
27761
+ this.protocolVersion = data.result.protocolVersion;
27762
+ }
27694
27763
  if (this.shouldAttemptOAuth() && this.isJsonRpcAuthError(data)) {
27695
27764
  await this.ensureOAuthToken(response.headers.get("www-authenticate"), {
27696
27765
  forceRefresh: true
@@ -27700,12 +27769,24 @@ var HTTPTransport = class {
27700
27769
  JSON.stringify(message),
27701
27770
  abortController.signal
27702
27771
  );
27772
+ this.captureResponseSession(retryResponse);
27703
27773
  if (!retryResponse.ok) {
27704
27774
  throw new MCPTransportError(
27705
27775
  `HTTP error ${retryResponse.status}: ${retryResponse.statusText}`
27706
27776
  );
27707
27777
  }
27778
+ if (retryResponse.status === 202) {
27779
+ return;
27780
+ }
27781
+ const retryContentType = retryResponse.headers.get("content-type")?.toLowerCase() ?? "";
27782
+ if (retryContentType.includes("text/event-stream")) {
27783
+ await this.parseSseResponse(retryResponse);
27784
+ return;
27785
+ }
27708
27786
  const retryData = await retryResponse.json();
27787
+ if (retryData.result && typeof retryData.result === "object" && retryData.result !== null && "protocolVersion" in retryData.result && typeof retryData.result.protocolVersion === "string") {
27788
+ this.protocolVersion = retryData.result.protocolVersion;
27789
+ }
27709
27790
  this.messageCallback?.(retryData);
27710
27791
  return;
27711
27792
  }