swagger_mcp_tool 0.1.2 → 0.1.4
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 +4 -4
- data/README.md +52 -14
- data/lib/swagger_mcp_tool/config.rb +5 -2
- data/lib/swagger_mcp_tool/helpers/request.rb +10 -2
- data/lib/swagger_mcp_tool/helpers/tool_register.rb +14 -0
- data/lib/swagger_mcp_tool/server.rb +2 -3
- data/lib/swagger_mcp_tool/stdio_server.rb +53 -0
- data/lib/swagger_mcp_tool/tool_registry.rb +5 -2
- data/lib/swagger_mcp_tool/version.rb +1 -1
- data/lib/swagger_mcp_tool.rb +9 -0
- metadata +8 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a7c9a8135edeb2cc144723ca07ee9723179d99b542e47ad2319793434dd8ed8
|
4
|
+
data.tar.gz: e651d04bc9a2211aeeb93f6dd5e19b505097173d793feb56f63d8cf14fce245c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f941db14c926b9f36c6e3dda7ecff8e925ca70bf1886ed0e5e09babe1642afb98fa578da306fa29a2ae57cea906834f6edc6fd8516a1c9ba89fdcd1cc220517
|
7
|
+
data.tar.gz: ac9c8534744bd32efbd5f62e5ba4d9ee23ae60f40f81339a3bf48492129cab789eaac626bbc78551f89b6f2929da18f8f35fe5a14fff063e1e2e0cafa2012f00
|
data/README.md
CHANGED
@@ -21,10 +21,10 @@ A Ruby gem that automatically generates MCP (Model Context Protocol) tools from
|
|
21
21
|
- Gems:
|
22
22
|
- sinatra
|
23
23
|
- sinatra-contrib
|
24
|
-
- mcp
|
24
|
+
- mcp
|
25
25
|
- json
|
26
26
|
- puma
|
27
|
-
- rackup
|
27
|
+
- rackup
|
28
28
|
- pry (development)
|
29
29
|
- rake (development)
|
30
30
|
|
@@ -85,10 +85,38 @@ swagger_mcp_server --help
|
|
85
85
|
|
86
86
|
### STDIO Server (Recommended for MCP Integration)
|
87
87
|
|
88
|
-
Use the provided example for direct integration with MCP clients like
|
88
|
+
Use the provided example for direct integration with MCP clients like Kiro IDE:
|
89
89
|
|
90
90
|
**File: `examples/mcp_stdio_server.rb`**
|
91
91
|
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
|
95
|
+
require 'swagger_mcp_tool'
|
96
|
+
|
97
|
+
SwaggerMCPTool.configure do |config|
|
98
|
+
# Swagger settings
|
99
|
+
config.swagger_url = 'https://petstore.swagger.io/v2/swagger.json'
|
100
|
+
|
101
|
+
# MCP settings
|
102
|
+
config.mcp_name = 'my_company_api'
|
103
|
+
config.mcp_name_for_human = 'My Company API'
|
104
|
+
config.mcp_description_for_human = "Tools for interacting with My Company's API"
|
105
|
+
config.auth_header_name = :'Token'
|
106
|
+
config.protocol_version = "2025-03-26"
|
107
|
+
|
108
|
+
# Force stdio mode
|
109
|
+
config.stdio_mode = true if config.respond_to?(:stdio_mode=)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Start the MCP server in stdio mode
|
113
|
+
SwaggerMCPTool.start_stdio_server
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
### OR
|
118
|
+
|
119
|
+
|
92
120
|
```ruby
|
93
121
|
#!/usr/bin/env ruby
|
94
122
|
|
@@ -227,6 +255,7 @@ Use the provided example for HTTP-based integration:
|
|
227
255
|
|
228
256
|
gem install swagger_mcp_tool
|
229
257
|
|
258
|
+
|
230
259
|
require 'swagger_mcp_tool'
|
231
260
|
SwaggerMCPTool.configure do |config|
|
232
261
|
# Server settings
|
@@ -240,7 +269,7 @@ SwaggerMCPTool.configure do |config|
|
|
240
269
|
config.mcp_name = 'my_company_api'
|
241
270
|
config.mcp_name_for_human = 'My Company API'
|
242
271
|
config.mcp_description_for_human = "Tools for interacting with My Company's API"
|
243
|
-
config.auth_header_name = :'Token'
|
272
|
+
config.auth_header_name = :'X-Auth-Token'
|
244
273
|
end
|
245
274
|
|
246
275
|
# Start the server
|
@@ -297,6 +326,7 @@ end
|
|
297
326
|
| `mcp_name` | 'swagger_api_tools' | MCP server name |
|
298
327
|
| `auth_type` | 'none' | Authentication type |
|
299
328
|
| `auth_header_name` | 'Token' | Auth header name |
|
329
|
+
| `protocol_version` | '2025-03-26' | Protocol Version Supported by Client |
|
300
330
|
|
301
331
|
## Working with Prompts
|
302
332
|
|
@@ -324,9 +354,9 @@ end
|
|
324
354
|
|
325
355
|
## MCP Integration
|
326
356
|
|
327
|
-
###
|
357
|
+
### Kiro IDE Integration
|
328
358
|
|
329
|
-
To use with
|
359
|
+
To use with Kiro IDE, add the server to your MCP configuration file (`.kiro/settings/mcp.json`):
|
330
360
|
|
331
361
|
```json
|
332
362
|
{
|
@@ -334,11 +364,11 @@ To use with Vscode IDE, add the server to your MCP configuration file (`.vscode/
|
|
334
364
|
"swagger-mcp-tool": {
|
335
365
|
"command": "/Users/<user>/.rbenv/versions/3.2.2/bin/ruby",
|
336
366
|
"args": [
|
337
|
-
"/Users/<user>/swagger_mcp_tool/examples/mcp_stdio_server.rb"
|
367
|
+
"/Users/<user>/projects/dummy/swagger_mcp_tool/examples/mcp_stdio_server.rb"
|
338
368
|
],
|
339
369
|
"env": {
|
340
370
|
"RBENV_VERSION": "3.2.2",
|
341
|
-
"PATH": "/Users/<user>/.rbenv/versions/3.2.2/bin:/Users
|
371
|
+
"PATH": "/Users/<user>/.rbenv/versions/3.2.2/bin:/Users/<user>/.rbenv/shims:/usr/local/bin:/usr/bin:/bin"
|
342
372
|
},
|
343
373
|
"disabled": false,
|
344
374
|
"autoApprove": []
|
@@ -372,6 +402,16 @@ The server automatically generates MCP tools based on your Swagger specification
|
|
372
402
|
|
373
403
|
## Authentication
|
374
404
|
|
405
|
+
### Setting an Auth Token (HTTP Server)
|
406
|
+
|
407
|
+
You can set an auth token for a user by making a POST request to the `/set_auth_token` endpoint:
|
408
|
+
|
409
|
+
```bash
|
410
|
+
curl -X POST http://localhost:3001/set_auth_token \
|
411
|
+
-H "Content-Type: application/json" \
|
412
|
+
-d '{"user_id": "user123", "auth_token": "Bearer token123"}'
|
413
|
+
```
|
414
|
+
|
375
415
|
### Passing User Context (HTTP Server)
|
376
416
|
|
377
417
|
When making requests to the HTTP MCP server, you can pass user context in the headers:
|
@@ -382,7 +422,7 @@ curl -X POST http://localhost:3001/mcp \
|
|
382
422
|
-H "X-User-ID: user123" \
|
383
423
|
-H "X-Username: John Doe" \
|
384
424
|
-H "X-Email: john@example.com" \
|
385
|
-
-H "Auth-Token: Bearer token123" \
|
425
|
+
-H "X-Auth-Token: Bearer token123" \
|
386
426
|
-d '{"jsonrpc":"2.0","id":"1","method":"tools/list"}'
|
387
427
|
```
|
388
428
|
|
@@ -423,9 +463,9 @@ When running the HTTP server, the following endpoints are available:
|
|
423
463
|
|
424
464
|
## Testing Your Tools
|
425
465
|
|
426
|
-
### With
|
466
|
+
### With Kiro IDE
|
427
467
|
|
428
|
-
Once configured in
|
468
|
+
Once configured in Kiro, you can test tools directly:
|
429
469
|
|
430
470
|
```
|
431
471
|
# Test getInventory (no parameters)
|
@@ -512,7 +552,7 @@ end
|
|
512
552
|
|
513
553
|
- **STDIO Server**: Logs go to stderr, check your terminal output
|
514
554
|
- **HTTP Server**: Logs go to the configured log file (default: `mcp_server.log`)
|
515
|
-
- **
|
555
|
+
- **Kiro Integration**: Check Kiro's MCP server logs in the IDE
|
516
556
|
|
517
557
|
### Verifying Tool Generation
|
518
558
|
|
@@ -582,8 +622,6 @@ The gem consists of several key components:
|
|
582
622
|
|
583
623
|
After checking out the repo, run `bundle install` to install dependencies.
|
584
624
|
|
585
|
-
After checking out the repo, run `bundle install` to install dependencies.
|
586
|
-
|
587
625
|
To install this gem onto your local machine, run `bundle exec rake install`.
|
588
626
|
|
589
627
|
## Contributing
|
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
require 'logger'
|
4
4
|
require 'singleton'
|
5
|
-
|
6
5
|
module SwaggerMCPTool
|
7
6
|
# Configuration management for SwaggerMCPTool
|
8
7
|
#
|
@@ -15,7 +14,8 @@ module SwaggerMCPTool
|
|
15
14
|
:log_level, :log_file, :swagger_url, :mcp_name,
|
16
15
|
:mcp_name_for_human, :mcp_description_for_human,
|
17
16
|
:mcp_description_for_model, :auth_type, :default_token,
|
18
|
-
:prompts, :resources, :auth_header_name, :tools
|
17
|
+
:prompts, :resources, :auth_header_name, :tools, :protocol_version,
|
18
|
+
:exclude_tools
|
19
19
|
|
20
20
|
DEFAULT_SERVER_PORT = 3001
|
21
21
|
DEFAULT_SERVER_BIND = '0.0.0.0'
|
@@ -29,6 +29,7 @@ module SwaggerMCPTool
|
|
29
29
|
DEFAULT_MCP_DESCRIPTION_FOR_MODEL = 'This MCP server provides tools for interacting with APIs via Swagger/OpenAPI specifications.'
|
30
30
|
DEFAULT_AUTH_TYPE = 'none'
|
31
31
|
DEFAULT_LOG_FILE = nil
|
32
|
+
DEFAULT_PROTOCOL_VERSION = "2024-11-05"
|
32
33
|
|
33
34
|
def initialize
|
34
35
|
# Server settings
|
@@ -47,6 +48,7 @@ module SwaggerMCPTool
|
|
47
48
|
@tools = []
|
48
49
|
@resources = []
|
49
50
|
@prompts = [] # Will be generated lazily
|
51
|
+
@exclude_tools = []
|
50
52
|
end
|
51
53
|
|
52
54
|
def setup_server_defaults
|
@@ -67,6 +69,7 @@ module SwaggerMCPTool
|
|
67
69
|
@mcp_name_for_human = DEFAULT_MCP_NAME_FOR_HUMAN
|
68
70
|
@mcp_description_for_human = DEFAULT_MCP_DESCRIPTION_FOR_HUMAN
|
69
71
|
@mcp_description_for_model = DEFAULT_MCP_DESCRIPTION_FOR_MODEL
|
72
|
+
@protocol_version = DEFAULT_PROTOCOL_VERSION
|
70
73
|
end
|
71
74
|
|
72
75
|
def setup_auth_defaults
|
@@ -19,7 +19,7 @@ module SwaggerMCPTool
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def create_http_request(method, uri, params)
|
22
|
-
req_method = request_methods(method.downcase)
|
22
|
+
req_method = request_methods(method.downcase&.to_sym)
|
23
23
|
request = req_method.new(uri.request_uri)
|
24
24
|
|
25
25
|
set_json_body(request, params) if body_required?(method.downcase)
|
@@ -58,7 +58,10 @@ module SwaggerMCPTool
|
|
58
58
|
|
59
59
|
def add_headers_to_request(request, headers, auth_token)
|
60
60
|
# Add custom headers
|
61
|
-
headers.each
|
61
|
+
headers.each do |key, value|
|
62
|
+
next unless valid_header?(key, value)
|
63
|
+
request[key] = sanitize_header_value(value)
|
64
|
+
end
|
62
65
|
|
63
66
|
# Add authorization header if available
|
64
67
|
add_authorization_header(request, auth_token)
|
@@ -109,6 +112,11 @@ module SwaggerMCPTool
|
|
109
112
|
def filter_unused_params(params, used_params)
|
110
113
|
params.reject { |key, _| used_params.include?(key) }
|
111
114
|
end
|
115
|
+
|
116
|
+
def sanitize_header_value(value)
|
117
|
+
# Remove any potentially dangerous characters
|
118
|
+
value.to_s.gsub(/[\r\n\0]/, '')
|
119
|
+
end
|
112
120
|
end
|
113
121
|
end
|
114
122
|
end
|
@@ -2,6 +2,20 @@
|
|
2
2
|
|
3
3
|
module SwaggerMCPTool
|
4
4
|
module Helpers
|
5
|
+
# Provides helper methods for registering and initializing tools using Swagger definitions.
|
6
|
+
# This module includes methods to set up the tool registry, generate tools from a Swagger URL,
|
7
|
+
# and handle dynamic tool registration. It is intended to be included in classes that require
|
8
|
+
# integration with the SwaggerMCPTool tool registry system.
|
9
|
+
#
|
10
|
+
# Example usage:
|
11
|
+
# include SwaggerMCPTool::Helpers::ToolRegister
|
12
|
+
#
|
13
|
+
# @see SwaggerMCPTool::ToolRegistry
|
14
|
+
# @see SwaggerClient
|
15
|
+
# @see ToolGenerator
|
16
|
+
#
|
17
|
+
# @author Your Name
|
18
|
+
# @since 1.0.0
|
5
19
|
module ToolRegister
|
6
20
|
def tool_registry
|
7
21
|
SwaggerMCPTool::ToolRegistry.instance
|
@@ -5,7 +5,6 @@ require 'sinatra/json'
|
|
5
5
|
require 'mcp'
|
6
6
|
require 'json'
|
7
7
|
require 'logger'
|
8
|
-
require 'byebug'
|
9
8
|
require_relative 'tool_registry'
|
10
9
|
require_relative 'logging'
|
11
10
|
require_relative 'config'
|
@@ -103,7 +102,7 @@ module SwaggerMCPTool
|
|
103
102
|
self.class.before do
|
104
103
|
headers 'Access-Control-Allow-Origin' => '*',
|
105
104
|
'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
|
106
|
-
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-User-ID, Auth-Token, X-Username, X-Email'
|
105
|
+
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-User-ID, X-Auth-Token, X-Username, X-Email'
|
107
106
|
end
|
108
107
|
end
|
109
108
|
|
@@ -111,7 +110,7 @@ module SwaggerMCPTool
|
|
111
110
|
self.class.options '*' do
|
112
111
|
response.headers['Allow'] = 'GET, POST, OPTIONS'
|
113
112
|
response.headers['Access-Control-Allow-Headers'] =
|
114
|
-
'Authorization, Content-Type, Accept, X-User-ID, Auth-Token, X-Username, X-Email'
|
113
|
+
'Authorization, Content-Type, Accept, X-User-ID, X-Auth-Token, X-Username, X-Email'
|
115
114
|
200
|
116
115
|
end
|
117
116
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'mcp'
|
2
|
+
require 'mcp/server/transports/stdio_transport'
|
3
|
+
require 'json'
|
4
|
+
require 'logger'
|
5
|
+
require_relative 'tool_registry'
|
6
|
+
require_relative 'logging'
|
7
|
+
require_relative 'config'
|
8
|
+
require_relative 'helpers/tool_register'
|
9
|
+
|
10
|
+
module SwaggerMCPTool
|
11
|
+
# This module provides the StdioServer for handling MCP tool operations via standard IO.
|
12
|
+
# StdioServer handles MCP tool operations via standard IO.
|
13
|
+
# It initializes the server, sets up configuration and logging,
|
14
|
+
# registers available tools, and starts the server using standard IO transport.
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# server = SwaggerMCPTool::StdioServer.new
|
18
|
+
# server.start_stdio_server
|
19
|
+
#
|
20
|
+
# @see SwaggerMCPTool::Logging
|
21
|
+
# @see SwaggerMCPTool::Helpers::ToolRegister
|
22
|
+
# @see MCP::Server
|
23
|
+
# @see MCP::Server::Transports::StdioTransport
|
24
|
+
class StdioServer
|
25
|
+
include SwaggerMCPTool::Logging
|
26
|
+
include SwaggerMCPTool::Helpers::ToolRegister
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@config = Config.instance
|
30
|
+
@logger = @config.logger || Logger.new($stdout)
|
31
|
+
log_server_initialization
|
32
|
+
setup_tool_registry
|
33
|
+
initialize_tools
|
34
|
+
end
|
35
|
+
|
36
|
+
def start_stdio_server(context = {})
|
37
|
+
user_details = context
|
38
|
+
configuration = MCP::Configuration.new(protocol_version: @config.protocol_version)
|
39
|
+
|
40
|
+
# Create MCP server with our tools and prompts
|
41
|
+
mcp = MCP::Server.new(
|
42
|
+
name: @config.mcp_name,
|
43
|
+
tools: tool_registry.dynamic_tools,
|
44
|
+
prompts: @config.prompts,
|
45
|
+
server_context: user_details,
|
46
|
+
configuration: configuration
|
47
|
+
)
|
48
|
+
|
49
|
+
transport = MCP::Server::Transports::StdioTransport.new(mcp)
|
50
|
+
transport.open
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -25,9 +25,12 @@ module SwaggerMCPTool
|
|
25
25
|
@dynamic_tools = @config.tools if @config&.tools
|
26
26
|
validate_inputs!(tools, base_url)
|
27
27
|
@logger.info "Registering #{tools.size} dynamic tools"
|
28
|
-
|
28
|
+
excluded_tools = Set.new(@config.exclude_tools)
|
29
|
+
|
29
30
|
tools.each do |tool_def|
|
30
|
-
|
31
|
+
unless excluded_tools.include?(tool_def['name'])
|
32
|
+
register_tool(tool_def, base_url) unless tool_exists?(tool_def['name'])
|
33
|
+
end
|
31
34
|
end
|
32
35
|
|
33
36
|
@logger.info "Successfully registered #{@dynamic_tools.size} tools"
|
data/lib/swagger_mcp_tool.rb
CHANGED
@@ -5,7 +5,11 @@ require 'swagger_mcp_tool/swagger_client'
|
|
5
5
|
require 'swagger_mcp_tool/tool_generator'
|
6
6
|
require 'swagger_mcp_tool/api_client'
|
7
7
|
require 'swagger_mcp_tool/auth_handler'
|
8
|
+
require 'swagger_mcp_tool/stdio_server'
|
8
9
|
|
10
|
+
# The SwaggerMCPTool module provides functionality to start and configure a server,
|
11
|
+
# as well as to run a stdio-based server for integration with other tools.
|
12
|
+
# It defines custom error handling and exposes methods for server lifecycle management.
|
9
13
|
module SwaggerMCPTool
|
10
14
|
class Error < StandardError; end
|
11
15
|
|
@@ -18,4 +22,9 @@ module SwaggerMCPTool
|
|
18
22
|
def self.configure(&block)
|
19
23
|
Config.configure(&block)
|
20
24
|
end
|
25
|
+
|
26
|
+
def self.start_stdio_server(context={})
|
27
|
+
server = StdioServer.new
|
28
|
+
server.start_stdio_server(context)
|
29
|
+
end
|
21
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swagger_mcp_tool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ankit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -34,16 +34,16 @@ dependencies:
|
|
34
34
|
name: mcp
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: 0.
|
39
|
+
version: 0.3.0
|
40
40
|
type: :runtime
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 0.
|
46
|
+
version: 0.3.0
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: puma
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -152,20 +152,6 @@ dependencies:
|
|
152
152
|
- - ">="
|
153
153
|
- !ruby/object:Gem::Version
|
154
154
|
version: 3.13.1
|
155
|
-
- !ruby/object:Gem::Dependency
|
156
|
-
name: yard
|
157
|
-
requirement: !ruby/object:Gem::Requirement
|
158
|
-
requirements:
|
159
|
-
- - "~>"
|
160
|
-
- !ruby/object:Gem::Version
|
161
|
-
version: '0.9'
|
162
|
-
type: :development
|
163
|
-
prerelease: false
|
164
|
-
version_requirements: !ruby/object:Gem::Requirement
|
165
|
-
requirements:
|
166
|
-
- - "~>"
|
167
|
-
- !ruby/object:Gem::Version
|
168
|
-
version: '0.9'
|
169
155
|
description: A Model Context Protocol (MCP) server that generates tools from Swagger/OpenAPI
|
170
156
|
specifications
|
171
157
|
email:
|
@@ -191,6 +177,7 @@ files:
|
|
191
177
|
- lib/swagger_mcp_tool/helpers/tool_register.rb
|
192
178
|
- lib/swagger_mcp_tool/logging.rb
|
193
179
|
- lib/swagger_mcp_tool/server.rb
|
180
|
+
- lib/swagger_mcp_tool/stdio_server.rb
|
194
181
|
- lib/swagger_mcp_tool/swagger_client.rb
|
195
182
|
- lib/swagger_mcp_tool/tool_generator.rb
|
196
183
|
- lib/swagger_mcp_tool/tool_registry.rb
|
@@ -207,7 +194,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
207
194
|
requirements:
|
208
195
|
- - ">="
|
209
196
|
- !ruby/object:Gem::Version
|
210
|
-
version:
|
197
|
+
version: '0'
|
211
198
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
212
199
|
requirements:
|
213
200
|
- - ">="
|