Skip to content

Commit c48014e

Browse files
added --max_tokens ### to stdio mode
1 parent 5e5ba5a commit c48014e

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/bin/cratedocs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ enum Commands {
3232
/// Summarize output by stripping LICENSE and VERSION sections (TL;DR mode)
3333
#[arg(long)]
3434
tldr: bool,
35+
/// Maximum number of tokens for output (token-aware truncation)
36+
#[arg(long)]
37+
max_tokens: Option<usize>,
3538
},
3639
/// Run the server with HTTP/SSE interface
3740
Http {
@@ -112,7 +115,7 @@ async fn main() -> Result<()> {
112115
println!("{}", env!("CARGO_PKG_VERSION"));
113116
Ok(())
114117
},
115-
Commands::Stdio { debug, tldr } => run_stdio_server(debug, tldr).await,
118+
Commands::Stdio { debug, tldr, max_tokens } => run_stdio_server(debug, tldr, max_tokens).await,
116119
Commands::Http { address, debug } => run_http_server(address, debug).await,
117120
Commands::Test {
118121
tool,
@@ -148,7 +151,7 @@ async fn main() -> Result<()> {
148151
}
149152
}
150153

151-
async fn run_stdio_server(debug: bool, tldr: bool) -> Result<()> {
154+
async fn run_stdio_server(debug: bool, tldr: bool, max_tokens: Option<usize>) -> Result<()> {
152155
// Set up file appender for logging
153156
let file_appender = RollingFileAppender::new(Rotation::DAILY, "logs", "stdio-server.log");
154157

@@ -168,7 +171,7 @@ async fn run_stdio_server(debug: bool, tldr: bool) -> Result<()> {
168171

169172
// Create an instance of our documentation router
170173
// If tldr is needed globally, you may want to pass it to DocRouter or handle it in tool output
171-
let router = RouterService(DocRouter::new_with_tldr(tldr));
174+
let router = RouterService(DocRouter::new_with_tldr_and_max_tokens(tldr, max_tokens));
172175

173176
// Create and run the server
174177
let server = Server::new(router);

src/tools/docs/docs.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct DocRouter {
4949
pub client: Client,
5050
pub cache: DocCache,
5151
pub tldr: bool,
52+
pub max_tokens: Option<usize>,
5253
}
5354

5455
impl Default for DocRouter {
@@ -58,15 +59,19 @@ impl Default for DocRouter {
5859
}
5960

6061
impl DocRouter {
61-
pub fn new_with_tldr(tldr: bool) -> Self {
62+
pub fn new_with_tldr_and_max_tokens(tldr: bool, max_tokens: Option<usize>) -> Self {
6263
Self {
6364
client: Client::new(),
6465
cache: DocCache::new(),
6566
tldr,
67+
max_tokens,
6668
}
6769
}
70+
pub fn new_with_tldr(tldr: bool) -> Self {
71+
Self::new_with_tldr_and_max_tokens(tldr, None)
72+
}
6873
pub fn new() -> Self {
69-
Self::new_with_tldr(false)
74+
Self::new_with_tldr_and_max_tokens(false, None)
7075
}
7176

7277
// Fetch crate documentation from docs.rs
@@ -370,6 +375,7 @@ impl mcp_server::Router for DocRouter {
370375
let tool_name = tool_name.to_string();
371376
let arguments = arguments.clone();
372377
let tldr = self.tldr;
378+
let max_tokens = self.max_tokens;
373379

374380
Box::pin(async move {
375381
let mut result = match tool_name.as_str() {
@@ -473,6 +479,27 @@ impl mcp_server::Router for DocRouter {
473479
}
474480
}
475481

482+
// Apply max_tokens truncation if enabled
483+
if let Some(max_tokens) = max_tokens {
484+
for content in &mut result {
485+
if let Content::Text(text) = content {
486+
if let Ok(token_count) = crate::tools::count_tokens(&text.text) {
487+
if token_count > max_tokens {
488+
let mut truncated = text.text.clone();
489+
while crate::tools::count_tokens(&truncated).map_or(0, |c| c) > max_tokens && !truncated.is_empty() {
490+
truncated.pop();
491+
}
492+
if let Some(last_space) = truncated.rfind(' ') {
493+
truncated.truncate(last_space);
494+
}
495+
truncated.push_str(" 内容被截断");
496+
text.text = truncated;
497+
}
498+
}
499+
}
500+
}
501+
}
502+
476503
Ok(result)
477504
})
478505
}

0 commit comments

Comments
 (0)