Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

cursor: tell the agent how to call non-native tools; plain Read text

Hector Bernstorffdbb42291b488
diff --git a/src/providers/cursor/request.rs b/src/providers/cursor/request.rs
index b9d8fd1..3739354 100644
--- a/src/providers/cursor/request.rs
+++ b/src/providers/cursor/request.rs
@@ -9,6 +9,17 @@ pub struct CursorSelectedImage {
pub mime_type: String,
}
+/// How the model calls a tool from `<tools>`. Without it the agent only knows
+/// its own built-in tools: it tried MCP tools and ToolSearch on Cursor's
+/// channel, and Cursor answered "Tool not available" (measured 13/10). The
+/// format is the one `tool_use_xml` parses and the transcript already uses.
+const TOOL_PROTOCOL: &str = "These tools run on the user's machine and are available in this \
+session, although they are not in your built-in tool list. To read files, write files or \
+run shell commands, use your built-in tools. To call any other tool listed here, write the \
+call as text, exactly in this form, and stop: \
+<tool_use name=\"TOOL_NAME\">{\"param\": \"value\"}</tool_use> \
+The result comes back as <tool_result>. Never call these tools through your built-in tool channel.";
+
/// Render the full Cursor prompt from an Anthropic MessagesRequest.
///
/// Includes:
@@ -35,6 +46,7 @@ pub fn render_cursor_prompt(req: &MessagesRequest) -> String {
{
let tool_lines: Vec<String> = tools
.iter()
+ .filter(|t| t.get("name").and_then(|n| n.as_str()) != Some("DeferredToolPlaceholder"))
.map(|t| {
let name = t.get("name").and_then(|n| n.as_str()).unwrap_or("");
let description = t.get("description").and_then(|d| d.as_str()).unwrap_or("");
@@ -51,7 +63,10 @@ pub fn render_cursor_prompt(req: &MessagesRequest) -> String {
})
.collect();
if !tool_lines.is_empty() {
- sections.push(format!("<tools>\n{}\n</tools>", tool_lines.join("\n")));
+ sections.push(format!(
+ "<tools>\n{TOOL_PROTOCOL}\n\n{}\n</tools>",
+ tool_lines.join("\n")
+ ));
}
}
@@ -228,6 +243,13 @@ fn render_tool_result_block(block: &serde_json::Value) -> Option<String> {
let block_type = block.get("type").and_then(|t| t.as_str())?;
match block_type {
"text" | "image" | "tool_use" | "tool_result" | "thinking" => render_block(block),
+ // Tool search: the harness answers ToolSearch with a reference and puts
+ // the tool's schema in the request's `tools`, where the prompt's
+ // <tools> section already carries it.
+ "tool_reference" => {
+ let name = block.get("tool_name").and_then(|n| n.as_str())?;
+ Some(format!("Tool loaded: {name} (its schema is in <tools>)."))
+ }
_ => {
let type_str = block_type.to_string();
Some(format!("[unsupported tool result block: {type_str}]"))
@@ -333,6 +355,7 @@ mod tests {
.unwrap();
let rendered = render_cursor_prompt(&req);
assert!(rendered.contains("<tools>"));
+ assert!(rendered.contains(r#"<tool_use name="TOOL_NAME">"#));
assert!(rendered.contains("Read"));
}
@@ -515,4 +538,27 @@ mod tests {
let rendered = render_cursor_prompt(&req);
assert!(rendered.contains("<web_search_tool_result tool_use_id=\"ws1\">"));
}
+
+ #[test]
+ fn tool_search_reference_renders_as_a_loaded_tool_and_the_placeholder_is_hidden() {
+ let req: MessagesRequest = serde_json::from_value(serde_json::json!({
+ "model": "cursor:grok-4.7",
+ "max_tokens": 16,
+ "messages": [
+ {"role": "user", "content": "use whoami"},
+ {"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "ToolSearch", "input": {"query": "select:whoami"}}]},
+ {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": [{"type": "tool_reference", "tool_name": "mcp__x__whoami"}]}]}
+ ],
+ "tools": [
+ {"name": "DeferredToolPlaceholder", "description": "", "input_schema": {"type": "object"}, "defer_loading": true},
+ {"name": "mcp__x__whoami", "description": "who", "input_schema": {"type": "object"}, "defer_loading": true}
+ ]
+ }))
+ .unwrap();
+ let rendered = render_cursor_prompt(&req);
+ assert!(rendered.contains("Tool loaded: mcp__x__whoami"));
+ assert!(!rendered.contains("unsupported tool result block"));
+ assert!(!rendered.contains("DeferredToolPlaceholder"));
+ assert!(rendered.contains("\"name\":\"mcp__x__whoami\""));
+ }
}
diff --git a/src/providers/cursor/tool_bridge.rs b/src/providers/cursor/tool_bridge.rs
index dabd308..a74775f 100644
--- a/src/providers/cursor/tool_bridge.rs
+++ b/src/providers/cursor/tool_bridge.rs
@@ -711,6 +711,32 @@ async fn answer(exec: &proto::ExecServerMessage, message: ExecAnswer, run: &Live
true
}
+/// Claude Code's Read answers in `cat -n` form (` 1\talpha`), and may add
+/// `<system-reminder>` text after the file. Cursor's `ReadSuccess.content` is
+/// the file itself: handing it the numbered form made the agent write the
+/// numbers back into the file (measured 13/10: `1\tbeta` on disk).
+fn raw_file_text(read_output: &str) -> String {
+ let mut lines = Vec::new();
+ for line in read_output.lines() {
+ let digits = line.trim_start();
+ let Some(tab) = digits.find('\t') else { break };
+ if tab == 0 || !digits[..tab].bytes().all(|b| b.is_ascii_digit()) {
+ break;
+ }
+ lines.push(&digits[tab + 1..]);
+ }
+ if lines.is_empty() {
+ // An empty file comes back as a reminder only; anything else is not
+ // the numbered form and passes through untouched.
+ return if read_output.trim_start().starts_with("<system-reminder>") {
+ String::new()
+ } else {
+ read_output.to_string()
+ };
+ }
+ lines.join("\n")
+}
+
fn stream_close(id: u32) -> proto::AgentClientMessage {
proto::AgentClientMessage {
exec_client_control_message: Some(proto::ExecClientControlMessage {
@@ -743,6 +769,7 @@ pub fn exec_result_messages(
error: content,
})
} else {
+ let content = raw_file_text(&content);
proto::read_result::Result::Success(proto::ReadSuccess {
path: path.clone(),
total_lines: content.lines().count() as i32,
@@ -1733,4 +1760,27 @@ mod tests {
});
BridgeRegistry::insert(bridge);
}
+
+ #[test]
+ fn read_result_gives_cursor_the_file_without_line_numbers() {
+ assert_eq!(raw_file_text("1\talpha\n2\t"), "alpha\n");
+ assert_eq!(
+ raw_file_text(" 1\tfn a() {}\n 2\t\tx\n"),
+ "fn a() {}\n\tx"
+ );
+ assert_eq!(
+ raw_file_text("1\tline\n\n<system-reminder>\nnote\n</system-reminder>"),
+ "line"
+ );
+ assert_eq!(
+ raw_file_text(
+ "<system-reminder>Warning: the file exists but the contents are empty.</system-reminder>"
+ ),
+ ""
+ );
+ assert_eq!(
+ raw_file_text("plain text, not numbered"),
+ "plain text, not numbered"
+ );
+ }
}