Skip to main content

agent_client_protocol/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![deny(missing_docs)]
3
4//! # agent-client-protocol -- the Agent Client Protocol (ACP) SDK
5//!
6//! **agent-client-protocol** is a Rust SDK for building [Agent-Client Protocol (ACP)][acp] applications.
7//! ACP is a protocol for communication between AI agents and their clients (IDEs, CLIs, etc.),
8//! enabling features like tool use, permission requests, and streaming responses.
9//!
10//! [acp]: https://agentclientprotocol.com/
11//!
12//! ## What can you build with agent-client-protocol?
13//!
14//! - **Clients** that talk to ACP agents (like building your own Claude Code interface)
15//! - **Proxies** that add capabilities to existing agents (like adding custom tools via MCP)
16//! - **Agents** that respond to prompts with AI-powered responses
17//!
18//! ## Quick Start: Connecting to an Agent
19//!
20//! The most common use case is connecting to an existing ACP agent as a client.
21//! Here's a minimal example that initializes a connection, creates a session,
22//! and sends a prompt:
23//!
24//! ```no_run
25//! use agent_client_protocol::Client;
26//! use agent_client_protocol::schema::{ProtocolVersion, v1::InitializeRequest};
27//!
28//! # async fn run(transport: impl agent_client_protocol::ConnectTo<agent_client_protocol::Client>) -> agent_client_protocol::Result<()> {
29//! Client.builder()
30//!     .name("my-client")
31//!     .connect_with(transport, async |cx| {
32//!         // Step 1: Initialize the connection
33//!         cx.send_request(InitializeRequest::new(ProtocolVersion::V1))
34//!             .block_task().await?;
35//!
36//!         // Step 2: Create a session and send a prompt
37//!         cx.build_session_cwd()?
38//!             .block_task()
39//!             .run_until(async |mut session| {
40//!                 session.send_prompt("What is 2 + 2?")?;
41//!                 let response = session.read_to_string().await?;
42//!                 println!("{}", response);
43//!                 Ok(())
44//!             })
45//!             .await
46//!     })
47//!     .await
48//! # }
49//! ```
50//!
51//! For a complete working example, see [`yolo_one_shot_client.rs`][yolo].
52//!
53//! [yolo]: https://github.com/agentclientprotocol/rust-sdk/blob/main/src/agent-client-protocol/examples/yolo_one_shot_client.rs
54//!
55//! ## Cookbook
56//!
57//! The [`agent_client_protocol_cookbook`] crate contains practical guides and examples:
58//!
59//! - Connecting as a client
60//! - Global MCP server
61//! - Per-session MCP server with workspace context
62//! - Building agents and reusable components
63//! - Running proxies with the conductor
64//!
65//! [`agent_client_protocol_cookbook`]: https://docs.rs/agent-client-protocol-cookbook
66//!
67//! ## Core Concepts
68//!
69//! The [`concepts`] module provides detailed explanations of how agent-client-protocol works,
70//! including connections, sessions, callbacks, ordering guarantees, and more.
71//!
72//! ## Related Crates
73//!
74//! - [`agent-client-protocol-conductor`] - Binary for running proxy chains
75//!
76//! [`agent-client-protocol-conductor`]: https://crates.io/crates/agent-client-protocol-conductor
77
78/// Capability management for the `_meta.symposium` object
79mod capabilities;
80/// Component abstraction for agents and proxies
81pub mod component;
82/// Core concepts for understanding and using agent-client-protocol
83pub mod concepts;
84/// JSON-RPC connection and handler infrastructure
85mod jsonrpc;
86/// MCP server support for providing MCP tools over ACP
87pub mod mcp_server;
88/// Role types for ACP connections
89pub mod role;
90/// ACP protocol schema types - all message types, requests, responses, and supporting types
91pub mod schema;
92/// Utility functions and types
93pub mod util;
94
95pub use capabilities::*;
96
97pub use jsonrpc::{
98    Builder, ByteStreams, Channel, ConnectionTo, Dispatch, HandleConnectionClose,
99    HandleDispatchFrom, Handled, INCOMING_TRANSPORT_CLOSED_REASON, IntoHandled, JsonRpcMessage,
100    JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, Lines, NullClose, NullHandler,
101    RawJsonRpcMessage, RawJsonRpcParams, Responder, ResponseRouter, SentRequest, UntypedMessage,
102    is_incoming_transport_closed,
103    run::{ChainRun, NullRun, RunWithConnectionTo},
104};
105pub use jsonrpc::{RequestCancellation, is_cancel_request_notification};
106
107#[cfg(feature = "unstable_protocol_v2")]
108pub use role::acp::AgentProtocolRouter;
109#[cfg(feature = "unstable_protocol_v2")]
110pub use role::acp::ClientProtocolConnector;
111pub use role::{
112    Role, RoleId, UntypedRole,
113    acp::{Agent, Client, Conductor, Proxy},
114};
115
116pub use component::{ConnectTo, DynConnectTo};
117
118// Re-export BoxFuture for implementing Component traits
119pub use futures::future::BoxFuture;
120
121// Re-export commonly used infrastructure types for convenience
122pub use schema::v1::{Error, ErrorCode, Result};
123
124// Re-export derive macros for custom JSON-RPC types
125pub use agent_client_protocol_derive::{JsonRpcNotification, JsonRpcRequest, JsonRpcResponse};
126
127mod session;
128pub use session::*;
129
130mod acp_agent;
131pub use acp_agent::{AcpAgent, LineDirection};
132
133mod stdio;
134pub use stdio::Stdio;
135
136/// This is a hack that must be given as the final argument of
137/// the MCP server builder's `tool_fn_mut` method when defining tools.
138///
139/// The `agent-client-protocol-rmcp` crate provides the builder this macro is
140/// typically used with.
141/// Look away, lest ye be blinded by its vileness!
142///
143/// Fine, if you MUST know, it's a horrific workaround for not having
144/// [return-type notation](https://github.com/rust-lang/rust/issues/109417)
145/// and for [this !@$#!%! bug](https://github.com/rust-lang/rust/issues/110338).
146/// Trust me, the need for it hurts me more than it hurts you. --nikomatsakis
147#[macro_export]
148macro_rules! tool_fn_mut {
149    () => {
150        |func, params, context| Box::pin(func(params, context))
151    };
152}
153
154/// This is a hack that must be given as the final argument of
155/// the MCP server builder's `tool_fn` method when defining stateless concurrent tools.
156///
157/// The `agent-client-protocol-rmcp` crate provides the builder this macro is
158/// typically used with.
159/// See [`tool_fn_mut!`] for the gory details.
160#[macro_export]
161macro_rules! tool_fn {
162    () => {
163        |func, params, context| Box::pin(func(params, context))
164    };
165}
166
167/// This macro is used for the value of the `to_future_hack` parameter of
168/// [`Builder::on_receive_request`] and [`Builder::on_receive_request_from`].
169///
170/// It expands to `|f, req, responder, cx| Box::pin(f(req, responder, cx))`.
171///
172/// This is needed until [return-type notation](https://github.com/rust-lang/rust/issues/109417)
173/// is stabilized.
174#[macro_export]
175macro_rules! on_receive_request {
176    () => {
177        |f: &mut _, req, responder, cx| Box::pin(f(req, responder, cx))
178    };
179}
180
181/// This macro is used for the value of the `to_future_hack` parameter of
182/// [`Builder::on_receive_notification`] and [`Builder::on_receive_notification_from`].
183///
184/// It expands to `|f, notif, cx| Box::pin(f(notif, cx))`.
185///
186/// This is needed until [return-type notation](https://github.com/rust-lang/rust/issues/109417)
187/// is stabilized.
188#[macro_export]
189macro_rules! on_receive_notification {
190    () => {
191        |f: &mut _, notif, cx| Box::pin(f(notif, cx))
192    };
193}
194
195/// This macro is used for the value of the `to_future_hack` parameter of
196/// [`Builder::on_receive_dispatch`] and [`Builder::on_receive_dispatch_from`].
197///
198/// It expands to `|f, dispatch, cx| Box::pin(f(dispatch, cx))`.
199///
200/// This is needed until [return-type notation](https://github.com/rust-lang/rust/issues/109417)
201/// is stabilized.
202#[macro_export]
203macro_rules! on_receive_dispatch {
204    () => {
205        |f: &mut _, dispatch, cx| Box::pin(f(dispatch, cx))
206    };
207}