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::{InitializeRequest, ProtocolVersion};
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/// Cookbook of common patterns for building ACP components
85pub mod cookbook;
86/// JSON-RPC handler types for building custom message handlers
87pub mod handler;
88/// JSON-RPC connection and handler infrastructure
89mod jsonrpc;
90/// MCP server support for providing MCP tools over ACP
91pub mod mcp_server;
92/// Role types for ACP connections
93pub mod role;
94/// ACP protocol schema types - all message types, requests, responses, and supporting types
95pub mod schema;
96/// Utility functions and types
97pub mod util;
98
99pub use capabilities::*;
100
101/// JSON-RPC message types.
102///
103/// This module re-exports types from the `jsonrpcmsg` crate that are transitively
104/// reachable through the public API (e.g., via [`Channel`]).
105///
106/// Users of the `agent-client-protocol` crate can use these types without adding a direct dependency
107/// on `jsonrpcmsg`.
108pub mod jsonrpcmsg {
109 pub use jsonrpcmsg::{Error, Id, Message, Params, Request, Response};
110}
111
112pub use jsonrpc::{
113 Builder, ByteStreams, Channel, ConnectionTo, Dispatch, HandleDispatchFrom, Handled,
114 IntoHandled, JsonRpcMessage, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, Lines,
115 NullHandler, Responder, ResponseRouter, SentRequest, UntypedMessage,
116 run::{ChainRun, NullRun, RunWithConnectionTo},
117};
118
119pub use role::{
120 Role, RoleId, UntypedRole,
121 acp::{Agent, Client, Conductor, Proxy},
122};
123
124pub use component::{ConnectTo, DynConnectTo};
125
126// Re-export BoxFuture for implementing Component traits
127pub use futures::future::BoxFuture;
128
129// Re-export the six primary message enum types at the root
130pub use schema::{
131 AgentNotification, AgentRequest, AgentResponse, ClientNotification, ClientRequest,
132 ClientResponse,
133};
134
135// Re-export commonly used infrastructure types for convenience
136pub use schema::{Error, ErrorCode, Result};
137
138// Re-export derive macros for custom JSON-RPC types
139pub use agent_client_protocol_derive::{JsonRpcNotification, JsonRpcRequest, JsonRpcResponse};
140
141mod session;
142pub use session::*;
143
144mod acp_agent;
145pub use acp_agent::{AcpAgent, LineDirection};
146
147mod stdio;
148pub use stdio::Stdio;
149
150/// This is a hack that must be given as the final argument of
151/// the MCP server builder's `tool_fn_mut` method when defining tools.
152///
153/// The `agent-client-protocol-rmcp` crate provides the builder this macro is
154/// typically used with.
155/// Look away, lest ye be blinded by its vileness!
156///
157/// Fine, if you MUST know, it's a horrific workaround for not having
158/// [return-type notation](https://github.com/rust-lang/rust/issues/109417)
159/// and for [this !@$#!%! bug](https://github.com/rust-lang/rust/issues/110338).
160/// Trust me, the need for it hurts me more than it hurts you. --nikomatsakis
161#[macro_export]
162macro_rules! tool_fn_mut {
163 () => {
164 |func, params, context| Box::pin(func(params, context))
165 };
166}
167
168/// This is a hack that must be given as the final argument of
169/// the MCP server builder's `tool_fn` method when defining stateless concurrent tools.
170///
171/// The `agent-client-protocol-rmcp` crate provides the builder this macro is
172/// typically used with.
173/// See [`tool_fn_mut!`] for the gory details.
174#[macro_export]
175macro_rules! tool_fn {
176 () => {
177 |func, params, context| Box::pin(func(params, context))
178 };
179}
180
181/// This macro is used for the value of the `to_future_hack` parameter of
182/// [`Builder::on_receive_request`] and [`Builder::on_receive_request_from`].
183///
184/// It expands to `|f, req, responder, cx| Box::pin(f(req, responder, 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_request {
190 () => {
191 |f: &mut _, req, responder, cx| Box::pin(f(req, responder, cx))
192 };
193}
194
195/// This macro is used for the value of the `to_future_hack` parameter of
196/// [`Builder::on_receive_notification`] and [`Builder::on_receive_notification_from`].
197///
198/// It expands to `|f, notif, cx| Box::pin(f(notif, 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_notification {
204 () => {
205 |f: &mut _, notif, cx| Box::pin(f(notif, cx))
206 };
207}
208
209/// This macro is used for the value of the `to_future_hack` parameter of
210/// [`Builder::on_receive_dispatch`] and [`Builder::on_receive_dispatch_from`].
211///
212/// It expands to `|f, dispatch, cx| Box::pin(f(dispatch, cx))`.
213///
214/// This is needed until [return-type notation](https://github.com/rust-lang/rust/issues/109417)
215/// is stabilized.
216#[macro_export]
217macro_rules! on_receive_dispatch {
218 () => {
219 |f: &mut _, dispatch, cx| Box::pin(f(dispatch, cx))
220 };
221}