Crate tmcp

Crate tmcp 

Source
Expand description

§tmcp

A complete Rust implementation of the Model Context Protocol (MCP), providing both client and server capabilities for building AI-integrated applications.

§Overview

tmcp offers an ergonomic API for implementing MCP servers and clients with support for tools, resources, and prompts. The library uses async/await patterns with Tokio and provides procedural macros to eliminate boilerplate.

§Features

  • Derive Macros: Simple #[mcp_server] attribute for automatic implementation
  • Multiple Transports: TCP, HTTP (with SSE), and stdio support
  • Type Safety: Strongly typed protocol messages with serde
  • Async-First: Built on Tokio for high-performance async I/O

§Quick Example

use serde::{Deserialize, Serialize};
use tmcp::{mcp_server, schema::*, schemars, tool, Result, Server, ServerCtx};

#[derive(Default)]
struct WeatherServer;

// Tool input schema is automatically derived from the struct using serde and schemars.
#[derive(Debug, Serialize, Deserialize, schemars::JsonSchema)]
struct WeatherParams {
    city: String,
}

// The mcp_server macro generates the necessary boilerplate to expose methods as MCP tools.
#[mcp_server]
impl WeatherServer {
    // The doc comment becomes the tool's description in the MCP schema.
    #[tool]
    /// Get current weather for a city
    async fn get_weather(&self, _ctx: &ServerCtx, params: WeatherParams) -> Result<CallToolResult> {
        // Simulate weather API call
        let temperature = 22.5;
        let conditions = "Partly cloudy";

        Ok(CallToolResult::new().with_text_content(format!(
            "Weather in {}: {}°C, {}",
            params.city, temperature, conditions
        )))
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let server = Server::default().with_connection(WeatherServer::default);
    server.serve_stdio().await?;
    Ok(())
}

§Transport Options

  • TCP: server.listen_tcp("127.0.0.1:3000")
  • HTTP: server.listen_http("127.0.0.1:3000") (uses SSE for server->client)
  • Stdio: server.listen_stdio() for subprocess integration

Re-exports§

pub use schemars;

Modules§

auth
OAuth Authentication Module for MCP
schema
Public schema types for MCP messages.
testutils
Test utilities for tmcp.

Structs§

Arguments
Generic argument map used for passing parameters to tools and prompts.
Client
MCP Client implementation
ClientCtx
Context provided to ClientConnection implementations for interacting with the server
Server
MCP Server implementation
ServerCtx
Context provided to ServerConn implementations for interacting with clients
ServerHandle
Handle for controlling a running MCP server instance

Enums§

Error
Error type for MCP operations.

Traits§

ClientAPI
ClientAPI holds all client methods defined by the MCP specification. These methods are exposed by the client and called by the server.
ClientConn
Connection trait that client implementers must implement. Each client connection will have its own instance of the implementation
ServerAPI
ServerAPI holds all server methods defined by the MCP specification. These methods are exposed by the server and called by the client.
ServerConn
Connection trait that server implementers must implement Each client connection will have its own instance of the implementation

Type Aliases§

Result
Result alias using the crate error type.

Attribute Macros§

mcp_server
Derive the ServerConn methods from an impl block.
tool
Mark a method as an mcp tool.