completable.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.McpZodTypeKind = exports.COMPLETABLE_SYMBOL = void 0;
  4. exports.completable = completable;
  5. exports.isCompletable = isCompletable;
  6. exports.getCompleter = getCompleter;
  7. exports.unwrapCompletable = unwrapCompletable;
  8. exports.COMPLETABLE_SYMBOL = Symbol.for('mcp.completable');
  9. /**
  10. * Wraps a Zod type to provide autocompletion capabilities. Useful for, e.g., prompt arguments in MCP.
  11. * Works with both Zod v3 and v4 schemas.
  12. */
  13. function completable(schema, complete) {
  14. Object.defineProperty(schema, exports.COMPLETABLE_SYMBOL, {
  15. value: { complete },
  16. enumerable: false,
  17. writable: false,
  18. configurable: false
  19. });
  20. return schema;
  21. }
  22. /**
  23. * Checks if a schema is completable (has completion metadata).
  24. */
  25. function isCompletable(schema) {
  26. return !!schema && typeof schema === 'object' && exports.COMPLETABLE_SYMBOL in schema;
  27. }
  28. /**
  29. * Gets the completer callback from a completable schema, if it exists.
  30. */
  31. function getCompleter(schema) {
  32. const meta = schema[exports.COMPLETABLE_SYMBOL];
  33. return meta?.complete;
  34. }
  35. /**
  36. * Unwraps a completable schema to get the underlying schema.
  37. * For backward compatibility with code that called `.unwrap()`.
  38. */
  39. function unwrapCompletable(schema) {
  40. return schema;
  41. }
  42. // Legacy exports for backward compatibility
  43. // These types are deprecated but kept for existing code
  44. var McpZodTypeKind;
  45. (function (McpZodTypeKind) {
  46. McpZodTypeKind["Completable"] = "McpCompletable";
  47. })(McpZodTypeKind || (exports.McpZodTypeKind = McpZodTypeKind = {}));
  48. //# sourceMappingURL=completable.js.map