naider 1.18.0 → 1.19.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 (2) hide show
  1. package/mcp/server.js +222 -0
  2. package/package.json +4 -2
package/mcp/server.js ADDED
@@ -0,0 +1,222 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { createInterface } from 'readline';
4
+ import { resolve, dirname } from 'path';
5
+ import { readFileSync } from 'fs';
6
+ import { fileURLToPath } from 'url';
7
+
8
+ const __dirname = dirname(fileURLToPath(import.meta.url));
9
+
10
+ let compileModule = null;
11
+ async function getCompiler() {
12
+ if (!compileModule) {
13
+ compileModule = await import(resolve(__dirname, '..', 'src', 'index.js'));
14
+ }
15
+ return compileModule;
16
+ }
17
+
18
+ function send(msg) {
19
+ const json = JSON.stringify(msg);
20
+ const buf = Buffer.from(json, 'utf-8');
21
+ process.stdout.write(`Content-Length: ${buf.length}\r\n\r\n`);
22
+ process.stdout.write(buf);
23
+ }
24
+
25
+ function respond(id, result) {
26
+ send({ jsonrpc: '2.0', id, result });
27
+ }
28
+
29
+ function respondError(id, code, message) {
30
+ send({ jsonrpc: '2.0', id, error: { code, message } });
31
+ }
32
+
33
+ const TOOLS = [
34
+ {
35
+ name: 'naide_compile',
36
+ description: 'Compile NAIDE code to a target language. NAIDE is an AI-optimized language that transpiles to 15 targets.',
37
+ inputSchema: {
38
+ type: 'object',
39
+ properties: {
40
+ code: { type: 'string', description: 'NAIDE source code to compile' },
41
+ target: {
42
+ type: 'string',
43
+ description: 'Target language (default: node)',
44
+ enum: ['node', 'python', 'bun', 'typescript', 'go', 'java', 'rust', 'cpp', 'c', 'csharp', 'kotlin', 'swift', 'dart', 'php', 'ruby']
45
+ }
46
+ },
47
+ required: ['code']
48
+ }
49
+ },
50
+ {
51
+ name: 'naide_run',
52
+ description: 'Compile NAIDE code to JavaScript and execute it. Returns stdout output.',
53
+ inputSchema: {
54
+ type: 'object',
55
+ properties: {
56
+ code: { type: 'string', description: 'NAIDE source code to run' }
57
+ },
58
+ required: ['code']
59
+ }
60
+ },
61
+ {
62
+ name: 'naide_targets',
63
+ description: 'List all available NAIDE compilation targets with details.',
64
+ inputSchema: { type: 'object', properties: {} }
65
+ },
66
+ {
67
+ name: 'naide_spec',
68
+ description: 'Get the NAIDE language specification. Use this to understand NAIDE syntax before writing code.',
69
+ inputSchema: {
70
+ type: 'object',
71
+ properties: {
72
+ section: {
73
+ type: 'string',
74
+ description: 'Optional section to retrieve (e.g. "variables", "functions", "server"). Omit for full spec.'
75
+ }
76
+ }
77
+ }
78
+ }
79
+ ];
80
+
81
+ const TARGETS_INFO = [
82
+ { name: 'node', language: 'JavaScript (ES Modules)', server: 'Express', flag: 'default' },
83
+ { name: 'python', language: 'Python', server: 'Flask', flag: '--target python' },
84
+ { name: 'bun', language: 'JavaScript (Bun)', server: 'Bun.serve', flag: '--target bun' },
85
+ { name: 'typescript', language: 'TypeScript', server: 'Express', flag: '--target ts' },
86
+ { name: 'go', language: 'Go', server: 'net/http', flag: '--target go' },
87
+ { name: 'java', language: 'Java', server: 'HttpServer', flag: '--target java' },
88
+ { name: 'rust', language: 'Rust', server: 'actix-web', flag: '--target rust' },
89
+ { name: 'cpp', language: 'C++', server: 'cpp-httplib', flag: '--target cpp' },
90
+ { name: 'c', language: 'C', server: 'libmicrohttpd', flag: '--target c' },
91
+ { name: 'csharp', language: 'C#', server: 'ASP.NET', flag: '--target csharp' },
92
+ { name: 'kotlin', language: 'Kotlin', server: 'Ktor', flag: '--target kotlin' },
93
+ { name: 'swift', language: 'Swift', server: 'Vapor', flag: '--target swift' },
94
+ { name: 'dart', language: 'Dart', server: 'shelf', flag: '--target dart' },
95
+ { name: 'php', language: 'PHP', server: 'Built-in / Laravel', flag: '--target php' },
96
+ { name: 'ruby', language: 'Ruby', server: 'Sinatra', flag: '--target ruby' },
97
+ ];
98
+
99
+ async function handleToolCall(name, args) {
100
+ const { compile, compileAsync } = await getCompiler();
101
+
102
+ switch (name) {
103
+ case 'naide_compile': {
104
+ const target = args.target || 'node';
105
+ try {
106
+ if (target === 'node') {
107
+ const result = compile(args.code);
108
+ return { content: [{ type: 'text', text: result.js }] };
109
+ }
110
+ const result = await compileAsync(args.code, { target });
111
+ return { content: [{ type: 'text', text: result.code }] };
112
+ } catch (e) {
113
+ return { content: [{ type: 'text', text: `Compilation error: ${e.message}` }], isError: true };
114
+ }
115
+ }
116
+
117
+ case 'naide_run': {
118
+ try {
119
+ const result = compile(args.code);
120
+ const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
121
+ let output = '';
122
+ const fakeConsole = { log: (...a) => { output += a.map(String).join(' ') + '\n'; }, error: (...a) => { output += a.map(String).join(' ') + '\n'; } };
123
+ const fn = new AsyncFunction('console', result.js);
124
+ await fn(fakeConsole);
125
+ return { content: [{ type: 'text', text: output || '(no output)' }] };
126
+ } catch (e) {
127
+ return { content: [{ type: 'text', text: `Error: ${e.message}` }], isError: true };
128
+ }
129
+ }
130
+
131
+ case 'naide_targets': {
132
+ const text = TARGETS_INFO.map(t => `${t.name.padEnd(12)} ${t.language.padEnd(25)} ${t.server.padEnd(20)} ${t.flag}`).join('\n');
133
+ return { content: [{ type: 'text', text: `NAIDE Compilation Targets (15):\n\n${'Target'.padEnd(12)} ${'Language'.padEnd(25)} ${'Server'.padEnd(20)} Flag\n${'─'.repeat(75)}\n${text}` }] };
134
+ }
135
+
136
+ case 'naide_spec': {
137
+ try {
138
+ let spec = readFileSync(resolve(__dirname, '..', 'SPEC.naide'), 'utf-8');
139
+ if (args.section) {
140
+ const s = args.section.toLowerCase();
141
+ const lines = spec.split('\n');
142
+ const chunks = [];
143
+ let capturing = false;
144
+ for (const line of lines) {
145
+ if (line.startsWith('# ---- ') && line.toLowerCase().includes(s)) capturing = true;
146
+ else if (line.startsWith('# ---- ') && capturing) break;
147
+ if (capturing) chunks.push(line);
148
+ }
149
+ if (chunks.length > 0) spec = chunks.join('\n');
150
+ }
151
+ return { content: [{ type: 'text', text: spec }] };
152
+ } catch (e) {
153
+ return { content: [{ type: 'text', text: `Error reading spec: ${e.message}` }], isError: true };
154
+ }
155
+ }
156
+
157
+ default:
158
+ return { content: [{ type: 'text', text: `Unknown tool: ${name}` }], isError: true };
159
+ }
160
+ }
161
+
162
+ async function handleMessage(msg) {
163
+ switch (msg.method) {
164
+ case 'initialize':
165
+ respond(msg.id, {
166
+ protocolVersion: '2024-11-05',
167
+ capabilities: { tools: {} },
168
+ serverInfo: { name: 'naide-mcp', version: '1.18.0' }
169
+ });
170
+ break;
171
+
172
+ case 'notifications/initialized':
173
+ break;
174
+
175
+ case 'tools/list':
176
+ respond(msg.id, { tools: TOOLS });
177
+ break;
178
+
179
+ case 'tools/call':
180
+ try {
181
+ const result = await handleToolCall(msg.params.name, msg.params.arguments || {});
182
+ respond(msg.id, result);
183
+ } catch (e) {
184
+ respondError(msg.id, -32000, e.message);
185
+ }
186
+ break;
187
+
188
+ case 'ping':
189
+ respond(msg.id, {});
190
+ break;
191
+
192
+ default:
193
+ if (msg.id !== undefined) {
194
+ respondError(msg.id, -32601, `Method not found: ${msg.method}`);
195
+ }
196
+ }
197
+ }
198
+
199
+ let buffer = '';
200
+ process.stdin.setEncoding('utf-8');
201
+ process.stdin.on('data', (chunk) => {
202
+ buffer += chunk;
203
+ while (true) {
204
+ const headerEnd = buffer.indexOf('\r\n\r\n');
205
+ if (headerEnd === -1) break;
206
+ const header = buffer.slice(0, headerEnd);
207
+ const match = header.match(/Content-Length:\s*(\d+)/i);
208
+ if (!match) { buffer = buffer.slice(headerEnd + 4); continue; }
209
+ const len = parseInt(match[1], 10);
210
+ const bodyStart = headerEnd + 4;
211
+ if (buffer.length < bodyStart + len) break;
212
+ const body = buffer.slice(bodyStart, bodyStart + len);
213
+ buffer = buffer.slice(bodyStart + len);
214
+ try {
215
+ handleMessage(JSON.parse(body));
216
+ } catch (e) {
217
+ process.stderr.write(`Parse error: ${e.message}\n`);
218
+ }
219
+ }
220
+ });
221
+
222
+ process.stderr.write('NAIDE MCP Server running on stdio\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naider",
3
- "version": "1.18.0",
3
+ "version": "1.19.0",
4
4
  "description": "NAIDE - Simpler than Python, compiles to 15 targets. AI-specialized language with 35+ built-in functions, syntax sugar (unless/until/repeat/swap/is/isnt), and 47 features. Targets: Node.js, Python, TypeScript, C, C++, Java, Go, Rust, PHP, Ruby, Kotlin, Swift, Dart, C#, Bun.",
5
5
  "main": "src/index.js",
6
6
  "exports": {
@@ -8,13 +8,15 @@
8
8
  "./runtime": "./src/runtime.js"
9
9
  },
10
10
  "bin": {
11
- "naide": "bin/naide.js"
11
+ "naide": "bin/naide.js",
12
+ "naide-mcp": "mcp/server.js"
12
13
  },
13
14
  "type": "module",
14
15
  "files": [
15
16
  "bin/",
16
17
  "src/",
17
18
  "assets/",
19
+ "mcp/",
18
20
  "lsp/",
19
21
  "examples/",
20
22
  "vscode-naide/",