Skip to content

Commit 1a2dfef

Browse files
committed
fix clippy, rename tool
1 parent 9c96381 commit 1a2dfef

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

dsc/locales/en-us.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ failedToStartMcpServer = "Failed to start MCP server: %{error}"
6060

6161
[mcp.mod]
6262
failedToInitialize = "Failed to initialize MCP server: %{error}"
63+
failedToStart = "Failed to start MCP server: %{error}"
6364
instructions = "This server provides tools that work with DSC (DesiredStateConfiguration) which enables users to manage and configure their systems declaratively."
6465
serverStopped = "MCP server stopped"
6566
failedToWait = "Failed to wait for MCP server: %{error}"
67+
failedToCreateRuntime = "Failed to create async runtime: %{error}"
6668

6769
[resolve]
6870
processingInclude = "Processing Include input"

dsc/src/mcp/list_resources.rs renamed to dsc/src/mcp/list_dsc_resources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl McpServer {
3939
open_world_hint = true,
4040
)
4141
)]
42-
async fn list_resources(&self) -> Result<Json<ResourceListResult>, McpError> {
42+
async fn list_dsc_resources(&self) -> Result<Json<ResourceListResult>, McpError> {
4343
let result = task::spawn_blocking(move || {
4444
let mut dsc = DscManager::new();
4545
let mut resources = Vec::new();

dsc/src/mcp/mod.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rmcp::{
1313
};
1414
use rust_i18n::t;
1515

16-
pub mod list_resources;
16+
pub mod list_dsc_resources;
1717

1818
#[derive(Debug, Clone)]
1919
pub struct McpServer {
@@ -48,24 +48,32 @@ impl ServerHandler for McpServer {
4848
/// # Errors
4949
///
5050
/// This function will return an error if the MCP server fails to start.
51-
#[tokio::main(flavor = "multi_thread")]
52-
pub async fn start_mcp_server() -> Result<(), McpError> {
53-
let service = match McpServer::new().serve(stdio()).await {
54-
Ok(service) => service,
55-
Err(err) => {
56-
tracing::error!(error = %err, "Failed to start MCP server");
57-
return Err(McpError::internal_error(t!("mcp.mod.failedToInitialize", error = err.to_string()), None));
58-
}
59-
};
51+
pub async fn start_mcp_server_async() -> Result<(), McpError> {
52+
// Initialize the MCP server
53+
let server = McpServer::new();
6054

61-
match service.waiting().await {
62-
Ok(_) => {
63-
tracing::info!("{}", t!("mcp.mod.serverStopped"));
64-
}
65-
Err(err) => {
66-
tracing::error!("{}", t!("mcp.mod.failedToWait", error = err));
67-
}
68-
}
55+
// Try to create the service with proper error handling
56+
let service = server.serve(stdio()).await
57+
.map_err(|err| McpError::internal_error(t!("mcp.mod.failedToInitialize", error = err.to_string()), None))?;
58+
59+
// Wait for the service to complete with proper error handling
60+
service.waiting().await
61+
.map_err(|err| McpError::internal_error(t!("mcp.mod.serverWaitFailed", error = err.to_string()), None))?;
62+
63+
tracing::info!("{}", t!("mcp.mod.serverStopped"));
64+
Ok(())
65+
}
66+
67+
/// Synchronous wrapper to start the MCP server
68+
///
69+
/// # Errors
70+
///
71+
/// This function will return an error if the MCP server fails to start or if the tokio runtime cannot be created.
72+
pub fn start_mcp_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
73+
let rt = tokio::runtime::Runtime::new()
74+
.map_err(|e| McpError::internal_error(t!("mcp.mod.failedToCreateRuntime", error = e.to_string()), None))?;
6975

76+
rt.block_on(start_mcp_server_async())
77+
.map_err(|e| McpError::internal_error(t!("mcp.mod.failedToStart", error = e.to_string()), None))?;
7078
Ok(())
7179
}

dsc/tests/dsc_mcp.tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ Describe 'Tests for MCP server' {
7373

7474
$response.id | Should -Be 2
7575
$response.result.tools.Count | Should -Be 1
76-
$response.result.tools[0].name | Should -BeExactly 'list_resources'
76+
$response.result.tools[0].name | Should -BeExactly 'list_dsc_resources'
7777
}
7878

79-
It 'Calling list_resources works' {
79+
It 'Calling list_dsc_resources works' {
8080
$mcpRequest = @{
8181
jsonrpc = "2.0"
8282
id = 3
8383
method = "tools/call"
8484
params = @{
85-
name = "list_resources"
85+
name = "list_dsc_resources"
8686
arguments = @{}
8787
}
8888
}

0 commit comments

Comments
 (0)