chore: clean up generate_mcp_types.py so codegen matches existing output (#1620)

This commit is contained in:
Michael Bolin
2025-07-18 21:40:39 -04:00
committed by GitHub
parent 83eefb55fb
commit a06d4f58e4

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# flake8: noqa: E501 # flake8: noqa: E501
import argparse
import json import json
import subprocess import subprocess
import sys import sys
@@ -26,19 +27,27 @@ DEFINITIONS: dict[str, Any] = {}
CLIENT_REQUEST_TYPE_NAMES: list[str] = [] CLIENT_REQUEST_TYPE_NAMES: list[str] = []
# Concrete *Notification types that make up the ServerNotification enum. # Concrete *Notification types that make up the ServerNotification enum.
SERVER_NOTIFICATION_TYPE_NAMES: list[str] = [] SERVER_NOTIFICATION_TYPE_NAMES: list[str] = []
# Enum types that will need a `allow(clippy::large_enum_variant)` annotation in
# order to compile without warnings.
LARGE_ENUMS = {"ServerResult"}
def main() -> int: def main() -> int:
num_args = len(sys.argv) parser = argparse.ArgumentParser(
if num_args == 1: description="Embed, cluster and analyse text prompts via the OpenAI API.",
schema_file = ( )
Path(__file__).resolve().parent / "schema" / SCHEMA_VERSION / "schema.json"
) default_schema_file = (
elif num_args == 2: Path(__file__).resolve().parent / "schema" / SCHEMA_VERSION / "schema.json"
schema_file = Path(sys.argv[1]) )
else: parser.add_argument(
print("Usage: python3 codegen.py <schema.json>") "schema_file",
return 1 nargs="?",
default=default_schema_file,
help="schema.json file to process",
)
args = parser.parse_args()
schema_file = args.schema_file
lib_rs = Path(__file__).resolve().parent / "src/lib.rs" lib_rs = Path(__file__).resolve().parent / "src/lib.rs"
@@ -197,6 +206,8 @@ def add_definition(name: str, definition: dict[str, Any], out: list[str]) -> Non
if name.endswith("Result"): if name.endswith("Result"):
out.extend(f"impl From<{name}> for serde_json::Value {{\n") out.extend(f"impl From<{name}> for serde_json::Value {{\n")
out.append(f" fn from(value: {name}) -> Self {{\n") out.append(f" fn from(value: {name}) -> Self {{\n")
out.append(" // Leave this as it should never fail\n")
out.append(" #[expect(clippy::unwrap_used)]\n")
out.append(" serde_json::to_value(value).unwrap()\n") out.append(" serde_json::to_value(value).unwrap()\n")
out.append(" }\n") out.append(" }\n")
out.append("}\n\n") out.append("}\n\n")
@@ -439,6 +450,8 @@ def define_any_of(
if serde := get_serde_annotation_for_anyof_type(name): if serde := get_serde_annotation_for_anyof_type(name):
out.append(serde + "\n") out.append(serde + "\n")
if name in LARGE_ENUMS:
out.append("#[allow(clippy::large_enum_variant)]\n")
out.append(f"pub enum {name} {{\n") out.append(f"pub enum {name} {{\n")
if name == "ClientRequest": if name == "ClientRequest":