-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmcp.go
More file actions
115 lines (96 loc) · 2.66 KB
/
mcp.go
File metadata and controls
115 lines (96 loc) · 2.66 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
package main
import (
"context"
"encoding/json"
"github.com/c0tton-fluff/sentinelone-mcp-server/tools"
)
// JSON-RPC 2.0 types for MCP protocol.
type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
type JSONRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id,omitempty"`
Result any `json:"result,omitempty"`
Error *RPCError `json:"error,omitempty"`
}
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}
// MCP-specific types.
type InitializeResult struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities Capabilities `json:"capabilities"`
ServerInfo ServerInfo `json:"serverInfo"`
}
type Capabilities struct {
Tools *struct{} `json:"tools,omitempty"`
}
type ServerInfo struct {
Name string `json:"name"`
Version string `json:"version"`
}
type ToolsListResult struct {
Tools []tools.ToolDef `json:"tools"`
}
type ToolsCallParams struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
}
// HandleRequest dispatches a JSON-RPC request to the appropriate handler.
func HandleRequest(ctx context.Context, req JSONRPCRequest) *JSONRPCResponse {
switch req.Method {
case "initialize":
return &JSONRPCResponse{
JSONRPC: "2.0",
ID: req.ID,
Result: InitializeResult{
ProtocolVersion: "2024-11-05",
Capabilities: Capabilities{Tools: &struct{}{}},
ServerInfo: ServerInfo{Name: "sentinelone", Version: "1.0.0"},
},
}
case "notifications/initialized":
return nil
case "ping":
return &JSONRPCResponse{
JSONRPC: "2.0",
ID: req.ID,
Result: map[string]any{},
}
case "tools/list":
return &JSONRPCResponse{
JSONRPC: "2.0",
ID: req.ID,
Result: ToolsListResult{Tools: tools.AllTools()},
}
case "tools/call":
var params ToolsCallParams
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
return errorResponse(req.ID, -32602, "invalid params")
}
result := tools.DispatchTool(ctx, params.Name, params.Arguments)
return &JSONRPCResponse{
JSONRPC: "2.0",
ID: req.ID,
Result: result,
}
default:
// Silently ignore unknown notifications (method without id).
if req.ID == nil {
return nil
}
return errorResponse(req.ID, -32601, "method not found")
}
}
func errorResponse(id json.RawMessage, code int, msg string) *JSONRPCResponse {
return &JSONRPCResponse{
JSONRPC: "2.0",
ID: id,
Error: &RPCError{Code: code, Message: msg},
}
}