@corbat-tech/coco 2.25.13 → 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/cli/index.js CHANGED
@@ -21541,6 +21541,8 @@ var init_http = __esm({
21541
21541
  pendingRequests = /* @__PURE__ */ new Map();
21542
21542
  oauthToken;
21543
21543
  oauthInFlight = null;
21544
+ sessionId;
21545
+ protocolVersion = "2024-11-05";
21544
21546
  /**
21545
21547
  * Get authentication token
21546
21548
  */
@@ -21560,12 +21562,20 @@ var init_http = __esm({
21560
21562
  /**
21561
21563
  * Build request headers
21562
21564
  */
21563
- buildHeaders() {
21565
+ buildHeaders(method) {
21564
21566
  const headers = {
21565
- "Content-Type": "application/json",
21566
- Accept: "application/json",
21567
+ ...method === "POST" ? {
21568
+ "Content-Type": "application/json",
21569
+ Accept: "application/json, text/event-stream"
21570
+ } : {
21571
+ Accept: "text/event-stream"
21572
+ },
21573
+ ...this.protocolVersion ? { "MCP-Protocol-Version": this.protocolVersion } : {},
21567
21574
  ...this.config.headers
21568
21575
  };
21576
+ if (this.sessionId) {
21577
+ headers["Mcp-Session-Id"] = this.sessionId;
21578
+ }
21569
21579
  if (this.oauthToken) {
21570
21580
  headers["Authorization"] = `Bearer ${this.oauthToken}`;
21571
21581
  return headers;
@@ -21616,7 +21626,7 @@ var init_http = __esm({
21616
21626
  async sendRequestWithOAuthRetry(method, body, signal) {
21617
21627
  const doFetch = async () => fetch(this.config.url, {
21618
21628
  method,
21619
- headers: this.buildHeaders(),
21629
+ headers: this.buildHeaders(method),
21620
21630
  ...body ? { body } : {},
21621
21631
  signal
21622
21632
  });
@@ -21645,6 +21655,53 @@ var init_http = __esm({
21645
21655
  if (!payload.error) return false;
21646
21656
  return this.looksLikeAuthErrorMessage(payload.error.message);
21647
21657
  }
21658
+ captureResponseSession(response) {
21659
+ const sessionId = response.headers.get("mcp-session-id");
21660
+ if (sessionId) {
21661
+ this.sessionId = sessionId;
21662
+ }
21663
+ }
21664
+ async parseSseResponse(response) {
21665
+ if (!response.body) {
21666
+ throw new MCPTransportError("SSE response has no body");
21667
+ }
21668
+ const reader = response.body.getReader();
21669
+ const decoder = new TextDecoder();
21670
+ let buffer = "";
21671
+ let eventData = "";
21672
+ const flushEvent = () => {
21673
+ if (!eventData) return;
21674
+ const payload = eventData.trim();
21675
+ eventData = "";
21676
+ if (!payload) return;
21677
+ const parsed = JSON.parse(payload);
21678
+ if (parsed.result && typeof parsed.result === "object" && parsed.result !== null && "protocolVersion" in parsed.result && typeof parsed.result.protocolVersion === "string") {
21679
+ this.protocolVersion = parsed.result.protocolVersion;
21680
+ }
21681
+ this.messageCallback?.(parsed);
21682
+ };
21683
+ while (true) {
21684
+ const { done, value } = await reader.read();
21685
+ if (done) break;
21686
+ buffer += decoder.decode(value, { stream: true });
21687
+ const lines = buffer.split(/\r?\n/);
21688
+ buffer = lines.pop() ?? "";
21689
+ for (const line of lines) {
21690
+ if (line === "") {
21691
+ flushEvent();
21692
+ continue;
21693
+ }
21694
+ if (line.startsWith(":")) continue;
21695
+ if (line.startsWith("data:")) {
21696
+ eventData += (eventData ? "\n" : "") + line.slice(5).trimStart();
21697
+ }
21698
+ }
21699
+ }
21700
+ if (buffer.length > 0 && buffer.startsWith("data:")) {
21701
+ eventData += (eventData ? "\n" : "") + buffer.slice(5).trimStart();
21702
+ }
21703
+ flushEvent();
21704
+ }
21648
21705
  /**
21649
21706
  * Connect to the HTTP transport
21650
21707
  */
@@ -21696,10 +21753,22 @@ var init_http = __esm({
21696
21753
  abortController.signal
21697
21754
  );
21698
21755
  clearTimeout(timeoutId);
21756
+ this.captureResponseSession(response);
21699
21757
  if (!response.ok) {
21700
21758
  throw new MCPTransportError(`HTTP error ${response.status}: ${response.statusText}`);
21701
21759
  }
21760
+ if (response.status === 202) {
21761
+ return;
21762
+ }
21763
+ const contentType = response.headers.get("content-type")?.toLowerCase() ?? "";
21764
+ if (contentType.includes("text/event-stream")) {
21765
+ await this.parseSseResponse(response);
21766
+ return;
21767
+ }
21702
21768
  const data = await response.json();
21769
+ if (data.result && typeof data.result === "object" && data.result !== null && "protocolVersion" in data.result && typeof data.result.protocolVersion === "string") {
21770
+ this.protocolVersion = data.result.protocolVersion;
21771
+ }
21703
21772
  if (this.shouldAttemptOAuth() && this.isJsonRpcAuthError(data)) {
21704
21773
  await this.ensureOAuthToken(response.headers.get("www-authenticate"), {
21705
21774
  forceRefresh: true
@@ -21709,12 +21778,24 @@ var init_http = __esm({
21709
21778
  JSON.stringify(message),
21710
21779
  abortController.signal
21711
21780
  );
21781
+ this.captureResponseSession(retryResponse);
21712
21782
  if (!retryResponse.ok) {
21713
21783
  throw new MCPTransportError(
21714
21784
  `HTTP error ${retryResponse.status}: ${retryResponse.statusText}`
21715
21785
  );
21716
21786
  }
21787
+ if (retryResponse.status === 202) {
21788
+ return;
21789
+ }
21790
+ const retryContentType = retryResponse.headers.get("content-type")?.toLowerCase() ?? "";
21791
+ if (retryContentType.includes("text/event-stream")) {
21792
+ await this.parseSseResponse(retryResponse);
21793
+ return;
21794
+ }
21717
21795
  const retryData = await retryResponse.json();
21796
+ if (retryData.result && typeof retryData.result === "object" && retryData.result !== null && "protocolVersion" in retryData.result && typeof retryData.result.protocolVersion === "string") {
21797
+ this.protocolVersion = retryData.result.protocolVersion;
21798
+ }
21718
21799
  this.messageCallback?.(retryData);
21719
21800
  return;
21720
21801
  }