Skip to main content

CodeExecutor

Trait CodeExecutor 

Source
pub trait CodeExecutor: Send + Sync {
    // Required method
    fn execute<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        code: &'life1 str,
        variables: Option<&'life2 Value>,
    ) -> Pin<Box<dyn Future<Output = Result<Value, ExecutionError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
}
Expand description

High-level trait for executing validated code.

Implementations handle the execution of code that has already passed validation and token verification. This is the primary public API for code execution – it replaces the internal HttpExecutor, SdkExecutor, and McpExecutor traits for external server developers.

§Execution Patterns

The four supported patterns (all implemented via this single trait):

  • Pattern A (SQL): Direct SQL execution, no JS runtime
  • Pattern B (JS+HTTP): JavaScript plan compiled and executed via HTTP calls
  • Pattern C (JS+SDK): JavaScript plan executed via AWS SDK calls
  • Pattern D (JS+MCP): JavaScript plan executed via MCP tool calls

§API Stability Note

[Addresses divergent review concern: CodeExecutor trait surface area] This v0.1.0 API uses a simple (code, variables) signature per D-04. A future v0.2.0 may add an ExecutionContext parameter carrying timeout, cancellation token, and request metadata. The (code, variables) signature will be preserved as a default-method wrapper for backward compatibility.

§Example

use pmcp_code_mode::{CodeExecutor, ExecutionError};
use serde_json::Value;

struct MyExecutor { /* database pool, http client, etc. */ }

#[pmcp_code_mode::async_trait]
impl CodeExecutor for MyExecutor {
    async fn execute(
        &self,
        code: &str,
        variables: Option<&Value>,
    ) -> Result<Value, ExecutionError> {
        // Execute validated code against your backend
        todo!()
    }
}

Required Methods§

Source

fn execute<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, code: &'life1 str, variables: Option<&'life2 Value>, ) -> Pin<Box<dyn Future<Output = Result<Value, ExecutionError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Execute validated code and return the result.

code has already passed validation and token verification. variables are optional user-provided parameters (e.g., GraphQL variables).

Implementations should NOT re-verify the token – that is handled by the Code Mode framework before calling this method.

Implementors§