Skip to main content

mcp

Attribute Macro mcp 

Source
#[mcp]
Expand description

Generate MCP (Model Context Protocol) tools from an impl block.

§Basic Usage

use server_less::mcp;

#[mcp]
impl FileTools {
    fn read_file(&self, path: String) -> String { /* ... */ }
}

§With Namespace

#[mcp(namespace = "file")]
impl FileTools {
    // Exposed as "file_read_file" tool
    fn read_file(&self, path: String) -> String { /* ... */ }
}

§Streaming Support

Methods returning impl Stream<Item = T> are automatically collected into arrays:

use futures::stream::{self, Stream};

#[mcp]
impl DataService {
    // Returns JSON array: [0, 1, 2, 3, 4]
    fn stream_numbers(&self, count: u32) -> impl Stream<Item = u32> + use<> {
        stream::iter(0..count)
    }
}

// Call with:
service.mcp_call_async("stream_numbers", json!({"count": 5})).await
// Returns: [0, 1, 2, 3, 4]

Note: Streaming methods require mcp_call_async, not mcp_call.

§Generated Methods

  • mcp_tools() -> Vec<serde_json::Value> - Tool definitions
  • mcp_call(&self, name, args) -> Result<Value, String> - Execute tool (sync only)
  • mcp_call_async(&self, name, args).await - Execute tool (supports async & streams)