Skip to main content

synwire_core/mcp/
traits.rs

1//! MCP transport and server status traits.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::BoxFuture;
7use crate::agents::error::AgentError;
8
9// ---------------------------------------------------------------------------
10// Tool descriptor
11// ---------------------------------------------------------------------------
12
13/// Descriptor for a tool exposed by an MCP server.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct McpToolDescriptor {
16    /// Unique tool name within the server.
17    pub name: String,
18    /// Human-readable description.
19    pub description: String,
20    /// JSON Schema for the tool's input parameters.
21    pub input_schema: Value,
22}
23
24// ---------------------------------------------------------------------------
25// Server status
26// ---------------------------------------------------------------------------
27
28/// Connection state of an MCP server.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
30#[non_exhaustive]
31pub enum McpConnectionState {
32    /// Not yet connected.
33    #[default]
34    Disconnected,
35    /// Connection attempt in progress.
36    Connecting,
37    /// Connected and ready.
38    Connected,
39    /// Reconnection in progress after a drop.
40    Reconnecting,
41    /// Server has been shut down.
42    Shutdown,
43}
44
45/// Status snapshot for an MCP server connection.
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct McpServerStatus {
48    /// Server name.
49    pub name: String,
50    /// Current connection state.
51    pub state: McpConnectionState,
52    /// Number of successful tool calls.
53    pub calls_succeeded: u64,
54    /// Number of failed tool calls.
55    pub calls_failed: u64,
56    /// Whether this server is currently enabled.
57    pub enabled: bool,
58}
59
60// ---------------------------------------------------------------------------
61// McpTransport trait
62// ---------------------------------------------------------------------------
63
64/// Low-level transport layer for communicating with an MCP server.
65pub trait McpTransport: Send + Sync {
66    /// Establish (or re-establish) a connection to the server.
67    fn connect(&self) -> BoxFuture<'_, Result<(), AgentError>>;
68
69    /// Reconnect after a connection drop.
70    fn reconnect(&self) -> BoxFuture<'_, Result<(), AgentError>>;
71
72    /// Return the current connection status.
73    fn status(&self) -> BoxFuture<'_, McpServerStatus>;
74
75    /// List all tools advertised by the server.
76    fn list_tools(&self) -> BoxFuture<'_, Result<Vec<McpToolDescriptor>, AgentError>>;
77
78    /// Invoke a tool by name with the given arguments.
79    fn call_tool(
80        &self,
81        tool_name: &str,
82        arguments: Value,
83    ) -> BoxFuture<'_, Result<Value, AgentError>>;
84
85    /// Disconnect from the server cleanly.
86    fn disconnect(&self) -> BoxFuture<'_, Result<(), AgentError>>;
87}