1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
use libmcp::{
DetailLevel, JsonPorcelainConfig, RenderMode, render_json_porcelain,
with_presentation_properties,
};
use serde::Serialize;
use serde_json::{Value, json};
use crate::mcp::fault::{FaultKind, FaultRecord, FaultStage};
const CONCISE_PORCELAIN_MAX_LINES: usize = 12;
const CONCISE_PORCELAIN_MAX_INLINE_CHARS: usize = 160;
const FULL_PORCELAIN_MAX_LINES: usize = 40;
const FULL_PORCELAIN_MAX_INLINE_CHARS: usize = 512;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Presentation {
pub render: RenderMode,
pub detail: DetailLevel,
}
#[derive(Debug, Clone)]
pub(crate) struct ToolOutput {
concise: Value,
full: Value,
concise_text: String,
full_text: Option<String>,
}
impl ToolOutput {
#[must_use]
pub(crate) fn from_values(
concise: Value,
full: Value,
concise_text: impl Into<String>,
full_text: Option<String>,
) -> Self {
Self {
concise,
full,
concise_text: concise_text.into(),
full_text,
}
}
fn structured(&self, detail: DetailLevel) -> &Value {
match detail {
DetailLevel::Concise => &self.concise,
DetailLevel::Full => &self.full,
}
}
fn porcelain_text(&self, detail: DetailLevel) -> String {
match detail {
DetailLevel::Concise => self.concise_text.clone(),
DetailLevel::Full => self
.full_text
.clone()
.unwrap_or_else(|| render_json_porcelain(&self.full, full_porcelain_config())),
}
}
}
pub(crate) fn split_presentation(
arguments: Value,
operation: &str,
stage: FaultStage,
) -> Result<(Presentation, Value), FaultRecord> {
let Value::Object(mut object) = arguments else {
return Ok((Presentation::default(), arguments));
};
let render = object
.remove("render")
.map(|value| {
serde_json::from_value::<RenderMode>(value).map_err(|error| {
FaultRecord::new(
FaultKind::InvalidInput,
stage,
operation,
format!("invalid render mode: {error}"),
)
})
})
.transpose()?
.unwrap_or(RenderMode::Porcelain);
let detail = object
.remove("detail")
.map(|value| {
serde_json::from_value::<DetailLevel>(value).map_err(|error| {
FaultRecord::new(
FaultKind::InvalidInput,
stage,
operation,
format!("invalid detail level: {error}"),
)
})
})
.transpose()?
.unwrap_or(DetailLevel::Concise);
Ok((Presentation { render, detail }, Value::Object(object)))
}
pub(crate) fn tool_output(
value: &impl Serialize,
stage: FaultStage,
operation: &str,
) -> Result<ToolOutput, FaultRecord> {
let structured = serde_json::to_value(value).map_err(|error| {
FaultRecord::new(FaultKind::Internal, stage, operation, error.to_string())
})?;
let concise_text = render_json_porcelain(&structured, concise_porcelain_config());
Ok(ToolOutput::from_values(
structured.clone(),
structured,
concise_text,
None,
))
}
pub(crate) fn detailed_tool_output(
concise: &impl Serialize,
full: &impl Serialize,
concise_text: impl Into<String>,
full_text: Option<String>,
stage: FaultStage,
operation: &str,
) -> Result<ToolOutput, FaultRecord> {
let concise = serde_json::to_value(concise).map_err(|error| {
FaultRecord::new(FaultKind::Internal, stage, operation, error.to_string())
})?;
let full = serde_json::to_value(full).map_err(|error| {
FaultRecord::new(FaultKind::Internal, stage, operation, error.to_string())
})?;
Ok(ToolOutput::from_values(
concise,
full,
concise_text,
full_text,
))
}
pub(crate) fn tool_success(
output: ToolOutput,
presentation: Presentation,
stage: FaultStage,
operation: &str,
) -> Result<Value, FaultRecord> {
let structured = output.structured(presentation.detail).clone();
let text = match presentation.render {
RenderMode::Porcelain => output.porcelain_text(presentation.detail),
RenderMode::Json => crate::to_pretty_json(&structured).map_err(|error| {
FaultRecord::new(FaultKind::Internal, stage, operation, error.to_string())
})?,
};
Ok(json!({
"content": [{
"type": "text",
"text": text,
}],
"structuredContent": structured,
"isError": false,
}))
}
pub(crate) fn with_common_presentation(schema: Value) -> Value {
with_presentation_properties(schema)
}
const fn concise_porcelain_config() -> JsonPorcelainConfig {
JsonPorcelainConfig {
max_lines: CONCISE_PORCELAIN_MAX_LINES,
max_inline_chars: CONCISE_PORCELAIN_MAX_INLINE_CHARS,
}
}
const fn full_porcelain_config() -> JsonPorcelainConfig {
JsonPorcelainConfig {
max_lines: FULL_PORCELAIN_MAX_LINES,
max_inline_chars: FULL_PORCELAIN_MAX_INLINE_CHARS,
}
}
impl Default for Presentation {
fn default() -> Self {
Self {
render: RenderMode::Porcelain,
detail: DetailLevel::Concise,
}
}
}
|