pub struct McpMessage {
pub jsonrpc: String,
pub id: Option<Value>,
pub method: Option<String>,
pub params: Option<Value>,
pub result: Option<Value>,
pub error: Option<JsonRpcError>,
}Expand description
A parsed MCP message (JSON-RPC 2.0 envelope).
MCP uses JSON-RPC 2.0 over stdio or HTTP. Every message is one of:
- Request: has
method+params, hasid - Response: has
resultorerror, hasid - Notification: has
method+params, noid
Fields§
§jsonrpc: String§id: Option<Value>Request/response correlation ID. None for notifications.
method: Option<String>Method name (e.g. “tools/call”, “tools/list”, “initialize”).
params: Option<Value>Method parameters.
result: Option<Value>Success response payload.
error: Option<JsonRpcError>Error response payload.
Implementations§
Source§impl McpMessage
impl McpMessage
Sourcepub fn parse(raw: &str) -> Result<Self, Error>
pub fn parse(raw: &str) -> Result<Self, Error>
Parse a JSON-RPC message from a raw JSON string.
Sourcepub fn to_json(&self) -> Result<String, Error>
pub fn to_json(&self) -> Result<String, Error>
Serialize this message to a JSON string (no trailing newline).
Sourcepub fn is_request(&self) -> bool
pub fn is_request(&self) -> bool
True if this is a request (has method + id).
Sourcepub fn is_notification(&self) -> bool
pub fn is_notification(&self) -> bool
True if this is a notification (has method, no id).
Sourcepub fn is_response(&self) -> bool
pub fn is_response(&self) -> bool
True if this is a response (has result or error + id).
Sourcepub fn tool_name(&self) -> Option<&str>
pub fn tool_name(&self) -> Option<&str>
If this is a tools/call request, extract the tool name.
Sourcepub fn resource_uri(&self) -> Option<&str>
pub fn resource_uri(&self) -> Option<&str>
If this is a resources/read request, extract the resource URI from params.uri.
Sourcepub fn prompt_name(&self) -> Option<&str>
pub fn prompt_name(&self) -> Option<&str>
If this is a prompts/get request, extract the prompt name from params.name.
Sourcepub fn method_resource_name(&self) -> Option<&str>
pub fn method_resource_name(&self) -> Option<&str>
Return the primary “resource identifier” being accessed for any MCP method.
tools/call-> tool name (params.name)resources/read-> resource URI (params.uri)prompts/get-> prompt name (params.name)- Listing methods (
resources/list,tools/list,prompts/list) ->None - All other methods ->
None
Trait Implementations§
Source§impl Clone for McpMessage
impl Clone for McpMessage
Source§fn clone(&self) -> McpMessage
fn clone(&self) -> McpMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more