completable.js 1.3 KB

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