Skip to main content

mcpkit_rs/wasm/
mod.rs

1//! WASM tool execution support for MCP servers
2//!
3//! This module provides the ability to load and execute WebAssembly-based tools
4//! alongside native Rust tool handlers. WASM tools run in isolated sandboxed
5//! environments with controlled access to credentials and resources.
6
7// Manifest is always available when either feature is enabled
8pub mod manifest;
9
10// Runtime modules only available with full wasm-tools feature
11#[cfg(feature = "wasm-tools")]
12pub mod credentials;
13#[cfg(feature = "wasm-tools")]
14pub mod executor;
15#[cfg(feature = "wasm-tools")]
16pub mod integration;
17#[cfg(feature = "wasm-tools")]
18pub mod loader;
19#[cfg(feature = "wasm-tools")]
20pub mod metering;
21#[cfg(feature = "wasm-tools")]
22pub mod runtime;
23
24// Re-export manifest types always
25// Re-export runtime types only with wasm-tools
26#[cfg(feature = "wasm-tools")]
27pub use credentials::{CredentialProvider, CredentialValue};
28#[cfg(feature = "wasm-tools")]
29pub use executor::WasmToolExecutor;
30#[cfg(all(feature = "wasm-tools", feature = "config"))]
31pub use integration::load_wasm_tools_with_config;
32#[cfg(feature = "wasm-tools")]
33pub use integration::{CompositeToolHandler, WasmToolHandler, load_wasm_tools_from_directory};
34#[cfg(feature = "wasm-tools")]
35pub use loader::{LoadedWasmTool, WasmToolRegistry};
36pub use manifest::{
37    BundleContents, BundleDependencies, BundleEnvVar, BundleManifest, BundleMetadata,
38    BundleVerifier, CredentialRequirement, CredentialType, ManifestLoader, ManifestSaver,
39    McpToolInfo, RuntimeRequirements, ServerConfig, ServiceDependency, WasmToolManifest,
40};
41#[cfg(feature = "wasm-tools")]
42pub use metering::{
43    ComputeUnits, DisplayFormat, EnforcementMode, FuelMetrics, FuelUpdate, MemoryLimits,
44    MeteringConfig, MeteringMonitor, RuntimeMetering, SamplingStrategy,
45};
46#[cfg(feature = "wasm-tools")]
47pub use runtime::{WasmContext, WasmRuntime};
48
49use crate::ErrorData;
50
51/// Errors that can occur during WASM tool operations
52#[derive(Debug, thiserror::Error)]
53pub enum WasmError {
54    #[error("Failed to load WASM module: {0}")]
55    LoadError(String),
56
57    #[error("Failed to compile WASM module: {0}")]
58    CompileError(String),
59
60    #[error("Runtime error: {0}")]
61    RuntimeError(String),
62
63    #[error("Credential resolution failed: {0}")]
64    CredentialError(String),
65
66    #[error("Tool execution timeout")]
67    Timeout,
68
69    #[error("Invalid manifest: {0}")]
70    ManifestError(String),
71
72    #[error("Tool not found: {0}")]
73    ToolNotFound(String),
74
75    #[error("Invalid output from WASM tool: {0}")]
76    InvalidOutput(String),
77}
78
79impl From<WasmError> for ErrorData {
80    fn from(err: WasmError) -> Self {
81        match err {
82            WasmError::ToolNotFound(name) => {
83                ErrorData::invalid_request(format!("WASM tool not found: {}", name), None)
84            }
85            WasmError::Timeout => {
86                ErrorData::internal_error("WASM tool execution timeout".to_string(), None)
87            }
88            _ => ErrorData::internal_error(err.to_string(), None),
89        }
90    }
91}