| Crates.io | pctx_code_execution_runtime |
| lib.rs | pctx_code_execution_runtime |
| version | 0.1.1 |
| created_at | 2026-01-12 20:20:59.091234+00 |
| updated_at | 2026-01-21 01:42:33.465397+00 |
| description | JavaScript/TypeScript execution runtime for pctx |
| homepage | |
| repository | https://github.com/portofcontext/pctx |
| max_upload_size | |
| id | 2038776 |
| size | 143,946 |
A Deno extension providing:
The main use case is running PCTX as a TypeScript SDK where users define tools with their own dependencies:
// User has Zod in their environment
import { z } from 'npm:zod';
// Define a tool that uses Zod
registerJsLocalTool({
name: "createUser",
description: "Creates a user with validation"
}, (args) => {
const UserSchema = z.object({
name: z.string().min(2),
email: z.string().email(),
age: z.number().min(0).max(120)
});
return UserSchema.parse(args); // Uses Zod for validation!
});
// Sandboxed code calls the tool (doesn't need Zod directly)
const user = await callJsLocalTool("createUser", {
name: "Alice",
email: "alice@example.com",
age: 30
});
Key benefit: Dependencies (Zod, database clients, APIs, etc.) are available where tools are defined (trusted zone), but sandboxed code just calls the tools without needing dependency access.
See the complete example: typescript_sdk_with_dependencies.rs
use deno_core::{JsRuntime, RuntimeOptions};
use pctx_code_execution_runtime::{
pctx_runtime_snapshot, MCPRegistry, JsCallableToolRegistry,
AllowedHosts, RUNTIME_SNAPSHOT
};
// Create registries
let mcp_registry = MCPRegistry::new();
let js_local_tool_registry = JsCallableToolRegistry::new();
let allowed_hosts = AllowedHosts::new(Some(vec!["example.com".to_string()]));
let mut runtime = JsRuntime::new(RuntimeOptions {
startup_snapshot: Some(RUNTIME_SNAPSHOT),
extensions: vec![pctx_runtime_snapshot::init(
mcp_registry,
js_local_tool_registry,
allowed_hosts
)],
..Default::default()
});
// MCP API and JS Local Tools are now available in JavaScript
let code = r#"
// Register an MCP server
registerMCP({ name: "my-server", url: "http://localhost:3000" });
// Register a local tool with a callback
registerJsLocalTool({
name: "calculator",
description: "Performs arithmetic",
}, (args) => {
return args.a + args.b;
});
// Call MCP tool
const mcpResult = await callMCPTool({
name: "my-server",
tool: "get_data",
arguments: { id: 42 }
});
// Call local tool
const localResult = await callJsLocalTool("calculator", { a: 10, b: 5 });
console.log("MCP:", mcpResult, "Local:", localResult);
"#;
runtime.execute_script("<main>", code)?;
MCPRegistryThread-safe registry for MCP server configurations.
let registry = MCPRegistry::new();
JsCallableToolRegistryThread-safe registry for local tool metadata (callbacks stored in JS).
let callable_registry = JsCallableToolRegistry::new();
// Query tools
if callable_registry.has("my-tool") {
println!("Tool exists!");
}
let metadata = callable_registry.get_metadata("my-tool");
let all_tools = callable_registry.list();
AllowedHostsWhitelist of hosts allowed for network access.
let allowed_hosts = AllowedHosts::new(Some(vec![
"example.com".to_string(),
"api.service.com".to_string(),
]));
RUNTIME_SNAPSHOTPre-compiled V8 snapshot containing the runtime.
pub static RUNTIME_SNAPSHOT: &[u8] = /* ... */;
let code = r#"
console.log("Line 1");
console.log("Line 2");
console.error("Error line");
export default {
stdout: globalThis.__stdout,
stderr: globalThis.__stderr
};
"#;
let result = runtime.execute_script("<capture>", code)?;
// Extract captured output
let scope = &mut runtime.handle_scope();
let local = v8::Local::new(scope, result);
let output = serde_v8::from_v8::<serde_json::Value>(scope, local)?;
println!("Stdout: {:?}", output["stdout"]);
println!("Stderr: {:?}", output["stderr"]);
// Allow only specific hosts
let allowed_hosts = AllowedHosts::new(Some(vec![
"api.example.com".to_string(),
"cdn.example.com".to_string(),
]));
let mut runtime = JsRuntime::new(RuntimeOptions {
startup_snapshot: Some(RUNTIME_SNAPSHOT),
extensions: vec![pctx_runtime_snapshot::init(
MCPRegistry::new(),
allowed_hosts
)],
..Default::default()
});
let code = r#"
// This will succeed
await fetch("http://api.example.com/data");
// This will fail - host not allowed
try {
await fetch("http://malicious.com/data");
} catch (e) {
console.error("Blocked:", e.message);
}
"#;
runtime.execute_script("<permissions>", code)?;
fetch()MIT
Contributions welcome! Please ensure:
cargo test --package pctx_runtimecargo fmtLocal tools allow you to define JavaScript callbacks that can be invoked from sandboxed code. See JS_LOCAL_TOOLS.md for comprehensive documentation.
Quick example:
// Register a tool with a callback
registerJsLocalTool({
name: "file-reader",
description: "Reads a file from the host system",
inputSchema: {
type: "object",
properties: {
path: { type: "string" }
}
}
}, async (args) => {
const fs = require('fs').promises;
return await fs.readFile(args.path, 'utf8');
});
// Call the tool from sandboxed code
const content = await callJsLocalTool("file-reader", { path: "./data.txt" });
Benefits:
Connect to external MCP servers for tool integration.
registerMCP({
name: "github",
url: "http://localhost:3000"
});
const result = await callMCPTool({
name: "github",
tool: "create_issue",
arguments: { title: "Bug report" }
});
See the examples/ directory:
pctx_type_check - TypeScript type checking runtimepctx_executor - Complete TypeScript execution environment