@@ -2,15 +2,26 @@ package tools
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
- "os/exec"
5
+ "strconv"
7
6
8
7
"github.com/mark3labs/mcp-go/mcp"
9
8
"github.com/mark3labs/mcp-go/server"
10
9
)
11
10
12
- // GetServerTools returns all NATS server tools
13
- func (n * NATSServerTools ) GetServerTools () []Tool {
11
+ // ServerTools represents all NATS server-related tools
12
+ type ServerTools struct {
13
+ nats * NATSServerTools
14
+ }
15
+
16
+ // NewServerTools creates a new ServerTools instance
17
+ func NewServerTools (nats * NATSServerTools ) * ServerTools {
18
+ return & ServerTools {
19
+ nats : nats ,
20
+ }
21
+ }
22
+
23
+ // GetTools implements the ToolCategory interface
24
+ func (s * ServerTools ) GetTools () []Tool {
14
25
return []Tool {
15
26
{
16
27
Tool : mcp.Tool {
@@ -19,32 +30,32 @@ func (n *NATSServerTools) GetServerTools() []Tool {
19
30
InputSchema : mcp.ToolInputSchema {
20
31
Type : "object" ,
21
32
Properties : map [string ]interface {}{
22
- "random_string " : map [string ]interface {}{
23
- "type" : "string " ,
24
- "description" : "Dummy parameter for no-parameter tools " ,
33
+ "expect " : map [string ]interface {}{
34
+ "type" : "integer " ,
35
+ "description" : "How many servers to expect " ,
25
36
},
26
37
},
27
- Required : []string {"random_string" },
38
+ Required : []string {},
28
39
},
29
40
},
30
- Handler : n .serverListHandler (),
41
+ Handler : s .serverListHandler (),
31
42
},
32
43
{
33
44
Tool : mcp.Tool {
34
- Name : "server_check " ,
35
- Description : "Check NATS server health " ,
45
+ Name : "server_info " ,
46
+ Description : "Get NATS server info " ,
36
47
InputSchema : mcp.ToolInputSchema {
37
48
Type : "object" ,
38
49
Properties : map [string ]interface {}{
39
- "random_string " : map [string ]interface {}{
50
+ "server " : map [string ]interface {}{
40
51
"type" : "string" ,
41
- "description" : "Dummy parameter for no-parameter tools " ,
52
+ "description" : "Server ID or Name to inspect " ,
42
53
},
43
54
},
44
- Required : []string {"random_string" },
55
+ Required : []string {},
45
56
},
46
57
},
47
- Handler : n . serverCheckHandler (),
58
+ Handler : s . serverInfoHandler (),
48
59
},
49
60
{
50
61
Tool : mcp.Tool {
@@ -53,54 +64,73 @@ func (n *NATSServerTools) GetServerTools() []Tool {
53
64
InputSchema : mcp.ToolInputSchema {
54
65
Type : "object" ,
55
66
Properties : map [string ]interface {}{
56
- "random_string " : map [string ]interface {}{
57
- "type" : "string " ,
58
- "description" : "Dummy parameter for no-parameter tools " ,
67
+ "expect " : map [string ]interface {}{
68
+ "type" : "integer " ,
69
+ "description" : "How many servers to expect " ,
59
70
},
60
71
},
61
- Required : []string {"random_string" },
72
+ Required : []string {},
62
73
},
63
74
},
64
- Handler : n .serverPingHandler (),
75
+ Handler : s .serverPingHandler (),
65
76
},
66
77
}
67
78
}
68
79
69
- func (n * NATSServerTools ) executeNATSCommand (args ... string ) (string , error ) {
70
- baseArgs := []string {"-s" , n .natsURL , "--creds" , n .natsCredsPath }
71
- args = append (baseArgs , args ... )
80
+ // nats server list
81
+ // Args:
82
+ //
83
+ // [<expect>] How many servers to expect
84
+ func (s * ServerTools ) serverListHandler () server.ToolHandlerFunc {
85
+ return func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
86
+ var args []string
87
+ args = append (args , "server" , "list" )
72
88
73
- cmd := exec .Command ("nats" , args ... )
74
- output , err := cmd .CombinedOutput ()
75
- if err != nil {
76
- return "" , fmt .Errorf ("NATS command failed: %v, output: %s" , err , string (output ))
77
- }
78
- return string (output ), nil
79
- }
89
+ if expect , ok := request .Params .Arguments ["expect" ].(int ); ok {
90
+ args = append (args , strconv .Itoa (expect ))
91
+ }
80
92
81
- func (n * NATSServerTools ) serverListHandler () server.ToolHandlerFunc {
82
- return func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
83
- output , err := n .executeNATSCommand ("server" , "list" )
93
+ output , err := s .nats .GetSysExecutor ().ExecuteCommand (args ... )
84
94
if err != nil {
85
95
return nil , err
86
96
}
87
97
return mcp .NewToolResultText (output ), nil
88
98
}
89
99
}
90
100
91
- func (n * NATSServerTools ) serverCheckHandler () server.ToolHandlerFunc {
101
+ // nats server info
102
+ // Args:
103
+ //
104
+ // [<server>] Server ID or Name to inspect
105
+ func (s * ServerTools ) serverInfoHandler () server.ToolHandlerFunc {
92
106
return func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
93
- output , err := n .executeNATSCommand ("server" , "check" )
107
+ var args []string
108
+ args = append (args , "server" , "info" )
109
+
110
+ if server , ok := request .Params .Arguments ["server" ].(string ); ok {
111
+ args = append (args , server )
112
+ }
113
+ output , err := s .nats .GetSysExecutor ().ExecuteCommand (args ... )
94
114
if err != nil {
95
115
return nil , err
96
116
}
97
117
return mcp .NewToolResultText (output ), nil
98
118
}
99
119
}
100
120
101
- func (n * NATSServerTools ) serverPingHandler () server.ToolHandlerFunc {
121
+ // nats server ping
122
+ // Args:
123
+ //
124
+ // [<expect>] How many servers to expect
125
+ func (s * ServerTools ) serverPingHandler () server.ToolHandlerFunc {
102
126
return func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
103
- output , err := n .executeNATSCommand ("server" , "ping" )
127
+ var args []string
128
+ args = append (args , "server" , "ping" )
129
+
130
+ if expect , ok := request .Params .Arguments ["expect" ].(string ); ok {
131
+ args = append (args , expect )
132
+ }
133
+ output , err := s .nats .GetSysExecutor ().ExecuteCommand (args ... )
104
134
if err != nil {
105
135
return nil , err
106
136
}
0 commit comments