fix: ensure jsonrpc field is serialized as "2.0"

This commit is contained in:
Michael Bolin
2025-05-02 14:38:59 -07:00
parent 83961e0299
commit c8f85b45e1
3 changed files with 34 additions and 5 deletions

View File

@@ -13,6 +13,8 @@ from pathlib import Path
# Helper first so it is defined when other functions call it.
from typing import Any, Literal
SCHEMA_VERSION = "2025-03-26"
JSONRPC_VERSION = "2.0"
STANDARD_DERIVE = "#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]\n"
@@ -30,7 +32,7 @@ def main() -> int:
num_args = len(sys.argv)
if num_args == 1:
schema_file = (
Path(__file__).resolve().parent / "schema" / "2025-03-26" / "schema.json"
Path(__file__).resolve().parent / "schema" / SCHEMA_VERSION / "schema.json"
)
elif num_args == 2:
schema_file = Path(sys.argv[1])
@@ -61,6 +63,9 @@ use serde::Serialize;
use serde::de::DeserializeOwned;
use std::convert::TryFrom;
pub const MCP_SCHEMA_VERSION: &str = "{SCHEMA_VERSION}";
pub const JSONRPC_VERSION: &str = "{JSONRPC_VERSION}";
/// Paired request/response types for the Model Context Protocol (MCP).
pub trait ModelContextProtocolRequest {
const METHOD: &'static str;
@@ -74,6 +79,8 @@ pub trait ModelContextProtocolNotification {
type Params: DeserializeOwned + Serialize + Send + Sync + 'static;
}
fn default_jsonrpc() -> String {{ JSONRPC_VERSION.to_owned() }}
"""
]
definitions = schema_json["definitions"]
@@ -245,10 +252,6 @@ class StructField:
serde: str | None = None
def append(self, out: list[str], supports_const: bool) -> None:
# Omit these for now.
if self.name == "jsonrpc":
return
if self.serde:
out.append(f" {self.serde}\n")
if self.viz == "const":
@@ -273,6 +276,16 @@ def define_struct(
if prop_name == "_meta":
# TODO?
continue
elif prop_name == "jsonrpc":
fields.append(
StructField(
"pub",
"jsonrpc",
"String", # cannot use `&'static str` because of Deserialize
'#[serde(rename = "jsonrpc", default = "default_jsonrpc")]',
)
)
continue
prop_type = map_type(prop, prop_name, name)
if prop_name not in required_props:

View File

@@ -10,6 +10,9 @@ use serde::Deserialize;
use serde::Serialize;
use std::convert::TryFrom;
pub const MCP_SCHEMA_VERSION: &str = "2025-03-26";
pub const JSONRPC_VERSION: &str = "2.0";
/// Paired request/response types for the Model Context Protocol (MCP).
pub trait ModelContextProtocolRequest {
const METHOD: &'static str;
@@ -23,6 +26,10 @@ pub trait ModelContextProtocolNotification {
type Params: DeserializeOwned + Serialize + Send + Sync + 'static;
}
fn default_jsonrpc() -> String {
JSONRPC_VERSION.to_owned()
}
/// Optional annotations for the client. The client can use annotations to inform how objects are used or displayed
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Annotations {
@@ -370,6 +377,8 @@ pub type JSONRPCBatchResponse = Vec<JSONRPCBatchResponseItem>;
pub struct JSONRPCError {
pub error: JSONRPCErrorError,
pub id: RequestId,
#[serde(rename = "jsonrpc", default = "default_jsonrpc")]
pub jsonrpc: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
@@ -394,6 +403,8 @@ pub enum JSONRPCMessage {
/// A notification which does not expect a response.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct JSONRPCNotification {
#[serde(rename = "jsonrpc", default = "default_jsonrpc")]
pub jsonrpc: String,
pub method: String,
pub params: Option<serde_json::Value>,
}
@@ -402,6 +413,8 @@ pub struct JSONRPCNotification {
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct JSONRPCRequest {
pub id: RequestId,
#[serde(rename = "jsonrpc", default = "default_jsonrpc")]
pub jsonrpc: String,
pub method: String,
pub params: Option<serde_json::Value>,
}
@@ -410,6 +423,8 @@ pub struct JSONRPCRequest {
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct JSONRPCResponse {
pub id: RequestId,
#[serde(rename = "jsonrpc", default = "default_jsonrpc")]
pub jsonrpc: String,
pub result: Result,
}

View File

@@ -30,6 +30,7 @@ fn deserialize_initialize_request() {
};
let expected_req = JSONRPCRequest {
jsonrpc: JSONRPC_VERSION.into(),
id: RequestId::Integer(1),
method: "initialize".into(),
params: Some(json!({