File size: 4,480 Bytes
07af8f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# OpenAPI Support in MCPHub

MCPHub now supports OpenAPI 3.1.1 servers as a new server type, allowing you to integrate REST APIs directly into your MCP workflow.

## Features

- βœ… **Full OpenAPI 3.1.1 Support**: Load and parse OpenAPI specifications
- βœ… **Multiple Security Types**: None, API Key, HTTP Authentication, OAuth 2.0, OpenID Connect
- βœ… **Dynamic Tool Generation**: Automatically creates MCP tools from OpenAPI operations
- βœ… **Type Safety**: Full TypeScript support with proper type definitions
- βœ… **Frontend Integration**: Easy-to-use forms for configuring OpenAPI servers
- βœ… **Internationalization**: Support for English and Chinese languages

## Configuration

### Basic Configuration

```json
{
  "type": "openapi",
  "openapi": {
    "url": "https://api.example.com/v1/openapi.json",
    "version": "3.1.0",
    "security": {
      "type": "none"
    }
  }
}
```

### With API Key Authentication

```json
{
  "type": "openapi",
  "openapi": {
    "url": "https://api.example.com/v1/openapi.json",
    "version": "3.1.0",
    "security": {
      "type": "apiKey",
      "apiKey": {
        "name": "X-API-Key",
        "in": "header",
        "value": "your-api-key-here"
      }
    }
  },
  "headers": {
    "Accept": "application/json"
  }
}
```

### With HTTP Bearer Authentication

```json
{
  "type": "openapi",
  "openapi": {
    "url": "https://api.example.com/v1/openapi.json",
    "version": "3.1.0",
    "security": {
      "type": "http",
      "http": {
        "scheme": "bearer",
        "credentials": "your-bearer-token-here"
      }
    }
  }
}
```

## Supported Security Types

1. **None**: No authentication required
2. **API Key**: API key in header or query parameter
3. **HTTP**: Basic, Bearer, or Digest authentication
4. **OAuth 2.0**: OAuth 2.0 access tokens
5. **OpenID Connect**: OpenID Connect ID tokens

## How It Works

1. **Specification Loading**: The OpenAPI client fetches and parses the OpenAPI specification
2. **Tool Generation**: Each operation in the spec becomes an MCP tool
3. **Request Handling**: Tools handle parameter validation and API calls
4. **Response Processing**: API responses are returned as tool results

## Frontend Usage

1. Navigate to the Servers page
2. Click "Add Server"
3. Select "OpenAPI" as the server type
4. Enter the OpenAPI specification URL
5. Configure security settings if needed
6. Add any additional headers
7. Save the configuration

## Testing

You can test the OpenAPI integration using the provided test scripts:

```bash
# Test OpenAPI client directly
npx tsx test-openapi.ts

# Test full integration
npx tsx test-integration.ts
```

## Example: Swagger Petstore

The Swagger Petstore API is a perfect example for testing:

```json
{
  "type": "openapi",
  "openapi": {
    "url": "https://petstore3.swagger.io/api/v3/openapi.json",
    "version": "3.1.0",
    "security": {
      "type": "none"
    }
  }
}
```

This will create tools like:

- `addPet`: Add a new pet to the store
- `findPetsByStatus`: Find pets by status
- `getPetById`: Find pet by ID
- And many more...

## Error Handling

The OpenAPI client includes comprehensive error handling:

- Network errors are properly caught and reported
- Invalid specifications are rejected with clear error messages
- API errors include response status and body information
- Type validation ensures proper parameter handling

## Limitations

- Only supports OpenAPI 3.x specifications (3.0.0 and above)
- Complex authentication flows (like OAuth 2.0 authorization code flow) require manual token management
- Large specifications may take time to parse initially
- Some advanced OpenAPI features may not be fully supported

## Contributing

To add new features or fix bugs in the OpenAPI integration:

1. Backend types: `src/types/index.ts`
2. OpenAPI client: `src/clients/openapi.ts`
3. Service integration: `src/services/mcpService.ts`
4. Frontend forms: `frontend/src/components/ServerForm.tsx`
5. Internationalization: `frontend/src/locales/`

## Troubleshooting

**Q: My OpenAPI server won't connect**
A: Check that the specification URL is accessible and returns valid JSON/YAML

**Q: Tools aren't showing up**
A: Verify that your OpenAPI specification includes valid operations with required fields

**Q: Authentication isn't working**
A: Double-check your security configuration matches the API's requirements

**Q: Getting CORS errors**
A: The API server needs to allow CORS requests from your MCPHub domain