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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! MCP protocol layer for tool discovery and dispatch
//!
//! This module handles the core MCP protocol functionality including tool discovery,
//! execution dispatch, and protocol communication. It serves as the interface between
//! AI agents and the SCIM server operations.
use super::core::{ScimMcpServer, ScimToolResult};
use super::handlers::{system_info, user_crud, user_queries};
use super::tools::{system_schemas, user_schemas};
use crate::ResourceProvider;
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
/// MCP JSON-RPC request structure
#[derive(Debug, Deserialize)]
struct McpRequest {
jsonrpc: String,
id: Option<Value>,
method: String,
params: Option<Value>,
}
/// MCP JSON-RPC response structure
#[derive(Debug, Serialize)]
pub struct McpResponse {
pub jsonrpc: String,
pub id: Option<Value>,
pub result: Option<Value>,
pub error: Option<Value>,
}
/// MCP JSON-RPC error structure
#[derive(Debug, Serialize)]
struct McpError {
code: i32,
message: String,
data: Option<Value>,
}
impl McpResponse {
/// Create a successful response
fn success(id: Option<Value>, result: Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: Some(result),
error: None,
}
}
/// Create an error response
fn error(id: Option<Value>, code: i32, message: String) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: None,
error: Some(json!(McpError {
code,
message,
data: None
})),
}
}
}
impl<P: ResourceProvider + Send + Sync + 'static> ScimMcpServer<P> {
/// Get the list of available MCP tools as JSON
///
/// Returns all tool definitions that AI agents can discover and execute.
/// Each tool includes its schema, parameters, and documentation.
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "mcp")]
/// use scim_server::mcp_integration::ScimMcpServer;
/// use scim_server::providers::StandardResourceProvider;
/// use scim_server::storage::InMemoryStorage;
/// # async fn example(mcp_server: ScimMcpServer<StandardResourceProvider<InMemoryStorage>>) {
/// let tools = mcp_server.get_tools();
/// println!("Available tools: {}", tools.len());
/// # }
/// ```
pub fn get_tools(&self) -> Vec<Value> {
vec![
user_schemas::create_user_tool(),
user_schemas::get_user_tool(),
user_schemas::update_user_tool(),
user_schemas::delete_user_tool(),
user_schemas::list_users_tool(),
user_schemas::search_users_tool(),
user_schemas::user_exists_tool(),
system_schemas::get_schemas_tool(),
system_schemas::get_server_info_tool(),
]
}
/// Execute a tool by name with arguments
///
/// This is the main dispatch function that routes tool execution requests
/// to the appropriate handler based on the tool name.
///
/// # Arguments
/// * `tool_name` - The name of the tool to execute
/// * `arguments` - JSON arguments for the tool execution
///
/// # Returns
/// A `ScimToolResult` containing the execution outcome
pub async fn execute_tool(&self, tool_name: &str, arguments: Value) -> ScimToolResult {
debug!("Executing MCP tool: {} with args: {}", tool_name, arguments);
match tool_name {
// User CRUD operations
"scim_create_user" => user_crud::handle_create_user(self, arguments).await,
"scim_get_user" => user_crud::handle_get_user(self, arguments).await,
"scim_update_user" => user_crud::handle_update_user(self, arguments).await,
"scim_delete_user" => user_crud::handle_delete_user(self, arguments).await,
// User query operations
"scim_list_users" => user_queries::handle_list_users(self, arguments).await,
"scim_search_users" => user_queries::handle_search_users(self, arguments).await,
"scim_user_exists" => user_queries::handle_user_exists(self, arguments).await,
// System information operations
"scim_get_schemas" => system_info::handle_get_schemas(self, arguments).await,
"scim_server_info" => system_info::handle_server_info(self, arguments).await,
// Unknown tool
_ => ScimToolResult {
success: false,
content: json!({
"error": "Unknown tool",
"tool_name": tool_name
}),
metadata: None,
},
}
}
/// Run the MCP server using stdio communication
///
/// Starts the MCP server and begins listening for tool execution requests
/// over standard input/output. This is the standard MCP communication method.
///
/// # Examples
///
/// ```rust,no_run
/// # #[cfg(feature = "mcp")]
/// use scim_server::mcp_integration::ScimMcpServer;
/// use scim_server::providers::StandardResourceProvider;
/// use scim_server::storage::InMemoryStorage;
/// # async fn example(mcp_server: ScimMcpServer<StandardResourceProvider<InMemoryStorage>>) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// // Run MCP server
/// mcp_server.run_stdio().await?;
/// # Ok(())
/// # }
/// ```
pub async fn run_stdio(self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
info!("SCIM MCP server starting stdio communication");
info!(
"Available tools: {:?}",
self.get_tools()
.iter()
.map(|t| t.get("name"))
.collect::<Vec<_>>()
);
let stdin = tokio::io::stdin();
let mut stdout = tokio::io::stdout();
let mut reader = BufReader::new(stdin);
let mut line = String::new();
info!("SCIM MCP server ready - listening on stdio");
loop {
line.clear();
match reader.read_line(&mut line).await {
Ok(0) => {
debug!("EOF received, shutting down");
break; // EOF
}
Ok(_) => {
let line_content = line.trim();
if line_content.is_empty() {
continue;
}
debug!("Received request: {}", line_content);
if let Some(response) = self.handle_mcp_request(line_content).await {
let response_json = match serde_json::to_string(&response) {
Ok(json) => json,
Err(e) => {
error!("Failed to serialize response: {}", e);
continue;
}
};
debug!("Sending response: {}", response_json);
if let Err(e) = stdout.write_all(response_json.as_bytes()).await {
error!("Failed to write response: {}", e);
break;
}
if let Err(e) = stdout.write_all(b"\n").await {
error!("Failed to write newline: {}", e);
break;
}
if let Err(e) = stdout.flush().await {
error!("Failed to flush stdout: {}", e);
break;
}
}
}
Err(e) => {
error!("Error reading from stdin: {}", e);
break;
}
}
}
info!("SCIM MCP server shutting down");
Ok(())
}
/// Handle a single MCP request and return the appropriate response
pub async fn handle_mcp_request(&self, line: &str) -> Option<McpResponse> {
let request: McpRequest = match serde_json::from_str(line) {
Ok(req) => req,
Err(e) => {
warn!("Failed to parse JSON request: {} - Input: {}", e, line);
return Some(McpResponse::error(None, -32700, "Parse error".to_string()));
}
};
debug!(
"Processing method: {} with id: {:?}",
request.method, request.id
);
match request.method.as_str() {
"initialize" => Some(self.handle_initialize(request.id)),
"notifications/initialized" => {
debug!("Received initialized notification - handshake complete");
None // Notifications don't require responses
}
"tools/list" => Some(self.handle_tools_list(request.id)),
"tools/call" => Some(self.handle_tools_call(request.id, request.params).await),
"ping" => Some(self.handle_ping(request.id)),
_ => {
warn!("Unknown method: {}", request.method);
Some(McpResponse::error(
request.id,
-32601,
"Method not found".to_string(),
))
}
}
}
/// Handle initialize request
fn handle_initialize(&self, id: Option<Value>) -> McpResponse {
debug!("Handling initialize request");
let result = json!({
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {}
},
"serverInfo": {
"name": self.server_info.name,
"version": self.server_info.version,
"description": self.server_info.description
}
});
McpResponse::success(id, result)
}
/// Handle tools/list request
fn handle_tools_list(&self, id: Option<Value>) -> McpResponse {
debug!("Handling tools/list request");
let tools = self.get_tools();
let result = json!({
"tools": tools
});
McpResponse::success(id, result)
}
/// Handle tools/call request
async fn handle_tools_call(&self, id: Option<Value>, params: Option<Value>) -> McpResponse {
debug!("Handling tools/call request");
let params = match params {
Some(p) => p,
None => {
return McpResponse::error(
id,
-32602,
"Invalid params: missing parameters".to_string(),
);
}
};
let tool_name = match params.get("name").and_then(|n| n.as_str()) {
Some(name) => name,
None => {
return McpResponse::error(
id,
-32602,
"Invalid params: missing tool name".to_string(),
);
}
};
let arguments = params.get("arguments").cloned().unwrap_or(json!({}));
debug!(
"Executing tool: {} with arguments: {}",
tool_name, arguments
);
let tool_result = self.execute_tool(tool_name, arguments).await;
if tool_result.success {
let result = json!({
"content": [
{
"type": "text",
"text": serde_json::to_string_pretty(&tool_result.content)
.unwrap_or_else(|_| "Error serializing result".to_string())
}
],
"_meta": tool_result.metadata
});
McpResponse::success(id, result)
} else {
McpResponse::error(
id,
-32000,
format!(
"Tool execution failed: {}",
tool_result
.content
.get("error")
.and_then(|e| e.as_str())
.unwrap_or("Unknown error")
),
)
}
}
/// Handle ping request
fn handle_ping(&self, id: Option<Value>) -> McpResponse {
debug!("Handling ping request");
McpResponse::success(id, json!({}))
}
}