swagger_mcp_tool 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +594 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/bin/swagger_mcp_server +45 -0
- data/lib/swagger_mcp_tool/api_client.rb +154 -0
- data/lib/swagger_mcp_tool/auth_handler.rb +82 -0
- data/lib/swagger_mcp_tool/config.rb +143 -0
- data/lib/swagger_mcp_tool/helpers/parameter_processor.rb +84 -0
- data/lib/swagger_mcp_tool/helpers/request.rb +114 -0
- data/lib/swagger_mcp_tool/helpers/request_body_processor.rb +66 -0
- data/lib/swagger_mcp_tool/helpers/swagger_validator.rb +42 -0
- data/lib/swagger_mcp_tool/helpers/tool_builder.rb +43 -0
- data/lib/swagger_mcp_tool/helpers/tool_register.rb +40 -0
- data/lib/swagger_mcp_tool/logging.rb +61 -0
- data/lib/swagger_mcp_tool/server.rb +254 -0
- data/lib/swagger_mcp_tool/swagger_client.rb +116 -0
- data/lib/swagger_mcp_tool/tool_generator.rb +85 -0
- data/lib/swagger_mcp_tool/tool_registry.rb +211 -0
- data/lib/swagger_mcp_tool/version.rb +5 -0
- data/lib/swagger_mcp_tool.rb +21 -0
- metadata +221 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6cf12fe2486b1ca69e692d0ecfdda98dde1184f2a0559ca81ac1f683e1ffb02
|
4
|
+
data.tar.gz: a3c688bab5b1e810e845cd0b143634dfb84f912f4e4504de9bd47a10efc9aee7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a0b2ae03de67f6241e845d7f0f980166ef44ebd945f87add61f15d7ca75e90f2afb5488ecd9f20f27c83a64a11f4b1f88878bad727fb66e7e9805a70acdcc5c2
|
7
|
+
data.tar.gz: c4c1c5896bd5835b5d6304900eb3c7d0c1c530569e90d075359e1858ffddcc1c6bf0c871a3dd23c076de5feed76c578bfb7eeb0c0d1178595fe345a7e2b40a2b
|
data/README.md
ADDED
@@ -0,0 +1,594 @@
|
|
1
|
+
# SwaggerMCPTool
|
2
|
+
|
3
|
+
A Ruby gem that automatically generates MCP (Model Context Protocol) tools from Swagger/OpenAPI specifications. This gem allows you to create both HTTP and STDIO MCP servers that dynamically generate tools based on API endpoints defined in Swagger/OpenAPI documentation.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- **Automatic Tool Generation**: Dynamically creates MCP tools from Swagger/OpenAPI specifications
|
8
|
+
- **Dual Server Support**: Both HTTP server and STDIO server implementations
|
9
|
+
- **Path Parameter Handling**: Properly substitutes path parameters in API endpoints (e.g., `/pet/{petId}`)
|
10
|
+
- **Multiple Server Types**: Supports Puma and other Rack-compatible servers
|
11
|
+
- **Authentication Support**: Token-based authentication with configurable headers
|
12
|
+
- **Comprehensive Logging**: Detailed request/response logging with configurable levels
|
13
|
+
- **Error Handling**: Robust error handling for API failures and edge cases
|
14
|
+
- **User Context**: Passes user context from LLM to API calls
|
15
|
+
- **Easy Configuration**: Simple Ruby-based configuration system
|
16
|
+
- **Command Line Interface**: Built-in CLI for quick server startup
|
17
|
+
|
18
|
+
## Dependencies
|
19
|
+
|
20
|
+
- Ruby 3.2 or higher
|
21
|
+
- Gems:
|
22
|
+
- sinatra
|
23
|
+
- sinatra-contrib
|
24
|
+
- mcp (~> 0.1.0)
|
25
|
+
- json
|
26
|
+
- puma
|
27
|
+
- rackup (~> 2.2)
|
28
|
+
- pry (development)
|
29
|
+
- rake (development)
|
30
|
+
|
31
|
+
## Installation
|
32
|
+
|
33
|
+
### From RubyGems
|
34
|
+
|
35
|
+
```bash
|
36
|
+
gem install swagger_mcp_tool
|
37
|
+
```
|
38
|
+
|
39
|
+
### From Source
|
40
|
+
|
41
|
+
Clone the repository:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
git clone https://github.com/yourusername/swagger_mcp_tool.git
|
45
|
+
cd swagger_mcp_tool
|
46
|
+
```
|
47
|
+
|
48
|
+
Build and install the gem:
|
49
|
+
|
50
|
+
```bash
|
51
|
+
gem build swagger_mcp_tool.gemspec
|
52
|
+
gem install swagger_mcp_tool-0.1.0.gem
|
53
|
+
```
|
54
|
+
|
55
|
+
### Using Bundler
|
56
|
+
|
57
|
+
Add this line to your application's Gemfile:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
gem 'swagger_mcp_tool'
|
61
|
+
```
|
62
|
+
|
63
|
+
And then execute:
|
64
|
+
|
65
|
+
```bash
|
66
|
+
bundle install
|
67
|
+
```
|
68
|
+
|
69
|
+
## Starting the Server
|
70
|
+
|
71
|
+
### Command Line Interface
|
72
|
+
|
73
|
+
The gem provides a command-line executable for quick server startup:
|
74
|
+
|
75
|
+
```bash
|
76
|
+
# Start with default settings (Petstore API)
|
77
|
+
swagger_mcp_server
|
78
|
+
|
79
|
+
# Start with custom port and Swagger URL
|
80
|
+
swagger_mcp_server --port 3001 --swagger-url https://api.example.com/swagger.json
|
81
|
+
|
82
|
+
# Show help
|
83
|
+
swagger_mcp_server --help
|
84
|
+
```
|
85
|
+
|
86
|
+
### STDIO Server (Recommended for MCP Integration)
|
87
|
+
|
88
|
+
Use the provided example for direct integration with MCP clients like Vscode IDE:
|
89
|
+
|
90
|
+
**File: `examples/mcp_stdio_server.rb`**
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
#!/usr/bin/env ruby
|
94
|
+
|
95
|
+
gem install swagger_mcp_tool
|
96
|
+
# Load swagger_mcp_tool modules
|
97
|
+
require 'swagger_mcp_tool'
|
98
|
+
|
99
|
+
class WorkingSwaggerMCPStdioServer
|
100
|
+
include SwaggerMCPTool::Logging
|
101
|
+
include SwaggerMCPTool::Helpers::ToolRegister
|
102
|
+
|
103
|
+
def initialize
|
104
|
+
@logger = Logger.new($stderr)
|
105
|
+
@logger.level = Logger::INFO
|
106
|
+
@logger.info 'Initializing WorkingSwaggerMCPStdioServer with swagger_mcp_tool'
|
107
|
+
|
108
|
+
# Configure the swagger_mcp_tool
|
109
|
+
@config = SwaggerMCPTool.configure do |config|
|
110
|
+
# to switch to other swagger documentation just change url here
|
111
|
+
config.swagger_url = 'https://petstore.swagger.io/v2/swagger.json'
|
112
|
+
config.mcp_name = 'swagger_api_tools12'
|
113
|
+
config.mcp_name_for_human = 'Swagger API Tools'
|
114
|
+
config.mcp_description_for_human = 'Tools for interacting with APIs via Swagger/OpenAPI specifications'
|
115
|
+
config.mcp_description_for_model = 'This MCP server provides tools for interacting with APIs via Swagger/OpenAPI specifications.'
|
116
|
+
|
117
|
+
# Add default prompts
|
118
|
+
|
119
|
+
end
|
120
|
+
setup_tool_registry
|
121
|
+
initialize_tools
|
122
|
+
|
123
|
+
|
124
|
+
@logger.info 'Configuration set, generating tools using swagger_mcp_tool components'
|
125
|
+
|
126
|
+
# Generate tools using the swagger_mcp_tool components directly
|
127
|
+
@tools = tool_registry.dynamic_tools
|
128
|
+
@logger.info "Generated #{@tools.size} tools from Swagger specification"
|
129
|
+
|
130
|
+
# Log all tool names
|
131
|
+
@tools.each { |tool| @logger.info "Available tool: #{tool.name}" }
|
132
|
+
end
|
133
|
+
|
134
|
+
def start_stdio_server
|
135
|
+
@logger.info "Starting MCP stdio server with #{@tools.size} tools"
|
136
|
+
|
137
|
+
# Create MCP server with our dynamically generated tools
|
138
|
+
mcp_server = MCP::Server.new(
|
139
|
+
name: 'swagger_api_tools',
|
140
|
+
tools: @tools,
|
141
|
+
server_context: { user_id: 1 }
|
142
|
+
)
|
143
|
+
|
144
|
+
@logger.info 'MCP server created, starting stdio loop'
|
145
|
+
|
146
|
+
# Handle stdio communication
|
147
|
+
$stdin.each_line do |line|
|
148
|
+
line = line.strip
|
149
|
+
next if line.empty?
|
150
|
+
|
151
|
+
@logger.debug "Received: #{line}"
|
152
|
+
|
153
|
+
# Validate JSON-RPC request format
|
154
|
+
begin
|
155
|
+
request_data = JSON.parse(line)
|
156
|
+
unless request_data.is_a?(Hash) && request_data['jsonrpc'] && request_data['method']
|
157
|
+
@logger.error "Invalid JSON-RPC request format: #{line}"
|
158
|
+
next
|
159
|
+
end
|
160
|
+
rescue JSON::ParserError => e
|
161
|
+
@logger.error "Invalid JSON in request: #{e.message}"
|
162
|
+
next
|
163
|
+
end
|
164
|
+
|
165
|
+
# Process the JSON-RPC request
|
166
|
+
@logger.info "Processing JSON-RPC request: #{request_data['method']}"
|
167
|
+
response = mcp_server.handle_json(line)
|
168
|
+
|
169
|
+
@logger.info "Generated response for method: #{request_data['method']}"
|
170
|
+
|
171
|
+
# Send response to stdout
|
172
|
+
puts response
|
173
|
+
$stdout.flush
|
174
|
+
rescue StandardError => e
|
175
|
+
@logger.error "Error processing request: #{e.message}"
|
176
|
+
@logger.error "Error class: #{e.class}"
|
177
|
+
@logger.error e.backtrace.join("\n")
|
178
|
+
|
179
|
+
# Extract request ID if possible
|
180
|
+
request_id = nil
|
181
|
+
begin
|
182
|
+
parsed_request = JSON.parse(line)
|
183
|
+
request_id = parsed_request['id']
|
184
|
+
rescue StandardError
|
185
|
+
# Ignore parsing errors for error response
|
186
|
+
end
|
187
|
+
|
188
|
+
# Send error response
|
189
|
+
error_response = {
|
190
|
+
jsonrpc: '2.0',
|
191
|
+
id: request_id,
|
192
|
+
error: {
|
193
|
+
code: -32_603,
|
194
|
+
message: "Internal error: #{e.message}"
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
error_json = error_response.to_json
|
199
|
+
@logger.debug "Sending error response: #{error_json}"
|
200
|
+
puts error_json
|
201
|
+
$stdout.flush
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
# Start the server if this file is executed directly
|
207
|
+
if __FILE__ == $0
|
208
|
+
server = WorkingSwaggerMCPStdioServer.new
|
209
|
+
server.start_stdio_server
|
210
|
+
end
|
211
|
+
```
|
212
|
+
|
213
|
+
Run the STDIO server:
|
214
|
+
|
215
|
+
```bash
|
216
|
+
ruby examples/mcp_stdio_server.rb
|
217
|
+
```
|
218
|
+
|
219
|
+
### HTTP Server
|
220
|
+
|
221
|
+
Use the provided example for HTTP-based integration:
|
222
|
+
|
223
|
+
**File: `examples/start_server.rb`**
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
#!/usr/bin/env ruby
|
227
|
+
|
228
|
+
gem install swagger_mcp_tool
|
229
|
+
|
230
|
+
|
231
|
+
require 'swagger_mcp_tool'
|
232
|
+
SwaggerMCPTool.configure do |config|
|
233
|
+
# Server settings
|
234
|
+
config.server_port = 3001
|
235
|
+
config.server_bind = '0.0.0.0'
|
236
|
+
|
237
|
+
# Swagger settings
|
238
|
+
config.swagger_url = 'https://petstore.swagger.io/v2/swagger.json'
|
239
|
+
|
240
|
+
# MCP settings
|
241
|
+
config.mcp_name = 'my_company_api'
|
242
|
+
config.mcp_name_for_human = 'My Company API'
|
243
|
+
config.mcp_description_for_human = "Tools for interacting with My Company's API"
|
244
|
+
config.auth_header_name = :'Token'
|
245
|
+
end
|
246
|
+
|
247
|
+
# Start the server
|
248
|
+
SwaggerMCPTool.start_server
|
249
|
+
```
|
250
|
+
|
251
|
+
Run the HTTP server:
|
252
|
+
|
253
|
+
```bash
|
254
|
+
ruby examples/start_server.rb
|
255
|
+
```
|
256
|
+
|
257
|
+
## Configuration
|
258
|
+
|
259
|
+
The gem uses a singleton `Config` class for configuration. You can configure it using a block:
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
SwaggerMCPTool.configure do |config|
|
263
|
+
# Server settings
|
264
|
+
config.server_port = 3001
|
265
|
+
config.server_bind = '0.0.0.0'
|
266
|
+
config.server_type = :puma
|
267
|
+
config.log_level = Logger::INFO
|
268
|
+
config.log_file = 'mcp_server.log'
|
269
|
+
|
270
|
+
# Swagger settings
|
271
|
+
config.swagger_url = "https://petstore.swagger.io/v2/swagger.json"
|
272
|
+
|
273
|
+
# MCP settings
|
274
|
+
config.mcp_name = "swagger_api_tools"
|
275
|
+
config.mcp_name_for_human = "Swagger API Tools"
|
276
|
+
config.mcp_description_for_human = "Tools for interacting with APIs via Swagger/OpenAPI specifications"
|
277
|
+
config.mcp_description_for_model = "This MCP server provides tools for interacting with APIs via Swagger/OpenAPI specifications."
|
278
|
+
|
279
|
+
# Auth settings
|
280
|
+
config.auth_type = "none" # or "bearer", "basic", etc.
|
281
|
+
config.auth_header_name = "Token" # Default header name for auth
|
282
|
+
config.default_token = nil
|
283
|
+
config.prompts = []
|
284
|
+
config.resources = []
|
285
|
+
end
|
286
|
+
```
|
287
|
+
|
288
|
+
### Configuration Options
|
289
|
+
|
290
|
+
| Option | Default | Description |
|
291
|
+
|--------|---------|-------------|
|
292
|
+
| `server_port` | 3001 | HTTP server port |
|
293
|
+
| `server_bind` | '0.0.0.0' | Server bind address |
|
294
|
+
| `server_type` | :puma | Server type (Puma, etc.) |
|
295
|
+
| `log_level` | Logger::INFO | Logging level |
|
296
|
+
| `log_file` | 'mcp_server.log' | Log file path |
|
297
|
+
| `swagger_url` | Petstore demo | Swagger/OpenAPI spec URL |
|
298
|
+
| `mcp_name` | 'swagger_api_tools' | MCP server name |
|
299
|
+
| `auth_type` | 'none' | Authentication type |
|
300
|
+
| `auth_header_name` | 'Token' | Auth header name |
|
301
|
+
|
302
|
+
## Working with Prompts
|
303
|
+
|
304
|
+
### Add Prompts
|
305
|
+
|
306
|
+
The gem comes with some built-in prompts that you can use:
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
SwaggerMCPTool.configure do |config|
|
310
|
+
# Add built-in prompts
|
311
|
+
config.prompts = ['prompt1', 'prompt2']
|
312
|
+
end
|
313
|
+
```
|
314
|
+
|
315
|
+
## Adding Resources
|
316
|
+
|
317
|
+
You can add resources that will be passed to the MCP server:
|
318
|
+
|
319
|
+
```ruby
|
320
|
+
SwaggerMCPTool.configure do |config|
|
321
|
+
# Add a simple text resource
|
322
|
+
config.resources = ['resource1', 'resouce2']
|
323
|
+
end
|
324
|
+
```
|
325
|
+
|
326
|
+
## MCP Integration
|
327
|
+
|
328
|
+
### Vscode IDE Integration
|
329
|
+
|
330
|
+
To use with Vscode IDE, add the server to your MCP configuration file (`.vscode/settings/mcp.json`):
|
331
|
+
|
332
|
+
```json
|
333
|
+
{
|
334
|
+
"mcpServers": {
|
335
|
+
"swagger-mcp-tool": {
|
336
|
+
"command": "/Users/<user>/.rbenv/versions/3.2.2/bin/ruby",
|
337
|
+
"args": [
|
338
|
+
"/Users/<user>/swagger_mcp_tool/examples/mcp_stdio_server.rb"
|
339
|
+
],
|
340
|
+
"env": {
|
341
|
+
"RBENV_VERSION": "3.2.2",
|
342
|
+
"PATH": "/Users/<user>/.rbenv/versions/3.2.2/bin:/Users/aki/.rbenv/shims:/usr/local/bin:/usr/bin:/bin"
|
343
|
+
},
|
344
|
+
"disabled": false,
|
345
|
+
"autoApprove": []
|
346
|
+
}
|
347
|
+
}
|
348
|
+
}
|
349
|
+
```
|
350
|
+
|
351
|
+
**Note**: Update the paths to match your Ruby installation and project location.
|
352
|
+
|
353
|
+
### Generated Tools
|
354
|
+
|
355
|
+
The server automatically generates MCP tools based on your Swagger specification. For example, with the Petstore API:
|
356
|
+
|
357
|
+
- `getInventory` - Get pet inventory by status
|
358
|
+
- `getPetById` - Find pet by ID (handles path parameters)
|
359
|
+
- `findPetsByStatus` - Find pets by status (handles query parameters)
|
360
|
+
- `addPet` - Add a new pet (handles request body)
|
361
|
+
- `updatePet` - Update an existing pet
|
362
|
+
- `deletePet` - Delete a pet
|
363
|
+
- `getUserByName` - Get user by username
|
364
|
+
- `deleteUser` - Delete user
|
365
|
+
|
366
|
+
### Tool Features
|
367
|
+
|
368
|
+
- **Path Parameters**: Automatically substitutes `{petId}` with actual values
|
369
|
+
- **Query Parameters**: Handles array and single-value query parameters
|
370
|
+
- **Request Bodies**: Processes JSON request bodies for POST/PUT operations
|
371
|
+
- **Error Handling**: Returns proper error responses for 404, 400, etc.
|
372
|
+
- **Type Validation**: Validates parameter types based on Swagger schema
|
373
|
+
|
374
|
+
## Authentication
|
375
|
+
|
376
|
+
### Passing User Context (HTTP Server)
|
377
|
+
|
378
|
+
When making requests to the HTTP MCP server, you can pass user context in the headers:
|
379
|
+
|
380
|
+
```bash
|
381
|
+
curl -X POST http://localhost:3001/mcp \
|
382
|
+
-H "Content-Type: application/json" \
|
383
|
+
-H "X-User-ID: user123" \
|
384
|
+
-H "X-Username: John Doe" \
|
385
|
+
-H "X-Email: john@example.com" \
|
386
|
+
-H "Auth-Token: Bearer token123" \
|
387
|
+
-d '{"jsonrpc":"2.0","id":"1","method":"tools/list"}'
|
388
|
+
```
|
389
|
+
|
390
|
+
## Usage Examples
|
391
|
+
|
392
|
+
### Using the Examples
|
393
|
+
|
394
|
+
The gem includes working examples in the `examples/` directory:
|
395
|
+
|
396
|
+
1. **STDIO Server**: `examples/mcp_stdio_server.rb` - For MCP client integration
|
397
|
+
2. **HTTP Server**: `examples/start_server.rb` - For web-based integration
|
398
|
+
3. **CLI Tool**: `bin/swagger_mcp_server` - Command-line interface
|
399
|
+
|
400
|
+
### Customizing for Your API
|
401
|
+
|
402
|
+
To use with your own API, modify the Swagger URL in the examples:
|
403
|
+
|
404
|
+
```ruby
|
405
|
+
# In examples/mcp_stdio_server.rb or examples/start_server.rb
|
406
|
+
SwaggerMCPTool.configure do |config|
|
407
|
+
config.swagger_url = 'https://your-api.com/swagger.json' # Change this
|
408
|
+
config.mcp_name = 'your_api_tools'
|
409
|
+
config.mcp_name_for_human = 'Your API Tools'
|
410
|
+
# ... other configuration
|
411
|
+
end
|
412
|
+
```
|
413
|
+
|
414
|
+
### Available Endpoints (HTTP Server)
|
415
|
+
|
416
|
+
When running the HTTP server, the following endpoints are available:
|
417
|
+
|
418
|
+
- `GET /mcp/manifest` - MCP manifest for integration
|
419
|
+
- `POST /mcp` - MCP JSON-RPC requests
|
420
|
+
- `POST /set_auth_token` - Set authentication tokens
|
421
|
+
- `POST /generate_tools` - Regenerate tools from Swagger URL
|
422
|
+
- `GET /health` - Health check and server status
|
423
|
+
- `GET /logo.png` - Server logo (if available)
|
424
|
+
|
425
|
+
## Testing Your Tools
|
426
|
+
|
427
|
+
### With Vscode IDE
|
428
|
+
|
429
|
+
Once configured in Vscode, you can test tools directly:
|
430
|
+
|
431
|
+
```
|
432
|
+
# Test getInventory (no parameters)
|
433
|
+
Use the getInventory tool
|
434
|
+
|
435
|
+
# Test getPetById (path parameter)
|
436
|
+
Use getPetById with petId: 1
|
437
|
+
|
438
|
+
# Test findPetsByStatus (query parameter)
|
439
|
+
Use findPetsByStatus with status: ["available"]
|
440
|
+
|
441
|
+
# Test addPet (request body)
|
442
|
+
Use addPet with name: "Fluffy", photoUrls: ["http://example.com/photo.jpg"], status: "available"
|
443
|
+
```
|
444
|
+
|
445
|
+
### With HTTP Server
|
446
|
+
|
447
|
+
Test tools via HTTP requests:
|
448
|
+
|
449
|
+
```bash
|
450
|
+
# List available tools
|
451
|
+
curl -X POST http://localhost:3001/mcp \
|
452
|
+
-H "Content-Type: application/json" \
|
453
|
+
-d '{"jsonrpc":"2.0","id":"1","method":"tools/list"}'
|
454
|
+
|
455
|
+
# Call a tool
|
456
|
+
curl -X POST http://localhost:3001/mcp \
|
457
|
+
-H "Content-Type: application/json" \
|
458
|
+
-d '{
|
459
|
+
"jsonrpc":"2.0",
|
460
|
+
"id":"1",
|
461
|
+
"method":"tools/call",
|
462
|
+
"params":{
|
463
|
+
"name":"getInventory",
|
464
|
+
"arguments":{}
|
465
|
+
}
|
466
|
+
}'
|
467
|
+
```
|
468
|
+
|
469
|
+
### Expected Tool Output
|
470
|
+
|
471
|
+
For the Petstore API, you'll see tools like:
|
472
|
+
|
473
|
+
- **getInventory**: Returns inventory counts by status
|
474
|
+
```json
|
475
|
+
{
|
476
|
+
"available": 487,
|
477
|
+
"pending": 10,
|
478
|
+
"sold": 22
|
479
|
+
}
|
480
|
+
```
|
481
|
+
|
482
|
+
- **getPetById**: Returns pet details
|
483
|
+
```json
|
484
|
+
{
|
485
|
+
"id": 1,
|
486
|
+
"name": "Fluffy",
|
487
|
+
"status": "available",
|
488
|
+
"photoUrls": ["http://example.com/photo.jpg"]
|
489
|
+
}
|
490
|
+
```
|
491
|
+
|
492
|
+
## Troubleshooting
|
493
|
+
|
494
|
+
### Common Issues
|
495
|
+
|
496
|
+
1. **Path Parameter Substitution**: Ensure your Swagger spec properly defines path parameters like `{petId}`
|
497
|
+
2. **Authentication**: Check that auth headers are properly configured in your config
|
498
|
+
3. **CORS Issues**: The HTTP server includes CORS headers for web integration
|
499
|
+
4. **Tool Generation**: Verify your Swagger URL is accessible and returns valid JSON
|
500
|
+
5. **Ruby Version**: Ensure you're using Ruby 3.2 or higher
|
501
|
+
|
502
|
+
### Debug Mode
|
503
|
+
|
504
|
+
Enable debug logging in your configuration:
|
505
|
+
|
506
|
+
```ruby
|
507
|
+
SwaggerMCPTool.configure do |config|
|
508
|
+
config.log_level = Logger::DEBUG
|
509
|
+
end
|
510
|
+
```
|
511
|
+
|
512
|
+
### Checking Logs
|
513
|
+
|
514
|
+
- **STDIO Server**: Logs go to stderr, check your terminal output
|
515
|
+
- **HTTP Server**: Logs go to the configured log file (default: `mcp_server.log`)
|
516
|
+
- **Vscode Integration**: Check Vscode's MCP server logs in the IDE
|
517
|
+
|
518
|
+
### Verifying Tool Generation
|
519
|
+
|
520
|
+
Check if tools are being generated correctly:
|
521
|
+
|
522
|
+
```bash
|
523
|
+
# Run the STDIO server and check stderr output
|
524
|
+
ruby examples/mcp_stdio_server.rb
|
525
|
+
|
526
|
+
# Look for messages like:
|
527
|
+
# "Generated 15 tools from Swagger specification"
|
528
|
+
# "Available tool: getInventory"
|
529
|
+
# "Available tool: getPetById"
|
530
|
+
```
|
531
|
+
|
532
|
+
### Testing Swagger URL
|
533
|
+
|
534
|
+
Verify your Swagger URL is accessible:
|
535
|
+
|
536
|
+
```bash
|
537
|
+
curl -s https://petstore.swagger.io/v2/swagger.json | jq .info.title
|
538
|
+
# Should return: "Swagger Petstore"
|
539
|
+
```
|
540
|
+
|
541
|
+
### Vs code config for stdio
|
542
|
+
```
|
543
|
+
{
|
544
|
+
"mcpServers": {
|
545
|
+
"fetch": {
|
546
|
+
"command": "uvx",
|
547
|
+
"args": [
|
548
|
+
"mcp-server-fetch"
|
549
|
+
],
|
550
|
+
"env": {},
|
551
|
+
"disabled": false,
|
552
|
+
"autoApprove": []
|
553
|
+
},
|
554
|
+
"swagger-api-tools": {
|
555
|
+
"command": "/Users/<user>/.rbenv/shims/ruby",
|
556
|
+
"args": [
|
557
|
+
"< absolute path to mcp_stdio_server.rb >"
|
558
|
+
],
|
559
|
+
"disabled": false,
|
560
|
+
"autoApprove": [
|
561
|
+
"getInventory",
|
562
|
+
"getPetById",
|
563
|
+
"findPetsByStatus"
|
564
|
+
]
|
565
|
+
}
|
566
|
+
}
|
567
|
+
}
|
568
|
+
```
|
569
|
+
|
570
|
+
## Architecture
|
571
|
+
|
572
|
+
The gem consists of several key components:
|
573
|
+
|
574
|
+
- **Config**: Singleton configuration management
|
575
|
+
- **SwaggerClient**: Fetches and parses Swagger/OpenAPI specifications
|
576
|
+
- **ToolGenerator**: Converts Swagger operations to tool definitions
|
577
|
+
- **ApiClient**: Handles HTTP requests with proper parameter substitution
|
578
|
+
- **ToolRegistry**: Manages dynamic tool registration
|
579
|
+
- **Server**: HTTP server implementation
|
580
|
+
- **STDIO Server**: Direct MCP integration server
|
581
|
+
|
582
|
+
## Development
|
583
|
+
|
584
|
+
After checking out the repo, run `bundle install` to install dependencies.
|
585
|
+
|
586
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
587
|
+
|
588
|
+
## Contributing
|
589
|
+
|
590
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/swagger_mcp_tool.
|
591
|
+
|
592
|
+
## License
|
593
|
+
|
594
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "swagger_mcp_tool"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
require "irb"
|
11
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'swagger_mcp_tool'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
options = {
|
7
|
+
port: nil,
|
8
|
+
swagger_url: nil
|
9
|
+
}
|
10
|
+
|
11
|
+
# Parse command line options
|
12
|
+
OptionParser.new do |opts|
|
13
|
+
opts.banner = 'Usage: swagger_mcp_server [options]'
|
14
|
+
|
15
|
+
opts.on('-p', '--port PORT', 'Server port') do |port|
|
16
|
+
options[:port] = port.to_i
|
17
|
+
end
|
18
|
+
|
19
|
+
opts.on('-s', '--swagger-url URL', 'Swagger/OpenAPI specification URL') do |url|
|
20
|
+
options[:swagger_url] = url
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on('-h', '--help', 'Show this help message') do
|
24
|
+
puts opts
|
25
|
+
exit
|
26
|
+
end
|
27
|
+
end.parse!
|
28
|
+
|
29
|
+
# Configure the server
|
30
|
+
SwaggerMCPTool::Config.configure do |config|
|
31
|
+
config.server_port = options[:port] if options[:port]
|
32
|
+
config.swagger_url = options[:swagger_url] if options[:swagger_url]
|
33
|
+
|
34
|
+
# Add default prompts
|
35
|
+
config.prommpts = []
|
36
|
+
|
37
|
+
# Add resources
|
38
|
+
config.resources = []
|
39
|
+
end
|
40
|
+
|
41
|
+
# Start the server
|
42
|
+
puts "Starting Swagger MCP Server on port #{SwaggerMCPTool::Config.instance.server_port}..."
|
43
|
+
puts "Using Swagger URL: #{SwaggerMCPTool::Config.instance.swagger_url}"
|
44
|
+
puts 'Press Ctrl+C to stop the server'
|
45
|
+
SwaggerMCPTool.start_server
|