|
1 | 1 | use mcp_core::protocol::{ |
2 | | - CallToolResult, Implementation, InitializeResult, JsonRpcError, JsonRpcMessage, |
3 | | - JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, ListResourcesResult, ListToolsResult, |
4 | | - ReadResourceResult, ServerCapabilities, METHOD_NOT_FOUND, |
| 2 | + CallToolResult, GetPromptResult, Implementation, InitializeResult, JsonRpcError, |
| 3 | + JsonRpcMessage, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, ListPromptsResult, |
| 4 | + ListResourcesResult, ListToolsResult, ReadResourceResult, ServerCapabilities, METHOD_NOT_FOUND, |
5 | 5 | }; |
6 | 6 | use serde::{Deserialize, Serialize}; |
7 | 7 | use serde_json::Value; |
@@ -93,6 +93,10 @@ pub trait McpClientTrait: Send + Sync { |
93 | 93 | async fn list_tools(&self, next_cursor: Option<String>) -> Result<ListToolsResult, Error>; |
94 | 94 |
|
95 | 95 | async fn call_tool(&self, name: &str, arguments: Value) -> Result<CallToolResult, Error>; |
| 96 | + |
| 97 | + async fn list_prompts(&self, next_cursor: Option<String>) -> Result<ListPromptsResult, Error>; |
| 98 | + |
| 99 | + async fn get_prompt(&self, name: &str, arguments: Value) -> Result<GetPromptResult, Error>; |
96 | 100 | } |
97 | 101 |
|
98 | 102 | /// The MCP client is the interface for MCP operations. |
@@ -346,4 +350,42 @@ where |
346 | 350 | // https://modelcontextprotocol.io/docs/concepts/tools#error-handling-2 |
347 | 351 | self.send_request("tools/call", params).await |
348 | 352 | } |
| 353 | + |
| 354 | + async fn list_prompts(&self, next_cursor: Option<String>) -> Result<ListPromptsResult, Error> { |
| 355 | + if !self.completed_initialization() { |
| 356 | + return Err(Error::NotInitialized); |
| 357 | + } |
| 358 | + |
| 359 | + // If prompts is not supported, return an error |
| 360 | + if self.server_capabilities.as_ref().unwrap().prompts.is_none() { |
| 361 | + return Err(Error::RpcError { |
| 362 | + code: METHOD_NOT_FOUND, |
| 363 | + message: "Server does not support 'prompts' capability".to_string(), |
| 364 | + }); |
| 365 | + } |
| 366 | + |
| 367 | + let payload = next_cursor |
| 368 | + .map(|cursor| serde_json::json!({"cursor": cursor})) |
| 369 | + .unwrap_or_else(|| serde_json::json!({})); |
| 370 | + |
| 371 | + self.send_request("prompts/list", payload).await |
| 372 | + } |
| 373 | + |
| 374 | + async fn get_prompt(&self, name: &str, arguments: Value) -> Result<GetPromptResult, Error> { |
| 375 | + if !self.completed_initialization() { |
| 376 | + return Err(Error::NotInitialized); |
| 377 | + } |
| 378 | + |
| 379 | + // If prompts is not supported, return an error |
| 380 | + if self.server_capabilities.as_ref().unwrap().prompts.is_none() { |
| 381 | + return Err(Error::RpcError { |
| 382 | + code: METHOD_NOT_FOUND, |
| 383 | + message: "Server does not support 'prompts' capability".to_string(), |
| 384 | + }); |
| 385 | + } |
| 386 | + |
| 387 | + let params = serde_json::json!({ "name": name, "arguments": arguments }); |
| 388 | + |
| 389 | + self.send_request("prompts/get", params).await |
| 390 | + } |
349 | 391 | } |
0 commit comments