feat(core): per-revision spec reference types and the 2026-07-28 wire contract surface (#2252)
This commit is contained in:
parent
ab552c30d5
commit
8d55531dab
1 changed files with 4146 additions and 1005 deletions
|
|
@ -7,12 +7,32 @@ const __filename = fileURLToPath(import.meta.url);
|
|||
const __dirname = dirname(__filename);
|
||||
const PROJECT_ROOT = join(__dirname, '..');
|
||||
|
||||
/**
|
||||
* The protocol revisions the SDK keeps reference types for:
|
||||
* - `2025-11-25`: the frozen, released schema.
|
||||
* - `2026-07-28`: the upcoming protocol revision.
|
||||
*
|
||||
* Each is written to `packages/core/src/types/spec.types.<version>.ts`.
|
||||
*/
|
||||
const SUPPORTED_VERSIONS = ['2025-11-25', '2026-07-28'] as const;
|
||||
type SpecVersion = (typeof SUPPORTED_VERSIONS)[number];
|
||||
|
||||
/**
|
||||
* Upstream schema directory per revision. Until the 2026-07-28 revision is
|
||||
* published, its schema lives in the spec repository's draft directory; this
|
||||
* mapping drops once `schema/2026-07-28/` exists upstream.
|
||||
*/
|
||||
const UPSTREAM_SCHEMA_DIRS: Record<SpecVersion, string> = {
|
||||
'2025-11-25': '2025-11-25',
|
||||
'2026-07-28': 'draft'
|
||||
};
|
||||
|
||||
interface GitHubCommit {
|
||||
sha: string;
|
||||
}
|
||||
|
||||
async function fetchLatestSHA(): Promise<string> {
|
||||
const url = 'https://api.github.com/repos/modelcontextprotocol/modelcontextprotocol/commits?path=schema/draft/schema.ts&per_page=1';
|
||||
async function fetchLatestSHA(version: SpecVersion): Promise<string> {
|
||||
const url = `https://api.github.com/repos/modelcontextprotocol/modelcontextprotocol/commits?path=schema/${UPSTREAM_SCHEMA_DIRS[version]}/schema.ts&per_page=1`;
|
||||
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
|
|
@ -27,8 +47,8 @@ async function fetchLatestSHA(): Promise<string> {
|
|||
return commits[0].sha;
|
||||
}
|
||||
|
||||
async function fetchSpecTypes(sha: string): Promise<string> {
|
||||
const url = `https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/${sha}/schema/draft/schema.ts`;
|
||||
async function fetchSpecTypes(version: SpecVersion, sha: string): Promise<string> {
|
||||
const url = `https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/${sha}/schema/${UPSTREAM_SCHEMA_DIRS[version]}/schema.ts`;
|
||||
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
|
|
@ -38,49 +58,70 @@ async function fetchSpecTypes(sha: string): Promise<string> {
|
|||
return await response.text();
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
// Check if SHA is provided as command line argument
|
||||
const providedSHA = process.argv[2];
|
||||
async function updateSpecTypes(version: SpecVersion, providedSHA?: string): Promise<void> {
|
||||
let sha: string;
|
||||
if (providedSHA) {
|
||||
console.log(`[${version}] Using provided SHA: ${providedSHA}`);
|
||||
sha = providedSHA;
|
||||
} else {
|
||||
console.log(`[${version}] Fetching latest commit SHA...`);
|
||||
sha = await fetchLatestSHA(version);
|
||||
}
|
||||
|
||||
let latestSHA: string;
|
||||
if (providedSHA) {
|
||||
console.log(`Using provided SHA: ${providedSHA}`);
|
||||
latestSHA = providedSHA;
|
||||
} else {
|
||||
console.log('Fetching latest commit SHA...');
|
||||
latestSHA = await fetchLatestSHA();
|
||||
}
|
||||
console.log(`[${version}] Fetching schema.ts from commit: ${sha}`);
|
||||
|
||||
console.log(`Fetching spec.types.ts from commit: ${latestSHA}`);
|
||||
const specContent = await fetchSpecTypes(version, sha);
|
||||
|
||||
const specContent = await fetchSpecTypes(latestSHA);
|
||||
|
||||
// Read header template
|
||||
const headerTemplate = `/**
|
||||
// Read header template
|
||||
const headerTemplate = `/**
|
||||
* This file is automatically generated from the Model Context Protocol specification.
|
||||
*
|
||||
* Source: https://github.com/modelcontextprotocol/modelcontextprotocol
|
||||
* Pulled from: https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/main/schema/draft/schema.ts
|
||||
* Pulled from: https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/main/schema/{SCHEMA_DIR}/schema.ts
|
||||
* Last updated from commit: {SHA}
|
||||
*
|
||||
* DO NOT EDIT THIS FILE MANUALLY. Changes will be overwritten by automated updates.
|
||||
* To update this file, run: pnpm run fetch:spec-types
|
||||
* To update this file, run: pnpm run fetch:spec-types {VERSION}
|
||||
*/`;
|
||||
|
||||
const header = headerTemplate.replace('{SHA}', latestSHA);
|
||||
const header = headerTemplate.replaceAll('{VERSION}', version).replaceAll('{SCHEMA_DIR}', UPSTREAM_SCHEMA_DIRS[version]).replace('{SHA}', sha);
|
||||
|
||||
// Combine header and content
|
||||
const fullContent = header + specContent;
|
||||
// Combine header and content
|
||||
const fullContent = header + specContent;
|
||||
|
||||
// Format with prettier using the project's config so the output passes lint
|
||||
const outputPath = join(PROJECT_ROOT, 'packages', 'core', 'src', 'types', 'spec.types.ts');
|
||||
const prettierConfig = await prettier.resolveConfig(outputPath);
|
||||
const formatted = await prettier.format(fullContent, { ...prettierConfig, filepath: outputPath });
|
||||
// Format with prettier using the project's config so the output passes lint
|
||||
const outputPath = join(PROJECT_ROOT, 'packages', 'core', 'src', 'types', `spec.types.${version}.ts`);
|
||||
const prettierConfig = await prettier.resolveConfig(outputPath);
|
||||
const formatted = await prettier.format(fullContent, { ...prettierConfig, filepath: outputPath });
|
||||
|
||||
writeFileSync(outputPath, formatted, 'utf-8');
|
||||
writeFileSync(outputPath, formatted, 'utf-8');
|
||||
|
||||
console.log('Successfully updated packages/core/src/types/spec.types.ts');
|
||||
console.log(`[${version}] Successfully updated packages/core/src/types/spec.types.${version}.ts`);
|
||||
}
|
||||
|
||||
function isSupportedVersion(value: string): value is SpecVersion {
|
||||
return (SUPPORTED_VERSIONS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
// Usage: fetch-spec-types.ts [version] [sha]
|
||||
// With no version, all supported versions are fetched at their latest upstream SHA.
|
||||
const providedVersion = process.argv[2];
|
||||
const providedSHA = process.argv[3];
|
||||
|
||||
if (providedVersion === undefined) {
|
||||
for (const version of SUPPORTED_VERSIONS) {
|
||||
await updateSpecTypes(version);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isSupportedVersion(providedVersion)) {
|
||||
throw new Error(`Unsupported version "${providedVersion}". Supported versions: ${SUPPORTED_VERSIONS.join(', ')}`);
|
||||
}
|
||||
|
||||
await updateSpecTypes(providedVersion, providedSHA);
|
||||
} catch (error) {
|
||||
console.error('Error:', error instanceof Error ? error.message : String(error));
|
||||
process.exit(1);
|
||||
|
|
|
|||
Loading…
Reference in a new issue