@orkestrel/mcp 0.0.35 → 0.0.37

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.
@@ -249,6 +249,8 @@ var JSONRPC_INTERNAL_ERROR = -32603;
249
249
  * Retained for the legacy branch alone. A modern fault answers
250
250
  * {@link JSONRPC_INTERNAL_ERROR}; this code survives only where an old-wire peer was
251
251
  * already characterized against it.
252
+ * A `handshake` hook that rejects with an `MCPError` answers the legacy `initialize`
253
+ * with that error's own code and data.
252
254
  */
253
255
  var JSONRPC_SERVER_ERROR = -32e3;
254
256
  /**
@@ -4113,6 +4115,26 @@ function isInitializeRequest(value) {
4113
4115
  return owned.success && isJSONRPCInvocation(owned.value) && owned.value["method"] === "initialize";
4114
4116
  }
4115
4117
  /**
4118
+ * Determines whether a parsed value is a legacy MCP `ping` request.
4119
+ *
4120
+ * @remarks
4121
+ * Requires a valid request id and excludes the modern per-request wire shape.
4122
+ * Notifications and hostile or malformed values return false.
4123
+ *
4124
+ * @param value - The already-parsed value to test
4125
+ * @returns True if `value` is a legacy `ping` request; false otherwise
4126
+ *
4127
+ * @example
4128
+ * ```ts
4129
+ * isPingRequest({ jsonrpc: '2.0', method: 'ping', id: 1 }) // true
4130
+ * isPingRequest({ jsonrpc: '2.0', method: 'ping' }) // false
4131
+ * ```
4132
+ */
4133
+ function isPingRequest(value) {
4134
+ const owned = (0, _orkestrel_contract.attempt)(() => (0, _orkestrel_contract.cloneJSONRecord)(value));
4135
+ return owned.success && isJSONRPCRequest(owned.value) && owned.value.method === "ping" && !isModernRequest(owned.value);
4136
+ }
4137
+ /**
4116
4138
  * Determines whether a JSON-RPC invocation uses the modern per-request MCP wire shape.
4117
4139
  *
4118
4140
  * @remarks
@@ -4752,6 +4774,23 @@ var MCPLegacy = class {
4752
4774
  }) === void 0) return buildJSONRPCError(void 0, JSONRPC_INVALID_REQUEST, "Invalid Request");
4753
4775
  switch (invocation.method) {
4754
4776
  case "initialize": {
4777
+ if (this.#options.handshake !== void 0) {
4778
+ const closure = new AbortController();
4779
+ const resolved = buildMethodOptions(options ?? {}, closure.signal);
4780
+ try {
4781
+ await this.#options.handshake(resolved);
4782
+ } catch (error) {
4783
+ if (resolved.signal.aborted) throw error;
4784
+ if (isMCPError(error)) {
4785
+ const context = (0, _orkestrel_contract.attempt)(() => (0, _orkestrel_contract.parseJSON)(JSON.stringify(error.context)));
4786
+ return buildJSONRPCError(id, error.code, error.message, context.success ? context.value : void 0);
4787
+ }
4788
+ this.emitter.emit("error", error);
4789
+ return buildJSONRPCError(id, JSONRPC_INTERNAL_ERROR, "Server error");
4790
+ } finally {
4791
+ closure.abort();
4792
+ }
4793
+ }
4755
4794
  const requested = invocation.params?.["protocolVersion"];
4756
4795
  return buildJSONRPCResult(id, buildInitializeResult(this.#options.identity.name, this.#options.identity.version, (0, _orkestrel_contract.isString)(requested) ? requested : void 0));
4757
4796
  }
@@ -5111,6 +5150,9 @@ var MCPServer = class {
5111
5150
  get identity() {
5112
5151
  return this.#options.identity;
5113
5152
  }
5153
+ get handshake() {
5154
+ return this.#options.handshake;
5155
+ }
5114
5156
  get methods() {
5115
5157
  return this.#methods;
5116
5158
  }
@@ -5527,7 +5569,7 @@ var MCPServer = class {
5527
5569
  if (!isBoundedString(verified, this.#limits.state) || verified.length === 0) return this.#contain(/* @__PURE__ */ new Error("Continuation port opened a value outside the configured state bound"), id);
5528
5570
  const state = parseMCPInputState(verified);
5529
5571
  if (state === void 0) return this.#contain(/* @__PURE__ */ new Error("Continuation port opened a malformed protected payload"), id);
5530
- if (state.expiry <= Date.now() || state.id === id || state.version !== context.version || state.method !== request.method || state.name !== name || state.digest !== digest) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: request state could not be verified for this retry");
5572
+ if (state.expiry <= (configured.clock ?? Date.now)() || state.id === id || state.version !== context.version || state.method !== request.method || state.name !== name || state.digest !== digest) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: request state could not be verified for this retry");
5531
5573
  const responses = this.#checkAnswers(state.requests, inputResponses);
5532
5574
  if (responses === void 0) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: an input response is missing or malformed");
5533
5575
  const principal = await configured.principal(request, options);
@@ -5539,7 +5581,7 @@ var MCPServer = class {
5539
5581
  responses,
5540
5582
  ...state.state !== void 0 ? { state: state.state } : {}
5541
5583
  }, options);
5542
- if (state.expiry <= Date.now()) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: request state could not be verified for this retry");
5584
+ if (state.expiry <= (configured.clock ?? Date.now)()) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: request state could not be verified for this retry");
5543
5585
  if (selected === void 0) return void 0;
5544
5586
  const round = this.#ownRound(selected);
5545
5587
  if (round === void 0) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: input policy returned an invalid round or continuation context");
@@ -5580,8 +5622,8 @@ var MCPServer = class {
5580
5622
  depth: this.#limits.depth
5581
5623
  });
5582
5624
  if (configured === void 0 || context === void 0 || !(0, _orkestrel_contract.isString)(principal) || principal.length === 0 || !(0, _orkestrel_contract.isFiniteNumber)(configured.ttl) || configured.ttl <= 0) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: input policy returned an invalid round or continuation context");
5583
- if (previous !== void 0 && previous <= Date.now()) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: request state could not be verified for this retry");
5584
- const expiry = Date.now() + configured.ttl;
5625
+ if (previous !== void 0 && previous <= (configured.clock ?? Date.now)()) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: request state could not be verified for this retry");
5626
+ const expiry = (configured.clock ?? Date.now)() + configured.ttl;
5585
5627
  const protectedState = {
5586
5628
  principal,
5587
5629
  expiry,
@@ -5604,7 +5646,7 @@ var MCPServer = class {
5604
5646
  if (serialized === void 0) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: request state exceeds the configured limit");
5605
5647
  const requestState = await configured.continuation.seal(serialized);
5606
5648
  if (!isBoundedString(requestState, this.#limits.state) || requestState.length === 0) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, "Invalid params: request state exceeds the configured limit");
5607
- if (expiry <= Date.now() || previous !== void 0 && previous <= Date.now()) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, previous === void 0 ? "Invalid params: request state expired before it could be issued" : "Invalid params: request state could not be verified for this retry");
5649
+ if (expiry <= (configured.clock ?? Date.now)() || previous !== void 0 && previous <= (configured.clock ?? Date.now)()) return buildJSONRPCError(id, JSONRPC_INVALID_PARAMS, previous === void 0 ? "Invalid params: request state expired before it could be issued" : "Invalid params: request state could not be verified for this retry");
5608
5650
  return buildJSONRPCResult(id, {
5609
5651
  resultType: "input_required",
5610
5652
  inputRequests: round.requests,
@@ -6753,7 +6795,8 @@ function createMCPServer(options) {
6753
6795
  function createMCPLegacy(server) {
6754
6796
  return new MCPLegacy({
6755
6797
  dispatcher: server,
6756
- identity: server.identity
6798
+ identity: server.identity,
6799
+ ...server.handshake === void 0 ? {} : { handshake: server.handshake }
6757
6800
  });
6758
6801
  }
6759
6802
  /**
@@ -7028,6 +7071,7 @@ exports.isMCPTextResource = isMCPTextResource;
7028
7071
  exports.isMCPToolAnnotations = isMCPToolAnnotations;
7029
7072
  exports.isMCPVersion = isMCPVersion;
7030
7073
  exports.isModernRequest = isModernRequest;
7074
+ exports.isPingRequest = isPingRequest;
7031
7075
  exports.isRFC3339Date = isRFC3339Date;
7032
7076
  exports.isRFC3339DateTime = isRFC3339DateTime;
7033
7077
  exports.isStandardBase64 = isStandardBase64;