The CodeMode struct is the central engine for executing TypeScript code in a sandboxed Deno runtime. It manages the lifecycle of tools (MCP servers and callbacks) and provides methods to inspect available functions or execute code.
Key Capabilities
Configuration (Builder/Mutable methods):
with_server() / with_servers(): Add MCP servers (Builder pattern).with_callback() / with_callbacks(): Add callback tools (Builder pattern).add_server() / add_servers(): Add MCP servers (Mutable).add_callback() / add_callbacks(): Add callback tools (Mutable).add_tool_set(): Add a pre-built ToolSet directly.
Inspection:
list_functions(): Returns a list of all available functions with minimal interfaces.get_function_details(GetFunctionDetailsInput): Returns full typed TypeScript interfaces for specific functions.tool_sets(), server_tool_sets(), servers(), callbacks(): Accessors for registered configurations.
Execution:
execute_typescript(code, disclosure, registry): Runs TypeScript code in the sandbox.execute_bash(command): Runs a bash command within the virtual filesystem.
use pctx_code_mode::CodeMode;
use pctx_code_mode::model::{CallbackConfig, GetFunctionDetailsInput, FunctionId};
use serde_json::json;
let mut code_mode = CodeMode::default();
// Add callback tools
code_mode.add_callback(&CallbackConfig {
namespace: "DataApi".to_string(),
name: "fetchData".to_string(),
description: Some("Fetches data from API".to_string()),
input_schema: Some(json!({
"type": "object",
"properties": { "id": { "type": "integer" } },
"required": ["id"]
})),
output_schema: None,
})?;
// List available functions
let list = code_mode.list_functions();
for func in list.functions {
println!("{}.{}: {:?}", func.namespace, func.name, func.description);
}
// Get detailed type information
let details = code_mode.get_function_details(GetFunctionDetailsInput {
functions: vec![
FunctionId { mod_name: "DataApi".into(), fn_name: "fetchData".into() }
],
});
println!("TypeScript definitions:\n{}", details.code);