Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions crates/mcpls-core/src/mcp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
//! The actual tool implementations use the `#[tool]` macro from rmcp
//! and are defined in the `server` module.

use std::sync::Arc;
use std::sync::{
Arc,
atomic::{AtomicU64, Ordering},
};

use rmcp::task_manager::OperationProcessor;
use tokio::sync::Mutex;

use crate::bridge::{ResourceSubscriptions, Translator};
Expand All @@ -18,22 +22,34 @@ use crate::bridge::{ResourceSubscriptions, Translator};
pub struct HandlerContext {
/// Translator for converting MCP calls to LSP requests.
pub translator: Arc<Mutex<Translator>>,
/// Processor for MCP task-augmented tool calls.
pub task_processor: Arc<Mutex<OperationProcessor>>,
/// Monotonic task ID source.
pub task_counter: Arc<AtomicU64>,
/// Set of resource URIs the MCP client has subscribed to.
pub subscriptions: Arc<ResourceSubscriptions>,
}

impl HandlerContext {
/// Create a new handler context.
#[must_use]
pub const fn new(
pub fn new(
translator: Arc<Mutex<Translator>>,
subscriptions: Arc<ResourceSubscriptions>,
) -> Self {
Self {
translator,
task_processor: Arc::new(Mutex::new(OperationProcessor::new())),
task_counter: Arc::new(AtomicU64::new(1)),
subscriptions,
}
}

/// Generate a new server-side task identifier.
pub fn next_task_id(&self) -> String {
let id = self.task_counter.fetch_add(1, Ordering::Relaxed);
format!("mcpls-task-{id}")
}
}

#[cfg(test)]
Expand All @@ -47,5 +63,8 @@ mod tests {
let subscriptions = Arc::new(ResourceSubscriptions::new());
let context = HandlerContext::new(translator, subscriptions);
assert!(Arc::strong_count(&context.translator) == 1);
assert!(Arc::strong_count(&context.task_processor) == 1);
assert_eq!(context.next_task_id(), "mcpls-task-1");
assert_eq!(context.next_task_id(), "mcpls-task-2");
}
}
Loading