1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! MCP server implementation built on the official rmcp SDK.
//!
//! This module provides MCP (Model Context Protocol) server functionality using
//! the official `rmcp` crate (v0.16+). It supports:
//!
//! - Compile-time tool registration via `inventory`
//! - Stateful and stateless server modes
//! - MCP 2026-07-28 protocol adaptation (headers, cache semantics, MRTR)
//! - Integration with SDForge's `#[service_api]` macro
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use sdforge::mcp::{SdForgeTool, McpToolRegistration};
//! use sdforge::core::ApiMetadata;
//!
//! struct MyTool;
//! impl SdForgeTool for MyTool {
//! fn name(&self) -> &str { "my_tool" }
//! fn description(&self) -> &str { "Does something useful" }
//! fn input_schema(&self) -> serde_json::Value {
//! serde_json::json!({"type": "object"})
//! }
//! fn call(&self, input: Option<serde_json::Value>) -> Result<rmcp::model::CallToolResult, rmcp::model::ErrorData> {
//! Ok(rmcp::model::CallToolResult::success(vec![
//! rmcp::model::Content::text("Hello from my_tool".to_string()),
//! ]))
//! }
//! }
//!
//! inventory::submit!(McpToolRegistration::new(
//! "my_tool",
//! "v1",
//! || std::sync::Arc::new(MyTool) as std::sync::Arc<dyn SdForgeTool>,
//! || ApiMetadata::default(),
//! ));
//! ```
use crate;
use cratedefine_registration;
use ErrorData;
use Value;
use Arc;
/// Type alias for rmcp's `ErrorData`, kept for API compatibility with the
/// `SdForgeTool` trait's `call` method return type.
///
/// In rmcp 0.16, the error type is `ErrorData` (not `McpError`). We re-export
/// it under the `McpError` name so existing code referencing `McpError` continues
/// to work without changes.
pub type McpError = ErrorData;
// ============================================================================
// Sub-modules
// ============================================================================
// Sub-modules for MCP 2026-07-28 protocol adaptation.
// Sub-modules for server and handler (extracted for maintainability).
// Test module (only compiled in test builds).
// ============================================================================
// Re-exports of public types
// ============================================================================
pub use McpToolInstance;
pub use McpHeaderInfo;
pub use ;
pub use SdForgeMcpServer;
pub use StatelessServerHandler;
// ============================================================================
// SdForgeTool trait — bridges SDForge tools to rmcp's ServerHandler
// ============================================================================
/// Trait for MCP tools registered via SDForge's inventory system.
///
/// Each tool provides its name, description, JSON Schema for inputs, and a
/// `call` handler that returns a `CallToolResult`. This trait replaces the
/// old `mcp_sdk::tools::Tool` trait from the unmaintained 0.0.3 crate.
// ============================================================================
// McpToolRegistration — compile-time registration via inventory
// ============================================================================
// MCP tool registration for compile-time registration.
//
// Generated by `define_registration!` macro for the unified registration system.
// The instance type is `Arc<dyn SdForgeTool>` so tools can be shared across
// threads without copying.
define_registration!;
/// Stub struct for non-MCP builds
;
// ============================================================================
// get_mcp_tools — collect all registered tools from inventory
// ============================================================================
/// Get all registered MCP tools as runtime instances.
///
/// This function collects all `McpToolRegistration` entries from the
/// `inventory` registry and creates `McpToolInstance` objects with the
/// associated metadata.
// ============================================================================
// build — construct an MCP server ready to serve
// ============================================================================
/// Build a `SdForgeMcpServer` from registered tools.
///
/// This constructs a server that implements `rmcp::handler::server::ServerHandler`.
/// To start the server, use `ServiceExt::serve()` on the returned server with
/// a transport (e.g., `rmcp::transport::stdio()`).
///
/// # Example
///
/// ```rust,ignore
/// use rmcp::{ServiceExt, transport::stdio};
///
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// let server = sdforge::mcp::build();
/// let service = server.serve(stdio()).await?;
/// service.waiting().await?;
/// # Ok(())
/// # }
/// ```