|
| 1 | +import { writeFileSync } from 'node:fs'; |
| 2 | +import { dirname, join } from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url); |
| 6 | +const __dirname = dirname(__filename); |
| 7 | +const PROJECT_ROOT = join(__dirname, '..'); |
| 8 | + |
| 9 | +interface GitHubCommit { |
| 10 | + sha: string; |
| 11 | +} |
| 12 | + |
| 13 | +async function fetchLatestSHA(): Promise<string> { |
| 14 | + const url = 'https://api.github.com/repos/modelcontextprotocol/modelcontextprotocol/commits?path=schema/draft/schema.ts&per_page=1'; |
| 15 | + |
| 16 | + const response = await fetch(url); |
| 17 | + if (!response.ok) { |
| 18 | + throw new Error(`Failed to fetch commit info: ${response.status} ${response.statusText}`); |
| 19 | + } |
| 20 | + |
| 21 | + const commits = (await response.json()) as GitHubCommit[]; |
| 22 | + if (!commits || commits.length === 0) { |
| 23 | + throw new Error('No commits found'); |
| 24 | + } |
| 25 | + |
| 26 | + return commits[0].sha; |
| 27 | +} |
| 28 | + |
| 29 | +async function fetchSpecTypes(sha: string): Promise<string> { |
| 30 | + const url = `https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/${sha}/schema/draft/schema.ts`; |
| 31 | + |
| 32 | + const response = await fetch(url); |
| 33 | + if (!response.ok) { |
| 34 | + throw new Error(`Failed to fetch spec types: ${response.status} ${response.statusText}`); |
| 35 | + } |
| 36 | + |
| 37 | + return await response.text(); |
| 38 | +} |
| 39 | + |
| 40 | +async function main() { |
| 41 | + try { |
| 42 | + // Check if SHA is provided as command line argument |
| 43 | + const providedSHA = process.argv[2]; |
| 44 | + |
| 45 | + let latestSHA: string; |
| 46 | + if (providedSHA) { |
| 47 | + console.log(`Using provided SHA: ${providedSHA}`); |
| 48 | + latestSHA = providedSHA; |
| 49 | + } else { |
| 50 | + console.log('Fetching latest commit SHA...'); |
| 51 | + latestSHA = await fetchLatestSHA(); |
| 52 | + } |
| 53 | + |
| 54 | + console.log(`Fetching spec.types.ts from commit: ${latestSHA}`); |
| 55 | + |
| 56 | + const specContent = await fetchSpecTypes(latestSHA); |
| 57 | + |
| 58 | + // Read header template |
| 59 | + const headerTemplate = `/** |
| 60 | + * This file is automatically generated from the Model Context Protocol specification. |
| 61 | + * |
| 62 | + * Source: https://github.com/modelcontextprotocol/modelcontextprotocol |
| 63 | + * Pulled from: https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/main/schema/draft/schema.ts |
| 64 | + * Last updated from commit: {SHA} |
| 65 | + * |
| 66 | + * DO NOT EDIT THIS FILE MANUALLY. Changes will be overwritten by automated updates. |
| 67 | + * To update this file, run: npm run fetch:spec-types |
| 68 | + */`; |
| 69 | + |
| 70 | + const header = headerTemplate.replace('{SHA}', latestSHA); |
| 71 | + |
| 72 | + // Combine header and content |
| 73 | + const fullContent = header + specContent; |
| 74 | + |
| 75 | + // Write to file |
| 76 | + const outputPath = join(PROJECT_ROOT, 'src', 'spec.types.ts'); |
| 77 | + writeFileSync(outputPath, fullContent, 'utf-8'); |
| 78 | + |
| 79 | + console.log('Successfully updated src/spec.types.ts'); |
| 80 | + } catch (error) { |
| 81 | + console.error('Error:', error instanceof Error ? error.message : String(error)); |
| 82 | + process.exit(1); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +main(); |
0 commit comments