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