mcp_rs_sdk/
types.rs

1//! Contains all the data structures that represent the Model-Context-Protocol specification.
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5/// Represents the role of the entity creating a message.
6#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
7#[serde(rename_all = "camelCase")]
8pub enum Role {
9    System,
10    User,
11    Assistant,
12    Tool,
13}
14
15/// A single message in the conversation history.
16#[derive(Serialize, Deserialize, Debug, Clone)]
17#[serde(rename_all = "camelCase")]
18pub struct Message {
19    pub role: Role,
20    pub content: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub tool_calls: Option<Vec<ToolCall>>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub tool_call_id: Option<String>,
25}
26
27/// A request from the model to call a tool (a function).
28#[derive(Serialize, Deserialize, Debug, Clone)]
29#[serde(rename_all = "camelCase")]
30pub struct ToolCall {
31    pub id: String,
32    pub r#type: String,
33    pub function: FunctionCall,
34}
35
36/// The details of a function call requested by the model.
37#[derive(Serialize, Deserialize, Debug, Clone)]
38pub struct FunctionCall {
39    pub name: String,
40    pub arguments: String,
41}
42
43/// The result of a tool call, to be sent back to the model.
44#[derive(Serialize, Deserialize, Debug, Clone)]
45#[serde(rename_all = "camelCase")]
46pub struct ToolResult {
47    pub tool_call_id: String,
48    pub content: String,
49}
50
51/// A definition of a tool that the model can request to use.
52#[derive(Serialize, Deserialize, Debug, Clone)]
53#[serde(rename_all = "camelCase")]
54pub struct Tool {
55    pub r#type: String,
56    pub function: FunctionDefinition,
57}
58
59/// The definition of a function, including its parameters.
60#[derive(Serialize, Deserialize, Debug, Clone)]
61pub struct FunctionDefinition {
62    pub name: String,
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub description: Option<String>,
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub parameters: Option<ToolInputSchema>,
67}
68
69/// The JSON schema for a tool's input parameters.
70#[derive(Serialize, Deserialize, Debug, Clone)]
71pub struct ToolInputSchema {
72    pub r#type: String,
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub properties: Option<HashMap<String, serde_json::Value>>,
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub required: Option<Vec<String>>,
77}
78
79/// Information about the user interacting with the agent.
80#[derive(Serialize, Deserialize, Debug, Clone)]
81#[serde(rename_all = "camelCase")]
82pub struct UserInfo {
83    pub user_id: String,
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub display_name: Option<String>,
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub details: Option<HashMap<String, serde_json::Value>>,
88}
89
90/// The main request object sent from the MCP host to the agent.
91#[derive(Serialize, Deserialize, Debug, Clone)]
92#[serde(rename_all = "camelCase")]
93pub struct GetContextRequest {
94    pub messages: Vec<Message>,
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub tools: Option<Vec<Tool>>,
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub user: Option<UserInfo>,
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub max_tokens: Option<i32>,
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub client_context: Option<HashMap<String, serde_json::Value>>,
103}
104
105/// The response from an agent's handler, which can contain content, tool calls, or tool results.
106/// This is the primary object that agent handlers should create and return.
107#[derive(Serialize, Deserialize, Debug, Clone, Default)]
108#[serde(rename_all = "camelCase")]
109pub struct Response {
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub content: Option<String>,
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub tool_calls: Option<Vec<ToolCall>>,
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub tool_results: Option<Vec<ToolResult>>,
116}
117
118/// The final response object sent from the agent back to the MCP host in a non-streaming context.
119#[derive(Serialize, Deserialize, Debug, Clone)]
120#[serde(rename_all = "camelCase")]
121pub struct GetContextResponse {
122    pub response: Response,
123}
124
125// --- Streaming Types ---
126
127/// Represents a part of a streamed response from the agent.
128/// This is a wrapper around the standard `Response` object, allowing for incremental streaming.
129#[derive(Serialize, Deserialize, Debug, Clone)]
130#[serde(rename_all = "camelCase")]
131pub struct PartialResponse {
132    #[serde(flatten)]
133    pub response: Response,
134}