agent: Show MCP tool primary argument in tool-call header (#54604)

## Problem

Built-in tools like `Fetch` render their primary argument next to the
tool name in the collapsed tool-call header (e.g. `Fetch
https://example.com`). MCP tools don't — they always show `Run MCP tool
<name>` and the argument is hidden inside the expandable "Raw Input"
JSON block.

For Playwright, fetch, search, or any MCP tool where one argument
carries all the context, this makes every call opaque. You have to click
to expand just to see which URL is being opened or which file is being
read.

Before:

```
⊙ mcp__playwright__open_url_in_browser
  [click to expand]
  Raw Input:
  {
    "url": "https://linkedin.com/in/neuodev"
  }
```

After:

```
⊙ Run MCP tool `mcp__playwright__open_url_in_browser` https://linkedin.com/in/neuodev
```

## Root cause

`ContextServerTool::initial_title` at
`crates/agent/src/tools/context_server_registry.rs` discards the input:

```rust
fn initial_title(&self, _input: serde_json::Value, _cx: &mut App) -> SharedString {
    format!("Run MCP tool `{}`", self.tool.name).into()
}
```

Built-in tools read the input (`fetch_tool.rs`: `format!("Fetch {}",
MarkdownEscaped(&input.url))`) but the generic MCP path never did.

## Change

When the MCP tool's input is a JSON object with exactly one
string-valued field, inline that value next to the tool name. All other
shapes (multi-field objects, non-string values, null/empty input) fall
back to the existing `Run MCP tool <name>` label, so behavior is
unchanged for them.

- Value is truncated at 120 chars with `…` on overflow (char-safe, not
byte-safe — "café" won't panic).
- Value is run through `MarkdownEscaped` so markdown metacharacters
render literally.
- Single-string heuristic was chosen because the MCP spec has no
standard field for a display template, and the common case (`url`,
`path`, `query`, `selector`, `name`) is covered. I considered honoring
`Tool.annotations.title` or `_meta["zed/title"]` as a template — happy
to follow up with that if the team prefers an opt-in mechanism, but
wanted to start with something that works out of the box for every
existing MCP server.

## Tests

Added 10 unit tests in the existing `mod tests` block covering: happy
path, no args, null input, multi-field fallback, non-string fallback,
truncation, markdown escaping, multibyte chars, and empty-string
preservation.

`cargo check -p agent --tests` passes locally. I couldn't run the full
test binary on my machine because `wasmtime-c-api-impl` (a transitive
test-dep) needs `cmake`, but the new code is pure string-manipulation
with no wasm/runtime touch — CI should cover.

## Scope

One file, +127/-3. No behavior change for any tool shape other than
single-string-input MCP tools.

## Per CONTRIBUTING.md

Happy to convert to a discussion first if the team prefers — didn't see
a tracking issue for this. Also filed under CLA (signed).

Release Notes:

- Show MCP tool primary argument in tool header if space allows

---------

Co-authored-by: Chris Biscardi <chris@christopherbiscardi.com>
This commit is contained in:
Ahmed GhaitandChris Biscardi authored and GitHub committed 2026-07-13 14:00:40 +00:00
1 parent 44dac13934
commit 8a174b567e
2 files changed
+130 -4

No files matched your search

@@ -8,7 +8,11 @@ use gpui::{App, AppContext, AsyncApp, Context, Entity, EventEmitter, SharedStrin
use language_model::{LanguageModelImage, LanguageModelImageExt, LanguageModelToolResultContent};
use project::context_server_store::{ContextServerStatus, ContextServerStore};
use std::sync::Arc;
use util::ResultExt;
use util::{ResultExt, markdown::MarkdownEscaped};
/// Maximum number of characters to show from a tool argument in the
/// collapsed tool-call header. Longer values are truncated with an ellipsis.
const MAX_INLINE_ARG_LEN: usize = 120;
/// Generates a tool ID for an MCP tool that can be used in settings.
///
@@ -310,8 +314,8 @@ impl AnyAgentTool for ContextServerTool {
acp::ToolKind::Other
}
fn initial_title(&self, _input: serde_json::Value, _cx: &mut App) -> SharedString {
format!("Run MCP tool `{}`", self.tool.name).into()
fn initial_title(&self, input: serde_json::Value, _cx: &mut App) -> SharedString {
format_mcp_initial_title(&self.tool.name, &input).into()
}
fn input_schema(
@@ -474,6 +478,38 @@ impl AnyAgentTool for ContextServerTool {
}
}
/// Builds the header label shown for an MCP tool call. When the input is an
/// object with a single string-valued field, the value is inlined next to the
/// tool name so the primary argument (e.g. a URL, path, or query) is visible
/// without expanding the input block — matching the UX of built-in tools like
/// `Fetch`. All other shapes fall back to the tool name alone.
fn format_mcp_initial_title(tool_name: &str, input: &serde_json::Value) -> String {
if let Some(value) = single_string_arg(input) {
let preview = truncate_chars(value, MAX_INLINE_ARG_LEN);
format!("Run MCP tool `{}` {}", tool_name, MarkdownEscaped(&preview))
} else {
format!("Run MCP tool `{}`", tool_name)
}
}
fn single_string_arg(input: &serde_json::Value) -> Option<&str> {
let obj = input.as_object()?;
if obj.len() != 1 {
return None;
}
obj.values().next()?.as_str()
}
fn truncate_chars(s: &str, max: usize) -> String {
if s.chars().count() <= max {
s.to_string()
} else {
let mut out: String = s.chars().take(max).collect();
out.push('…');
out
}
}
pub fn get_prompt(
server_store: &Entity<ContextServerStore>,
server_id: &ContextServerId,
@@ -531,4 +567,92 @@ mod tests {
// Note: Tests for MCP tool ID collision with built-in tools and permission
// decisions are in crates/agent/src/tool_permissions.rs to avoid duplication.
#[test]
fn test_format_mcp_initial_title_inlines_single_string_arg() {
let input = serde_json::json!({ "url": "https://example.com/page" });
assert_eq!(
format_mcp_initial_title("open_url_in_browser", &input),
"Run MCP tool `open_url_in_browser` https://example.com/page"
);
}
#[test]
fn test_format_mcp_initial_title_no_args() {
let input = serde_json::json!({});
assert_eq!(
format_mcp_initial_title("cleanup", &input),
"Run MCP tool `cleanup`"
);
}
#[test]
fn test_format_mcp_initial_title_null_input() {
assert_eq!(
format_mcp_initial_title("cleanup", &serde_json::Value::Null),
"Run MCP tool `cleanup`"
);
}
#[test]
fn test_format_mcp_initial_title_multiple_fields_falls_back() {
let input = serde_json::json!({ "x": "a", "y": "b" });
assert_eq!(
format_mcp_initial_title("do_thing", &input),
"Run MCP tool `do_thing`"
);
}
#[test]
fn test_format_mcp_initial_title_non_string_field_falls_back() {
let input = serde_json::json!({ "count": 42 });
assert_eq!(
format_mcp_initial_title("tick", &input),
"Run MCP tool `tick`"
);
}
#[test]
fn test_format_mcp_initial_title_truncates_long_values() {
let long = "x".repeat(MAX_INLINE_ARG_LEN + 50);
let input = serde_json::json!({ "q": long });
let title = format_mcp_initial_title("search", &input);
assert!(
title.ends_with('…'),
"expected truncation ellipsis, got: {title}"
);
// Prefix + backticked name + space + MAX chars + ellipsis — no full 170-char value.
assert!(title.chars().count() < MAX_INLINE_ARG_LEN + 50);
}
#[test]
fn test_format_mcp_initial_title_escapes_markdown_in_value() {
let input = serde_json::json!({ "q": "**bold** _italic_" });
let title = format_mcp_initial_title("search", &input);
// Asterisks and underscores must be escaped so the header renders literally.
assert!(title.contains("\\*"), "expected \\*, got: {title}");
assert!(title.contains("\\_"), "expected \\_, got: {title}");
}
#[test]
fn test_truncate_chars_boundary() {
assert_eq!(truncate_chars("abc", 3), "abc");
assert_eq!(truncate_chars("abcd", 3), "abc…");
}
#[test]
fn test_truncate_chars_handles_multibyte() {
// "café" is 4 chars but 5 bytes — byte-based truncation would panic.
assert_eq!(truncate_chars("café", 4), "café");
assert_eq!(truncate_chars("café", 3), "caf…");
}
#[test]
fn test_single_string_arg_ignores_empty_string() {
// An empty string is still a string — we inline it rather than fall back,
// which lets callers tell "the server sent an empty arg" apart from
// "no args at all".
let input = serde_json::json!({ "q": "" });
assert_eq!(single_string_arg(&input), Some(""));
}
}
+3 -1
View File
@@ -105,6 +105,8 @@ extend-ignore-re = [
# Abbreviation for foreignObject in mermaid SVG processing
"fo",
# Mermaid CSS class name for state diagram composites
"composit"
"composit",
# Used in truncating tests to ensure byte-length vs char like in café
"caf…"
]
check-filename = true