-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmcpnetbird.go
More file actions
193 lines (162 loc) · 5.61 KB
/
mcpnetbird.go
File metadata and controls
193 lines (162 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package mcpnetbird
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/mark3labs/mcp-go/server"
)
const (
defaultNetbirdHost = "api.netbird.io"
defaultNetbirdURL = "https://" + defaultNetbirdHost
netbirdAPIPath = "/api"
netbirdHostEnvVar = "NETBIRD_HOST"
netbirdAPIEnvVar = "NETBIRD_API_TOKEN"
)
// NetbirdClient provides methods to interact with the Netbird API
type NetbirdClient struct {
baseURL string
client *http.Client
}
// Single global variable for testing
var TestNetbirdClient *NetbirdClient
// NewNetbirdClient creates a new NetbirdClient with the given API key
func NewNetbirdClient() *NetbirdClient {
host := os.Getenv(netbirdHostEnvVar)
if host == "" {
host = defaultNetbirdHost
}
baseURL := "https://" + host + netbirdAPIPath
return &NetbirdClient{
baseURL: baseURL,
client: &http.Client{},
}
}
func NewNetbirdClientWithBaseURL(baseURL string) *NetbirdClient {
return &NetbirdClient{
baseURL: baseURL,
client: &http.Client{},
}
}
// do performs an HTTP request to the Netbird API
func (c *NetbirdClient) do(ctx context.Context, method, path string, body, v any) error {
token := NetbirdAPIKeyFromContext(ctx)
if token == "" {
return fmt.Errorf("netbird API token not found in context")
}
var bodyReader io.Reader
if body != nil {
bodyBytes, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("marshaling request body: %w", err)
}
bodyReader = bytes.NewReader(bodyBytes)
}
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader)
if err != nil {
return fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("making request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body))
}
if v != nil && resp.StatusCode != http.StatusNoContent {
if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
return fmt.Errorf("decoding response: %w", err)
}
}
return nil
}
// Get performs a GET request to the Netbird API
func (c *NetbirdClient) Get(ctx context.Context, path string, v any) error {
return c.do(ctx, http.MethodGet, path, nil, v)
}
// Put performs a PUT request to the Netbird API
func (c *NetbirdClient) Put(ctx context.Context, path string, body, v any) error {
return c.do(ctx, http.MethodPut, path, body, v)
}
// Post performs a POST request to the Netbird API
func (c *NetbirdClient) Post(ctx context.Context, path string, body, v any) error {
return c.do(ctx, http.MethodPost, path, body, v)
}
// Delete performs a DELETE request to the Netbird API
func (c *NetbirdClient) Delete(ctx context.Context, path string) error {
return c.do(ctx, http.MethodDelete, path, nil, nil)
}
// Patch performs a PATCH request to the Netbird API
func (c *NetbirdClient) Patch(ctx context.Context, path string, body, v any) error {
return c.do(ctx, http.MethodPatch, path, body, v)
}
type netbirdAPIKeyKey struct{}
// ExtractNetbirdInfoFromEnv is a StdioContextFunc that extracts Netbird configuration
// from environment variables and injects it into the context.
var ExtractNetbirdInfoFromEnv server.StdioContextFunc = func(ctx context.Context) context.Context {
apiKey := os.Getenv(netbirdAPIEnvVar)
if apiKey == "" {
log.Printf("Warning: %s environment variable not found", netbirdAPIEnvVar)
} else {
log.Printf("Found %s environment variable", netbirdAPIEnvVar)
}
return WithNetbirdAPIKey(ctx, apiKey)
}
// ExtractNetbirdInfoFromEnvSSE is an SSEContextFunc that extracts Netbird configuration
// from environment variables and injects it into the context.
var ExtractNetbirdInfoFromEnvSSE server.SSEContextFunc = func(ctx context.Context, req *http.Request) context.Context {
apiKey := os.Getenv(netbirdAPIEnvVar)
if apiKey == "" {
log.Printf("SSE MODE - Warning: %s environment variable not found", netbirdAPIEnvVar)
} else {
log.Printf("SSE MODE - Found %s environment variable with length %d", netbirdAPIEnvVar, len(apiKey))
}
return WithNetbirdAPIKey(ctx, apiKey)
}
// WithNetbirdAPIKey adds the Netbird API key to the context.
func WithNetbirdAPIKey(ctx context.Context, apiKey string) context.Context {
return context.WithValue(ctx, netbirdAPIKeyKey{}, apiKey)
}
// NetbirdAPIKeyFromContext extracts the Netbird API key from the context.
func NetbirdAPIKeyFromContext(ctx context.Context) string {
if v := ctx.Value(netbirdAPIKeyKey{}); v != nil {
return v.(string)
}
return ""
}
// ComposeStdioContextFuncs composes multiple StdioContextFuncs into a single one.
func ComposeStdioContextFuncs(funcs ...server.StdioContextFunc) server.StdioContextFunc {
return func(ctx context.Context) context.Context {
for _, f := range funcs {
ctx = f(ctx)
}
return ctx
}
}
// ComposeSSEContextFuncs composes multiple SSEContextFuncs into a single one.
func ComposeSSEContextFuncs(funcs ...server.SSEContextFunc) server.SSEContextFunc {
return func(ctx context.Context, req *http.Request) context.Context {
for _, f := range funcs {
ctx = f(ctx, req)
}
return ctx
}
}
// ComposedStdioContextFunc is a StdioContextFunc that comprises all predefined StdioContextFuncs.
var ComposedStdioContextFunc = ComposeStdioContextFuncs(
ExtractNetbirdInfoFromEnv,
)
// ComposedSSEContextFunc is an SSEContextFunc that comprises all predefined SSEContextFuncs.
var ComposedSSEContextFunc = ComposeSSEContextFuncs(
ExtractNetbirdInfoFromEnvSSE,
)