@sveltium/mcp 0.1.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.
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env node
2
+ 'use strict'
3
+
4
+ /**
5
+ * NW.js MCP Server
6
+ *
7
+ * Standalone Node.js server that:
8
+ * 1. Communicates with Claude Code via stdio (MCP protocol)
9
+ * 2. Accepts WebSocket connections from NW.js apps
10
+ * 3. Proxies tool calls to connected NW.js apps
11
+ * 4. Also accepts HTTP requests for stdio proxy mode
12
+ *
13
+ * Usage: node server/index.js [--port 3940]
14
+ */
15
+
16
+ var http = require('http')
17
+ var MCPHandler = require('./mcp-handler')
18
+ var WebSocketBridge = require('./ws-bridge')
19
+
20
+ var DEFAULT_WS_PORT = 3940
21
+
22
+ // Parse command line args
23
+ var wsPort = DEFAULT_WS_PORT
24
+ var args = process.argv.slice(2)
25
+
26
+ // Handle 'init' command - creates .mcp.json in current directory
27
+ if (args[0] === 'init') {
28
+ var fs = require('fs')
29
+ var path = require('path')
30
+ var cwd = process.cwd()
31
+ var mcpJsonPath = path.join(cwd, '.mcp.json')
32
+ var serverPath = path.join(__dirname, 'index.js')
33
+
34
+ var mcpConfig = {}
35
+ if (fs.existsSync(mcpJsonPath)) {
36
+ try {
37
+ mcpConfig = JSON.parse(fs.readFileSync(mcpJsonPath, 'utf8'))
38
+ } catch (e) {
39
+ mcpConfig = {}
40
+ }
41
+ }
42
+
43
+ if (!mcpConfig.mcpServers) {
44
+ mcpConfig.mcpServers = {}
45
+ }
46
+
47
+ mcpConfig.mcpServers.nwjs = {
48
+ command: 'node',
49
+ args: [serverPath]
50
+ }
51
+
52
+ fs.writeFileSync(mcpJsonPath, JSON.stringify(mcpConfig, null, 2) + '\n')
53
+ console.log('[nwjs-mcp] Created .mcp.json at:', mcpJsonPath)
54
+ console.log('[nwjs-mcp] Server path:', serverPath)
55
+ process.exit(0)
56
+ }
57
+
58
+ for (var i = 0; i < args.length; i++) {
59
+ if ((args[i] === '--port' || args[i] === '-p') && args[i + 1]) {
60
+ wsPort = parseInt(args[i + 1], 10)
61
+ i++
62
+ } else if (args[i].indexOf('--port=') === 0) {
63
+ wsPort = parseInt(args[i].split('=')[1], 10)
64
+ }
65
+ }
66
+
67
+ var httpPort = wsPort + 1 // 3941 for HTTP
68
+
69
+ // Create WebSocket bridge for NW.js apps
70
+ var wsBridge = new WebSocketBridge(wsPort)
71
+
72
+ // Create MCP handler
73
+ var mcpHandler = new MCPHandler(wsBridge)
74
+
75
+ // Start WebSocket bridge
76
+ wsBridge.start()
77
+
78
+ // Start MCP stdio listener
79
+ mcpHandler.start()
80
+
81
+ // Also start HTTP server for proxy mode
82
+ var httpServer = http.createServer(function(req, res) {
83
+ if (req.method === 'POST' && req.url === '/mcp') {
84
+ var body = ''
85
+ req.on('data', function(chunk) {
86
+ body += chunk
87
+ })
88
+ req.on('end', function() {
89
+ mcpHandler.handleMessage(body, function(response) {
90
+ res.writeHead(200, { 'Content-Type': 'application/json' })
91
+ res.end(response)
92
+ })
93
+ })
94
+ } else {
95
+ res.writeHead(404)
96
+ res.end('Not found')
97
+ }
98
+ })
99
+
100
+ httpServer.listen(httpPort, function() {
101
+ process.stderr.write('[nwjs-mcp] HTTP server listening on port ' + httpPort + '\n')
102
+ })
103
+
104
+ // Handle shutdown
105
+ process.on('SIGINT', function() {
106
+ wsBridge.stop()
107
+ httpServer.close()
108
+ process.exit(0)
109
+ })
110
+
111
+ process.on('SIGTERM', function() {
112
+ wsBridge.stop()
113
+ httpServer.close()
114
+ process.exit(0)
115
+ })