(
value: &serde_json::Value,
strict_tool_names: bool,
)
| 325 | } |
| 326 | |
| 327 | fn parse_mcp_call( |
| 328 | value: &serde_json::Value, |
| 329 | strict_tool_names: bool, |
| 330 | ) -> std::result::Result<JsonRpcCallInfo, String> { |
| 331 | if value.get("id").is_some() { |
| 332 | // Typed parsing validates known MCP params, but policy method profiles |
| 333 | // stay OpenShell-owned; see McpOptions in proto/sandbox.proto. |
| 334 | let request: JsonRpcRequest = serde_json::from_value(value.clone()) |
| 335 | .map_err(|error| format!("invalid MCP request: {error}"))?; |
| 336 | request |
| 337 | .validate() |
| 338 | .map_err(|error| format!("invalid MCP request: {error:?}"))?; |
| 339 | let mcp_request = McpRequest::from_jsonrpc(&request) |
| 340 | .map_err(|error| format!("invalid MCP request params: {error}"))?; |
| 341 | let tool = mcp_tool_name(&mcp_request); |
| 342 | if strict_tool_names && let Some(tool_name) = tool.as_deref() { |
| 343 | validate_mcp_tool_name(tool_name)?; |
| 344 | } |
| 345 | |
| 346 | let params = mcp_policy_params(tool.as_deref()); |
| 347 | return Ok(JsonRpcCallInfo { |
| 348 | method: mcp_request.method_name().to_string(), |
| 349 | params, |
| 350 | tool, |
| 351 | }); |
| 352 | } |
| 353 | |
| 354 | // Notifications have no id and no response expectation. Validate them as |
| 355 | // MCP notifications but keep extension notifications addressable. |
| 356 | let notification: JsonRpcNotification = serde_json::from_value(value.clone()) |
| 357 | .map_err(|error| format!("invalid MCP notification: {error}"))?; |
| 358 | if notification.jsonrpc != JSONRPC_VERSION { |
| 359 | return Err(format!( |
| 360 | "unsupported JSON-RPC version '{}'", |
| 361 | notification.jsonrpc |
| 362 | )); |
| 363 | } |
| 364 | McpNotification::from_jsonrpc(¬ification) |
| 365 | .map_err(|error| format!("invalid MCP notification params: {error}"))?; |
| 366 | |
| 367 | Ok(JsonRpcCallInfo { |
| 368 | method: notification.method, |
| 369 | params: HashMap::new(), |
| 370 | tool: None, |
| 371 | }) |
| 372 | } |
| 373 | |
| 374 | fn mcp_tool_name(request: &McpRequest) -> Option<String> { |
| 375 | if let McpRequest::CallTool(params) = request { |
no test coverage detected