Skip to main content

muse_code_sdk/msp/method/lifecycle/
session_mcp.rs

1// @generated from the MSP stable schema and protocol sources; do not edit by hand.
2
3use super::session_config_mt::SessionModelEndpointTransportOverride;
4use super::session_config_mt::SessionSettingsOverrides;
5use super::session_mcp_server::SessionMcpNameKind;
6pub use super::session_mcp_server::SessionMcpServerConfig;
7use super::session_mcp_server::validate_name;
8use serde::Deserialize;
9use serde::Deserializer;
10use serde::Serialize;
11use serde::de::Error as _;
12use std::collections::BTreeMap;
13use std::fmt;
14
15/// The open configuration extension object shared by `session/start` and
16/// `session/resume`.
17///
18/// Unknown keys are ignored on the wire, never rejected: the live decoder
19/// tolerates them, and no exported bundle carries `additionalProperties:
20/// false`. The Rust typed model's `deny_unknown_fields` is internal and never
21/// exported — the closedness export applies nowhere, and the `x-msp-closed`
22/// opt-in mechanism stays inert. Recognized `mcpServers` values are typed and
23/// validated before they leave the wire boundary.
24#[derive(Clone, Default, PartialEq, Eq, Serialize)]
25#[serde(rename_all = "camelCase")]
26pub struct SessionConfig {
27    /// Native MCP servers added only to this session runtime.
28    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
29    pub mcp_servers: BTreeMap<String, SessionMcpServerConfig>,
30    /// Experimental: a flat settings patch composing for this session's loads
31    /// only. Secret-shaped keys fail the decode.
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub settings_overrides: Option<SessionSettingsOverrides>,
34    /// Experimental: the model endpoint transport this session's model
35    /// consumers resolve against at load.
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub model_endpoint_transport_override: Option<SessionModelEndpointTransportOverride>,
38    /// Experimental: this session's tool-output byte budget, as `muse exec
39    /// --max-tool-output-bytes` sets it for a headless run. `0` means no limit;
40    /// a positive value must be at least 9. The budget applies only on a host
41    /// whose default-off `tool_output_budget` gate serves it: there, each
42    /// model-visible tool result, from workspace and MCP tools alike, is
43    /// bounded at this many bytes from the runtime's construction. Other hosts
44    /// accept a valid value and ignore it.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub max_tool_output_bytes: Option<u64>,
47    /// Experimental (owner decision: build tools and MCP first, design kt-2n1z):
48    /// the per-session built-in tool restriction on `session/start` and
49    /// `session/resume`: omitted = no restriction, `[]` = no built-in
50    /// tools, a non-empty list = exactly those tools. Names are exact,
51    /// case-sensitive built-in names; order is insignificant. This
52    /// decoder checks the SHAPE only. Until the last enforcing piece
53    /// lands, a host refuses any spelled member (P5, kt-kqr8).
54    #[serde(default, skip_serializing_if = "Option::is_none")]
55    pub allowed_built_in_tools: Option<Vec<String>>,
56    /// Experimental: per-session admission of this session's
57    /// `config.mcpServers` roster. Spelled `true` admits exactly that
58    /// roster; spelled `false` is withheld, spelled. Omitted keeps
59    /// today's withheld behaviour. Explicit `null` and any non-boolean
60    /// fail the decode. Until the admission gate and its enforcing
61    /// pieces land, the doors refuse every spelled value
62    /// unconditionally, so no host can believe a roster was admitted.
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub admit_session_mcp_servers: Option<bool>,
65    /// Developer instructions for this session only, on `session/start` and
66    /// `session/resume`. The text is trimmed. A nonblank value replaces the
67    /// settings-derived developer instructions; an omitted member is no
68    /// override and keeps the full developer context. Explicit `null` and blank
69    /// text are the explicit clear (the Rust value is an empty string): no
70    /// override, and on a host that serves developer instructions they also
71    /// turn off the session's suppressible developer context (the agent catalog
72    /// and the memory snapshot). Any other type fails the decode. A host that
73    /// does not serve developer instructions ignores a valid member.
74    #[serde(default, skip_serializing_if = "Option::is_none")]
75    pub developer_prompt: Option<String>,
76}
77
78impl fmt::Debug for SessionConfig {
79    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
80        formatter
81            .debug_struct("SessionConfig")
82            .field(
83                "mcp_server_names",
84                &self.mcp_servers.keys().collect::<Vec<_>>(),
85            )
86            .field("settings_overrides", &self.settings_overrides)
87            .field(
88                "model_endpoint_transport_override",
89                &self.model_endpoint_transport_override,
90            )
91            .field("max_tool_output_bytes", &self.max_tool_output_bytes)
92            .field("admit_session_mcp_servers", &self.admit_session_mcp_servers)
93            .field(
94                "allowed_built_in_tool_count",
95                &self.allowed_built_in_tools.as_ref().map(Vec::len),
96            )
97            .field(
98                "developer_prompt_bytes",
99                &self.developer_prompt.as_ref().map(String::len),
100            )
101            .finish()
102    }
103}
104
105impl<'de> Deserialize<'de> for SessionConfig {
106    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
107    where
108        D: Deserializer<'de>,
109    {
110        #[derive(Deserialize)]
111        #[serde(rename_all = "camelCase")]
112        struct Wire {
113            #[serde(default)]
114            mcp_servers: BTreeMap<String, SessionMcpServerConfig>,
115            #[serde(default)]
116            settings_overrides: Option<SessionSettingsOverrides>,
117            #[serde(default)]
118            model_endpoint_transport_override: Option<SessionModelEndpointTransportOverride>,
119            #[serde(default)]
120            max_tool_output_bytes: Option<u64>,
121            #[serde(default)]
122            allowed_built_in_tools: Option<Vec<String>>,
123        }
124
125        let value = serde_json::Value::deserialize(deserializer)?;
126        let object = value
127            .as_object()
128            .ok_or_else(|| D::Error::custom("session config must be an object"))?;
129        if object
130            .get("mcpServers")
131            .is_some_and(|servers| !servers.is_object())
132        {
133            return Err(D::Error::custom(
134                "session config mcpServers must be an object",
135            ));
136        }
137        if object
138            .get("settingsOverrides")
139            .is_some_and(|overrides| !overrides.is_object())
140        {
141            return Err(D::Error::custom(
142                "session config settingsOverrides must be an object",
143            ));
144        }
145        if object
146            .get("modelEndpointTransportOverride")
147            .is_some_and(|transport| !transport.is_object())
148        {
149            return Err(D::Error::custom(
150                "session config modelEndpointTransportOverride must be an object",
151            ));
152        }
153        if object
154            .get("maxToolOutputBytes")
155            .is_some_and(|bytes| !bytes.is_u64())
156        {
157            return Err(D::Error::custom(
158                "session config maxToolOutputBytes must be a non-negative integer",
159            ));
160        }
161        if object
162            .get("allowedBuiltInTools")
163            .is_some_and(|tools| !is_non_empty_string_array(tools))
164        {
165            return Err(D::Error::custom(
166                "session config allowedBuiltInTools must be an array of non-empty strings",
167            ));
168        }
169        let admit_session_mcp_servers = match object.get("admitSessionMcpServers") {
170            None => None,
171            Some(serde_json::Value::Bool(admit)) => Some(*admit),
172            Some(_) => {
173                return Err(D::Error::custom(
174                    "session config admitSessionMcpServers must be a boolean",
175                ));
176            }
177        };
178        let developer_prompt = match object.get("developerPrompt") {
179            None => None,
180            Some(serde_json::Value::Null) => Some(String::new()),
181            Some(serde_json::Value::String(text)) => Some(text.trim().to_owned()),
182            Some(_) => {
183                return Err(D::Error::custom(
184                    "session config developerPrompt must be a string or null",
185                ));
186            }
187        };
188        let wire: Wire = serde_json::from_value(value).map_err(D::Error::custom)?;
189        for name in wire.mcp_servers.keys() {
190            validate_name(name, SessionMcpNameKind::Server).map_err(D::Error::custom)?;
191        }
192        Ok(Self {
193            mcp_servers: wire.mcp_servers,
194            settings_overrides: wire.settings_overrides,
195            model_endpoint_transport_override: wire.model_endpoint_transport_override,
196            max_tool_output_bytes: wire.max_tool_output_bytes,
197            allowed_built_in_tools: wire.allowed_built_in_tools,
198            admit_session_mcp_servers,
199            developer_prompt,
200        })
201    }
202}
203
204/// The `allowedBuiltInTools` shape: an array whose every element is a
205/// non-blank string. Anything else fails the decode.
206fn is_non_empty_string_array(value: &serde_json::Value) -> bool {
207    value.as_array().is_some_and(|tools| {
208        tools
209            .iter()
210            .all(|tool| tool.as_str().is_some_and(|tool| !tool.trim().is_empty()))
211    })
212}