@lushly-dev/afd-server 0.6.0 → 2.0.0

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.
Files changed (112) hide show
  1. package/README.md +320 -14
  2. package/dist/bootstrap/afd-context.d.ts +11 -2
  3. package/dist/bootstrap/afd-context.d.ts.map +1 -1
  4. package/dist/bootstrap/afd-context.js +56 -11
  5. package/dist/bootstrap/afd-context.js.map +1 -1
  6. package/dist/bootstrap/afd-docs.d.ts +6 -3
  7. package/dist/bootstrap/afd-docs.d.ts.map +1 -1
  8. package/dist/bootstrap/afd-docs.js +14 -9
  9. package/dist/bootstrap/afd-docs.js.map +1 -1
  10. package/dist/bootstrap/afd-help.d.ts +6 -3
  11. package/dist/bootstrap/afd-help.d.ts.map +1 -1
  12. package/dist/bootstrap/afd-help.js +10 -8
  13. package/dist/bootstrap/afd-help.js.map +1 -1
  14. package/dist/bootstrap/afd-schema.d.ts +8 -4
  15. package/dist/bootstrap/afd-schema.d.ts.map +1 -1
  16. package/dist/bootstrap/afd-schema.js +55 -42
  17. package/dist/bootstrap/afd-schema.js.map +1 -1
  18. package/dist/bootstrap/described-command.d.ts +32 -0
  19. package/dist/bootstrap/described-command.d.ts.map +1 -0
  20. package/dist/bootstrap/described-command.js +12 -0
  21. package/dist/bootstrap/described-command.js.map +1 -0
  22. package/dist/bootstrap/index.d.ts +1 -0
  23. package/dist/bootstrap/index.d.ts.map +1 -1
  24. package/dist/bootstrap/index.js.map +1 -1
  25. package/dist/bootstrap/json-schema-to-ts.d.ts +27 -0
  26. package/dist/bootstrap/json-schema-to-ts.d.ts.map +1 -0
  27. package/dist/bootstrap/json-schema-to-ts.js +154 -0
  28. package/dist/bootstrap/json-schema-to-ts.js.map +1 -0
  29. package/dist/bootstrap/registry.d.ts +17 -6
  30. package/dist/bootstrap/registry.d.ts.map +1 -1
  31. package/dist/bootstrap/registry.js +32 -5
  32. package/dist/bootstrap/registry.js.map +1 -1
  33. package/dist/command-names.d.ts +19 -0
  34. package/dist/command-names.d.ts.map +1 -0
  35. package/dist/command-names.js +33 -0
  36. package/dist/command-names.js.map +1 -0
  37. package/dist/command-routing.d.ts +47 -0
  38. package/dist/command-routing.d.ts.map +1 -0
  39. package/dist/command-routing.js +75 -0
  40. package/dist/command-routing.js.map +1 -0
  41. package/dist/context-scope.d.ts +26 -0
  42. package/dist/context-scope.d.ts.map +1 -0
  43. package/dist/context-scope.js +31 -0
  44. package/dist/context-scope.js.map +1 -0
  45. package/dist/define.d.ts +42 -0
  46. package/dist/define.d.ts.map +1 -0
  47. package/dist/define.js +41 -0
  48. package/dist/define.js.map +1 -0
  49. package/dist/direct-registry.d.ts +103 -0
  50. package/dist/direct-registry.d.ts.map +1 -0
  51. package/dist/direct-registry.js +102 -0
  52. package/dist/direct-registry.js.map +1 -0
  53. package/dist/execution.d.ts +9 -0
  54. package/dist/execution.d.ts.map +1 -1
  55. package/dist/execution.js +105 -321
  56. package/dist/execution.js.map +1 -1
  57. package/dist/http-handler.d.ts +24 -17
  58. package/dist/http-handler.d.ts.map +1 -1
  59. package/dist/http-handler.js +282 -253
  60. package/dist/http-handler.js.map +1 -1
  61. package/dist/http-security.d.ts +23 -0
  62. package/dist/http-security.d.ts.map +1 -0
  63. package/dist/http-security.js +101 -0
  64. package/dist/http-security.js.map +1 -0
  65. package/dist/http-sessions.d.ts +33 -0
  66. package/dist/http-sessions.d.ts.map +1 -0
  67. package/dist/http-sessions.js +65 -0
  68. package/dist/http-sessions.js.map +1 -0
  69. package/dist/http-sse.d.ts +30 -0
  70. package/dist/http-sse.d.ts.map +1 -0
  71. package/dist/http-sse.js +70 -0
  72. package/dist/http-sse.js.map +1 -0
  73. package/dist/index.d.ts +5 -4
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +4 -2
  76. package/dist/index.js.map +1 -1
  77. package/dist/json-rpc.d.ts +56 -0
  78. package/dist/json-rpc.d.ts.map +1 -0
  79. package/dist/json-rpc.js +73 -0
  80. package/dist/json-rpc.js.map +1 -0
  81. package/dist/lazy-tools.d.ts +2 -12
  82. package/dist/lazy-tools.d.ts.map +1 -1
  83. package/dist/lazy-tools.js +9 -14
  84. package/dist/lazy-tools.js.map +1 -1
  85. package/dist/meta-tools.d.ts +122 -0
  86. package/dist/meta-tools.d.ts.map +1 -0
  87. package/dist/meta-tools.js +156 -0
  88. package/dist/meta-tools.js.map +1 -0
  89. package/dist/middleware.d.ts +34 -7
  90. package/dist/middleware.d.ts.map +1 -1
  91. package/dist/middleware.js +82 -40
  92. package/dist/middleware.js.map +1 -1
  93. package/dist/schema.d.ts +27 -6
  94. package/dist/schema.d.ts.map +1 -1
  95. package/dist/schema.js +17 -12
  96. package/dist/schema.js.map +1 -1
  97. package/dist/server-types.d.ts +100 -13
  98. package/dist/server-types.d.ts.map +1 -1
  99. package/dist/server-types.js.map +1 -1
  100. package/dist/server.d.ts +10 -2
  101. package/dist/server.d.ts.map +1 -1
  102. package/dist/server.js +165 -48
  103. package/dist/server.js.map +1 -1
  104. package/dist/tool-router.d.ts +6 -5
  105. package/dist/tool-router.d.ts.map +1 -1
  106. package/dist/tool-router.js +104 -120
  107. package/dist/tool-router.js.map +1 -1
  108. package/dist/tools.d.ts +10 -420
  109. package/dist/tools.d.ts.map +1 -1
  110. package/dist/tools.js +99 -208
  111. package/dist/tools.js.map +1 -1
  112. package/package.json +13 -8
@@ -0,0 +1,154 @@
1
+ /**
2
+ * @fileoverview A small JSON Schema → TypeScript type generator for afd-schema.
3
+ *
4
+ * Covers what `z.toJSONSchema()` emits for command inputs: primitive types,
5
+ * `integer`, `null`, arrays, objects (with `required` and `additionalProperties`),
6
+ * `enum`, `const`, `anyOf`/`oneOf` (unions) and `allOf` (intersections).
7
+ * Anything else, including `$ref`, becomes `unknown`.
8
+ */
9
+ const MAX_DEPTH = 32;
10
+ const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
11
+ function isSchema(value) {
12
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
13
+ }
14
+ function schemaList(value) {
15
+ return Array.isArray(value) && value.length > 0 ? value.filter(isSchema) : undefined;
16
+ }
17
+ function literal(value) {
18
+ return value === undefined ? 'undefined' : JSON.stringify(value);
19
+ }
20
+ function comment(description, indent) {
21
+ if (typeof description !== 'string' || description.length === 0)
22
+ return '';
23
+ return `${indent}/** ${description.replace(/\*\//g, '*\\/').replace(/\s*\n\s*/g, ' ')} */\n`;
24
+ }
25
+ /** Whether a type expression has a `|` or `&` outside brackets and string literals. */
26
+ function hasTopLevelOperator(type) {
27
+ let depth = 0;
28
+ let inString = false;
29
+ for (let i = 0; i < type.length; i++) {
30
+ const char = type[i];
31
+ if (inString) {
32
+ if (char === '\\')
33
+ i++;
34
+ else if (char === '"')
35
+ inString = false;
36
+ }
37
+ else if (char === '"')
38
+ inString = true;
39
+ else if (char === '{' || char === '(' || char === '<' || char === '[')
40
+ depth++;
41
+ else if (char === '}' || char === ')' || char === '>' || char === ']')
42
+ depth--;
43
+ else if (depth === 0 && (char === '|' || char === '&'))
44
+ return true;
45
+ }
46
+ return false;
47
+ }
48
+ function union(members, separator) {
49
+ const unique = [...new Set(members)];
50
+ if (unique.length === 1)
51
+ return unique[0] ?? 'unknown';
52
+ return unique
53
+ .map((member) => (hasTopLevelOperator(member) ? `(${member})` : member))
54
+ .join(separator);
55
+ }
56
+ function objectType(schema, indent, depth) {
57
+ const properties = isSchema(schema.properties) ? schema.properties : {};
58
+ const required = new Set(Array.isArray(schema.required) ? schema.required : []);
59
+ const inner = `${indent}\t`;
60
+ const lines = [];
61
+ for (const [key, value] of Object.entries(properties)) {
62
+ const name = IDENTIFIER.test(key) ? key : JSON.stringify(key);
63
+ const optional = required.has(key) ? '' : '?';
64
+ const description = isSchema(value) ? value.description : undefined;
65
+ lines.push(`${comment(description, inner)}${inner}${name}${optional}: ${toType(value, inner, depth + 1)};`);
66
+ }
67
+ const extra = schema.additionalProperties;
68
+ if (isSchema(extra)) {
69
+ lines.push(`${inner}[key: string]: ${toType(extra, inner, depth + 1)};`);
70
+ }
71
+ else if (extra === true) {
72
+ lines.push(`${inner}[key: string]: unknown;`);
73
+ }
74
+ return lines.length === 0 ? 'Record<string, never>' : `{\n${lines.join('\n')}\n${indent}}`;
75
+ }
76
+ function typeKeyword(type, schema, indent, depth) {
77
+ switch (type) {
78
+ case 'string':
79
+ return 'string';
80
+ case 'number':
81
+ case 'integer':
82
+ return 'number';
83
+ case 'boolean':
84
+ return 'boolean';
85
+ case 'null':
86
+ return 'null';
87
+ case 'array':
88
+ return isSchema(schema.items)
89
+ ? `Array<${toType(schema.items, indent, depth + 1)}>`
90
+ : 'unknown[]';
91
+ case 'object':
92
+ return objectType(schema, indent, depth);
93
+ default:
94
+ return 'unknown';
95
+ }
96
+ }
97
+ /**
98
+ * Convert a JSON Schema to a TypeScript type expression.
99
+ *
100
+ * @param schema - JSON Schema (draft-7)
101
+ * @param indent - Indentation of the line the type starts on
102
+ */
103
+ export function toType(schema, indent = '', depth = 0) {
104
+ if (!isSchema(schema) || depth > MAX_DEPTH)
105
+ return 'unknown';
106
+ if ('const' in schema)
107
+ return literal(schema.const);
108
+ if (Array.isArray(schema.enum) && schema.enum.length > 0) {
109
+ return union(schema.enum.map(literal), ' | ');
110
+ }
111
+ const alternatives = schemaList(schema.anyOf) ?? schemaList(schema.oneOf);
112
+ if (alternatives) {
113
+ return union(alternatives.map((member) => toType(member, indent, depth + 1)), ' | ');
114
+ }
115
+ const parts = schemaList(schema.allOf);
116
+ if (parts) {
117
+ return union(parts.map((member) => toType(member, indent, depth + 1)), ' & ');
118
+ }
119
+ if (Array.isArray(schema.type)) {
120
+ return union(schema.type.map((type) => typeKeyword(type, schema, indent, depth)), ' | ');
121
+ }
122
+ if (schema.type === undefined && isSchema(schema.properties)) {
123
+ return objectType(schema, indent, depth);
124
+ }
125
+ return typeKeyword(schema.type, schema, indent, depth);
126
+ }
127
+ /** `todo-create` → `TodoCreateInput`. */
128
+ export function inputTypeName(commandName) {
129
+ const pascal = commandName
130
+ .split(/[^A-Za-z0-9]+/)
131
+ .filter(Boolean)
132
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
133
+ .join('');
134
+ const base = pascal.length > 0 ? pascal : 'Command';
135
+ return `${/^[0-9]/.test(base) ? '_' : ''}${base}Input`;
136
+ }
137
+ /**
138
+ * Generate one TypeScript module declaring an input type per command
139
+ * (`todo-create` → `export type TodoCreateInput = …`).
140
+ */
141
+ export function generateInputTypes(commands) {
142
+ const used = new Set();
143
+ const declarations = commands.map((command) => {
144
+ const base = inputTypeName(command.name);
145
+ let name = base;
146
+ for (let suffix = 2; used.has(name); suffix++)
147
+ name = `${base}${suffix}`;
148
+ used.add(name);
149
+ const doc = comment(`Input of \`${command.name}\`: ${command.description}`, '');
150
+ return `${doc}export type ${name} = ${toType(command.inputSchema)};`;
151
+ });
152
+ return `${declarations.join('\n\n')}\n`;
153
+ }
154
+ //# sourceMappingURL=json-schema-to-ts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-schema-to-ts.js","sourceRoot":"","sources":["../../src/bootstrap/json-schema-to-ts.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,UAAU,GAAG,4BAA4B,CAAC;AAIhD,SAAS,QAAQ,CAAC,KAAc;IAC/B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC7E,CAAC;AAED,SAAS,UAAU,CAAC,KAAc;IACjC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACtF,CAAC;AAED,SAAS,OAAO,CAAC,KAAc;IAC9B,OAAO,KAAK,KAAK,SAAS,CAAC,CA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AE,GAAG,IAAI,OAAO,CAAC;AACxD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CACjC,QAAoF;IAEpF,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,MAAM,YAAY,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7C,MAAM,IAAI,GAAG,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACzC,IAAI,IAAI,GAAG,IAAI,CAAC;QAChB,KAAK,IAAI,MAAM,GAAG,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE;YAAE,IAAI,GAAG,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;QACzE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACf,MAAM,GAAG,GAAG,OAAO,CAAC,cAAc,OAAO,CAAC,IAAI,OAAO,OAAO,CAAC,WAAW,EAAE,EAAE,EAAE,CAAC,CAAC;QAChF,OAAO,GAAG,GAAG,eAAe,IAAI,MAAM,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC;IACtE,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;AACzC,CAAC"}
@@ -3,19 +3,30 @@
3
3
  *
4
4
  * Provides a function to get all bootstrap commands for an AFD server.
5
5
  */
6
- import type { CommandDefinition } from '@lushly-dev/afd-core';
6
+ import { type ZodCommandDefinition } from '../schema.js';
7
7
  import type { ContextConfig } from '../server-types.js';
8
8
  import { type ContextState } from './afd-context.js';
9
+ import type { DescribedCommand, GetDescribedCommands } from './described-command.js';
10
+ /** Names of the discovery bootstrap tools registered by `createMcpServer({ bootstrap: true })`. */
11
+ export declare const BOOTSTRAP_COMMAND_NAMES: readonly string[];
12
+ /** Names of the context tools registered when `createMcpServer({ contexts })` is set. */
13
+ export declare const CONTEXT_COMMAND_NAMES: readonly string[];
9
14
  /**
10
- * Get all bootstrap commands for an AFD server.
15
+ * Get all bootstrap commands for an AFD server: afd-help, afd-docs and afd-schema,
16
+ * plus the afd-context-* commands when `contexts` and `contextState` are given.
11
17
  *
12
- * @param getCommands - Function to get all user-defined commands
18
+ * Every returned command is a `ZodCommandDefinition` with `expose: { mcp: true }`.
19
+ * The discovery tools describe only the MCP-exposed commands that `getCommands`
20
+ * returns. With `createMcpServer`, prefer the `bootstrap: true` option, which also
21
+ * lists the built-in and context commands.
22
+ *
23
+ * @param getCommands - Function to get the commands to describe
13
24
  * @param options - Optional configuration
14
25
  * @returns Array of bootstrap command definitions
15
26
  */
16
- export declare function getBootstrapCommands(getCommands: () => CommandDefinition[], options?: {
17
- getJsonSchema?: (cmd: CommandDefinition) => Record<string, unknown>;
27
+ export declare function getBootstrapCommands(getCommands: GetDescribedCommands, options?: {
28
+ getJsonSchema?: (cmd: DescribedCommand) => Record<string, unknown>;
18
29
  contexts?: ContextConfig[];
19
30
  contextState?: ContextState;
20
- }): CommandDefinition[];
31
+ }): ZodCommandDefinition[];
21
32
  //# sourceMappingURL=registry.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/bootstrap/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EACN,KAAK,YAAY,EAIjB,MAAM,kBAAkB,CAAC;AAK1B;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CACnC,WAAW,EAAE,MAAM,iBAAiB,EAAE,EACtC,OAAO,CAAC,EAAE;IACT,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACpE,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC;IAC3B,YAAY,CAAC,EAAE,YAAY,CAAC;CAC5B,GACC,iBAAiB,EAAE,CA0BrB"}
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/bootstrap/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACxE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EACN,KAAK,YAAY,EAIjB,MAAM,kBAAkB,CAAC;AAI1B,OAAO,KAAK,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAErF,mGAAmG;AACnG,eAAO,MAAM,uBAAuB,EAAE,SAAS,MAAM,EAA2C,CAAC;AAEjG,yFAAyF;AACzF,eAAO,MAAM,qBAAqB,EAAE,SAAS,MAAM,EAIlD,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CACnC,WAAW,EAAE,oBAAoB,EACjC,OAAO,CAAC,EAAE;IACT,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnE,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC;IAC3B,YAAY,CAAC,EAAE,YAAY,CAAC;CAC5B,GACC,oBAAoB,EAAE,CA+BxB"}
@@ -3,20 +3,34 @@
3
3
  *
4
4
  * Provides a function to get all bootstrap commands for an AFD server.
5
5
  */
6
+ import { z } from 'zod';
7
+ import { defineCommand } from '../schema.js';
6
8
  import { createAfdContextEnterCommand, createAfdContextExitCommand, createAfdContextListCommand, } from './afd-context.js';
7
9
  import { createAfdDocsCommand } from './afd-docs.js';
8
10
  import { createAfdHelpCommand } from './afd-help.js';
9
11
  import { createAfdSchemaCommand } from './afd-schema.js';
12
+ /** Names of the discovery bootstrap tools registered by `createMcpServer({ bootstrap: true })`. */
13
+ export const BOOTSTRAP_COMMAND_NAMES = ['afd-help', 'afd-docs', 'afd-schema'];
14
+ /** Names of the context tools registered when `createMcpServer({ contexts })` is set. */
15
+ export const CONTEXT_COMMAND_NAMES = [
16
+ 'afd-context-list',
17
+ 'afd-context-enter',
18
+ 'afd-context-exit',
19
+ ];
10
20
  /**
11
- * Get all bootstrap commands for an AFD server.
21
+ * Get all bootstrap commands for an AFD server: afd-help, afd-docs and afd-schema,
22
+ * plus the afd-context-* commands when `contexts` and `contextState` are given.
12
23
  *
13
- * @param getCommands - Function to get all user-defined commands
24
+ * Every returned command is a `ZodCommandDefinition` with `expose: { mcp: true }`.
25
+ * The discovery tools describe only the MCP-exposed commands that `getCommands`
26
+ * returns. With `createMcpServer`, prefer the `bootstrap: true` option, which also
27
+ * lists the built-in and context commands.
28
+ *
29
+ * @param getCommands - Function to get the commands to describe
14
30
  * @param options - Optional configuration
15
31
  * @returns Array of bootstrap command definitions
16
32
  */
17
33
  export function getBootstrapCommands(getCommands, options) {
18
- // SAFETY: Each creator returns CommandDefinition<SpecificInput, SpecificOutput>, which is structurally
19
- // compatible with CommandDefinition but TypeScript's invariant generics require the double cast.
20
34
  const cmds = [
21
35
  createAfdHelpCommand(getCommands),
22
36
  createAfdDocsCommand(getCommands),
@@ -25,7 +39,20 @@ export function getBootstrapCommands(getCommands, options) {
25
39
  // Add context commands when contexts are configured
26
40
  if (options?.contexts?.length && options.contextState) {
27
41
  const getContexts = () => options.contexts ?? [];
28
- cmds.push(createAfdContextListCommand(getContexts, options.contextState), createAfdContextEnterCommand(getContexts, options.contextState), createAfdContextExitCommand(options.contextState));
42
+ const contextState = options.contextState;
43
+ cmds.push(defineCommand({
44
+ ...createAfdContextListCommand(getContexts, contextState),
45
+ input: z.object({}),
46
+ expose: { mcp: true },
47
+ }), defineCommand({
48
+ ...createAfdContextEnterCommand(getContexts, contextState),
49
+ input: z.object({ context: z.string().min(1).describe('Context name to enter') }),
50
+ expose: { mcp: true },
51
+ }), defineCommand({
52
+ ...createAfdContextExitCommand(contextState),
53
+ input: z.object({}),
54
+ expose: { mcp: true },
55
+ }));
29
56
  }
30
57
  return cmds;
31
58
  }
@@ -1 +1 @@
1
- {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/bootstrap/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAEN,4BAA4B,EAC5B,2BAA2B,EAC3B,2BAA2B,GAC3B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAEzD;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CACnC,WAAsC,EACtC,OAIC;IAED,uGAAuG;IACvG,iGAAiG;IACjG,MAAM,IAAI,GAAwB;QACjC,oBAAoB,CAAC,WAAW,CAAiC;QACjE,oBAAoB,CAAC,WAAW,CAAiC;QACjE,sBAAsB,CAAC,WAAW,EAAE,OAAO,EAAE,aAAa,CAAiC;KAC3F,CAAC;IAEF,oDAAoD;IACpD,IAAI,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;QACvD,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC;QACjD,IAAI,CAAC,IAAI,CACR,2BAA2B,CAC1B,WAAW,EACX,OAAO,CAAC,YAAY,CACY,EACjC,4BAA4B,CAC3B,WAAW,EACX,OAAO,CAAC,YAAY,CACY,EACjC,2BAA2B,CAAC,OAAO,CAAC,YAAY,CAAiC,CACjF,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC"}
1
+ {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/bootstrap/registry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAA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AK,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,uBAAuB,CAAC,EAAE,CAAC;YACjF,MAAM,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE;SACrB,CAAC,EACF,aAAa,CAAC;YACb,GAAG,2BAA2B,CAAC,YAAY,CAAC;YAC5C,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;YACnB,MAAM,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE;SACrB,CAAC,CACF,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @fileoverview Server-creation checks for command names: duplicates and names
3
+ * that the tool router or a built-in command would shadow.
4
+ */
5
+ /**
6
+ * Throw if two commands share a name, or if a command uses a name the server
7
+ * handles itself:
8
+ * - the router's meta-tools (`afd-call`, `afd-batch`, `afd-pipe`, `afd-discover`,
9
+ * `afd-detail`), which are always intercepted before command lookup;
10
+ * - `afd-help`, `afd-docs`, `afd-schema` when `bootstrap` is enabled;
11
+ * - `afd-context-list`, `afd-context-enter`, `afd-context-exit` when contexts are configured.
12
+ */
13
+ export declare function assertValidCommandNames(commands: ReadonlyArray<{
14
+ name: string;
15
+ }>, builtins: {
16
+ bootstrap: boolean;
17
+ contexts: boolean;
18
+ }): void;
19
+ //# sourceMappingURL=command-names.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-names.d.ts","sourceRoot":"","sources":["../src/command-names.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACtC,QAAQ,EAAE,aAAa,CAAC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,EACzC,QAAQ,EAAE;IAAE,SAAS,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,GACjD,IAAI,CAyBN"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @fileoverview Server-creation checks for command names: duplicates and names
3
+ * that the tool router or a built-in command would shadow.
4
+ */
5
+ import { BOOTSTRAP_COMMAND_NAMES, CONTEXT_COMMAND_NAMES } from './bootstrap/registry.js';
6
+ import { META_TOOL_NAMES } from './meta-tools.js';
7
+ /**
8
+ * Throw if two commands share a name, or if a command uses a name the server
9
+ * handles itself:
10
+ * - the router's meta-tools (`afd-call`, `afd-batch`, `afd-pipe`, `afd-discover`,
11
+ * `afd-detail`), which are always intercepted before command lookup;
12
+ * - `afd-help`, `afd-docs`, `afd-schema` when `bootstrap` is enabled;
13
+ * - `afd-context-list`, `afd-context-enter`, `afd-context-exit` when contexts are configured.
14
+ */
15
+ export function assertValidCommandNames(commands, builtins) {
16
+ const seen = new Set();
17
+ for (const { name } of commands) {
18
+ if (META_TOOL_NAMES.includes(name)) {
19
+ throw new Error(`[AFD] Command name '${name}' is reserved: the tool router handles '${name}' itself, so the command would be unreachable. Rename the command.`);
20
+ }
21
+ if (builtins.bootstrap && BOOTSTRAP_COMMAND_NAMES.includes(name)) {
22
+ throw new Error(`[AFD] Command name '${name}' is reserved: bootstrap: true registers a built-in '${name}'. Rename the command, or drop it (for example getBootstrapCommands() output) from commands.`);
23
+ }
24
+ if (builtins.contexts && CONTEXT_COMMAND_NAMES.includes(name)) {
25
+ throw new Error(`[AFD] Command name '${name}' is reserved: contexts registers a built-in '${name}'. Rename the command.`);
26
+ }
27
+ if (seen.has(name)) {
28
+ throw new Error(`[AFD] Duplicate command name '${name}': command names must be unique, otherwise one command shadows the other. Rename or remove one of them.`);
29
+ }
30
+ seen.add(name);
31
+ }
32
+ }
33
+ //# sourceMappingURL=command-names.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-names.js","sourceRoot":"","sources":["../src/command-names.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AACzF,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CACtC,QAAyC,EACzC,QAAmD;IAEnD,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,KAAK,MAAM,EAAE,IAAI,EAAE,IAAI,QAAQ,EAAE,CAAC;QACjC,IAAI,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YACpC,MAAM,IAAI,KAAK,CACd,uBAAuB,IAAI,2CAA2C,IAAI,oEAAoE,CAC9I,CAAC;QACH,CAAC;QACD,IAAI,QAAQ,CAAC,SAAS,IAAI,uBAAuB,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAClE,MAAM,IAAI,KAAK,CACd,uBAAuB,IAAI,wDAAwD,IAAI,8FAA8F,CACrL,CAAC;QACH,CAAC;QACD,IAAI,QAAQ,CAAC,QAAQ,IAAI,qBAAqB,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC/D,MAAM,IAAI,KAAK,CACd,uBAAuB,IAAI,iDAAiD,IAAI,wBAAwB,CACxG,CAAC;QACH,CAAC;QACD,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CACd,iCAAiC,IAAI,yGAAyG,CAC9I,CAAC;QACH,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;AACF,CAAC"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @fileoverview Shared helpers for tool listing, tool routing, discovery and
3
+ * execution: group/action derivation for the grouped strategy and context scoping.
4
+ */
5
+ import type { CommandError } from '@lushly-dev/afd-core';
6
+ import type { ZodCommandDefinition } from './schema.js';
7
+ /** Derives a grouped-strategy tool name from a command (`McpServerOptions.groupByFn`). */
8
+ export type GroupByFn = (command: ZodCommandDefinition) => string | undefined;
9
+ /** Minimal command shape needed for context checks. */
10
+ type Contextual = Pick<ZodCommandDefinition, 'contexts'>;
11
+ /**
12
+ * Default group (and discovery category) of a command: its `category`, else the
13
+ * first kebab-case segment of its name, else `'general'`.
14
+ */
15
+ export declare function defaultCommandGroup(command: Pick<ZodCommandDefinition, 'name' | 'category'>): string;
16
+ /**
17
+ * Grouped-strategy tool name of a command. Uses `groupByFn` when given (falling
18
+ * back to `'general'` when it returns nothing), otherwise {@link defaultCommandGroup}.
19
+ */
20
+ export declare function commandGroup(command: ZodCommandDefinition, groupByFn?: GroupByFn): string;
21
+ /**
22
+ * Grouped-strategy action of a command: the name without its first segment
23
+ * (`todo-create-batch` → `create-batch`), or the whole name if it has one segment.
24
+ */
25
+ export declare function commandAction(command: Pick<ZodCommandDefinition, 'name'>): string;
26
+ /**
27
+ * Whether a command is visible and callable in the active context. Everything is
28
+ * accessible when no context is active; commands without `contexts` are universal.
29
+ */
30
+ export declare function isAccessibleInContext(command: Contextual, activeContext: string | null | undefined): boolean;
31
+ /** Commands accessible in the active context (all of them when none is active). */
32
+ export declare function filterByContext<T extends Contextual>(commands: T[], activeContext: string | null | undefined): T[];
33
+ /**
34
+ * Recovery guidance for an unknown command name: at most
35
+ * {@link MAX_NOT_FOUND_MATCHES} close matches (bounded fuzzy matching; none for
36
+ * very long names) and a pointer to `afd-discover`. It never lists every
37
+ * command, which in lazy mode would load the whole surface into the agent's
38
+ * context on a single typo.
39
+ *
40
+ * @param commandName - The unknown name (untrusted; never echoed)
41
+ * @param commandNames - Names the caller may call
42
+ */
43
+ export declare function notFoundSuggestion(commandName: string, commandNames: string[]): string;
44
+ /** The `COMMAND_NOT_IN_CONTEXT` error for a command outside the active context. */
45
+ export declare function notInContextError(commandName: string, activeContext: string): CommandError;
46
+ export {};
47
+ //# sourceMappingURL=command-routing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-routing.d.ts","sourceRoot":"","sources":["../src/command-routing.ts"],"names":[],"mappings":"A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* execution: group/action derivation for the grouped strategy and context scoping.
4
+ */
5
+ import { findSimilarTools } from '@lushly-dev/afd-core';
6
+ /**
7
+ * Default group (and discovery category) of a command: its `category`, else the
8
+ * first kebab-case segment of its name, else `'general'`.
9
+ */
10
+ export function defaultCommandGroup(command) {
11
+ return command.category || command.name.split('-')[0] || 'general';
12
+ }
13
+ /**
14
+ * Grouped-strategy tool name of a command. Uses `groupByFn` when given (falling
15
+ * back to `'general'` when it returns nothing), otherwise {@link defaultCommandGroup}.
16
+ */
17
+ export function commandGroup(command, groupByFn) {
18
+ return groupByFn ? groupByFn(command) || 'general' : defaultCommandGroup(command);
19
+ }
20
+ /**
21
+ * Grouped-strategy action of a command: the name without its first segment
22
+ * (`todo-create-batch` → `create-batch`), or the whole name if it has one segment.
23
+ */
24
+ export function commandAction(command) {
25
+ const parts = command.name.split('-');
26
+ return parts.length > 1 ? parts.slice(1).join('-') : command.name;
27
+ }
28
+ /**
29
+ * Whether a command is visible and callable in the active context. Everything is
30
+ * accessible when no context is active; commands without `contexts` are universal.
31
+ */
32
+ export function isAccessibleInContext(command, activeContext) {
33
+ if (!activeContext)
34
+ return true;
35
+ if (!command.contexts?.length)
36
+ return true;
37
+ return command.contexts.includes(activeContext);
38
+ }
39
+ /** Commands accessible in the active context (all of them when none is active). */
40
+ export function filterByContext(commands, activeContext) {
41
+ if (!activeContext)
42
+ return commands;
43
+ return commands.filter((command) => isAccessibleInContext(command, activeContext));
44
+ }
45
+ /** How many close matches a `COMMAND_NOT_FOUND` suggestion names at most. */
46
+ const MAX_NOT_FOUND_MATCHES = 3;
47
+ /**
48
+ * Recovery guidance for an unknown command name: at most
49
+ * {@link MAX_NOT_FOUND_MATCHES} close matches (bounded fuzzy matching; none for
50
+ * very long names) and a pointer to `afd-discover`. It never lists every
51
+ * command, which in lazy mode would load the whole surface into the agent's
52
+ * context on a single typo.
53
+ *
54
+ * @param commandName - The unknown name (untrusted; never echoed)
55
+ * @param commandNames - Names the caller may call
56
+ */
57
+ export function notFoundSuggestion(commandName, commandNames) {
58
+ const discover = 'Use afd-discover to list all commands.';
59
+ const [best, ...others] = findSimilarTools(commandName, commandNames, MAX_NOT_FOUND_MATCHES);
60
+ if (best === undefined)
61
+ return discover;
62
+ const alsoClose = others.length
63
+ ? ` Other close matches: ${others.map((name) => `'${name}'`).join(', ')}.`
64
+ : '';
65
+ return `Did you mean '${best}'?${alsoClose} ${discover}`;
66
+ }
67
+ /** The `COMMAND_NOT_IN_CONTEXT` error for a command outside the active context. */
68
+ export function notInContextError(commandName, activeContext) {
69
+ return {
70
+ code: 'COMMAND_NOT_IN_CONTEXT',
71
+ message: `Command '${commandName}' is not available in context '${activeContext}'`,
72
+ suggestion: 'Use afd-context-list to see available contexts, or afd-context-enter to switch.',
73
+ };
74
+ }
75
+ //# sourceMappingURL=command-routing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-routing.js","sourceRoot":"","sources":["../src/command-routing.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AASxD;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAClC,OAAwD;IAExD,OAAO,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC;AACpE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,OAA6B,EAAE,SAAqB;IAChF,OAAO,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC;AACnF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,OAA2C;IACxE,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACtC,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAA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AI,QAAQ,EAAE,CAAC;AAC1D,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,iBAAiB,CAAC,WAAmB,EAAE,aAAqB;IAC3E,OAAO;QACN,IAAI,EAAE,wBAAwB;QAC9B,OAAO,EAAE,YAAY,WAAW,kCAAkC,aAAa,GAAG;QAClF,UAAU,EAAE,iFAAiF;KAC7F,CAAC;AACH,CAAC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @fileoverview Binds a caller's context state to the `CommandContext` of a remote call.
3
+ *
4
+ * HTTP clients each get their own context stack (keyed by MCP session), so the state cannot
5
+ * live in a closure shared by the whole server. The HTTP handler binds the caller's state to
6
+ * the command context under a private symbol; the tool router, execution engine and
7
+ * `afd-context-*` commands read it back. Symbol keys survive the `{ ...context }` copies made
8
+ * for batch items and pipeline steps, never serialize, and cannot be set by `createContext`.
9
+ */
10
+ import type { CommandContext } from '@lushly-dev/afd-core';
11
+ import type { ContextState } from './bootstrap/afd-context.js';
12
+ /**
13
+ * Return a copy of `context` bound to `state`.
14
+ *
15
+ * `null` marks a caller without a session (a stateless HTTP request): it sees no active
16
+ * context and cannot enter one.
17
+ */
18
+ export declare function bindContextState(context: CommandContext, state: ContextState | null): CommandContext;
19
+ /**
20
+ * The context state bound to `context`, or `fallback` when none is bound (stdio and
21
+ * in-process calls use the server's single state).
22
+ *
23
+ * @returns the bound state, `null` for a caller without a session, or `fallback`
24
+ */
25
+ export declare function resolveContextState(context: CommandContext | undefined, fallback?: ContextState): ContextState | null | undefined;
26
+ //# sourceMappingURL=context-scope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-scope.d.ts","sourceRoot":"","sources":["../src/context-scope.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAM/D;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC/B,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,YAAY,GAAG,IAAI,GACxB,cAAc,CAGhB;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAClC,OAAO,EAAE,cAAc,GAAG,SAAS,EACnC,QAAQ,CAAC,EAAE,YAAY,GACrB,YAAY,GAAG,IAAI,GAAG,SAAS,CAGjC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @fileoverview Binds a caller's context state to the `CommandContext` of a remote call.
3
+ *
4
+ * HTTP clients each get their own context stack (keyed by MCP session), so the state cannot
5
+ * live in a closure shared by the whole server. The HTTP handler binds the caller's state to
6
+ * the command context under a private symbol; the tool router, execution engine and
7
+ * `afd-context-*` commands read it back. Symbol keys survive the `{ ...context }` copies made
8
+ * for batch items and pipeline steps, never serialize, and cannot be set by `createContext`.
9
+ */
10
+ const CONTEXT_STATE = Symbol('afd.contextState');
11
+ /**
12
+ * Return a copy of `context` bound to `state`.
13
+ *
14
+ * `null` marks a caller without a session (a stateless HTTP request): it sees no active
15
+ * context and cannot enter one.
16
+ */
17
+ export function bindContextState(context, state) {
18
+ const scoped = { ...context, [CONTEXT_STATE]: state };
19
+ return scoped;
20
+ }
21
+ /**
22
+ * The context state bound to `context`, or `fallback` when none is bound (stdio and
23
+ * in-process calls use the server's single state).
24
+ *
25
+ * @returns the bound state, `null` for a caller without a session, or `fallback`
26
+ */
27
+ export function resolveContextState(context, fallback) {
28
+ const scoped = context;
29
+ return scoped && CONTEXT_STATE in scoped ? scoped[CONTEXT_STATE] : fallback;
30
+ }
31
+ //# sourceMappingURL=context-scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-scope.js","sourceRoot":"","sources":["../src/context-scope.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,MAAM,aAAa,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;AAIjD;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAC/B,OAAuB,EACvB,KAA0B;IAE1B,MAAM,MAAM,GAAkB,EAAE,GAAG,OAAO,EAAE,CAAC,aAAa,CAAC,EAAE,KAAK,EAAE,CAAC;IACrE,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAClC,OAAmC,EACnC,QAAuB;IAEvB,MAAM,MAAM,GAAG,OAAoC,CAAC;IACpD,OAAO,MAAM,IAAI,aAAa,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC7E,CAAC"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @fileoverview Transport-free entry point: `@lushly-dev/afd-server/define`
3
+ *
4
+ * Exposes `defineCommand`, the Zod schema helpers and the result helpers
5
+ * without loading the MCP SDK or any Node.js builtin (`node:http`, `node:tls`,
6
+ * `node:child_process`). Import from here in code that runs in the browser or
7
+ * that only defines commands, such as shared command modules, UI state
8
+ * packages and in-app agents.
9
+ *
10
+ * The root entry (`@lushly-dev/afd-server`) still exports everything below,
11
+ * plus `createMcpServer`, `createMcpHandler`, `createDirectRegistry` and the
12
+ * middleware, which need Node.js.
13
+ *
14
+ * Module-graph rule: this file and `schema.ts` may take runtime values from
15
+ * `@lushly-dev/afd-core` only through its browser-safe subpaths
16
+ * (`/commands`, `/result`). The core root entry still re-exports the
17
+ * process-spawning connectors. `define.test.ts` bundles this entry with
18
+ * esbuild for the browser to enforce the rule.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { z } from 'zod';
23
+ * import { defineCommand, success } from '@lushly-dev/afd-server/define';
24
+ *
25
+ * export const panelOpen = defineCommand({
26
+ * name: 'panel-open',
27
+ * description: 'Open a panel',
28
+ * input: z.object({ id: z.string() }),
29
+ * expose: { mcp: true },
30
+ * async handler(input) {
31
+ * return success({ id: input.id, open: true });
32
+ * },
33
+ * });
34
+ * ```
35
+ *
36
+ * @packageDocumentation
37
+ */
38
+ export type { CommandContext, CommandError, CommandExample, CommandResult, ExposeOptions, JsonSchema, } from '@lushly-dev/afd-core';
39
+ export { defaultExpose } from '@lushly-dev/afd-core/commands';
40
+ export { error, failure, isFailure, isSuccess, success } from '@lushly-dev/afd-core/result';
41
+ export { defineCommand, getRequiredFields, isObjectSchema, type ZodCommandDefinition, type ZodCommandOptions, zodToJsonSchema, } from './schema.js';
42
+ //# sourceMappingURL=define.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAIH,YAAY,EACX,cAAc,EACd,YAAY,EACZ,cAAc,EACd,aAAa,EACb,aAAa,EACb,UAAU,GACV,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAC5F,OAAO,EACN,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,eAAe,GACf,MAAM,aAAa,CAAC"}
package/dist/define.js ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @fileoverview Transport-free entry point: `@lushly-dev/afd-server/define`
3
+ *
4
+ * Exposes `defineCommand`, the Zod schema helpers and the result helpers
5
+ * without loading the MCP SDK or any Node.js builtin (`node:http`, `node:tls`,
6
+ * `node:child_process`). Import from here in code that runs in the browser or
7
+ * that only defines commands, such as shared command modules, UI state
8
+ * packages and in-app agents.
9
+ *
10
+ * The root entry (`@lushly-dev/afd-server`) still exports everything below,
11
+ * plus `createMcpServer`, `createMcpHandler`, `createDirectRegistry` and the
12
+ * middleware, which need Node.js.
13
+ *
14
+ * Module-graph rule: this file and `schema.ts` may take runtime values from
15
+ * `@lushly-dev/afd-core` only through its browser-safe subpaths
16
+ * (`/commands`, `/result`). The core root entry still re-exports the
17
+ * process-spawning connectors. `define.test.ts` bundles this entry with
18
+ * esbuild for the browser to enforce the rule.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { z } from 'zod';
23
+ * import { defineCommand, success } from '@lushly-dev/afd-server/define';
24
+ *
25
+ * export const panelOpen = defineCommand({
26
+ * name: 'panel-open',
27
+ * description: 'Open a panel',
28
+ * input: z.object({ id: z.string() }),
29
+ * expose: { mcp: true },
30
+ * async handler(input) {
31
+ * return success({ id: input.id, open: true });
32
+ * },
33
+ * });
34
+ * ```
35
+ *
36
+ * @packageDocumentation
37
+ */
38
+ export { defaultExpose } from '@lushly-dev/afd-core/commands';
39
+ export { error, failure, isFailure, isSuccess, success } from '@lushly-dev/afd-core/result';
40
+ export { defineCommand, getRequiredFields, isObjectSchema, zodToJsonSchema, } from './schema.js';
41
+ //# sourceMappingURL=define.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"define.js","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAYH,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAC5F,OAAO,EACN,aAAa,EACb,iBAAiB,EACjB,cAAc,EAGd,eAAe,GACf,MAAM,aAAa,CAAC"}