|
| 1 | +# Bulk Import Tools |
| 2 | + |
| 3 | +The MCP Gateway provides a bulk import endpoint for efficiently loading multiple tools in a single request, perfect for migrations, environment setup, and team onboarding. |
| 4 | + |
| 5 | +!!! info "Feature Flag Required" |
| 6 | + This feature is controlled by the `MCPGATEWAY_BULK_IMPORT_ENABLED` environment variable. |
| 7 | + Default: `true` (enabled). Set to `false` to disable this endpoint. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 🚀 Overview |
| 12 | + |
| 13 | +The `/admin/tools/import` endpoint allows you to register multiple tools at once, providing: |
| 14 | + |
| 15 | +- **Per-item validation** - One invalid tool won't fail the entire batch |
| 16 | +- **Detailed reporting** - Know exactly which tools succeeded or failed |
| 17 | +- **Rate limiting** - Protected against abuse (10 requests/minute) |
| 18 | +- **Batch size limits** - Maximum 200 tools per request |
| 19 | +- **Multiple input formats** - JSON payload or form data |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 📡 API Endpoint |
| 24 | + |
| 25 | +### Request |
| 26 | + |
| 27 | +```http |
| 28 | +POST /admin/tools/import |
| 29 | +Authorization: Bearer <jwt_token> |
| 30 | +Content-Type: application/json |
| 31 | +``` |
| 32 | + |
| 33 | +### Payload Structure |
| 34 | + |
| 35 | +```json |
| 36 | +[ |
| 37 | + { |
| 38 | + "name": "tool_name", |
| 39 | + "url": "https://api.example.com/endpoint", |
| 40 | + "integration_type": "REST", |
| 41 | + "request_type": "GET", |
| 42 | + "description": "Optional description", |
| 43 | + "headers": { |
| 44 | + "X-API-Key": "optional-key" |
| 45 | + }, |
| 46 | + "input_schema": { |
| 47 | + "type": "object", |
| 48 | + "properties": { |
| 49 | + "param": {"type": "string"} |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + // ... more tools |
| 54 | +] |
| 55 | +``` |
| 56 | + |
| 57 | +### Response |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "success": true, |
| 62 | + "created_count": 2, |
| 63 | + "failed_count": 1, |
| 64 | + "created": [ |
| 65 | + {"index": 0, "name": "tool1"}, |
| 66 | + {"index": 1, "name": "tool2"} |
| 67 | + ], |
| 68 | + "errors": [ |
| 69 | + { |
| 70 | + "index": 2, |
| 71 | + "name": "tool3", |
| 72 | + "error": { |
| 73 | + "message": "Validation failed: Invalid request_type", |
| 74 | + "details": [...] |
| 75 | + } |
| 76 | + } |
| 77 | + ] |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## 🛠️ Usage Examples |
| 84 | + |
| 85 | +### Using cURL |
| 86 | + |
| 87 | +```bash |
| 88 | +# Generate JWT token |
| 89 | +TOKEN=$(python3 -m mcpgateway.utils.create_jwt_token \ |
| 90 | + --username admin --exp 60 --secret $JWT_SECRET_KEY) |
| 91 | + |
| 92 | +# Import tools from file |
| 93 | +curl -X POST http://localhost:4444/admin/tools/import \ |
| 94 | + -H "Authorization: Bearer $TOKEN" \ |
| 95 | + -H "Content-Type: application/json" \ |
| 96 | + --data-binary @tools.json |
| 97 | +``` |
| 98 | + |
| 99 | +### Using Python |
| 100 | + |
| 101 | +```python |
| 102 | +import requests |
| 103 | +import json |
| 104 | + |
| 105 | +# Your tools data |
| 106 | +tools = [ |
| 107 | + { |
| 108 | + "name": "list_users", |
| 109 | + "url": "https://api.example.com/users", |
| 110 | + "integration_type": "REST", |
| 111 | + "request_type": "GET" |
| 112 | + }, |
| 113 | + { |
| 114 | + "name": "create_user", |
| 115 | + "url": "https://api.example.com/users", |
| 116 | + "integration_type": "REST", |
| 117 | + "request_type": "POST", |
| 118 | + "input_schema": { |
| 119 | + "type": "object", |
| 120 | + "properties": { |
| 121 | + "body": {"type": "object"} |
| 122 | + }, |
| 123 | + "required": ["body"] |
| 124 | + } |
| 125 | + } |
| 126 | +] |
| 127 | + |
| 128 | +# Make the request |
| 129 | +response = requests.post( |
| 130 | + "http://localhost:4444/admin/tools/import", |
| 131 | + headers={ |
| 132 | + "Authorization": f"Bearer {token}", |
| 133 | + "Content-Type": "application/json" |
| 134 | + }, |
| 135 | + json=tools |
| 136 | +) |
| 137 | + |
| 138 | +result = response.json() |
| 139 | +print(f"Created: {result['created_count']}, Failed: {result['failed_count']}") |
| 140 | +``` |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## 📋 Tool Schema Reference |
| 145 | + |
| 146 | +Each tool in the array must follow this schema: |
| 147 | + |
| 148 | +| Field | Type | Required | Description | |
| 149 | +|-------|------|----------|-------------| |
| 150 | +| `name` | string | ✅ | Unique tool identifier | |
| 151 | +| `url` | string | ✅ | Tool endpoint URL | |
| 152 | +| `integration_type` | string | ✅ | Must be "REST" or "MCP" | |
| 153 | +| `request_type` | string | ✅ | HTTP method: GET, POST, PUT, DELETE, PATCH, SSE, STDIO, STREAMABLEHTTP | |
| 154 | +| `description` | string | ❌ | Human-readable description | |
| 155 | +| `headers` | object | ❌ | HTTP headers to include | |
| 156 | +| `input_schema` | object | ❌ | JSON Schema for input validation | |
| 157 | +| `output_schema` | object | ❌ | JSON Schema for output validation | |
| 158 | +| `tags` | array | ❌ | List of tags for categorization | |
| 159 | +| `rate_limit` | integer | ❌ | Max requests per minute | |
| 160 | +| `timeout` | integer | ❌ | Request timeout in seconds | |
| 161 | +| `auth_type` | string | ❌ | Authentication type: "basic", "bearer", "api_key" | |
| 162 | +| `auth_value` | string | ❌ | Authentication credential | |
| 163 | + |
| 164 | +--- |
| 165 | + |
| 166 | +## ⚠️ Error Handling |
| 167 | + |
| 168 | +The endpoint provides detailed error information for each failed tool: |
| 169 | + |
| 170 | +### Validation Errors |
| 171 | +```json |
| 172 | +{ |
| 173 | + "index": 1, |
| 174 | + "name": "invalid_tool", |
| 175 | + "error": { |
| 176 | + "message": "Validation failed: Invalid request_type", |
| 177 | + "details": [ |
| 178 | + { |
| 179 | + "field": "request_type", |
| 180 | + "message": "Must be one of: GET, POST, PUT, DELETE, PATCH" |
| 181 | + } |
| 182 | + ] |
| 183 | + } |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +### Duplicate Tools |
| 188 | +```json |
| 189 | +{ |
| 190 | + "index": 2, |
| 191 | + "name": "existing_tool", |
| 192 | + "error": { |
| 193 | + "message": "Tool already exists: existing_tool" |
| 194 | + } |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +## 🎯 Best Practices |
| 201 | + |
| 202 | +1. **Validate locally first** - Check your JSON schema before importing |
| 203 | +2. **Use small batches** - Start with 10-20 tools to test your format |
| 204 | +3. **Handle partial success** - Check both created and errors arrays |
| 205 | +4. **Implement retry logic** - For failed items, fix and retry separately |
| 206 | +5. **Monitor rate limits** - Stay under 10 requests per minute |
| 207 | + |
| 208 | +--- |
| 209 | + |
| 210 | +## 🔒 Security Considerations |
| 211 | + |
| 212 | +- **Authentication required** - All requests must include a valid JWT token |
| 213 | +- **Rate limited** - 10 requests per minute per IP address |
| 214 | +- **Size limited** - Maximum 200 tools per request |
| 215 | +- **Audit logged** - All imports are logged with username and timestamp |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +## 🚦 Status Codes |
| 220 | + |
| 221 | +| Code | Meaning | |
| 222 | +|------|---------| |
| 223 | +| `200` | Request processed (check success field for results) | |
| 224 | +| `401` | Authentication required or invalid token | |
| 225 | +| `403` | Feature disabled (MCPGATEWAY_BULK_IMPORT_ENABLED=false) | |
| 226 | +| `413` | Payload too large (>200 tools) | |
| 227 | +| `422` | Invalid request format | |
| 228 | +| `429` | Rate limit exceeded | |
| 229 | +| `500` | Internal server error | |
| 230 | + |
| 231 | +--- |
| 232 | + |
| 233 | +## 💡 Tips |
| 234 | + |
| 235 | +- Use the bulk import for initial setup and migrations |
| 236 | +- Export existing tools first to understand the schema |
| 237 | +- Test with a small subset before importing hundreds of tools |
| 238 | +- Keep your import files in version control for reproducibility |
0 commit comments