Skip to main content

agent_client_protocol/role/
mcp.rs

1//! MCP (Model Context Protocol) role types.
2//!
3//! These roles are used for MCP connections, which are separate from ACP but
4//! use the same underlying connection infrastructure.
5
6use crate::{
7    Handled, RoleId,
8    jsonrpc::{Builder, handlers::NullHandler, run::NullRun},
9    role::{HasPeer, RemoteStyle, Role},
10};
11
12/// The MCP client role - connects to MCP servers to access tools and resources.
13#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct Client;
15
16impl Role for Client {
17    type Counterpart = Server;
18
19    fn role_id(&self) -> RoleId {
20        RoleId::from_singleton(self)
21    }
22
23    fn counterpart(&self) -> Self::Counterpart {
24        Server
25    }
26
27    async fn default_handle_dispatch_from(
28        &self,
29        message: crate::Dispatch,
30        _connection: crate::ConnectionTo<Self>,
31    ) -> Result<crate::Handled<crate::Dispatch>, crate::Error> {
32        Ok(Handled::No {
33            message,
34            retry: false,
35        })
36    }
37}
38
39impl Client {
40    /// Create a connection builder for an MCP client.
41    pub fn builder(self) -> Builder<Client, NullHandler, NullRun> {
42        Builder::new(self)
43    }
44}
45
46impl HasPeer<Client> for Client {
47    fn remote_style(&self, _peer: Client) -> RemoteStyle {
48        RemoteStyle::Counterpart
49    }
50}
51
52/// The MCP server role - provides tools and resources to MCP clients.
53#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
54pub struct Server;
55
56impl Role for Server {
57    type Counterpart = Client;
58
59    fn role_id(&self) -> RoleId {
60        RoleId::from_singleton(self)
61    }
62
63    fn counterpart(&self) -> Self::Counterpart {
64        Client
65    }
66
67    async fn default_handle_dispatch_from(
68        &self,
69        message: crate::Dispatch,
70        _connection: crate::ConnectionTo<Self>,
71    ) -> Result<crate::Handled<crate::Dispatch>, crate::Error> {
72        Ok(Handled::No {
73            message,
74            retry: false,
75        })
76    }
77}
78
79impl Server {
80    /// Create a connection builder for an MCP server.
81    pub fn builder(self) -> Builder<Server, NullHandler, NullRun> {
82        Builder::new(self)
83    }
84}
85
86impl HasPeer<Server> for Server {
87    fn remote_style(&self, _peer: Server) -> RemoteStyle {
88        RemoteStyle::Counterpart
89    }
90}