1
- -- WebSocket server for Claude Code Neovim integration
1
+ --- @brief WebSocket server for Claude Code Neovim integration
2
2
local M = {}
3
+ math.randomseed (os.time ()) -- Seed for random port selection
3
4
4
- -- Server state
5
+ --- @class ServerState
6
+ --- @field server table | nil The server instance
7
+ --- @field port number | nil The port server is running on
8
+ --- @field clients table A list of connected clients
9
+ --- @field handlers table Message handlers by method name
5
10
M .state = {
6
11
server = nil ,
7
12
port = nil ,
8
13
clients = {},
9
14
handlers = {},
10
15
}
11
16
12
- -- Find an available port in the given range
13
- function M .find_available_port (min , _ ) -- '_' for unused max param
17
+ --- @brief Find an available port in the given range
18
+ --- @param min number The minimum port number
19
+ --- @param max number The maximum port number
20
+ --- @return number port The selected port
21
+ function M .find_available_port (min , max )
14
22
-- TODO: Implement port scanning logic
15
- -- For now, return a mock value
16
- return min
23
+ if min > max then
24
+ -- Defaulting to min in this edge case to avoid math.random error.
25
+ -- Consider logging a warning or error here in a real scenario.
26
+ return min
27
+ end
28
+ return math.random (min , max )
17
29
end
18
30
19
- -- Initialize the WebSocket server
31
+ --- @brief Initialize the WebSocket server
32
+ --- @param config table Configuration options
33
+ --- @return boolean success Whether server started successfully
34
+ --- @return number | string port_or_error Port number or error message
20
35
function M .start (config )
21
36
if M .state .server then
22
- -- Already running
23
37
return false , " Server already running"
24
38
end
25
39
26
40
-- TODO: Implement actual WebSocket server
27
41
-- This is a placeholder that would be replaced with real implementation
28
42
29
- -- Find an available port
30
43
local port = M .find_available_port (config .port_range .min , config .port_range .max )
31
44
32
45
if not port then
33
46
return false , " No available ports found"
34
47
end
35
48
36
- -- Store the port in state
37
49
M .state .port = port
38
50
39
51
-- Mock server object for now
@@ -42,91 +54,78 @@ function M.start(config)
42
54
clients = {},
43
55
}
44
56
45
- -- Register message handlers
46
57
M .register_handlers ()
47
58
48
59
return true , port
49
60
end
50
61
51
- -- Stop the WebSocket server
62
+ --- @brief Stop the WebSocket server
63
+ --- @return boolean success Whether server stopped successfully
64
+ --- @return string | nil error_message Error message if any
52
65
function M .stop ()
53
66
if not M .state .server then
54
- -- Not running
55
67
return false , " Server not running"
56
68
end
57
69
58
70
-- TODO: Implement actual WebSocket server shutdown
59
- -- This is a placeholder
60
71
61
- -- Reset state
62
72
M .state .server = nil
63
73
M .state .port = nil
64
74
M .state .clients = {}
65
75
66
76
return true
67
77
end
68
78
69
- -- Register message handlers
79
+ --- @brief Register message handlers for the server
70
80
function M .register_handlers ()
71
81
-- TODO: Implement message handler registration
72
- -- These would be functions that handle specific message types
73
82
74
- -- Example handlers:
75
83
M .state .handlers = {
76
84
[" mcp.connect" ] = function (_ , _ ) -- '_' for unused args
77
- -- Handle connection handshake
78
85
-- TODO: Implement
79
86
end ,
80
87
81
88
[" mcp.tool.invoke" ] = function (_ , _ ) -- '_' for unused args
82
- -- Handle tool invocation
83
89
-- TODO: Implement by dispatching to tool implementations
84
90
end ,
85
91
}
86
92
end
87
93
88
- -- Send a message to a client
94
+ --- @brief Send a message to a client
95
+ --- @param _client table The client to send to
96
+ --- @param _method string The method name
97
+ --- @param _params table | nil The parameters to send
98
+ --- @return boolean success Whether message was sent successfully
89
99
function M .send (_client , _method , _params ) -- Prefix unused params with underscore
90
100
-- TODO: Implement sending WebSocket message
91
- -- This is a placeholder
92
-
93
- -- Structure what would be sent (commented out to avoid unused var warning)
94
- -- local message = {
95
- -- jsonrpc = "2.0",
96
- -- method = _method,
97
- -- params = _params,
98
- -- }
99
-
100
- -- Mock sending logic
101
- -- In real implementation, this would send the JSON-encoded message
102
- -- to the WebSocket client
103
101
104
102
return true
105
103
end
106
104
107
- -- Send a response to a client
108
- function M .send_response (_client , id , result , error_data ) -- Normal params since they're now used
105
+ --- @brief Send a response to a client
106
+ --- @param _client table The client to send to
107
+ --- @param id number | string The request ID to respond to
108
+ --- @param result any | nil The result data if successful
109
+ --- @param error_data table | nil The error data if failed
110
+ --- @return boolean success Whether response was sent successfully
111
+ function M .send_response (_client , id , result , error_data )
109
112
-- TODO: Implement sending WebSocket response
110
- -- This is a placeholder
111
113
112
- -- Structure what would be sent (this is a comment, we'll make a real variable below)
113
- -- In actual implementation, we would send the message via WebSocket
114
114
if error_data then
115
- -- Create error response
116
115
local _ = { jsonrpc = " 2.0" , id = id , error = error_data } -- luacheck: ignore
117
116
else
118
- -- Create result response
119
117
local _ = { jsonrpc = " 2.0" , id = id , result = result } -- luacheck: ignore
120
118
end
121
119
122
- -- Mock sending logic
123
120
return true
124
121
end
125
122
126
- -- Broadcast a message to all connected clients
123
+ --- @brief Broadcast a message to all connected clients
124
+ --- @param method string The method name
125
+ --- @param params table | nil The parameters to send
126
+ --- @return boolean success Whether broadcast was successful
127
127
function M .broadcast (method , params )
128
128
-- TODO: Implement broadcasting to all clients
129
- -- This is a placeholder
130
129
131
130
for _ , client in pairs (M .state .clients ) do
132
131
M .send (client , method , params )
0 commit comments