-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (121 loc) · 3.14 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// PeeringDB API response structures
type NetResponse struct {
Data []Network `json:"data"`
}
type Network struct {
ID int `json:"id"`
ASN int `json:"asn"`
Name string `json:"name"`
}
type NetixlanResponse struct {
Data []Netixlan `json:"data"`
}
type Netixlan struct {
IXName string `json:"name"`
City string `json:"city"`
Status bool `json:"operational"`
}
func main() {
// Create MCP server
s := server.NewMCPServer(
"PeeringDB Location Server",
"1.0.0",
server.WithPromptCapabilities(true),
)
// Add get locations tool
locationsTool := mcp.NewTool("get_peering_locations",
mcp.WithDescription("Get IX locations for an ASN"),
mcp.WithNumber("asn",
mcp.Required(),
mcp.Description("Autonomous System Number"),
),
)
// Register tool
s.AddTool(locationsTool, getPeeringLocationsHandler)
// Start the stdio server
if err := server.ServeStdio(s); err != nil {
fmt.Printf("Server error: %v\n", err)
}
}
func getPeeringLocationsHandler(arguments map[string]interface{}) (*mcp.CallToolResult, error) {
asn, ok := arguments["asn"].(float64) // JSON numbers come as float64
if !ok {
return mcp.NewToolResultError("ASN must be a number"), nil
}
// Get network ID from ASN
netID, err := getNetworkID(int(asn))
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Error getting network ID: %v", err)), nil
}
// Get peering locations
locations, err := getPeeringLocations(netID)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Error getting peering locations: %v", err)), nil
}
// Format the response
var response strings.Builder
response.WriteString(fmt.Sprintf("🌐 IX Locations for AS%d:\n\n", int(asn)))
if len(locations) == 0 {
response.WriteString("No IX locations found for this ASN.")
} else {
for i, loc := range locations {
status := "🟢 Operational"
if !loc.Status {
status = "🔴 Not Operational"
}
response.WriteString(fmt.Sprintf("%d. %s\n 📍 %s\n Status: %s\n\n",
i+1,
loc.IXName,
loc.City,
status))
}
}
return mcp.NewToolResultText(response.String()), nil
}
func getNetworkID(asn int) (int, error) {
url := fmt.Sprintf("https://api.peeringdb.com/api/net?asn=%d", asn)
resp, err := http.Get(url)
if err != nil {
return 0, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
var netResp NetResponse
if err := json.Unmarshal(body, &netResp); err != nil {
return 0, err
}
if len(netResp.Data) == 0 {
return 0, fmt.Errorf("ASN %d not found in PeeringDB", asn)
}
return netResp.Data[0].ID, nil
}
func getPeeringLocations(netID int) ([]Netixlan, error) {
url := fmt.Sprintf("https://api.peeringdb.com/api/netixlan?net_id=%d", netID)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var nixResp NetixlanResponse
if err := json.Unmarshal(body, &nixResp); err != nil {
return nil, err
}
return nixResp.Data, nil
}