Minimalistic Rust Implementation Of Model Context Protocol from Anthropic
Minimalistic Rust Implementation Of Model Context Protocol(MCP).
Learn more about MCP: MCP
Given it is still very early stage of MCP adoption, the goal is to remain agile and easy to understand.
This implementation aims to capture the core idea of MCP while maintaining compatibility with Claude Desktop.
Many optional features are not implemented yet.
Some guidelines:
Using a Tool
trait for better compile time reusability.
impl Tool for CreateEntitiesTool {
fn name(&self) -> String {
"create_entities".to_string()
}
fn description(&self) -> String {
"Create multiple new entities".to_string()
}
fn input_schema(&self) -> serde_json::Value {
json!({
"type":"object",
"properties":{
"entities":{
"type":"array",
"items":{
"type":"object",
"properties":{
"name":{"type":"string"},
"entityType":{"type":"string"},
"observations":{
"type":"array", "items":{"type":"string"}
}
},
"required":["name","entityType","observations"]
}
}
},
"required":["entities"]
})
}
fn call(&self, input: Option<serde_json::Value>) -> Result<CallToolResponse> {
let args = input.unwrap_or_default();
let entities = args
.get("entities")
.ok_or(anyhow::anyhow!("missing arguments `entities`"))?;
let entities: Vec<Entity> = serde_json::from_value(entities.clone())?;
let created = self.kg.lock().unwrap().create_entities(entities)?;
self.kg
.lock()
.unwrap()
.save_to_file(&self.memory_file_path)?;
Ok(CallToolResponse {
content: vec![ToolResponseContent::Text {
text: json!(created).to_string(),
}],
is_error: None,
meta: None,
})
}
}
let server = Server::builder(StdioTransport)
.capabilities(ServerCapabilities {
tools: Some(json!({})),
..Default::default()
})
.request_handler("tools/list", list_tools)
.request_handler("tools/call", call_tool)
.request_handler("resources/list", |_req: ListRequest| {
Ok(ResourcesListResponse {
resources: vec![],
next_cursor: None,
meta: None,
})
})
.build();
#[tokio::main]
async fn main() -> Result<()> {
#[cfg(unix)]
{
// Create transport connected to cat command which will stay alive
let transport = ClientStdioTransport::new("cat", &[])?;
// Open transport
transport.open()?;
let client = ClientBuilder::new(transport).build();
let client_clone = client.clone();
tokio::spawn(async move { client_clone.start().await });
let response = client
.request(
"echo",
None,
RequestOptions::default().timeout(Duration::from_secs(1)),
)
.await?;
println!("{:?}", response);
}
#[cfg(windows)]
{
println!("Windows is not supported yet");
}
Ok(())
}
For complete feature please refer to the MCP specification.
For now use claude desktop as client.
AntigmaLabs/mcp-sdk
November 29, 2024
May 30, 2025
Rust