Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crates/agent/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,7 @@ async fn test_mcp_tools(cx: &mut TestAppContext) {
"test_server",
vec![context_server::types::Tool {
name: "echo".into(),
title: None,
description: None,
input_schema: serde_json::to_value(EchoTool::input_schema(
LanguageModelToolSchemaFormat::JsonSchema,
Expand Down Expand Up @@ -1742,6 +1743,7 @@ async fn test_mcp_tool_result_displayed_when_server_disconnected(cx: &mut TestAp
"github_server",
vec![context_server::types::Tool {
name: "issue_read".into(),
title: None,
description: Some("Read a GitHub issue".into()),
input_schema: json!({
"type": "object",
Expand Down Expand Up @@ -1936,6 +1938,7 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
vec![
context_server::types::Tool {
name: "echo".into(), // Conflicts with native EchoTool
title: None,
description: None,
input_schema: serde_json::to_value(EchoTool::input_schema(
LanguageModelToolSchemaFormat::JsonSchema,
Expand All @@ -1946,6 +1949,7 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
},
context_server::types::Tool {
name: "unique_tool_1".into(),
title: None,
description: None,
input_schema: json!({"type": "object", "properties": {}}),
output_schema: None,
Expand All @@ -1961,6 +1965,7 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
vec![
context_server::types::Tool {
name: "echo".into(), // Also conflicts with native EchoTool
title: None,
description: None,
input_schema: serde_json::to_value(EchoTool::input_schema(
LanguageModelToolSchemaFormat::JsonSchema,
Expand All @@ -1971,20 +1976,23 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
},
context_server::types::Tool {
name: "unique_tool_2".into(),
title: None,
description: None,
input_schema: json!({"type": "object", "properties": {}}),
output_schema: None,
annotations: None,
},
context_server::types::Tool {
name: "a".repeat(MAX_TOOL_NAME_LENGTH - 2),
title: None,
description: None,
input_schema: json!({"type": "object", "properties": {}}),
output_schema: None,
annotations: None,
},
context_server::types::Tool {
name: "b".repeat(MAX_TOOL_NAME_LENGTH - 1),
title: None,
description: None,
input_schema: json!({"type": "object", "properties": {}}),
output_schema: None,
Expand All @@ -1999,20 +2007,23 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
vec![
context_server::types::Tool {
name: "a".repeat(MAX_TOOL_NAME_LENGTH - 2),
title: None,
description: None,
input_schema: json!({"type": "object", "properties": {}}),
output_schema: None,
annotations: None,
},
context_server::types::Tool {
name: "b".repeat(MAX_TOOL_NAME_LENGTH - 1),
title: None,
description: None,
input_schema: json!({"type": "object", "properties": {}}),
output_schema: None,
annotations: None,
},
context_server::types::Tool {
name: "c".repeat(MAX_TOOL_NAME_LENGTH + 1),
title: None,
description: None,
input_schema: json!({"type": "object", "properties": {}}),
output_schema: None,
Expand All @@ -2028,6 +2039,7 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
"Azure DevOps",
vec![context_server::types::Tool {
name: "echo".into(), // Also conflicts - will be disambiguated as azure_dev_ops_echo
title: None,
description: None,
input_schema: serde_json::to_value(EchoTool::input_schema(
LanguageModelToolSchemaFormat::JsonSchema,
Expand Down Expand Up @@ -4406,7 +4418,9 @@ fn setup_context_server(
),
server_info: context_server::types::Implementation {
name: name.into(),
title: None,
version: "1.0.0".to_string(),
description: None,
},
capabilities: context_server::types::ServerCapabilities {
tools: Some(context_server::types::ToolsCapabilities {
Expand Down
3 changes: 3 additions & 0 deletions crates/agent/src/tools/context_server_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ impl AnyAgentTool for ContextServerTool {
context_server::types::ToolResponseContent::Resource { .. } => {
log::warn!("Ignoring resource content from tool response");
}
context_server::types::ToolResponseContent::ResourceLink { .. } => {
log::warn!("Ignoring resource link content from tool response");
}
}
}
let raw_output = serde_json::Value::String(concatenated_text);
Expand Down
7 changes: 7 additions & 0 deletions crates/context_server/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,13 @@ impl Client {
Ok(())
}

/// Notify the underlying transport of the negotiated MCP protocol version
/// so it can stamp subsequent requests (e.g. HTTP's `MCP-Protocol-Version`
/// header required from 2025-06-18 onward).
pub(crate) fn set_protocol_version(&self, version: &str) {
self.transport.set_protocol_version(version);
}

#[must_use]
pub fn on_notification(
&self,
Expand Down
2 changes: 2 additions & 0 deletions crates/context_server/src/context_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ impl ContextServer {
let protocol = crate::protocol::ModelContextProtocol::new(client);
let client_info = types::Implementation {
name: "Zed".to_string(),
title: None,
version: env!("CARGO_PKG_VERSION").to_string(),
description: None,
};
let initialized_protocol = protocol.initialize(client_info).await?;

Expand Down
1 change: 1 addition & 0 deletions crates/context_server/src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl McpServer {
let registered_tool = RegisteredTool {
tool: Tool {
name: T::NAME.into(),
title: None,
description,
input_schema: input_schema.into(),
output_schema: if TypeId::of::<T::Output>() == TypeId::of::<()>() {
Expand Down
7 changes: 7 additions & 0 deletions crates/context_server/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ impl ModelContextProtocol {
fn supported_protocols() -> Vec<types::ProtocolVersion> {
vec![
types::ProtocolVersion(types::LATEST_PROTOCOL_VERSION.to_string()),
types::ProtocolVersion(types::VERSION_2025_06_18.to_string()),
types::ProtocolVersion(types::VERSION_2025_03_26.to_string()),
types::ProtocolVersion(types::VERSION_2024_11_05.to_string()),
]
}
Expand Down Expand Up @@ -59,6 +61,11 @@ impl ModelContextProtocol {

log::trace!("mcp server info {:?}", response.server_info);

// Per MCP 2025-06-18, HTTP transport must attach the negotiated version
// as `MCP-Protocol-Version` on every post-initialize request.
self.inner
.set_protocol_version(&response.protocol_version.0);

let initialized_protocol = InitializedContextServerProtocol {
inner: self.inner,
initialize: response,
Expand Down
2 changes: 2 additions & 0 deletions crates/context_server/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ fn create_initialize_response(server_name: String) -> InitializeResponse {
protocol_version: ProtocolVersion(crate::types::LATEST_PROTOCOL_VERSION.to_string()),
server_info: Implementation {
name: server_name,
title: None,
version: "1.0.0".to_string(),
description: None,
},
capabilities: ServerCapabilities::default(),
meta: None,
Expand Down
5 changes: 5 additions & 0 deletions crates/context_server/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ pub trait Transport: Send + Sync {
async fn send(&self, message: String) -> Result<()>;
fn receive(&self) -> Pin<Box<dyn Stream<Item = String> + Send>>;
fn receive_err(&self) -> Pin<Box<dyn Stream<Item = String> + Send>>;

/// Called after the MCP initialize handshake completes so transports that
/// need the negotiated version (currently only HTTP, which must attach an
/// `MCP-Protocol-Version` header from 2025-06-18 onward) can pick it up.
fn set_protocol_version(&self, _version: &str) {}
}
30 changes: 30 additions & 0 deletions crates/context_server/src/transport/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{pin::Pin, sync::Arc};

use crate::oauth::{self, OAuthTokenProvider, WwwAuthenticate};
use crate::transport::Transport;
use crate::types;

/// Typed errors returned by the HTTP transport that callers can downcast from
/// `anyhow::Error` to handle specific failure modes.
Expand All @@ -33,6 +34,7 @@ impl std::error::Error for TransportError {}

// Constants from MCP spec
const HEADER_SESSION_ID: &str = "Mcp-Session-Id";
const HEADER_PROTOCOL_VERSION: &str = "MCP-Protocol-Version";
const EVENT_STREAM_MIME_TYPE: &str = "text/event-stream";
const JSON_MIME_TYPE: &str = "application/json";

Expand All @@ -41,6 +43,11 @@ pub struct HttpTransport {
http_client: Arc<dyn HttpClient>,
endpoint: String,
session_id: Arc<SyncMutex<Option<String>>>,
/// Negotiated MCP protocol version, populated by `set_protocol_version`
/// after the initialize handshake. From 2025-06-18 onward the server
/// requires clients to echo this in the `MCP-Protocol-Version` header on
/// every subsequent request.
protocol_version: Arc<SyncMutex<Option<String>>>,
executor: BackgroundExecutor,
response_tx: async_channel::Sender<String>,
response_rx: async_channel::Receiver<String>,
Expand Down Expand Up @@ -78,6 +85,7 @@ impl HttpTransport {
executor,
endpoint,
session_id: Arc::new(SyncMutex::new(None)),
protocol_version: Arc::new(SyncMutex::new(None)),
response_tx,
response_rx,
error_tx,
Expand Down Expand Up @@ -114,6 +122,14 @@ impl HttpTransport {
request_builder = request_builder.header(HEADER_SESSION_ID, session_id.as_str());
}

// Echo the negotiated protocol version once initialization has
// completed. Required by servers speaking MCP 2025-06-18 or later.
if let Some(ref version) = *self.protocol_version.lock()
&& types::requires_protocol_version_header(version)
{
request_builder = request_builder.header(HEADER_PROTOCOL_VERSION, version.as_str());
}

Ok(request_builder.body(AsyncBody::from(message.to_vec()))?)
}

Expand Down Expand Up @@ -315,6 +331,10 @@ impl Transport for HttpTransport {
fn receive_err(&self) -> Pin<Box<dyn Stream<Item = String> + Send>> {
Box::pin(self.error_rx.clone())
}

fn set_protocol_version(&self, version: &str) {
*self.protocol_version.lock() = Some(version.to_string());
}
}

impl Drop for HttpTransport {
Expand All @@ -323,6 +343,7 @@ impl Drop for HttpTransport {
let http_client = self.http_client.clone();
let endpoint = self.endpoint.clone();
let session_id = self.session_id.lock().clone();
let protocol_version = self.protocol_version.lock().clone();
let headers = self.headers.clone();
let access_token = self.token_provider.as_ref().and_then(|p| p.access_token());

Expand All @@ -345,6 +366,15 @@ impl Drop for HttpTransport {
request_builder.header("Authorization", format!("Bearer {}", token));
}

// Stamp the negotiated MCP protocol version on the DELETE
// too, matching what `build_request` does for POSTs.
if let Some(ref version) = protocol_version
&& types::requires_protocol_version_header(version)
{
request_builder =
request_builder.header(HEADER_PROTOCOL_VERSION, version.as_str());
}

let request = request_builder.body(AsyncBody::empty());

if let Ok(request) = request {
Expand Down
55 changes: 54 additions & 1 deletion crates/context_server/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ use url::Url;

use crate::client::RequestId;

pub const LATEST_PROTOCOL_VERSION: &str = "2025-03-26";
pub const VERSION_2024_11_05: &str = "2024-11-05";
pub const VERSION_2025_03_26: &str = "2025-03-26";
pub const VERSION_2025_06_18: &str = "2025-06-18";
pub const LATEST_PROTOCOL_VERSION: &str = "2025-11-25";

/// Protocol versions that include the streamable HTTP transport's
/// `MCP-Protocol-Version` header requirement on post-initialize requests.
pub fn requires_protocol_version_header(version: &str) -> bool {
matches!(version, VERSION_2025_06_18 | LATEST_PROTOCOL_VERSION)
}

pub mod requests {
use super::*;
Expand Down Expand Up @@ -209,10 +217,21 @@ pub struct CompletionCompleteParams {
#[serde(rename = "ref")]
pub reference: CompletionReference,
pub argument: CompletionArgument,
/// Previously-resolved argument values so the server can provide
/// context-sensitive completions (added in MCP 2025-06-18).
#[serde(skip_serializing_if = "Option::is_none")]
pub context: Option<CompletionContext>,
#[serde(rename = "_meta", skip_serializing_if = "Option::is_none")]
pub meta: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionContext {
#[serde(skip_serializing_if = "Option::is_none")]
pub arguments: Option<HashMap<String, String>>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CompletionReference {
Expand Down Expand Up @@ -421,6 +440,9 @@ pub struct CompletionResult {
#[serde(rename_all = "camelCase")]
pub struct Prompt {
pub name: String,
/// Human-readable display name (added in MCP 2025-06-18).
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -431,6 +453,9 @@ pub struct Prompt {
#[serde(rename_all = "camelCase")]
pub struct PromptArgument {
pub name: String,
/// Human-readable display name (added in MCP 2025-06-18).
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -499,6 +524,9 @@ pub struct RootsCapabilities {
#[serde(rename_all = "camelCase")]
pub struct Tool {
pub name: String,
/// Human-readable display name (added in MCP 2025-06-18).
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub input_schema: serde_json::Value,
Expand Down Expand Up @@ -532,14 +560,23 @@ pub struct ToolAnnotations {
#[serde(rename_all = "camelCase")]
pub struct Implementation {
pub name: String,
/// Human-readable display name (added in MCP 2025-06-18).
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
pub version: String,
/// Human-readable description of the implementation (added in MCP 2025-11-25).
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Resource {
pub uri: Url,
pub name: String,
/// Human-readable display name (added in MCP 2025-06-18).
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -577,6 +614,9 @@ pub struct BlobResourceContents {
pub struct ResourceTemplate {
pub uri_template: String,
pub name: String,
/// Human-readable display name (added in MCP 2025-06-18).
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -709,6 +749,19 @@ pub enum ToolResponseContent {
Audio { data: String, mime_type: String },
#[serde(rename = "resource")]
Resource { resource: ResourceContents },
/// Link to an MCP resource on the server, without inlining its contents.
/// Added in MCP 2025-06-18.
#[serde(rename = "resource_link", rename_all = "camelCase")]
ResourceLink {
uri: Url,
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
mime_type: Option<String>,
},
}

impl ToolResponseContent {
Expand Down
Loading