Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: restore MCP startup with variable revisions (#11501)

Shahmir Varqha22b9846f6953
diff --git a/marimo/_messaging/notification.py b/marimo/_messaging/notification.py
index e6537c970c..94fd05595f 100644
--- a/marimo/_messaging/notification.py
+++ b/marimo/_messaging/notification.py
@@ -6,6 +6,7 @@ from __future__ import annotations
import re
import time
from typing import (
+ Annotated,
Any,
ClassVar,
Literal,
@@ -48,7 +49,7 @@ from marimo._types.ids import (
VariableName,
WidgetModelId,
)
-from marimo._utils.msgspec_basestruct import BaseStruct
+from marimo._utils.msgspec_basestruct import BaseStruct, MsgspecSchema
from marimo._utils.platform import is_pyodide, is_windows
LOGGER = loggers.marimo_logger()
@@ -717,7 +718,7 @@ class VariableValue(BaseStruct):
value: str | None
datatype: str | None
execution_id: str | None = None
- revision: Revision | None = None
+ revision: Annotated[Revision, MsgspecSchema] | None = None
class VariablesNotification(Notification, tag="variables"):
diff --git a/marimo/_utils/msgspec_basestruct.py b/marimo/_utils/msgspec_basestruct.py
index 36d9dea370..83d1bb4913 100644
--- a/marimo/_utils/msgspec_basestruct.py
+++ b/marimo/_utils/msgspec_basestruct.py
@@ -1,7 +1,7 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-from typing import TYPE_CHECKING, Any
+from typing import TYPE_CHECKING
import msgspec
@@ -13,43 +13,58 @@ if TYPE_CHECKING:
class BaseStruct(msgspec.Struct):
@classmethod
def __get_pydantic_core_schema__(
- cls, source_type: Any, handler: GetCoreSchemaHandler
+ cls, source_type: type[msgspec.Struct], handler: GetCoreSchemaHandler
) -> CoreSchema:
- # Lazy import pydantic_core
- from pydantic_core import core_schema
-
- # Build per-field schemas
- tdf: dict[str, core_schema.TypedDictField] = {}
- for f in msgspec.structs.fields(cls):
- tdf[f.name] = core_schema.typed_dict_field(
- schema=handler.generate_schema(f.type),
- required=(
- f.default is msgspec.UNSET
- and getattr(f, "default_factory", msgspec.UNSET)
- is msgspec.UNSET
- ),
- )
- td = core_schema.typed_dict_schema(tdf, total=True)
-
- # Create a function to convert a msgspec.Struct to a dictionary.
- def to_struct(values: dict[str, Any]) -> Any:
- return msgspec.convert(values, cls, from_attributes=True)
-
- # Create a chain schema to validate the dictionary and convert to the msgspec.Struct.
- chain = core_schema.chain_schema(
- [td, core_schema.no_info_plain_validator_function(to_struct)]
- )
+ return _struct_schema(cls, handler)
- # Return the json or python schema.
- return core_schema.json_or_python_schema(
- json_schema=chain,
- python_schema=core_schema.union_schema(
- [
- core_schema.is_instance_schema(cls), # fast-path
- chain,
- ]
- ),
- serialization=core_schema.plain_serializer_function_ser_schema(
- msgspec.to_builtins
+
+class MsgspecSchema:
+ """Pydantic annotation metadata for generated msgspec structs."""
+
+ @classmethod
+ def __get_pydantic_core_schema__(
+ cls, source_type: type[msgspec.Struct], handler: GetCoreSchemaHandler
+ ) -> CoreSchema:
+ return _struct_schema(source_type, handler)
+
+
+def _struct_schema(
+ struct_type: type[msgspec.Struct], handler: GetCoreSchemaHandler
+) -> CoreSchema:
+ # Lazy import pydantic_core
+ from pydantic_core import core_schema
+
+ # Build per-field schemas
+ tdf: dict[str, core_schema.TypedDictField] = {}
+ for f in msgspec.structs.fields(struct_type):
+ tdf[f.name] = core_schema.typed_dict_field(
+ schema=handler.generate_schema(f.type),
+ required=(
+ f.default is msgspec.UNSET
+ and getattr(f, "default_factory", msgspec.UNSET)
+ is msgspec.UNSET
),
)
+ td = core_schema.typed_dict_schema(tdf, total=True)
+
+ def to_struct(values: dict[str, object]) -> msgspec.Struct:
+ return msgspec.convert(values, struct_type, from_attributes=True)
+
+ # Create a chain schema to validate the dictionary and convert to the msgspec.Struct.
+ chain = core_schema.chain_schema(
+ [td, core_schema.no_info_plain_validator_function(to_struct)]
+ )
+
+ # Return the json or python schema.
+ return core_schema.json_or_python_schema(
+ json_schema=chain,
+ python_schema=core_schema.union_schema(
+ [
+ core_schema.is_instance_schema(struct_type), # fast-path
+ chain,
+ ]
+ ),
+ serialization=core_schema.plain_serializer_function_ser_schema(
+ msgspec.to_builtins
+ ),
+ )
diff --git a/tests/_utils/test_msgspec_basestruct.py b/tests/_utils/test_msgspec_basestruct.py
index ce5c4fa05a..e4f7ca8ba7 100644
--- a/tests/_utils/test_msgspec_basestruct.py
+++ b/tests/_utils/test_msgspec_basestruct.py
@@ -1,6 +1,9 @@
+import json
import typing as t
import msgspec
+import pytest
+from pydantic import TypeAdapter, ValidationError
from marimo._ai._tools.tools.cells import (
GetCellOutputArgs,
@@ -33,6 +36,8 @@ from marimo._ai._tools.tools.tables_and_variables import (
TablesAndVariablesArgs,
TablesAndVariablesOutput,
)
+from marimo._core.protocol import ExecutionId, Revision
+from marimo._messaging.notification import VariableValue
TOOL_IO_CLASSES = [
GetCellRuntimeDataArgs,
@@ -100,3 +105,38 @@ def test_tool_msgspec_structs_expose_pydantic_hook() -> None:
"These msgspec.Structs referenced by tools must use BaseStruct as their base class: "
+ ", ".join(offenders)
)
+
+
[email protected]("tool_io_class", TOOL_IO_CLASSES)
+def test_tool_io_exposes_pydantic_schema(tool_io_class: type) -> None:
+ TypeAdapter(tool_io_class).json_schema()
+
+
+def test_variable_value_revision_round_trip() -> None:
+ adapter = TypeAdapter(VariableValue)
+ value = VariableValue(
+ name="x",
+ value="1",
+ datatype="int",
+ execution_id="execution-1",
+ revision=Revision(execution=ExecutionId("execution-1"), update=2),
+ )
+ expected = {
+ "name": "x",
+ "value": "1",
+ "datatype": "int",
+ "execution_id": "execution-1",
+ "revision": {"execution": "execution-1", "update": 2},
+ }
+
+ assert adapter.validate_python(value) is value
+ assert adapter.validate_json(json.dumps(expected)) == value
+ assert adapter.dump_python(value, mode="json") == expected
+ assert json.loads(adapter.dump_json(value)) == expected
+
+ invalid = {
+ **expected,
+ "revision": {"execution": "execution-1", "update": -1},
+ }
+ with pytest.raises(ValidationError, match="Expected `int` >= 0"):
+ adapter.validate_json(json.dumps(invalid))