pub struct McpClient { /* private fields */ }
Expand description
MCP client
MCP server’s methods to call and respond to client feature requests from the server.
To create an McpClient
, use the with_server
method or McpClientBuilder
.
The method to create an McpClient
performs an initialize
request to the server and returns control when the request completes.
§Example
use mcp_attr::client::McpClient;
use mcp_attr::schema::{ListPromptsRequestParams, ListResourcesRequestParams, CallToolRequestParams};
use mcp_attr::server::{McpServer, mcp_server};
struct MyServer;
#[mcp_server]
impl McpServer for MyServer {}
let server = MyServer;
let client = McpClient::with_server(server).await?;
// Get prompt list
let prompts = client.prompts_list(None).await?;
// Get resource list
let resources = client.resources_list(None).await?;
// Call tool
let params = CallToolRequestParams {
name: "tool_name".to_string(),
arguments: Some(serde_json::Map::new()),
};
let result = client.tools_call(params).await?;
Implementations§
Source§impl McpClient
impl McpClient
Sourcepub async fn with_server(server: impl McpServer) -> SessionResult<Self>
pub async fn with_server(server: impl McpServer) -> SessionResult<Self>
Connects to the specified McpServer
in-process
Performs an initialize
request to the server and returns the result
Sourcepub async fn initialize(
session: Session,
p: InitializeRequestParams,
) -> SessionResult<Self>
pub async fn initialize( session: Session, p: InitializeRequestParams, ) -> SessionResult<Self>
Connects to an MCP server using the specified JSON RPC Session
This Session
uses the values returned from McpClientBuilder::build_raw
.
Performs an initialize
request to the server and returns the result
Sourcepub fn instructions(&self) -> Option<&str>
pub fn instructions(&self) -> Option<&str>
Gets the instructions
obtained from the initialize
request response
Sourcepub fn server_info(&self) -> &Implementation
pub fn server_info(&self) -> &Implementation
Gets the server_info
obtained from the initialize
request response
Sourcepub async fn prompts_list(
&self,
params: Option<ListPromptsRequestParams>,
) -> SessionResult<ListPromptsResult>
pub async fn prompts_list( &self, params: Option<ListPromptsRequestParams>, ) -> SessionResult<ListPromptsResult>
Calls prompts/list
Sourcepub async fn prompts_get(
&self,
params: GetPromptRequestParams,
) -> SessionResult<GetPromptResult>
pub async fn prompts_get( &self, params: GetPromptRequestParams, ) -> SessionResult<GetPromptResult>
Calls prompts/get
Sourcepub async fn resources_list(
&self,
params: Option<ListResourcesRequestParams>,
) -> SessionResult<ListResourcesResult>
pub async fn resources_list( &self, params: Option<ListResourcesRequestParams>, ) -> SessionResult<ListResourcesResult>
Calls resources/list
Sourcepub async fn resources_templates_list(
&self,
params: Option<ListResourceTemplatesRequestParams>,
) -> SessionResult<ListResourceTemplatesResult>
pub async fn resources_templates_list( &self, params: Option<ListResourceTemplatesRequestParams>, ) -> SessionResult<ListResourceTemplatesResult>
Calls resources/templates/list
Sourcepub async fn resources_read(
&self,
params: ReadResourceRequestParams,
) -> SessionResult<ReadResourceResult>
pub async fn resources_read( &self, params: ReadResourceRequestParams, ) -> SessionResult<ReadResourceResult>
Calls resources/read
Sourcepub async fn tools_list(
&self,
params: Option<ListToolsRequestParams>,
) -> SessionResult<ListToolsResult>
pub async fn tools_list( &self, params: Option<ListToolsRequestParams>, ) -> SessionResult<ListToolsResult>
Calls tools/list
Sourcepub async fn tools_call(
&self,
params: CallToolRequestParams,
) -> SessionResult<CallToolResult>
pub async fn tools_call( &self, params: CallToolRequestParams, ) -> SessionResult<CallToolResult>
Calls tools/call
Sourcepub async fn completion_complete(
&self,
params: CompleteRequestParams,
) -> SessionResult<CompleteResult>
pub async fn completion_complete( &self, params: CompleteRequestParams, ) -> SessionResult<CompleteResult>
Calls completion/complete
Sourcepub async fn ping(&self) -> SessionResult<()>
pub async fn ping(&self) -> SessionResult<()>
Calls ping