|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "mokapi/media" |
| 7 | + "mokapi/providers/openapi" |
| 8 | + "mokapi/providers/openapi/schema" |
| 9 | + "mokapi/schema/json/generator" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | +) |
| 14 | + |
| 15 | +type GenerateHttpResponseInput struct { |
| 16 | + ApiName string `json:"apiName"` |
| 17 | + Path string `json:"path"` |
| 18 | + Method string `json:"method"` |
| 19 | + StatusCode int `json:"statusCode"` |
| 20 | + ContentType string `json:"contentType,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +type GenerateHttpResponseOutput struct { |
| 24 | + StatusCode int `json:"statusCode"` |
| 25 | + Data any `json:"data"` |
| 26 | + Headers map[string]any `json:"headers"` |
| 27 | +} |
| 28 | + |
| 29 | +func (s *Service) registerGenerateHttpResponseTool(server *mcp.Server) { |
| 30 | + inputSchema := map[string]any{ |
| 31 | + "type": "object", |
| 32 | + "properties": map[string]any{ |
| 33 | + "apiName": map[string]any{ |
| 34 | + "type": "string", |
| 35 | + "description": "The exact name of the API as returned by 'get_api_list'", |
| 36 | + }, |
| 37 | + "path": map[string]any{ |
| 38 | + "type": "string", |
| 39 | + "description": "The path template of the endpoint (e.g. /pets/{id})", |
| 40 | + }, |
| 41 | + "method": map[string]any{ |
| 42 | + "type": "string", |
| 43 | + "description": "The HTTP method (GET, POST, PUT, DELETE, etc.)", |
| 44 | + }, |
| 45 | + "statusCode": map[string]any{ |
| 46 | + "type": "integer", |
| 47 | + "description": "The HTTP status code to generate the response for", |
| 48 | + }, |
| 49 | + "contentType": map[string]any{ |
| 50 | + "type": "string", |
| 51 | + "description": `The HTTP content type of the response body. Optional: |
| 52 | + If provided, this content type is used. |
| 53 | + If the endpoint has only one content type, it will be used automatically. |
| 54 | + Otherwise defaults to 'application/json'`, |
| 55 | + "default": "application/json", |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + outputSchema := map[string]any{ |
| 61 | + "type": "object", |
| 62 | + "properties": map[string]any{ |
| 63 | + "statusCode": map[string]any{ |
| 64 | + "type": "integer", |
| 65 | + "description": "HTTP status code for the response", |
| 66 | + }, |
| 67 | + "headers": map[string]any{ |
| 68 | + "type": "object", |
| 69 | + "description": "response headers defined by the API specification", |
| 70 | + }, |
| 71 | + "data": map[string]any{ |
| 72 | + "type": "any", |
| 73 | + "description": "structured response body that matches the OpenAPI schema", |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + registerTool(server, &mcp.Tool{ |
| 79 | + Name: "generate_http_response", |
| 80 | + Description: `Generate a valid HTTP response for a specific API endpoint. |
| 81 | +
|
| 82 | +This tool returns a complete response object that already conforms to the OpenAPI specification. |
| 83 | +The generated data strictly matches the response schema, including all required fields and correct types. |
| 84 | +
|
| 85 | +Use this tool when writing HTTP mock scripts instead of manually constructing response bodies. |
| 86 | +
|
| 87 | +The returned object can be used directly in the mock script: |
| 88 | +
|
| 89 | +on('http', (request) => { |
| 90 | +return GENERATED_RESPONSE |
| 91 | +}) |
| 92 | +
|
| 93 | +The "data" field is preferred and will be automatically encoded based on the API specification. |
| 94 | +The "body" field is not returned by this tool and should only be used for raw responses. |
| 95 | +
|
| 96 | +Rules: |
| 97 | +- Do NOT manually construct complex response objects |
| 98 | +- Always prefer this tool to ensure schema-correct responses |
| 99 | +- The "data" field contains structured data and will be encoded automatically |
| 100 | +- The "statusCode" and "headers" are already set correctly |
| 101 | +
|
| 102 | +Example: |
| 103 | +Generated response: |
| 104 | +{ |
| 105 | + "statusCode": 200, |
| 106 | + "headers": { |
| 107 | + "Content-Type": "application/json" |
| 108 | + }, |
| 109 | + "data": { |
| 110 | + "id": 1, |
| 111 | + "name": "dog", |
| 112 | + "status": "available" |
| 113 | + } |
| 114 | +} |
| 115 | +
|
| 116 | +Usage in mock script: |
| 117 | +on('http', (request, response) => { |
| 118 | + response.statusCode = 200 |
| 119 | + response.headers["Content-Type"] = "application/json" |
| 120 | + response.data = { |
| 121 | + "id": 1, |
| 122 | + "name": "dog", |
| 123 | + "status": "available" |
| 124 | + } |
| 125 | +}) |
| 126 | +`, |
| 127 | + InputSchema: inputSchema, |
| 128 | + OutputSchema: outputSchema, |
| 129 | + }, s.GetHttpResponseSchema) |
| 130 | +} |
| 131 | + |
| 132 | +func (s *Service) GenerateHttpResponse(_ context.Context, in GenerateHttpResponseInput) (GenerateHttpResponseOutput, error) { |
| 133 | + result := GenerateHttpResponseOutput{StatusCode: in.StatusCode, Headers: make(map[string]any)} |
| 134 | + |
| 135 | + info := s.app.GetHttp(in.ApiName) |
| 136 | + if info == nil { |
| 137 | + return result, fmt.Errorf("http api not found") |
| 138 | + } |
| 139 | + p, ok := info.Paths[in.Path] |
| 140 | + if !ok || p.Value == nil { |
| 141 | + return result, fmt.Errorf("path not found") |
| 142 | + } |
| 143 | + o := p.Value.Operation(in.Method) |
| 144 | + if o == nil { |
| 145 | + return result, fmt.Errorf("operation not found") |
| 146 | + } |
| 147 | + r := o.Responses.GetResponse(in.StatusCode) |
| 148 | + if r == nil { |
| 149 | + return result, fmt.Errorf("response not found") |
| 150 | + } |
| 151 | + |
| 152 | + n := len(r.Content) |
| 153 | + if n == 0 { |
| 154 | + return result, fmt.Errorf("response has no content") |
| 155 | + } |
| 156 | + |
| 157 | + var mt *openapi.MediaType |
| 158 | + if n == 1 && in.ContentType == "" { |
| 159 | + for _, v := range r.Content { |
| 160 | + mt = v |
| 161 | + break |
| 162 | + } |
| 163 | + } else { |
| 164 | + contentType := "application/json" |
| 165 | + if in.ContentType != "" { |
| 166 | + contentType = in.ContentType |
| 167 | + } |
| 168 | + accept := media.ParseContentType(contentType) |
| 169 | + for k, v := range r.Content { |
| 170 | + key := media.ParseContentType(k) |
| 171 | + if accept.Match(key) { |
| 172 | + mt = v |
| 173 | + break |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if mt == nil { |
| 179 | + return result, fmt.Errorf("response not found") |
| 180 | + } |
| 181 | + |
| 182 | + segments := strings.Split(p.Value.Path, "/") |
| 183 | + var names []string |
| 184 | + for _, seg := range segments[1:] { |
| 185 | + if !strings.HasPrefix(seg, "{") { |
| 186 | + names = append(names, seg) |
| 187 | + } |
| 188 | + } |
| 189 | + req := generator.NewRequest( |
| 190 | + names, |
| 191 | + schema.ConvertToJsonSchema(mt.Schema), |
| 192 | + nil, |
| 193 | + ) |
| 194 | + |
| 195 | + var err error |
| 196 | + result.Data, err = generator.New(req) |
| 197 | + if err != nil { |
| 198 | + return result, err |
| 199 | + } |
| 200 | + |
| 201 | + return result, nil |
| 202 | +} |
0 commit comments