Module handler

Module handler 

Source
Expand description

MCP Handler Trait and Core Types

This module defines the core McpHandler trait that servers must implement to provide MCP functionality. It also includes essential types for notifications, context management, and protocol data structures.

§Overview

The McpHandler trait is the main integration point for the solidmcp library. You can either implement this trait directly for full control, or use the higher-level framework API in the framework module for a more ergonomic experience.

§Example Implementation

use solidmcp::handler::{McpHandler, McpContext, ToolDefinition};
use anyhow::Result;
use async_trait::async_trait;
use serde_json::{json, Value};

struct MyHandler {
    // Your application state
}

#[async_trait]
impl McpHandler for MyHandler {
    async fn initialize(&self, params: Value, context: &McpContext) -> Result<Value> {
        Ok(json!({
            "protocolVersion": "2025-06-18",
            "capabilities": {
                "tools": {}
            },
            "serverInfo": {
                "name": "my-server",
                "version": "1.0.0"
            }
        }))
    }

    async fn list_tools(&self, context: &McpContext) -> Result<Vec<ToolDefinition>> {
        Ok(vec![
            ToolDefinition {
                name: "hello".to_string(),
                description: "Say hello".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "name": { "type": "string" }
                    },
                    "required": ["name"]
                }),
            }
        ])
    }

    async fn call_tool(&self, name: &str, arguments: Value, context: &McpContext) -> Result<Value> {
        match name {
            "hello" => {
                let name = arguments.get("name")
                    .and_then(|v| v.as_str())
                    .unwrap_or("World");
                Ok(json!({ "message": format!("Hello, {}!", name) }))
            }
            _ => Err(anyhow::anyhow!("Unknown tool: {}", name))
        }
    }
}

Structs§

McpContext
Context provided to MCP handler methods.
PromptArgument
Prompt argument definition.
PromptContent
Prompt content for MCP prompts/get response.
PromptInfo
Prompt information for MCP prompts/list response.
PromptMessage
Prompt message.
ResourceContent
Resource content for MCP resources/read response.
ResourceInfo
Resource information for MCP resources/list response.
ToolDefinition
Tool definition for MCP tools/list response.
TypedToolDefinition
Typed tool definition helper that provides compile-time type safety.

Enums§

LogLevel
Log levels for log message notifications.
McpNotification
Notification types that can be sent from server to client.

Traits§

McpHandler
Core trait that users must implement to provide MCP functionality.