Skip to main content

claude_code_rs/types/
control.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use super::hooks::HookOutput;
5use super::permissions::PermissionResult;
6
7/// A control protocol request from the CLI to the SDK.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct SDKControlRequest {
10    /// Unique request ID for correlation.
11    pub id: String,
12    /// The type of control request.
13    #[serde(rename = "type")]
14    pub request_type: ControlRequestType,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(tag = "method", content = "params", rename_all = "snake_case")]
19pub enum ControlRequestType {
20    /// Permission check for tool usage.
21    CanUseTool {
22        tool_name: String,
23        input: Value,
24    },
25    /// Hook callback invocation.
26    HookCallback {
27        hook_event: String,
28        hook_input: Value,
29    },
30    /// MCP JSONRPC message routed to an SDK MCP server.
31    McpMessage {
32        server_name: String,
33        message: Value,
34    },
35}
36
37/// A control protocol response from the SDK to the CLI.
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct SDKControlResponse {
40    /// Correlation ID matching the request.
41    pub id: String,
42    /// Response payload.
43    #[serde(flatten)]
44    pub body: ControlResponseBody,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(untagged)]
49pub enum ControlResponseBody {
50    Permission(PermissionResult),
51    Hook(HookOutput),
52    McpMessage { message: Value },
53    Error { error: String },
54}
55
56/// A control command sent from the SDK to the CLI.
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct SDKControlCommand {
59    #[serde(rename = "type")]
60    pub command_type: String,
61    #[serde(flatten)]
62    pub params: Value,
63}
64
65impl SDKControlCommand {
66    pub fn interrupt() -> Self {
67        Self {
68            command_type: "interrupt".into(),
69            params: Value::Object(Default::default()),
70        }
71    }
72
73    pub fn set_permission_mode(mode: &str) -> Self {
74        Self {
75            command_type: "set_permission_mode".into(),
76            params: serde_json::json!({ "mode": mode }),
77        }
78    }
79
80    pub fn set_model(model: &str) -> Self {
81        Self {
82            command_type: "set_model".into(),
83            params: serde_json::json!({ "model": model }),
84        }
85    }
86
87    pub fn rewind_files(user_message_id: &str) -> Self {
88        Self {
89            command_type: "rewind_files".into(),
90            params: serde_json::json!({ "user_message_id": user_message_id }),
91        }
92    }
93
94    pub fn get_mcp_status() -> Self {
95        Self {
96            command_type: "get_mcp_status".into(),
97            params: Value::Object(Default::default()),
98        }
99    }
100}
101
102/// Init handshake message sent from the SDK to the CLI.
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct SDKInitMessage {
105    #[serde(rename = "type")]
106    pub msg_type: String,
107    pub protocol_version: String,
108    #[serde(default, skip_serializing_if = "Option::is_none")]
109    pub capabilities: Option<SDKCapabilities>,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize, Default)]
113pub struct SDKCapabilities {
114    #[serde(default)]
115    pub hooks: bool,
116    #[serde(default)]
117    pub permissions: bool,
118    #[serde(default)]
119    pub mcp: bool,
120    #[serde(default, skip_serializing_if = "Vec::is_empty")]
121    pub agent_definitions: Vec<Value>,
122    #[serde(default, skip_serializing_if = "Vec::is_empty")]
123    pub mcp_servers: Vec<Value>,
124}
125
126impl SDKInitMessage {
127    pub fn new(capabilities: SDKCapabilities) -> Self {
128        Self {
129            msg_type: "sdk_init".into(),
130            protocol_version: "1".into(),
131            capabilities: Some(capabilities),
132        }
133    }
134}
135
136/// Init response from the CLI.
137#[derive(Debug, Clone, Serialize, Deserialize)]
138pub struct SDKInitResponse {
139    #[serde(rename = "type")]
140    pub msg_type: String,
141    #[serde(default)]
142    pub protocol_version: Option<String>,
143    #[serde(default)]
144    pub session_id: Option<String>,
145}