turul-mcp-server
High-level framework for building Model Context Protocol (MCP) servers with full MCP 2025-06-18 compliance.
Overview
turul-mcp-server
is the core framework crate that provides everything you need to build production-ready MCP servers. It offers four different approaches for tool creation, automatic session management, real-time SSE notifications, and pluggable storage backends.
Features
- ✅ MCP 2025-06-18 Compliance - Full protocol compliance with latest features
- ✅ Zero-Configuration - Framework auto-determines ALL methods from types
- ✅ Four Tool Creation Levels - Function/derive/builder/manual approaches
- ✅ Real-time Notifications - SSE streaming with JSON-RPC format
- ✅ Session Management - UUID v7 sessions with automatic cleanup
- ✅ Pluggable Storage - InMemory, SQLite, PostgreSQL, DynamoDB backends
- ✅ Type Safety - Compile-time schema generation and validation
Architectural Patterns
This framework supports two primary architectural patterns for building MCP servers:
-
Simple (Integrated Transport): This is the easiest way to get started. The
McpServer
includes a default HTTP transport layer. You can configure and run the server with a single builder chain, as shown in the Quick Start examples below. This is recommended for most use cases. -
Advanced (Pluggable Transport): For more complex scenarios, such as serverless deployments or custom transports, you can use
McpServer::builder()
to create a transport-agnostic configuration object. This object is then passed to a separate transport crate, liketurul-mcp-aws-lambda
orturul-http-mcp-server
, for execution. This provides maximum flexibility.
See the turul-http-mcp-server
README for a detailed example of the pluggable transport pattern.
Quick Start
Add this to your Cargo.toml
:
[]
= "0.2.0"
= "0.2.0" # Required for function macros and derive macros
= { = "1.0", = ["full"] }
Level 1: Function Macros (Simplest)
use mcp_tool;
use *;
async
async
Level 2: Derive Macros (Struct-Based)
Requires: turul-mcp-derive = "0.2.0"
dependency
use McpTool;
use *;
async
Level 3: Builder Pattern (Runtime Flexibility)
Note: ToolBuilder
is re-exported from the server crate for convenience. You can use either:
turul_mcp_server::ToolBuilder
(recommended for servers)turul_mcp_builders::ToolBuilder
(direct import)
use *;
use ToolBuilder;
use json;
async
Resources
The framework provides powerful resource management with automatic URI template detection and parameter extraction.
Resource Function Registration
Use .resource_fn()
to register resources created with the #[mcp_resource]
macro:
use mcp_resource;
use *;
use ResourceContent;
// Static resource
async
// Template resource - automatic parameter extraction
async
async
Alternative: Direct Resource Registration
You can also register resource instances directly:
use *;
use *;
use async_trait;
;
// Implement metadata traits...
let server = builder
.resource // Direct instance
.build?;
Session Management & Storage
Pluggable Storage Backends
use *;
use ;
use Arc;
// SQLite for single-instance deployments
let storage = new;
// PostgreSQL for multi-instance deployments
let storage = new;
let server = builder
.name
.version
.with_session_storage
.bind_address
// Add your tools here: .tool(your_tool)
.build?;
server.run.await
Session Context in Tools
use *;
use McpTool;
Real-time Notifications
SSE Streaming
The framework automatically provides SSE endpoints for real-time notifications:
- POST
/mcp
+Accept: application/json
→ JSON responses - GET
/mcp
+Accept: text/event-stream
→ SSE stream - Session isolation → Each session gets independent notification channels
// Notifications are sent automatically from tools
session.notify_progress.await;
session.notify_log.await;
Server Configuration
HTTP Server with Custom Port
use *;
let server = builder
.name
.version
.bind_address // Custom bind address
// Add your tools here: .tool(your_tool)
.build?;
server.run.await
Production Configuration
use *;
use PostgresSessionStorage;
use Arc;
let storage = new;
let server = builder
.name
.version
.bind_address
.with_session_storage
// Add your tools here: .tool(your_tool)
.build?;
server.run.await
Protocol Compliance
MCP 2025-06-18 Features
- ✅ Initialize/Initialized - Capability negotiation
- ✅ Tools - Dynamic tool calling with schema validation
- ✅ Resources - File and data resource access
- ✅ Prompts - Template-based prompt management
- ✅ Sampling - AI model sampling integration
- ✅ Completion - Context-aware autocompletion
- ✅ Roots - Secure file system access
- ✅ Notifications - Real-time event streaming
- ✅ Cancellation - Request cancellation with progress tokens
- ✅ Logging - Structured logging with multiple levels
Server Capabilities
Static servers should advertise truthful capabilities:
let server = builder
.name
.version
.tool
.build?;
// Framework automatically sets:
// - tools.listChanged = false (static tool list)
// - prompts.listChanged = false (static prompt list)
// - resources.subscribe = false (no subscriptions)
// - resources.listChanged = false (static resource list)
// Optional strict lifecycle: notifications/initialized can be sent after successful setup
Examples
The turul-mcp-framework repository contains 25+ comprehensive examples:
- minimal-server - Simplest possible server
- stateful-server - Session management patterns
- notification-server - Real-time SSE notifications
- comprehensive-server - All MCP features demonstrated
- lambda-mcp-server - AWS Lambda deployment
Architecture
Framework Layers
┌─────────────────────────┐
│ turul-mcp-server │ ← High-level framework (this crate)
├─────────────────────────┤
│ turul-http-mcp-server │ ← HTTP transport layer
├─────────────────────────┤
│ turul-mcp-json-rpc-server │ ← JSON-RPC dispatch
├─────────────────────────┤
│ turul-mcp-protocol │ ← Protocol types & traits
└─────────────────────────┘
Trait-Based Design
All MCP components use consistent trait patterns from turul-mcp-builders
:
- Tools →
ToolDefinition
trait with fine-grained composition - Resources →
ResourceDefinition
trait - Prompts →
PromptDefinition
trait - Notifications →
NotificationDefinition
trait
See turul-mcp-builders
for trait documentation.
Feature Flags
[]
= { = "0.2.0", = ["sqlite", "postgres"] }
default
- All features enabledhttp
- HTTP transport layer (included by default)sse
- Server-Sent Events streaming (included by default)sqlite
- SQLite session storage backendpostgres
- PostgreSQL session storage backenddynamodb
- DynamoDB session storage backend
Testing
# Run all tests
# Test with specific storage backend
# Integration tests
License
Licensed under the MIT License. See LICENSE for details.
Contributing
See the main repository CONTRIBUTING.md for contribution guidelines.