|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "mokapi/runtime" |
| 7 | + "reflect" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/dop251/goja" |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | + log "github.com/sirupsen/logrus" |
| 14 | +) |
| 15 | + |
| 16 | +//go:embed run.ts |
| 17 | +var types string |
| 18 | + |
| 19 | +type RunInput struct { |
| 20 | + Code string `json:"code"` |
| 21 | +} |
| 22 | + |
| 23 | +type RunOutput struct { |
| 24 | + Result any `json:"result"` |
| 25 | +} |
| 26 | + |
| 27 | +func (s *Service) registerRunTool(server *mcp.Server) { |
| 28 | + inputSchema := map[string]any{ |
| 29 | + "type": "object", |
| 30 | + "properties": map[string]any{ |
| 31 | + "code": map[string]any{ |
| 32 | + "type": "string", |
| 33 | + "description": "JavaScript code to execute in the Mokapi runtime. The last expression is returned as the result.", |
| 34 | + }, |
| 35 | + }, |
| 36 | + "required": []string{"code"}, |
| 37 | + } |
| 38 | + |
| 39 | + outputSchema := map[string]any{ |
| 40 | + "type": "object", |
| 41 | + "properties": map[string]any{ |
| 42 | + "result": map[string]any{ |
| 43 | + "description": "The result of the executed code.", |
| 44 | + "nullable": true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + "required": []string{"result"}, |
| 48 | + } |
| 49 | + |
| 50 | + registerTool(server, &mcp.Tool{ |
| 51 | + Name: "mokapi_execute_code", |
| 52 | + Description: `Executes JavaScript code in a sandboxed Mokapi runtime. |
| 53 | +The last expression in the code is returned as the result. |
| 54 | +
|
| 55 | +Important: |
| 56 | +Before writing any code, be sure to read the API definitions at api://execute-types to understand |
| 57 | +the available global objects, functions, and types. |
| 58 | +
|
| 59 | +Use this tool to: |
| 60 | +- Explore mocked APIs (OpenAPI, AsyncAPI, LDAP, Mail) |
| 61 | +- Inspect operations and schemas |
| 62 | +- Invoke API operations directly |
| 63 | +
|
| 64 | +Prefer this tool over retrieving full API specifications, as it returns only the computed result.`, |
| 65 | + InputSchema: inputSchema, |
| 66 | + OutputSchema: outputSchema, |
| 67 | + }, s.GenerateHttpMockResponse) |
| 68 | + |
| 69 | + server.AddResource(&mcp.Resource{ |
| 70 | + URI: "api://execute-types", |
| 71 | + Name: "api-docs", |
| 72 | + }, func(ctx context.Context, request *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) { |
| 73 | + return &mcp.ReadResourceResult{ |
| 74 | + Contents: []*mcp.ResourceContents{ |
| 75 | + { |
| 76 | + URI: "api://types", |
| 77 | + MIMEType: "application/typescript", |
| 78 | + Text: types, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, nil |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func (s *Service) GetRunResponse(_ context.Context, in RunInput) (RunOutput, error) { |
| 86 | + m := newMokapi(s.app) |
| 87 | + r, err := m.run(in.Code) |
| 88 | + if err != nil { |
| 89 | + return RunOutput{}, err |
| 90 | + } |
| 91 | + |
| 92 | + return RunOutput{Result: r}, nil |
| 93 | +} |
| 94 | + |
| 95 | +type mokapi struct { |
| 96 | + app *runtime.App |
| 97 | + vm *goja.Runtime |
| 98 | +} |
| 99 | + |
| 100 | +func newMokapi(app *runtime.App) *mokapi { |
| 101 | + vm := goja.New() |
| 102 | + vm.SetFieldNameMapper(&customFieldNameMapper{}) |
| 103 | + return &mokapi{app: app, vm: vm} |
| 104 | +} |
| 105 | + |
| 106 | +func (m *mokapi) run(code string) (any, error) { |
| 107 | + obj := m.vm.NewObject() |
| 108 | + m.init(obj) |
| 109 | + _ = m.vm.Set("mokapi", obj) |
| 110 | + v, err := m.vm.RunString(code) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + return v.Export(), nil |
| 115 | +} |
| 116 | + |
| 117 | +type ApiSummary struct { |
| 118 | + Name string `json:"name"` |
| 119 | + Type string `json:"type"` |
| 120 | +} |
| 121 | + |
| 122 | +func (m *mokapi) init(obj *goja.Object) { |
| 123 | + _ = obj.Set("getApis", m.getApis) |
| 124 | + _ = obj.Set("getApi", m.getApi) |
| 125 | +} |
| 126 | + |
| 127 | +func (m *mokapi) getApis() []ApiSummary { |
| 128 | + var result []ApiSummary |
| 129 | + for _, api := range m.app.ListHttp() { |
| 130 | + if api.Info.Name == "" { |
| 131 | + log.Warnf("mcp tool mokapi_get_api_spec: skip empty HTTTP API name") |
| 132 | + continue |
| 133 | + } |
| 134 | + result = append(result, ApiSummary{ |
| 135 | + Name: api.Info.Name, |
| 136 | + Type: "http", |
| 137 | + }) |
| 138 | + } |
| 139 | + slices.SortStableFunc(result, func(a, b ApiSummary) int { |
| 140 | + return strings.Compare(a.Name, b.Name) |
| 141 | + }) |
| 142 | + return result |
| 143 | +} |
| 144 | + |
| 145 | +func (m *mokapi) getApi(name string) any { |
| 146 | + for _, api := range m.app.ListHttp() { |
| 147 | + if api.Info.Name == name { |
| 148 | + return &OpenAPI{ |
| 149 | + Name: name, |
| 150 | + Type: "http", |
| 151 | + info: api, |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +type OpenAPI struct { |
| 159 | + Name string `json:"name"` |
| 160 | + Type string `json:"type"` |
| 161 | + |
| 162 | + info *runtime.HttpInfo |
| 163 | +} |
| 164 | + |
| 165 | +type OperationSummary struct { |
| 166 | + Method string `json:"method"` |
| 167 | + Path string `json:"path"` |
| 168 | + Summary string `json:"summary"` |
| 169 | +} |
| 170 | + |
| 171 | +type OperationDetails struct { |
| 172 | + OperationId string `json:"operationId"` |
| 173 | + Method string `json:"method"` |
| 174 | + Path string `json:"path"` |
| 175 | + Summary string `json:"summary"` |
| 176 | + Description string `json:"description"` |
| 177 | +} |
| 178 | + |
| 179 | +func (o *OpenAPI) GetOperations() []OperationSummary { |
| 180 | + var result []OperationSummary |
| 181 | + for _, p := range o.info.Paths { |
| 182 | + if p.Value == nil { |
| 183 | + continue |
| 184 | + } |
| 185 | + for method, op := range p.Value.Operations() { |
| 186 | + summary := op.Summary |
| 187 | + if summary == "" { |
| 188 | + summary = p.Value.Summary |
| 189 | + } |
| 190 | + result = append(result, OperationSummary{ |
| 191 | + Method: method, |
| 192 | + Path: p.Value.Path, |
| 193 | + Summary: summary, |
| 194 | + }) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + slices.SortStableFunc(result, func(a, b OperationSummary) int { |
| 199 | + c := strings.Compare(a.Path, b.Path) |
| 200 | + if c != 0 { |
| 201 | + return c |
| 202 | + } |
| 203 | + return strings.Compare(a.Method, b.Method) |
| 204 | + }) |
| 205 | + |
| 206 | + return result |
| 207 | +} |
| 208 | + |
| 209 | +func (o *OpenAPI) GetOperationDetails(path, method string) *OperationDetails { |
| 210 | + for _, p := range o.info.Paths { |
| 211 | + if p.Value == nil || p.Value.Path != path { |
| 212 | + continue |
| 213 | + } |
| 214 | + op := p.Value.Operation(method) |
| 215 | + if op == nil { |
| 216 | + continue |
| 217 | + } |
| 218 | + return &OperationDetails{ |
| 219 | + OperationId: op.OperationId, |
| 220 | + Method: method, |
| 221 | + Path: p.Value.Path, |
| 222 | + Summary: op.Summary, |
| 223 | + Description: op.Description, |
| 224 | + } |
| 225 | + } |
| 226 | + return nil |
| 227 | +} |
| 228 | + |
| 229 | +type customFieldNameMapper struct { |
| 230 | +} |
| 231 | + |
| 232 | +func (cfm customFieldNameMapper) FieldName(_ reflect.Type, f reflect.StructField) string { |
| 233 | + tag := f.Tag.Get("json") |
| 234 | + if len(tag) == 0 { |
| 235 | + return uncapitalize(f.Name) |
| 236 | + } |
| 237 | + if idx := strings.IndexByte(tag, ','); idx != -1 { |
| 238 | + tag = tag[:idx] |
| 239 | + } |
| 240 | + |
| 241 | + return tag |
| 242 | +} |
| 243 | + |
| 244 | +func (cfm customFieldNameMapper) MethodName(_ reflect.Type, m reflect.Method) string { |
| 245 | + return uncapitalize(m.Name) |
| 246 | +} |
| 247 | + |
| 248 | +func uncapitalize(s string) string { |
| 249 | + return strings.ToLower(s[0:1]) + s[1:] |
| 250 | +} |
0 commit comments