-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
173 lines (149 loc) · 4.68 KB
/
server.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
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
package mcp
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"github.com/sourcegraph/jsonrpc2"
"golang.org/x/time/rate"
)
const SupportedProtocolVersion = "2024-11-05"
type ToolDefinition struct {
Metadata Tool
Execute func(CallToolRequestParams) (CallToolResult, error)
RateLimit *rate.Limiter
}
type handler struct {
serverInfo Implementation
toolMetadata []Tool
tools map[string]ToolDefinition
}
type Server struct {
handler *handler
}
func NewServer(serverInfo Implementation, tools []ToolDefinition) *Server {
toolMetadata := make([]Tool, 0, len(tools))
toolFuncs := make(map[string]ToolDefinition, len(tools))
for _, t := range tools {
toolMetadata = append(toolMetadata, t.Metadata)
toolFuncs[t.Metadata.Name] = t
}
return &Server{handler: &handler{serverInfo: serverInfo, toolMetadata: toolMetadata, tools: toolFuncs}}
}
func (s *Server) Serve() {
stream := jsonrpc2.NewPlainObjectStream(&stdinStdoutReadWriter{})
conn := jsonrpc2.NewConn(context.Background(), stream, s.handler)
<-conn.DisconnectNotify()
}
func (h *handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
switch req.Method {
case "initialize":
h.handleInitialize(ctx, conn, req)
case "notifications/initialized":
case "ping":
h.replyWithResult(ctx, conn, req, struct{}{})
case "tools/list":
h.handleListTools(ctx, conn, req)
case "tools/call":
h.handleToolCall(ctx, conn, req)
default:
h.replyWithJSONRPCError(ctx, conn, req, &jsonrpc2.Error{
Code: jsonrpc2.CodeMethodNotFound,
Message: "Method not found",
})
}
}
func (h *handler) handleInitialize(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
var unsupported bool
response := InitializeResult{
ProtocolVersion: SupportedProtocolVersion,
ServerInfo: h.serverInfo,
Capabilities: ServerCapabilities{
Experimental: map[string]map[string]any{},
Tools: &ServerCapabilitiesTools{
ListChanged: &unsupported,
},
},
}
h.replyWithResult(ctx, conn, req, response)
}
func (h *handler) handleListTools(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
var params ListToolsRequestParams
if req.Params != nil {
// cursors are not supported so any cursor provided is invalid
if err := json.Unmarshal(*req.Params, ¶ms); err != nil || params.Cursor != nil {
h.replyWithJSONRPCError(ctx, conn, req, &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: "Invalid params",
})
return
}
}
h.replyWithResult(ctx, conn, req, ListToolsResult{Tools: h.toolMetadata})
}
func (h *handler) handleToolCall(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
var params CallToolRequestParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
h.replyWithJSONRPCError(ctx, conn, req, &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: "Invalid params",
})
return
}
t, ok := h.tools[params.Name]
if !ok {
h.replyWithJSONRPCError(ctx, conn, req, &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: fmt.Sprintf("Unknown tool: %s", params.Name),
})
return
}
if !t.RateLimit.Allow() {
h.replyWithToolError(ctx, conn, req, "rate limit exceeded")
return
}
for _, rqd := range t.Metadata.InputSchema.Required {
if _, ok := params.Arguments[rqd]; !ok {
h.replyWithJSONRPCError(ctx, conn, req, &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: "Invalid params",
})
return
}
}
response, err := t.Execute(params)
if err != nil {
h.replyWithToolError(ctx, conn, req, err.Error())
return
}
h.replyWithResult(ctx, conn, req, response)
}
func (h *handler) replyWithJSONRPCError(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request, rpcErr *jsonrpc2.Error) {
if err := conn.ReplyWithError(ctx, req.ID, rpcErr); err != nil {
slog.Error("problem replying with error", "method", req.Method, "error", err)
}
}
func (h *handler) replyWithResult(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request, result any) {
if err := conn.Reply(ctx, req.ID, result); err != nil {
slog.Error("problem replying with result", "method", req.Method, "error", err)
}
}
func (h *handler) replyWithToolError(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request, errMsg string) {
errorOccurred := true
result := CallToolResult{
Content: []any{TextContent{Type: "text", Text: errMsg}},
IsError: &errorOccurred,
}
h.replyWithResult(ctx, conn, req, result)
}
type stdinStdoutReadWriter struct{}
func (s stdinStdoutReadWriter) Read(p []byte) (int, error) {
return os.Stdin.Read(p)
}
func (s stdinStdoutReadWriter) Write(p []byte) (int, error) {
return os.Stdout.Write(p)
}
func (s stdinStdoutReadWriter) Close() error {
return nil
}