Skip to content

feat: implement ToMessage trait #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2025
Merged

Conversation

hashemix
Copy link
Member

@hashemix hashemix commented Feb 20, 2025

📌 Summary

Introduced new ToMessage Trait that simplifies the construction of MCP messages, reducing the amount of code required.

Example:

Before Introducing the ToMessage trait

async fn send_ping_request(ping_request: rust_mcp_schema::PingRequest) {
    let request_id = get_next_id();

    // construct a ServerMessage from the ping_request , with request_id
    let message = ServerMessage::Request(ServerJsonrpcRequest::new(
        request_id,
        RequestFromServer::ServerRequest(ServerRequest::PingRequest(ping_request)),
    ));

    // serialize message into a valid rpcJsonrpcMessage string
    let rpc_message_json_str = message.to_string();

    // send the ping request to the client
    tranport.send(rpc_message_json_str).await
}

After Introducing the ToMessage trait

async fn send_ping_request(ping_request: rust_mcp_schema::PingRequest) {
     let request_id = get_next_id();

    // construct a ServerMessage from the ping_request , with request_id
    let message: ServerMessage = ping_request
        .to_message(request_id)
        .unwrap();
    
    // serialize message into a valid rpcJsonrpcMessage string
    let rpc_message_json_str = message.to_string();

    // send the ping request to the client
    tranport.send(rpc_message_json_str).await
}

@hashemix hashemix self-assigned this Feb 20, 2025
@hashemix hashemix merged commit 435f18b into main Feb 21, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant