@type-crafter/mcp 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.
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@type-crafter/mcp",
3
+ "version": "0.1.0",
4
+ "description": "MCP server for Type Crafter - generate types from YAML specifications",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "bin": {
8
+ "type-crafter-mcp": "./dist/index.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "README.md",
13
+ "LICENSE",
14
+ "src/GUIDE.md",
15
+ "src/SPEC_RULES.md"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "dev": "tsc --watch",
20
+ "start": "node dist/index.js",
21
+ "lint": "eslint src --ext .ts",
22
+ "lint:fix": "eslint src --ext .ts --fix",
23
+ "format": "prettier --write \"**/*.{ts,md,json}\"",
24
+ "format:check": "prettier --check \"**/*.{ts,md,json}\"",
25
+ "prepublishOnly": "npm run build",
26
+ "prepack": "npm run build"
27
+ },
28
+ "keywords": [
29
+ "mcp",
30
+ "mcp-server",
31
+ "model-context-protocol",
32
+ "type-crafter",
33
+ "type-generation",
34
+ "typescript",
35
+ "yaml",
36
+ "code-generation",
37
+ "claude",
38
+ "ai-tools"
39
+ ],
40
+ "author": "Sahil Sinha",
41
+ "license": "ISC",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/sinha-sahil/type-crafter.git",
45
+ "directory": "mcp-server"
46
+ },
47
+ "bugs": {
48
+ "url": "https://github.com/sinha-sahil/type-crafter/issues"
49
+ },
50
+ "homepage": "https://github.com/sinha-sahil/type-crafter/tree/release/mcp-server#readme",
51
+ "engines": {
52
+ "node": ">=18.0.0"
53
+ },
54
+ "peerDependencies": {
55
+ "type-crafter": ">=0.11.0"
56
+ },
57
+ "peerDependenciesMeta": {
58
+ "type-crafter": {
59
+ "optional": false
60
+ }
61
+ },
62
+ "dependencies": {
63
+ "@modelcontextprotocol/sdk": "^1.0.4",
64
+ "yaml": "^2.3.2"
65
+ },
66
+ "devDependencies": {
67
+ "@types/node": "^20.8.4",
68
+ "@typescript-eslint/eslint-plugin": "^6.7.5",
69
+ "@typescript-eslint/parser": "^6.7.5",
70
+ "eslint": "^8.51.0",
71
+ "prettier": "^3.7.4",
72
+ "typescript": "^5.2.2"
73
+ }
74
+ }
package/src/GUIDE.md ADDED
@@ -0,0 +1,365 @@
1
+ # Type Crafter MCP Server - LLM Usage Guide
2
+
3
+ This guide helps AI assistants understand the Type Crafter workflow and how to use the MCP server effectively.
4
+
5
+ ## What is Type Crafter?
6
+
7
+ **Type Crafter** is a CLI tool that generates TypeScript type definitions from YAML specification files. Think of it as "OpenAPI Generator" but specifically for type definitions.
8
+
9
+ **The MCP Server** exposes Type Crafter functionality through Model Context Protocol, allowing AI assistants to help users create type definitions from YAML specs.
10
+
11
+ ## Core Concepts
12
+
13
+ ### YAML Spec → TypeScript Types
14
+
15
+ ```
16
+ Input (YAML): Output (TypeScript):
17
+ ------------- --------------------
18
+ types: export type User = {
19
+ User: id: string;
20
+ type: object email: string;
21
+ required: name: string | null;
22
+ - id };
23
+ - email
24
+ properties:
25
+ id: { type: string }
26
+ email: { type: string }
27
+ name: { type: string }
28
+ ```
29
+
30
+ ### Key Rules (Critical!)
31
+
32
+ 1. **Arrays cannot be top-level types** - They must be properties within objects
33
+ 2. **Properties not in `required` become `Type | null`** - This is automatic
34
+ 3. **External paths are from project root** - Not relative to current file
35
+ 4. **Two organization modes**:
36
+ - `types` - Flat/top-level types
37
+ - `groupedTypes` - Organized into groups/namespaces
38
+
39
+ ## Available Tools
40
+
41
+ ### 1. `get-spec-rules` 📖
42
+
43
+ **When to use:** ALWAYS use this FIRST before creating or modifying YAML specs
44
+
45
+ **What it does:** Returns comprehensive rules for writing Type Crafter YAML specs
46
+
47
+ **Workflow:**
48
+
49
+ ```
50
+ User asks: "Create types for my API"
51
+
52
+ 1. Call get-spec-rules to learn the format
53
+ 2. Read the returned rules (nullable types, arrays, paths, etc.)
54
+ 3. Now you know how to write valid YAML specs
55
+ ```
56
+
57
+ ### 2. `validate-spec` ✅
58
+
59
+ **When to use:** After creating or modifying a YAML spec
60
+
61
+ **What it does:** Validates the spec structure without generating types
62
+
63
+ **Workflow:**
64
+
65
+ ```
66
+ You created: my-types.yaml
67
+
68
+ 1. Call validate-spec with path to my-types.yaml
69
+ 2. Check if valid or see error messages
70
+ 3. Fix errors if needed and validate again
71
+ ```
72
+
73
+ ### 3. `generate-types` 🔨
74
+
75
+ **When to use:** After spec is validated and user wants TypeScript output
76
+
77
+ **What it does:** Generates TypeScript type definitions from the YAML spec
78
+
79
+ **Parameters:**
80
+
81
+ - `language`: `typescript` or `typescript-with-decoders`
82
+ - `specFilePath`: Path to YAML spec (from project root)
83
+ - `outputDirectory`: Where to save generated .ts files
84
+ - `typesWriterMode`: `SingleFile` or `Files` (default: SingleFile)
85
+ - `groupedTypesWriterMode`: `FolderWithFiles` or `SingleFile` (default: SingleFile)
86
+
87
+ **Workflow:**
88
+
89
+ ```
90
+ Spec is valid
91
+
92
+ 1. Call generate-types with appropriate parameters
93
+ 2. Types are generated in outputDirectory
94
+ 3. User can now import and use them in TypeScript
95
+ ```
96
+
97
+ ### 4. `get-spec-info` 📊
98
+
99
+ **When to use:** To understand what's in an existing spec file
100
+
101
+ **What it does:** Returns version, title, and lists all defined types
102
+
103
+ **Workflow:**
104
+
105
+ ```
106
+ User asks: "What types are in my spec?"
107
+
108
+ 1. Call get-spec-info with path to spec
109
+ 2. See all top-level types and grouped types
110
+ 3. Understand the structure before modifying
111
+ ```
112
+
113
+ ### 5. `list-languages` 📝
114
+
115
+ **When to use:** User asks what output formats are supported
116
+
117
+ **What it does:** Lists supported target languages
118
+
119
+ **Returns:**
120
+
121
+ - `typescript` - TypeScript type definitions
122
+ - `typescript-with-decoders` - TypeScript + runtime decoders
123
+
124
+ ## Complete Workflows
125
+
126
+ ### Workflow 1: Creating Types from Scratch
127
+
128
+ ```
129
+ User: "I need types for my e-commerce cart API"
130
+
131
+ Step 1: Learn the rules
132
+ → Call: get-spec-rules
133
+ → Read and understand: nullable types, arrays, structure
134
+
135
+ Step 2: Gather requirements
136
+ → Ask user: What fields does the cart have?
137
+ → Ask user: What's required vs optional?
138
+
139
+ Step 3: Create YAML spec
140
+ → Create spec file following the rules
141
+ → Remember: Properties not in 'required' → Type | null
142
+ → Remember: Arrays must be properties, not top-level
143
+
144
+ Step 4: Validate
145
+ → Call: validate-spec with the spec path
146
+ → Fix any errors
147
+
148
+ Step 5: Generate
149
+ → Call: generate-types with language and paths
150
+ → Types are now available in output directory
151
+
152
+ Step 6: Confirm
153
+ → Tell user where types were generated
154
+ → Optionally show a snippet of generated types
155
+ ```
156
+
157
+ ### Workflow 2: Modifying Existing Spec
158
+
159
+ ```
160
+ User: "Add a 'description' field to the Product type"
161
+
162
+ Step 1: Understand current spec
163
+ → Call: get-spec-info to see what types exist
164
+ → Optionally: get-spec-rules to refresh on rules
165
+
166
+ Step 2: Read the spec file
167
+ → Use file reading tools to see current Product type
168
+
169
+ Step 3: Modify the spec
170
+ → Add the new field
171
+ → Decide if it's required or optional (Type | null)
172
+
173
+ Step 4: Validate
174
+ → Call: validate-spec
175
+
176
+ Step 5: Regenerate
177
+ → Call: generate-types to update TypeScript files
178
+ ```
179
+
180
+ ### Workflow 3: Validating User's Spec
181
+
182
+ ```
183
+ User: "Can you check if my spec is valid?"
184
+
185
+ Step 1: Validate
186
+ → Call: validate-spec with their spec path
187
+
188
+ Step 2: Report results
189
+ → If valid: Confirm and show summary
190
+ → If invalid: Explain errors and suggest fixes
191
+
192
+ Step 3: Optionally generate
193
+ → Ask if they want to generate types now
194
+ → Call: generate-types if yes
195
+ ```
196
+
197
+ ## Common Patterns
198
+
199
+ ### Pattern: Required vs Optional Fields
200
+
201
+ ```yaml
202
+ User:
203
+ type: object
204
+ required:
205
+ - id # Required → id: string
206
+ - email # Required → email: string
207
+ properties:
208
+ id: { type: string }
209
+ email: { type: string }
210
+ name: { type: string } # Optional → name: string | null
211
+ age: { type: number } # Optional → age: number | null
212
+ ```
213
+
214
+ **Key insight:** The `required` array controls nullability!
215
+
216
+ ### Pattern: Arrays as Properties
217
+
218
+ ```yaml
219
+ # ❌ INVALID - Cannot do this
220
+ Tags:
221
+ type: array
222
+ items: { type: string }
223
+
224
+ # ✅ VALID - Arrays must be properties
225
+ Post:
226
+ type: object
227
+ properties:
228
+ tags:
229
+ type: array
230
+ items: { type: string }
231
+ ```
232
+
233
+ ### Pattern: External References
234
+
235
+ ```yaml
236
+ # Paths from project root (where command runs)
237
+ Cart:
238
+ type: object
239
+ properties:
240
+ items:
241
+ type: array
242
+ items:
243
+ $ref: './src/types/cart.yaml#/CartItem'
244
+ ```
245
+
246
+ **Key insight:** Paths are from project root, NOT relative to current file!
247
+
248
+ ### Pattern: Grouped Types
249
+
250
+ ```yaml
251
+ groupedTypes:
252
+ # Group name
253
+ Auth:
254
+ # Types in the group
255
+ LoginRequest: { ... }
256
+ LoginResponse: { ... }
257
+ User: { ... }
258
+
259
+ # Reference grouped types
260
+ $ref: '#/groupedTypes/Auth/User'
261
+ ```
262
+
263
+ ## Error Handling
264
+
265
+ ### Common Errors
266
+
267
+ 1. **"Invalid specification file"**
268
+ - Missing `info.version` or `info.title`
269
+ - Neither `types` nor `groupedTypes` exists
270
+ - **Fix:** Add required fields
271
+
272
+ 2. **"Specification file not found"**
273
+ - Wrong path
274
+ - **Fix:** Use correct path from project root
275
+
276
+ 3. **"type-crafter command not found"**
277
+ - CLI not installed
278
+ - **Fix:** User needs: `npm install -g type-crafter`
279
+
280
+ ### When Things Go Wrong
281
+
282
+ 1. Re-read spec rules with `get-spec-rules`
283
+ 2. Validate the spec with `validate-spec`
284
+ 3. Check error messages carefully
285
+ 4. Fix one issue at a time
286
+ 5. Re-validate after each fix
287
+
288
+ ## Best Practices for LLMs
289
+
290
+ ### DO ✅
291
+
292
+ - **ALWAYS** call `get-spec-rules` before creating specs
293
+ - **ALWAYS** validate specs before generating
294
+ - Ask user about required vs optional fields
295
+ - Use clear, descriptive type names (PascalCase)
296
+ - Group related types using `groupedTypes`
297
+ - Add descriptions to complex types
298
+ - Explain to users what was generated
299
+
300
+ ### DON'T ❌
301
+
302
+ - Create top-level array types
303
+ - Assume paths are relative to current file
304
+ - Skip validation step
305
+ - Generate types without confirming output directory
306
+ - Forget that non-required properties become `Type | null`
307
+
308
+ ## Quick Reference
309
+
310
+ ```
311
+ First time creating spec?
312
+ → get-spec-rules
313
+
314
+ Created/modified spec?
315
+ → validate-spec
316
+
317
+ Spec is valid?
318
+ → generate-types
319
+
320
+ Need to see what's in a spec?
321
+ → get-spec-info
322
+
323
+ User asks about supported languages?
324
+ → list-languages
325
+ ```
326
+
327
+ ## Example: Complete Session
328
+
329
+ ```
330
+ User: "Create types for a shopping cart with items and total price"
331
+
332
+ LLM:
333
+ 1. [Calls get-spec-rules]
334
+ 2. "I'll create a YAML spec for your shopping cart. Let me ask a few questions:
335
+ - Should cart items have quantity? (yes/no)
336
+ - Is the cart token required or optional?
337
+ - Any other fields needed?"
338
+
339
+ User: "Yes to quantity, token is required, add a currency field too"
340
+
341
+ LLM:
342
+ 3. [Creates cart.yaml with proper structure]
343
+ 4. [Calls validate-spec on cart.yaml]
344
+ 5. "Spec is valid! Where should I generate the TypeScript types?"
345
+
346
+ User: "./src/types"
347
+
348
+ LLM:
349
+ 6. [Calls generate-types with language: typescript, outputDirectory: ./src/types]
350
+ 7. "✅ Generated TypeScript types in ./src/types/
351
+ - Cart type with token, items[], totalPrice, currency
352
+ - Items are CartItem[] (array of cart items)
353
+ - All fields except currency are required (currency is string | null)"
354
+ ```
355
+
356
+ ## Summary
357
+
358
+ The Type Crafter MCP server helps users create TypeScript types from YAML specifications. As an LLM:
359
+
360
+ 1. **Learn first** - Call `get-spec-rules` to understand the format
361
+ 2. **Validate always** - Use `validate-spec` before generating
362
+ 3. **Generate carefully** - Confirm paths and options with user
363
+ 4. **Remember the rules** - Arrays as properties, nullable types, root paths
364
+
365
+ With these tools, you can help users maintain type-safe TypeScript projects with clean, generated type definitions! 🚀