@fnet/cli 0.115.0 → 0.116.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.
@@ -1,223 +1,327 @@
1
1
  {% if atom.doc.features.cli.enabled===true %}
2
- import argv from '../default/input.args.js';
3
- import { default as Engine } from '../default/{{atom.doc.features.cli_default_entry_file or atom.doc.features.main_default_entry_file}}';
4
2
 
5
- {% if atom.doc.features.cli.mcp.enabled===true %}
6
- import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
+ {# Define macros for reusable code blocks #}
4
+ {% macro importMcpDependencies() %}
5
+ import { Server, ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/server/mcp.js";
7
6
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
8
- {% endif %}
7
+ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
8
+ import express from "express";
9
+ {% endmacro %}
9
10
 
10
- {% if atom.doc.features.cli.http.enabled===true %}
11
- // Using Node.js built-in http module instead of express
12
- {% endif %}
13
-
14
- {% if atom.doc.features.cli.extend===true %}
15
- {# TYPE 1 #}
16
- import { default as runExtended } from '../../../cli';
11
+ {% macro mcpModeCodeExtended(runFn) %}
12
+ if (cliMode === 'mcp') {
13
+ // MCP mode code
14
+ const server = new Server({
15
+ name: "{{atom.doc.features.cli.mcp.name or atom.doc.name}}",
16
+ version: "{{atom.doc.version or '0.0.1'}}"
17
+ }, {
18
+ capabilities: {
19
+ tools: {}
20
+ }
21
+ });
17
22
 
18
- const run = async () => {
19
- const args = await argv();
20
- const cliMode = args['cli-mode'] || args.cli_mode || 'default';
23
+ // Define available tools
24
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
25
+ return {
26
+ tools: [{
27
+ name: "{{atom.doc.features.cli.mcp.tool.name or atom.doc.name}}",
28
+ description: "{{atom.doc.features.cli.mcp.tool.description or atom.doc.description}}",
29
+ inputSchema: {
30
+ type: "object",
31
+ properties: {},
32
+ additionalProperties: true
33
+ }
34
+ }]
35
+ };
36
+ });
21
37
 
22
- if (cliMode === 'default') {
23
- // Default mode code
24
- return await runExtended(args, { Engine });
38
+ // Handle tool execution
39
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
40
+ if (request.params.name === "{{atom.doc.features.cli.mcp.tool.name or atom.doc.name}}") {
41
+ try {
42
+ const result = await {{ runFn }}(request.params.arguments, { Engine });
43
+ return {
44
+ content: [{
45
+ type: "text",
46
+ text: JSON.stringify(result)
47
+ }]
48
+ };
49
+ } catch (error) {
50
+ return {
51
+ content: [{
52
+ type: "text",
53
+ text: `Error: ${error.message}`
54
+ }],
55
+ isError: true
56
+ };
57
+ }
25
58
  }
59
+ throw new Error("Tool not found");
60
+ });
26
61
 
27
- {% if atom.doc.features.cli.mcp.enabled===true %}
28
- if (cliMode === 'mcp') {
29
- // MCP mode code
30
- const server = new McpServer({
31
- name: "{{atom.doc.name}}",
32
- version: "{{atom.doc.version}}"
33
- });
34
-
35
- server.tool(
36
- "{{atom.doc.name}}",
37
- async (toolArgs) => {
38
- try {
39
- const result = await runExtended(toolArgs, { Engine });
40
- return {
41
- content: [{
42
- type: "text",
43
- text: JSON.stringify(result)
44
- }]
45
- };
46
- } catch (error) {
47
- return {
48
- content: [{
49
- type: "text",
50
- text: `Error: ${error.message}`
51
- }],
52
- isError: true
53
- };
54
- }
55
- }
56
- );
62
+ // Get transport type from arguments
63
+ const transportType = args['mcp-transport-type'] || args.mcp_transport_type || 'stdio';
64
+ let transport;
65
+
66
+ if (transportType === 'stdio') {
67
+ // Use stdio transport
68
+ transport = new StdioServerTransport();
69
+ console.log("MCP server started with stdio transport");
70
+ } else if (transportType === 'http') {
71
+ // Use HTTP transport
72
+ const app = express();
73
+ app.use(express.json());
74
+
75
+ const port = args['cli-port'] || args.cli_port || 3000;
76
+ const server = app.listen(port, () => {
77
+ console.log(`MCP server started with HTTP transport on port ${port}`);
78
+ });
79
+
80
+ transport = new StreamableHTTPServerTransport({
81
+ sessionIdGenerator: () => Math.random().toString(36).substring(2, 15),
82
+ });
83
+
84
+ app.post('/mcp', async (req, res) => {
85
+ await transport.handleRequest(req, res, req.body);
86
+ });
57
87
 
58
- const transport = new StdioServerTransport();
59
- await server.connect(transport);
60
- console.log("MCP server started with stdio transport");
61
- return;
88
+ app.get('/mcp', async (req, res) => {
89
+ await transport.handleRequest(req, res);
90
+ });
91
+
92
+ app.delete('/mcp', async (req, res) => {
93
+ await transport.handleRequest(req, res);
94
+ });
95
+ } else {
96
+ console.error(`Unknown MCP transport type: ${transportType}`);
97
+ console.error(`Supported types: stdio, http`);
98
+ process.exit(1);
99
+ }
100
+
101
+ await server.connect(transport);
102
+ return;
103
+ }
104
+ {% endmacro %}
105
+
106
+ {% macro mcpModeCodeEngine(engineVar) %}
107
+ if (cliMode === 'mcp') {
108
+ // MCP mode code
109
+ const server = new Server({
110
+ name: "{{atom.doc.features.cli.mcp.name or atom.doc.name}}",
111
+ version: "{{atom.doc.version or '0.0.1'}}"
112
+ }, {
113
+ capabilities: {
114
+ tools: {}
62
115
  }
63
- {% endif %}
64
-
65
- {% if atom.doc.features.cli.http.enabled===true %}
66
- if (cliMode === 'http') {
67
- // HTTP mode code using built-in http module
68
- const http = require('http');
69
-
70
- const server = http.createServer((req, res) => {
71
- if (req.method === 'POST' && req.url === '/{{atom.doc.name}}') {
72
- let body = '';
73
- req.on('data', chunk => {
74
- body += chunk.toString();
75
- });
76
- req.on('end', async () => {
77
- try {
78
- const data = JSON.parse(body);
79
- const result = await runExtended(data, { Engine });
80
- res.writeHead(200, { 'Content-Type': 'application/json' });
81
- res.end(JSON.stringify(result));
82
- } catch (error) {
83
- res.writeHead(500, { 'Content-Type': 'application/json' });
84
- res.end(JSON.stringify({ error: error.message }));
85
- }
86
- });
87
- } else {
88
- res.writeHead(404);
89
- res.end();
116
+ });
117
+
118
+ // Define available tools
119
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
120
+ return {
121
+ tools: [{
122
+ name: "{{atom.doc.features.cli.mcp.tool.name or atom.doc.name}}",
123
+ description: "{{atom.doc.features.cli.mcp.tool.description or atom.doc.description}}",
124
+ inputSchema: {
125
+ type: "object",
126
+ properties: {},
127
+ additionalProperties: true
90
128
  }
91
- });
129
+ }]
130
+ };
131
+ });
92
132
 
93
- const port = args['cli-port'] || args.cli_port || 3000;
94
- server.listen(port, () => {
95
- console.log(`HTTP server started on port ${port}`);
96
- });
97
- return;
133
+ // Handle tool execution
134
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
135
+ if (request.params.name === "{{atom.doc.features.cli.mcp.tool.name or atom.doc.name}}") {
136
+ try {
137
+ const result = await {{ engineVar }}.run(request.params.arguments);
138
+ return {
139
+ content: [{
140
+ type: "text",
141
+ text: JSON.stringify(result)
142
+ }]
143
+ };
144
+ } catch (error) {
145
+ return {
146
+ content: [{
147
+ type: "text",
148
+ text: `Error: ${error.message}`
149
+ }],
150
+ isError: true
151
+ };
152
+ }
98
153
  }
99
- {% endif %}
154
+ throw new Error("Tool not found");
155
+ });
100
156
 
101
- console.error(`Unknown CLI mode: ${cliMode}`);
102
- process.exit(1);
103
- };
104
-
105
- run()
106
- .then(() => {
107
- {# process.exit(0); #}
108
- })
109
- .catch((error) => {
110
- console.error(error.message);
111
- process.exit(1);
157
+ // Note: Direct access to workflow nodes is not implemented in this version
158
+ // In a future version, we could expose workflow nodes as separate MCP tools
159
+
160
+ // Get transport type from arguments
161
+ const transportType = args['mcp-transport-type'] || args.mcp_transport_type || 'stdio';
162
+ let transport;
163
+
164
+ if (transportType === 'stdio') {
165
+ // Use stdio transport
166
+ transport = new StdioServerTransport();
167
+ console.log("MCP server started with stdio transport");
168
+ } else if (transportType === 'http') {
169
+ // Use HTTP transport
170
+ const app = express();
171
+ app.use(express.json());
172
+
173
+ const port = args['cli-port'] || args.cli_port || 3000;
174
+ const server = app.listen(port, () => {
175
+ console.log(`MCP server started with HTTP transport on port ${port}`);
112
176
  });
113
- {% else %}
114
- {# TYPE 2 #}
115
- const run = async () => {
116
- const args = await argv();
117
- const cliMode = args['cli-mode'] || args.cli_mode || 'default';
118
- const engine = new Engine();
119
-
120
- if (cliMode === 'default') {
121
- // Default mode code
122
- const result = await engine.run(args);
123
-
124
- if (typeof result !== 'undefined') {
125
- const stdout_format = args['stdout-format'] || args.stdout_format || null;
126
-
127
- if (stdout_format === 'json') console.log(JSON.stringify(result, null, 2));
128
- else console.log(result);
129
- }
130
- return;
131
- }
132
177
 
133
- {% if atom.doc.features.cli.mcp.enabled===true %}
134
- if (cliMode === 'mcp') {
135
- // MCP mode code
136
- const server = new McpServer({
137
- name: "{{atom.doc.name}}",
138
- version: "{{atom.doc.version}}"
139
- });
140
-
141
- server.tool(
142
- "{{atom.doc.name}}",
143
- async (toolArgs) => {
144
- try {
145
- const result = await engine.run(toolArgs);
146
- return {
147
- content: [{
148
- type: "text",
149
- text: JSON.stringify(result)
150
- }]
151
- };
152
- } catch (error) {
153
- return {
154
- content: [{
155
- type: "text",
156
- text: `Error: ${error.message}`
157
- }],
158
- isError: true
159
- };
160
- }
161
- }
162
- );
178
+ transport = new StreamableHTTPServerTransport({
179
+ sessionIdGenerator: () => Math.random().toString(36).substring(2, 15),
180
+ });
163
181
 
164
- // Note: Direct access to workflow nodes is not implemented in this version
165
- // In a future version, we could expose workflow nodes as separate MCP tools
182
+ app.post('/mcp', async (req, res) => {
183
+ await transport.handleRequest(req, res, req.body);
184
+ });
166
185
 
167
- const transport = new StdioServerTransport();
168
- await server.connect(transport);
169
- console.log("MCP server started with stdio transport");
170
- return;
171
- }
172
- {% endif %}
173
-
174
- {% if atom.doc.features.cli.http.enabled===true %}
175
- if (cliMode === 'http') {
176
- // HTTP mode code using built-in http module
177
- const http = require('http');
178
-
179
- const server = http.createServer((req, res) => {
180
- if (req.method === 'POST' && req.url === '/{{atom.doc.name}}') {
181
- let body = '';
182
- req.on('data', chunk => {
183
- body += chunk.toString();
184
- });
185
- req.on('end', async () => {
186
- try {
187
- const data = JSON.parse(body);
188
- const result = await engine.run(data);
189
- res.writeHead(200, { 'Content-Type': 'application/json' });
190
- res.end(JSON.stringify(result));
191
- } catch (error) {
192
- res.writeHead(500, { 'Content-Type': 'application/json' });
193
- res.end(JSON.stringify({ error: error.message }));
194
- }
195
- });
196
- } else {
197
- res.writeHead(404);
198
- res.end();
199
- }
200
- });
186
+ app.get('/mcp', async (req, res) => {
187
+ await transport.handleRequest(req, res);
188
+ });
189
+
190
+ app.delete('/mcp', async (req, res) => {
191
+ await transport.handleRequest(req, res);
192
+ });
193
+ } else {
194
+ console.error(`Unknown MCP transport type: ${transportType}`);
195
+ console.error(`Supported types: stdio, http`);
196
+ process.exit(1);
197
+ }
201
198
 
202
- // Note: Direct access to workflow nodes is not implemented in this version
203
- // In a future version, we could expose workflow nodes as separate HTTP endpoints
199
+ await server.connect(transport);
200
+ return;
201
+ }
202
+ {% endmacro %}
204
203
 
205
- const port = args['cli-port'] || args.cli_port || 3000;
206
- server.listen(port, () => {
207
- console.log(`HTTP server started on port ${port}`);
208
- });
209
- return;
204
+ {% macro httpModeCodeExpress(runFn, engineParam) %}
205
+ if (cliMode === 'http') {
206
+ // HTTP mode code using Express
207
+ const app = express();
208
+ app.use(express.json());
209
+
210
+ app.post('/{{atom.doc.features.cli.http.path or atom.doc.name}}', async (req, res) => {
211
+ try {
212
+ const result = await {{ runFn }}(req.body{{ engineParam }});
213
+ res.json(result);
214
+ } catch (error) {
215
+ res.status(500).json({ error: error.message });
210
216
  }
211
- {% endif %}
217
+ });
212
218
 
213
- console.error(`Unknown CLI mode: ${cliMode}`);
214
- process.exit(1);
215
- };
219
+ const port = args['cli-port'] || args.cli_port || 3000;
220
+ app.listen(port, () => {
221
+ console.log(`HTTP server started on port ${port}`);
222
+ });
223
+ return;
224
+ }
225
+ {% endmacro %}
216
226
 
217
- run()
218
- .catch((error) => {
219
- console.error(error.message);
220
- process.exit(1);
221
- });
222
- {% endif %}
227
+ {% macro defaultModeExtended() %}
228
+ if (cliMode === 'default') {
229
+ // Default mode code
230
+ return await runExtended(args, { Engine });
231
+ }
232
+ {% endmacro %}
233
+
234
+ {% macro defaultModeEngine(engineVar) %}
235
+ if (cliMode === 'default') {
236
+ // Default mode code
237
+ const result = await {{ engineVar }}.run(args);
238
+
239
+ if (typeof result !== 'undefined') {
240
+ const stdout_format = args['stdout-format'] || args.stdout_format || null;
241
+
242
+ if (stdout_format === 'json') console.log(JSON.stringify(result, null, 2));
243
+ else console.log(result);
244
+ }
245
+ return;
246
+ }
247
+ {% endmacro %}
248
+
249
+ {% macro runWithCatch() %}
250
+ run()
251
+ .catch((error) => {
252
+ console.error(error.message);
253
+ process.exit(1);
254
+ });
255
+ {% endmacro %}
256
+
257
+ {% macro runWithThenCatch() %}
258
+ run()
259
+ .then(() => {
260
+ {# process.exit(0); #}
261
+ })
262
+ .catch((error) => {
263
+ console.error(error.message);
264
+ process.exit(1);
265
+ });
266
+ {% endmacro %}
267
+
268
+ {# Main template starts here #}
269
+ import argv from '../default/input.args.js';
270
+ import { default as Engine } from '../default/{{atom.doc.features.cli_default_entry_file or atom.doc.features.main_default_entry_file}}';
271
+
272
+ {% if atom.doc.features.cli.mcp.enabled===true %}
273
+ {{ importMcpDependencies() }}
274
+ {% endif %}
275
+
276
+ {% if atom.doc.features.cli.http.enabled===true %}
277
+ // Using express for HTTP mode
278
+ import express from 'express';
279
+ {% endif %}
280
+
281
+ {% if atom.doc.features.cli.extend===true %}
282
+ {# TYPE 1 #}
283
+ import { default as runExtended } from '../../../cli';
284
+
285
+ const run = async () => {
286
+ const args = await argv();
287
+ const cliMode = args['cli-mode'] || args.cli_mode || 'default';
288
+
289
+ {{ defaultModeExtended() }}
290
+
291
+ {% if atom.doc.features.cli.mcp.enabled===true %}
292
+ {{ mcpModeCodeExtended('runExtended') }}
293
+ {% endif %}
294
+
295
+ {% if atom.doc.features.cli.http.enabled===true %}
296
+ {{ httpModeCodeExpress('runExtended', ', { Engine }') }}
297
+ {% endif %}
298
+
299
+ console.error(`Unknown CLI mode: ${cliMode}`);
300
+ process.exit(1);
301
+ };
302
+
303
+ {{ runWithThenCatch() }}
304
+ {% else %}
305
+ {# TYPE 2 #}
306
+ const run = async () => {
307
+ const args = await argv();
308
+ const cliMode = args['cli-mode'] || args.cli_mode || 'default';
309
+ const engine = new Engine();
310
+
311
+ {{ defaultModeEngine('engine') }}
312
+
313
+ {% if atom.doc.features.cli.mcp.enabled===true %}
314
+ {{ mcpModeCodeEngine('engine') }}
315
+ {% endif %}
316
+
317
+ {% if atom.doc.features.cli.http.enabled===true %}
318
+ {{ httpModeCodeExpress('engine.run', '') }}
319
+ {% endif %}
320
+
321
+ console.error(`Unknown CLI mode: ${cliMode}`);
322
+ process.exit(1);
323
+ };
324
+
325
+ {{ runWithCatch() }}
326
+ {% endif %}
223
327
  {% endif %}
@@ -54,13 +54,22 @@
54
54
  "license": "{{atom.doc.license or 'MIT'}}",
55
55
  "scripts": {
56
56
  "build": "rollup --config",
57
+ "build:dev": "rollup --config --sourcemap --environment DEVELOPMENT",
57
58
  "watch": "rollup --config --watch --sourcemap --environment DEVELOPMENT",
58
59
  "serve": "bunx serve ./"
59
60
  {% if atom.doc.features.cli.enabled %}
60
61
  {% if atom.doc.features.project.format ==='cjs' %}
61
- ,"cli": "bun {{atom.doc.features.cli.node_options}} {{atom.doc.features.cli.dir}}/index.cjs"
62
+ ,"cli": "bun {{atom.doc.features.cli.node_options}} {{atom.doc.features.cli.dir}}/index.cjs",
63
+ {% if atom.doc.features.cli.mcp.enabled===true %}
64
+ ,"mcp": "bun {{atom.doc.features.cli.node_options}} {{atom.doc.features.cli.dir}}/index.cjs --cli-mode=mcp"
65
+ ,"mcp-inspect": "bunx @modelcontextprotocol/inspector bun {{atom.doc.features.cli.node_options}} {{atom.doc.features.cli.dir}}/index.cjs --cli-mode=mcp"
66
+ {% endif %}
62
67
  {% else %}
63
68
  ,"cli": "bun {{atom.doc.features.cli.node_options}} {{atom.doc.features.cli.dir}}/"
69
+ {% if atom.doc.features.cli.mcp.enabled===true %}
70
+ ,"mcp": "bun {{atom.doc.features.cli.node_options}} {{atom.doc.features.cli.dir}}/ --cli-mode=mcp"
71
+ ,"mcp-inspect": "bunx @modelcontextprotocol/inspector bun {{atom.doc.features.cli.node_options}} {{atom.doc.features.cli.dir}}/ --cli-mode=mcp"
72
+ {% endif %}
64
73
  {% endif %}
65
74
  ,"compile": "fbin compile {{atom.doc.features.cli.dir}}/index.js -o .bin/{{atom.doc['npm::bin'] or atom.doc['name'] or atom['id']}}"
66
75
  ,"install-bin": "fbin install ./.bin/{{atom.doc['npm::bin'] or atom.doc['name'] or atom['id']}} --yes"