feat: ToolError migration to ErrorData (#4051)
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
use anyhow::Result;
|
||||
use mcp_core::handler::{PromptError, ResourceError};
|
||||
use mcp_core::{handler::ToolError, protocol::ServerCapabilities};
|
||||
use mcp_core::protocol::ServerCapabilities;
|
||||
use mcp_server::router::{CapabilitiesBuilder, RouterService};
|
||||
use mcp_server::{ByteTransport, Router, Server};
|
||||
use rmcp::model::{
|
||||
Content, JsonRpcMessage, Prompt, PromptArgument, RawResource, Resource, Tool, ToolAnnotations,
|
||||
Content, ErrorCode, ErrorData, JsonRpcMessage, Prompt, PromptArgument, RawResource, Resource,
|
||||
Tool, ToolAnnotations,
|
||||
};
|
||||
use rmcp::object;
|
||||
use serde_json::Value;
|
||||
use std::borrow::Cow;
|
||||
use std::{future::Future, pin::Pin, sync::Arc};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::{
|
||||
@@ -30,19 +32,19 @@ impl CounterRouter {
|
||||
}
|
||||
}
|
||||
|
||||
async fn increment(&self) -> Result<i32, ToolError> {
|
||||
async fn increment(&self) -> Result<i32, ErrorData> {
|
||||
let mut counter = self.counter.lock().await;
|
||||
*counter += 1;
|
||||
Ok(*counter)
|
||||
}
|
||||
|
||||
async fn decrement(&self) -> Result<i32, ToolError> {
|
||||
async fn decrement(&self) -> Result<i32, ErrorData> {
|
||||
let mut counter = self.counter.lock().await;
|
||||
*counter -= 1;
|
||||
Ok(*counter)
|
||||
}
|
||||
|
||||
async fn get_value(&self) -> Result<i32, ToolError> {
|
||||
async fn get_value(&self) -> Result<i32, ErrorData> {
|
||||
let counter = self.counter.lock().await;
|
||||
Ok(*counter)
|
||||
}
|
||||
@@ -127,7 +129,7 @@ impl Router for CounterRouter {
|
||||
tool_name: &str,
|
||||
_arguments: Value,
|
||||
_notifier: mpsc::Sender<JsonRpcMessage>,
|
||||
) -> Pin<Box<dyn Future<Output = Result<Vec<Content>, ToolError>> + Send + 'static>> {
|
||||
) -> Pin<Box<dyn Future<Output = Result<Vec<Content>, ErrorData>> + Send + 'static>> {
|
||||
let this = self.clone();
|
||||
let tool_name = tool_name.to_string();
|
||||
|
||||
@@ -145,7 +147,11 @@ impl Router for CounterRouter {
|
||||
let value = this.get_value().await?;
|
||||
Ok(vec![Content::text(value.to_string())])
|
||||
}
|
||||
_ => Err(ToolError::NotFound(format!("Tool {} not found", tool_name))),
|
||||
_ => Err(ErrorData {
|
||||
code: ErrorCode::INVALID_REQUEST,
|
||||
message: Cow::from(format!("Tool {} not found", tool_name)),
|
||||
data: None,
|
||||
}),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::{
|
||||
|
||||
type PromptFuture = Pin<Box<dyn Future<Output = Result<String, PromptError>> + Send + 'static>>;
|
||||
use mcp_core::{
|
||||
handler::{PromptError, ResourceError, ToolError},
|
||||
handler::{PromptError, ResourceError},
|
||||
protocol::{
|
||||
CallToolResult, Implementation, InitializeResult, ListPromptsResult, ListResourcesResult,
|
||||
ListToolsResult, PromptsCapability, ReadResourceResult, ResourcesCapability,
|
||||
@@ -14,8 +14,9 @@ use mcp_core::{
|
||||
},
|
||||
};
|
||||
use rmcp::model::{
|
||||
Content, GetPromptResult, JsonRpcMessage, JsonRpcRequest, JsonRpcResponse, JsonRpcVersion2_0,
|
||||
Prompt, PromptMessage, PromptMessageRole, RequestId, Resource, ResourceContents,
|
||||
Content, ErrorData, GetPromptResult, JsonRpcMessage, JsonRpcRequest, JsonRpcResponse,
|
||||
JsonRpcVersion2_0, Prompt, PromptMessage, PromptMessageRole, RequestId, Resource,
|
||||
ResourceContents,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use tokio::sync::mpsc;
|
||||
@@ -92,7 +93,7 @@ pub trait Router: Send + Sync + 'static {
|
||||
tool_name: &str,
|
||||
arguments: Value,
|
||||
notifier: mpsc::Sender<JsonRpcMessage>,
|
||||
) -> Pin<Box<dyn Future<Output = Result<Vec<Content>, ToolError>> + Send + 'static>>;
|
||||
) -> Pin<Box<dyn Future<Output = Result<Vec<Content>, ErrorData>> + Send + 'static>>;
|
||||
fn list_resources(&self) -> Vec<Resource>;
|
||||
fn read_resource(
|
||||
&self,
|
||||
|
||||
Reference in New Issue
Block a user