-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle.go
More file actions
191 lines (176 loc) · 4.1 KB
/
Copy pathhandle.go
File metadata and controls
191 lines (176 loc) · 4.1 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
package mcp
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
)
func (s *Server) Handle(method string, body []byte, ctx context.Context) Response {
if ctx == nil {
ctx = context.Background()
}
switch method {
case "GET":
return Response{
Status: 200,
ContentType: "plain/text",
Payload: []byte("Ok"),
}
case "POST", "PATCH", "PUT":
return s.handleStreamableHttpRequest(body, ctx)
default:
return Response{
Status: 404,
ContentType: "plain/text",
Payload: []byte("404 Not found"),
}
}
}
func (s *Server) handleStreamableHttpRequest(body []byte, ctx context.Context) Response {
body = bytes.TrimSpace(body)
if len(body) == 0 {
return emptyResponse
}
requests := []Req{}
var err error
if body[0] == '{' {
// This is a single request
var req Req
err = json.Unmarshal(body, &req)
requests = []Req{req}
} else {
// This is a batch request
err = json.Unmarshal(body, &requests)
}
if err != nil {
return errorResponse(err)
}
resp := []any{}
outer:
for _, req := range requests {
addResp := func(result any) Res {
res := Res{
Jsonrpc: Jsonrpc,
Id: req.Id,
Result: result,
}
resp = append(resp, res)
return res
}
addErr := func(err error) Res {
res := Res{
Jsonrpc: Jsonrpc,
Id: req.Id,
Error: &Error{
Message: err.Error(),
},
}
resp = append(resp, res)
return res
}
if ctx.Err() != nil {
addErr(ctx.Err())
continue
}
switch req.Method {
case "initialize":
return jsonResponse(addResp(M{
"protocolVersion": protocolVersion,
"capabilities": M{
"tools": M{
// We do not actually support this, but this might be required by claude??
"listChanged": true,
},
},
"serverInfo": M{
"name": "test mcp",
"version": "1.0.0",
},
}))
case "ping":
addResp(M{})
case "notifications/initialized":
// FIXME
resp = append(resp, Req{
Jsonrpc: "2.0",
Method: "notifications/tools/list_changed",
})
case "notifications/cancelled":
// FIXME: https://modelcontextprotocol.io/specification/2025-03-26/basic/utilities/cancellation
case "tools/list":
if s.tools == nil {
s.tools = []internalToolT{}
}
addResp(M{
"tools": s.tools,
})
case "tools/call":
var params struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
}
err := json.Unmarshal(req.Params, ¶ms)
if err != nil {
addErr(err)
continue
}
var pickedTool *internalToolT
for _, tool := range s.tools {
if tool.Name == params.Name {
pickedTool = &tool
break
}
}
if pickedTool == nil {
addErr(errors.New("no tool named " + params.Name))
continue
}
out, err := pickedTool.Handler(params.Arguments, ctx)
if err != nil {
addResp(newToolResponse(true, err.Error()))
continue
}
outReflection := reflect.ValueOf(out)
for outReflection.Kind() == reflect.Ptr {
if outReflection.IsNil() {
addResp(newToolResponse(false, "null"))
continue outer
}
outReflection = outReflection.Elem()
}
switch outReflection.Kind() {
case reflect.String:
addResp(newToolResponse(false, outReflection.String()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
addResp(newToolResponse(false, fmt.Sprintf("%v", out)))
case reflect.Chan, reflect.Func, reflect.Pointer, reflect.Uintptr:
addResp(newToolResponse(true, "returned value that cannot be converted to a string"))
default:
outJson, err := json.Marshal(out)
if err != nil {
addResp(newToolResponse(true, err.Error()))
} else {
addResp(newToolResponse(false, string(outJson)))
}
}
case "prompts/list":
addResp(M{
"prompts": []M{},
})
case "resources/list":
addResp(M{
"resources": []M{},
})
}
}
var toMarshall any = resp
switch len(resp) {
case 0:
return emptyResponse
case 1:
toMarshall = resp[0]
}
return jsonResponse(toMarshall)
}