Skip to content

Commit e76e24c

Browse files
kargnasclaudeclaude[bot]
authored
feat(swagger): add automatic MCP tool generation from Swagger/OpenAPI… (#51)
* feat(swagger): add automatic MCP tool generation from Swagger/OpenAPI specs - Add command to generate MCP tools from Swagger 2.0 and OpenAPI 3.0 specifications - Support both local files and remote URLs as input sources - Parse endpoints and generate tool classes with HTTP client implementation - Handle authentication (Bearer tokens, API keys) - Auto-register generated tools in configuration - Include comprehensive test fixtures for Swagger/OpenAPI formats - Document new command with usage examples in README This enables rapid integration of external APIs by automatically converting their Swagger/OpenAPI documentation into functional MCP tools. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Revert "feat(swagger): add automatic MCP tool generation from Swagger/OpenAPI specs" This reverts commit 4868901. * feat: add automatic MCP tool generation from Swagger/OpenAPI specs (v1.4.0) Introduces powerful automatic tool generation from any Swagger/OpenAPI specification, dramatically reducing time needed to integrate external APIs. Major features: - SwaggerParser: Supports OpenAPI 3.x and Swagger 2.0 formats - SwaggerToMcpConverter: Converts API endpoints to MCP tool parameters - MakeSwaggerMcpToolCommand: Interactive CLI with endpoint selection - Smart naming: Detects hash-like operationIds and uses path-based naming - Authentication support: API Key, Bearer Token, OAuth2 generation - API testing: Validates connectivity before tool generation - Endpoint grouping: By tags, path prefixes, or individual selection - Comprehensive test coverage for naming conversion and parsing logic 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * feat(swagger): add resource generation support to Swagger/OpenAPI generator Extends the Swagger/OpenAPI generator to support creating both MCP Tools and Resources: - Add resource generation mode with smart type selection (GET → Resource, others → Tool) - Implement programmatic stub system for dynamic class generation - Add comprehensive resource generation tests - Update documentation to reflect dual generation capability - Enhance command UI with type selection workflow Allows users to generate read-only Resources for data endpoints alongside action-oriented Tools. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Fix styling * feat: implement security and maintainability improvements - Add URL validation to prevent SSRF attacks in SwaggerParser - Standardize error handling to use JsonRpcErrorException - Make HTTP timeouts configurable instead of hardcoded - Refactor generateHttpRequest() method into smaller focused methods Co-authored-by: Sangrak Choi <[email protected]> * Fix styling --------- Co-authored-by: Claude <[email protected]> Co-authored-by: kargnas <[email protected]> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Sangrak Choi <[email protected]>
1 parent 66dc467 commit e76e24c

13 files changed

+3153
-31
lines changed

README.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,32 @@
3232

3333
## ⚠️ Version Information & Breaking Changes
3434

35-
### v1.3.0 Changes (Current)
35+
### v1.4.0 Changes (Latest) 🚀
36+
37+
Version 1.4.0 introduces powerful automatic tool and resource generation from Swagger/OpenAPI specifications:
38+
39+
**New Features:**
40+
- **Swagger/OpenAPI Tool & Resource Generator**: Automatically generate MCP tools or resources from any Swagger/OpenAPI specification
41+
- Supports both OpenAPI 3.x and Swagger 2.0 formats
42+
- **Choose generation type**: Generate as Tools (for actions) or Resources (for read-only data)
43+
- Interactive endpoint selection with grouping options
44+
- Automatic authentication logic generation (API Key, Bearer Token, OAuth2)
45+
- Smart naming for readable class names (handles hash-based operationIds)
46+
- Built-in API testing before generation
47+
- Complete Laravel HTTP client integration with retry logic
48+
49+
**Example Usage:**
50+
```bash
51+
# Generate tools from OP.GG API
52+
php artisan make:swagger-mcp-tool https://api.op.gg/lol/swagger.json
53+
54+
# With options
55+
php artisan make:swagger-mcp-tool ./api-spec.json --test-api --group-by=tag --prefix=MyApi
56+
```
57+
58+
This feature dramatically reduces the time needed to integrate external APIs into your MCP server!
59+
60+
### v1.3.0 Changes
3661

3762
Version 1.3.0 introduces improvements to the `ToolInterface` for better communication control:
3863

@@ -310,6 +335,104 @@ This command:
310335
- Creates a properly structured tool class in `app/MCP/Tools`
311336
- Offers to automatically register the tool in your configuration
312337

338+
#### Generate Tools from Swagger/OpenAPI Specifications (v1.4.0+)
339+
340+
Automatically generate MCP tools from any Swagger/OpenAPI specification with a single command:
341+
342+
```bash
343+
# From URL
344+
php artisan make:swagger-mcp-tool https://api.example.com/swagger.json
345+
346+
# From local file
347+
php artisan make:swagger-mcp-tool ./specs/openapi.json
348+
349+
# With options
350+
php artisan make:swagger-mcp-tool https://api.example.com/swagger.json \
351+
--test-api \
352+
--group-by=tag \
353+
--prefix=MyApi
354+
```
355+
356+
**Real-world Example with OP.GG API:**
357+
358+
```bash
359+
➜ php artisan make:swagger-mcp-tool https://api.op.gg/lol/swagger.json
360+
361+
🚀 Swagger/OpenAPI to MCP Generator
362+
=========================================
363+
📄 Loading spec from: https://api.op.gg/lol/swagger.json
364+
✅ Spec loaded successfully!
365+
+-----------------+-------------------------+
366+
| Property | Value |
367+
+-----------------+-------------------------+
368+
| Title | OP.GG Api Documentation |
369+
| Version | openapi-3.0.0 |
370+
| Base URL | https://api.op.gg |
371+
| Total Endpoints | 6 |
372+
| Tags | Riot |
373+
| Security | |
374+
+-----------------+-------------------------+
375+
376+
🎯 What would you like to generate from this API?
377+
378+
Tools: For operations that perform actions (create, update, delete, compute)
379+
Resources: For read-only data endpoints that provide information
380+
381+
Generate as:
382+
[0] Tools (for actions)
383+
> 1
384+
[1] Resources (for read-only data)
385+
> 1
386+
387+
✓ Will generate as MCP Resources
388+
389+
Would you like to modify the base URL? Current: https://api.op.gg (yes/no) [no]:
390+
> no
391+
392+
📋 Select endpoints to generate resources for:
393+
Note: Only GET endpoints can be converted to resources
394+
Include tag: Riot (6 endpoints)? (yes/no) [yes]:
395+
> yes
396+
397+
Selected 6 endpoints.
398+
🛠️ Generating MCP resources...
399+
Note: operationId '5784a7dfd226e1621b0e6ee8c4f39407' looks like a hash, will use path-based naming
400+
Generating: LolRegionRankingsGameTypeResource
401+
✅ Generated: LolRegionRankingsGameTypeResource
402+
Generating: LolRegionServerStatsResource
403+
✅ Generated: LolRegionServerStatsResource
404+
...
405+
406+
📦 Generated 6 MCP resources:
407+
- LolRegionRankingsGameTypeResource
408+
- LolRegionServerStatsResource
409+
- LolMetaChampionsResource
410+
...
411+
412+
✅ MCP resources generated successfully!
413+
```
414+
415+
**Key Features:**
416+
- **Automatic API parsing**: Supports OpenAPI 3.x and Swagger 2.0 specifications
417+
- **Dual generation modes**:
418+
- **Tools**: For operations that perform actions (POST, PUT, DELETE, etc.)
419+
- **Resources**: For read-only GET endpoints that provide data
420+
- **Smart naming**: Converts paths like `/lol/{region}/server-stats` to `LolRegionServerStatsTool` or `LolRegionServerStatsResource`
421+
- **Hash detection**: Automatically detects MD5-like operationIds and uses path-based naming instead
422+
- **Interactive mode**: Select which endpoints to convert
423+
- **API testing**: Test API connectivity before generating
424+
- **Authentication support**: Automatically generates authentication logic for API Key, Bearer Token, and OAuth2
425+
- **Smart grouping**: Group endpoints by tags or path prefixes
426+
- **Code generation**: Creates ready-to-use classes with Laravel HTTP client integration
427+
428+
The generated tools include:
429+
- Proper input validation based on API parameters
430+
- Authentication headers configuration
431+
- Error handling for API responses with JsonRpcErrorException
432+
- Request retry logic (3 retries with 100ms delay)
433+
- Query parameter, path parameter, and request body handling
434+
- Laravel HTTP client with timeout configuration
435+
313436
You can also manually create and register tools in `config/mcp-server.php`:
314437

315438
```php

0 commit comments

Comments
 (0)