@stackkedjohn/mcp-factory-cli 0.1.0

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.
Files changed (60) hide show
  1. package/README.md +100 -0
  2. package/dist/cli.d.ts +2 -0
  3. package/dist/cli.js +33 -0
  4. package/dist/commands/create.d.ts +4 -0
  5. package/dist/commands/create.js +56 -0
  6. package/dist/commands/install.d.ts +1 -0
  7. package/dist/commands/install.js +79 -0
  8. package/dist/commands/list.d.ts +1 -0
  9. package/dist/commands/list.js +24 -0
  10. package/dist/commands/validate.d.ts +1 -0
  11. package/dist/commands/validate.js +27 -0
  12. package/dist/generator/analyzer.d.ts +2 -0
  13. package/dist/generator/analyzer.js +14 -0
  14. package/dist/generator/engine.d.ts +10 -0
  15. package/dist/generator/engine.js +46 -0
  16. package/dist/parsers/ai-parser.d.ts +2 -0
  17. package/dist/parsers/ai-parser.js +7 -0
  18. package/dist/parsers/detector.d.ts +6 -0
  19. package/dist/parsers/detector.js +38 -0
  20. package/dist/parsers/openapi.d.ts +5 -0
  21. package/dist/parsers/openapi.js +205 -0
  22. package/dist/parsers/postman.d.ts +2 -0
  23. package/dist/parsers/postman.js +4 -0
  24. package/dist/registry/manager.d.ts +13 -0
  25. package/dist/registry/manager.js +43 -0
  26. package/dist/schema/api-schema.d.ts +77 -0
  27. package/dist/schema/api-schema.js +1 -0
  28. package/dist/utils/errors.d.ts +13 -0
  29. package/dist/utils/errors.js +26 -0
  30. package/dist/utils/logger.d.ts +7 -0
  31. package/dist/utils/logger.js +19 -0
  32. package/docs/plans/2026-02-02-mcp-factory-design.md +306 -0
  33. package/docs/plans/2026-02-02-mcp-factory-implementation.md +1866 -0
  34. package/package.json +48 -0
  35. package/src/cli.ts +41 -0
  36. package/src/commands/create.ts +65 -0
  37. package/src/commands/install.ts +92 -0
  38. package/src/commands/list.ts +28 -0
  39. package/src/commands/validate.ts +29 -0
  40. package/src/generator/analyzer.ts +20 -0
  41. package/src/generator/engine.ts +73 -0
  42. package/src/parsers/ai-parser.ts +10 -0
  43. package/src/parsers/detector.ts +49 -0
  44. package/src/parsers/openapi.ts +238 -0
  45. package/src/parsers/postman.ts +6 -0
  46. package/src/registry/manager.ts +62 -0
  47. package/src/schema/api-schema.ts +87 -0
  48. package/src/utils/errors.ts +27 -0
  49. package/src/utils/logger.ts +23 -0
  50. package/templates/README.md.hbs +40 -0
  51. package/templates/client.ts.hbs +45 -0
  52. package/templates/index.ts.hbs +36 -0
  53. package/templates/package.json.hbs +20 -0
  54. package/templates/test.ts.hbs +1 -0
  55. package/templates/tools.ts.hbs +38 -0
  56. package/templates/tsconfig.json.hbs +13 -0
  57. package/templates/types.ts.hbs +1 -0
  58. package/templates/validation.ts.hbs +1 -0
  59. package/test-fixtures/weather-api.json +49 -0
  60. package/tsconfig.json +17 -0
@@ -0,0 +1,306 @@
1
+ # MCP Factory Design
2
+
3
+ **Date:** 2026-02-02
4
+ **Status:** Approved
5
+
6
+ ## Problem Statement
7
+
8
+ Building MCP servers from API documentation requires significant manual effort for each new API. This design establishes a framework to eliminate this repetitive work by generating production-ready MCP servers from API documentation in a single Claude Code session.
9
+
10
+ ## Goal
11
+
12
+ Create a CLI tool that transforms API documentation (OpenAPI specs, Swagger, Postman collections, raw docs, or URLs) into complete, production-ready MCP servers that work immediately with Claude Desktop and Claude Code, requiring zero additional work.
13
+
14
+ ## Success Criteria
15
+
16
+ - Generate working MCP servers from any API documentation in one command
17
+ - Follow best practices (TypeScript, proper validation, error handling, logging)
18
+ - Handle auth patterns automatically (API keys, Bearer tokens, OAuth, etc.)
19
+ - Work immediately with Claude Desktop/Code without configuration debugging
20
+ - Include tests that validate server functionality
21
+ - Produce comprehensive documentation
22
+ - Generate zero technical debt
23
+
24
+ ---
25
+
26
+ ## System Overview
27
+
28
+ The MCP Factory is a TypeScript-based CLI tool that transforms API documentation into production-ready MCP servers through a three-stage pipeline:
29
+
30
+ ### Stage 1: Input Processing
31
+
32
+ The CLI accepts diverse inputs (file paths, URLs, or current directory scan) and auto-detects the format. A format detector identifies OpenAPI/Swagger specs (JSON/YAML), Postman collections, or raw documentation. Structured formats are parsed directly using specialized parsers. When structured specs aren't found, an optional AI-powered parser (using Claude API with `--ai-parse` flag) extracts API structure from unstructured docs.
33
+
34
+ ### Stage 2: Normalization
35
+
36
+ All parsers output a unified internal schema called `APISchema`. This schema captures endpoints, parameters, authentication requirements, request/response types, pagination patterns, rate limiting info, and error response structures. The normalization layer ensures the generation stage works with consistent data regardless of input format.
37
+
38
+ ### Stage 3: Code Generation
39
+
40
+ A template engine uses the `APISchema` to generate a complete TypeScript MCP server package. Templates are pattern-aware - they detect cursor vs offset pagination, different auth patterns, rate limit headers, etc., and generate code specific to what the API actually uses. The output is a minimal but complete package ready for immediate use.
41
+
42
+ ### CLI State Management
43
+
44
+ The tool maintains a simple registry (`~/.mcp-factory/registry.json`) tracking generated servers for the `list` and `install` commands.
45
+
46
+ ---
47
+
48
+ ## CLI Architecture
49
+
50
+ ### Project Structure
51
+
52
+ ```
53
+ mcp-factory/
54
+ ├── src/
55
+ │ ├── cli.ts # Entry point, command routing
56
+ │ ├── commands/
57
+ │ │ ├── create.ts # Create command logic
58
+ │ │ ├── validate.ts # Validate command logic
59
+ │ │ ├── list.ts # List command logic
60
+ │ │ └── install.ts # Install command logic
61
+ │ ├── parsers/
62
+ │ │ ├── detector.ts # Format auto-detection
63
+ │ │ ├── openapi.ts # OpenAPI/Swagger parser
64
+ │ │ ├── postman.ts # Postman collection parser
65
+ │ │ └── ai-parser.ts # Claude API-powered parser
66
+ │ ├── schema/
67
+ │ │ └── api-schema.ts # Unified APISchema type definitions
68
+ │ ├── generator/
69
+ │ │ ├── engine.ts # Template engine core
70
+ │ │ ├── analyzer.ts # Pattern detection (pagination, auth, etc.)
71
+ │ │ └── templates/ # Handlebars templates
72
+ │ ├── registry/
73
+ │ │ └── manager.ts # Registry CRUD operations
74
+ │ └── utils/
75
+ │ ├── errors.ts # Error handling utilities
76
+ │ └── logger.ts # CLI logging
77
+ ├── templates/ # Template files (copied to dist)
78
+ └── package.json
79
+ ```
80
+
81
+ ### Technology Stack
82
+
83
+ - **Commander.js** for CLI parsing
84
+ - **Handlebars** for templating
85
+ - **Zod** for schema validation
86
+ - **Anthropic SDK** for AI parsing (optional)
87
+ - **openapi-typescript** for OpenAPI parsing
88
+
89
+ The CLI is published as `@mcp-factory/cli` on npm and installed globally with `npm install -g @mcp-factory/cli`.
90
+
91
+ ### CLI Commands
92
+
93
+ - `mcp-factory create <input>` - Generate MCP server from API documentation
94
+ - `mcp-factory validate <input>` - Validate API spec without generating code
95
+ - `mcp-factory list` - List all generated servers from registry
96
+ - `mcp-factory install <server-name>` - Auto-configure server in Claude Desktop/Code
97
+ - `mcp-factory --version` - Display CLI version
98
+ - `mcp-factory --help` - Show help information
99
+
100
+ ---
101
+
102
+ ## Input Processing Pipeline
103
+
104
+ ### Format Detection & Parsing Flow
105
+
106
+ When you run `mcp-factory create <input>`, the detector examines the input:
107
+
108
+ 1. If input is a URL, fetch the content first
109
+ 2. Check file extension (.json, .yaml, .yml)
110
+ 3. Parse and look for OpenAPI indicators (`openapi: "3.0"`, `swagger: "2.0"`)
111
+ 4. Check for Postman collection structure (`info.schema` field)
112
+ 5. If no structured format found and `--ai-parse` flag present, use AI parser
113
+ 6. Otherwise, fail with helpful error: "Could not detect format. Use --ai-parse for unstructured docs."
114
+
115
+ ### Unified APISchema
116
+
117
+ Each parser transforms its input into the unified `APISchema`:
118
+
119
+ ```typescript
120
+ interface APISchema {
121
+ name: string; // API name for package naming
122
+ baseUrl: string; // Base API URL
123
+ auth: AuthConfig; // Auth pattern detected
124
+ endpoints: Endpoint[]; // All available endpoints
125
+ commonHeaders?: Record<string, string>;
126
+ rateLimit?: RateLimitConfig;
127
+ pagination?: PaginationConfig;
128
+ }
129
+
130
+ interface Endpoint {
131
+ id: string; // Unique identifier for MCP tool
132
+ method: string; // HTTP method
133
+ path: string; // URL path with {params}
134
+ description: string; // Tool description
135
+ parameters: Parameter[]; // Query, path, body params
136
+ response: ResponseSchema; // Expected response structure
137
+ errors: ErrorSchema[]; // Known error responses
138
+ }
139
+ ```
140
+
141
+ ### AI Parser
142
+
143
+ The AI parser prompts Claude API with: "Extract API structure from these docs" and formats the response into `APISchema`. This handles unstructured documentation, raw HTML docs, curl examples, and incomplete specifications.
144
+
145
+ ---
146
+
147
+ ## Code Generation Engine
148
+
149
+ ### Pattern Analysis
150
+
151
+ Before generating code, the analyzer examines the `APISchema` to detect specific patterns:
152
+
153
+ ```typescript
154
+ interface DetectedPatterns {
155
+ authPattern: 'api-key' | 'bearer' | 'oauth' | 'basic' | 'none';
156
+ paginationStyle?: 'cursor' | 'offset' | 'page' | 'link-header';
157
+ rateLimitStrategy?: 'header-based' | 'retry-after' | 'none';
158
+ errorFormat: 'standard' | 'custom';
159
+ hasWebhooks: boolean;
160
+ }
161
+ ```
162
+
163
+ ### Template System
164
+
165
+ The engine uses Handlebars templates with the `APISchema` + `DetectedPatterns` as context:
166
+
167
+ **Core Templates:**
168
+ - `index.ts.hbs` - Main server with MCP SDK setup, tool registration
169
+ - `client.ts.hbs` - HTTP client with auth, retry logic, rate limiting
170
+ - `tools.ts.hbs` - Individual tool handlers for each endpoint
171
+ - `types.ts.hbs` - TypeScript interfaces from API schemas
172
+ - `validation.ts.hbs` - Zod schemas for input validation
173
+ - `test.ts.hbs` - Basic smoke test using Node test runner
174
+ - `package.json.hbs` - Dependencies and scripts
175
+ - `tsconfig.json.hbs` - TypeScript configuration
176
+ - `README.md.hbs` - Usage instructions and Claude config
177
+
178
+ Templates use conditionals (e.g., `{{#if patterns.pagination}}`) to only generate code for detected patterns. Each endpoint becomes an MCP tool with:
179
+ - Defensive input validation
180
+ - Detailed error messages
181
+ - Typed request/response handling
182
+ - Retry logic for rate limits/timeouts
183
+
184
+ ---
185
+
186
+ ## Generated Server Structure
187
+
188
+ ### Output Package Layout
189
+
190
+ ```
191
+ weather-api-mcp/
192
+ ├── src/
193
+ │ ├── index.ts # Main: MCP server setup, tool registration
194
+ │ ├── client.ts # HTTP client with auth & error handling
195
+ │ ├── tools.ts # Tool handler implementations
196
+ │ ├── types.ts # TypeScript interfaces for API models
197
+ │ └── validation.ts # Zod schemas for input validation
198
+ ├── build/ # Compiled output (after npm run build)
199
+ │ └── index.js
200
+ ├── test.ts # Basic smoke test
201
+ ├── package.json # Dependencies & scripts
202
+ ├── tsconfig.json # TypeScript config
203
+ └── README.md # Usage docs & Claude config instructions
204
+ ```
205
+
206
+ ### Key Dependencies
207
+
208
+ ```json
209
+ {
210
+ "dependencies": {
211
+ "@modelcontextprotocol/sdk": "^1.0.0",
212
+ "zod": "^3.22.0",
213
+ "node-fetch": "^3.3.0"
214
+ },
215
+ "devDependencies": {
216
+ "typescript": "^5.3.0",
217
+ "@types/node": "^20.0.0"
218
+ },
219
+ "scripts": {
220
+ "build": "tsc",
221
+ "test": "node --test test.ts"
222
+ }
223
+ }
224
+ ```
225
+
226
+ ### Generated Server Workflow
227
+
228
+ ```bash
229
+ cd weather-api-mcp
230
+ npm install
231
+ npm run build
232
+ npm test # Validates server starts correctly
233
+ ```
234
+
235
+ The generated server uses stdio transport and expects configuration (API keys) via the MCP config JSON when added to Claude Desktop/Code.
236
+
237
+ ---
238
+
239
+ ## Installation and Configuration
240
+
241
+ ### Auto-Configuration with `install` Command
242
+
243
+ The `mcp-factory install <server-name>` command automates adding generated servers to Claude Desktop and Claude Code configurations.
244
+
245
+ **Configuration File Locations:**
246
+ - **Claude Desktop (macOS):** `~/Library/Application Support/Claude/claude_desktop_config.json`
247
+ - **Claude Desktop (Windows):** `%APPDATA%\Claude\claude_desktop_config.json`
248
+ - **Claude Code:** `~/.claude/config.json`
249
+
250
+ ### Install Process
251
+
252
+ 1. Looks up server in registry (`~/.mcp-factory/registry.json`)
253
+ 2. Detects which Claude clients are installed (Desktop and/or Code)
254
+ 3. Reads existing config files
255
+ 4. Adds server entry with appropriate paths and env placeholders
256
+ 5. Writes updated config back
257
+ 6. Displays next steps (add API keys)
258
+
259
+ ### Generated Config Entry
260
+
261
+ ```json
262
+ {
263
+ "mcpServers": {
264
+ "weather-api": {
265
+ "command": "node",
266
+ "args": ["/absolute/path/to/weather-api-mcp/build/index.js"],
267
+ "env": {
268
+ "API_KEY": "YOUR_API_KEY_HERE"
269
+ }
270
+ }
271
+ }
272
+ }
273
+ ```
274
+
275
+ ### User Action Required
276
+
277
+ The install command outputs:
278
+ ```
279
+ ✓ Installed weather-api to Claude Desktop
280
+ → Edit config and replace YOUR_API_KEY_HERE with your actual API key
281
+ → Restart Claude Desktop to load the server
282
+ ```
283
+
284
+ ---
285
+
286
+ ## Design Decisions Summary
287
+
288
+ | Decision Point | Choice | Rationale |
289
+ |---------------|--------|-----------|
290
+ | Architecture | CLI code generation tool | Transparent, maintainable, no runtime dependencies |
291
+ | Input Handling | Auto-detect with AI fallback | Handles structured specs efficiently, AI for edge cases |
292
+ | Output Structure | Minimal monorepo-friendly package | Professional but lean, ready for production |
293
+ | Testing | One test file (Node built-in) | Validates functionality without external framework |
294
+ | Auth Config | MCP config JSON | Standard pattern, keeps secrets out of code |
295
+ | Code Generation | Template-based | Fast, deterministic, no API costs |
296
+ | Pattern Handling | Detect and generate | Lean output, API-specific code only |
297
+ | Error Handling | Defensive with detailed errors | Production-ready, easy debugging |
298
+ | CLI Commands | create, validate, list, install | Covers essential workflows without overengineering |
299
+
300
+ ---
301
+
302
+ ## Next Steps
303
+
304
+ 1. Set up git worktree for isolated development
305
+ 2. Create detailed implementation plan
306
+ 3. Phase-by-phase implementation with validation checkpoints