Skip to main content

Crate pmcp_code_mode_derive

Crate pmcp_code_mode_derive 

Source
Expand description

Derive macro for Code Mode validation and execution in MCP servers.

Provides #[derive(CodeMode)] which generates a register_code_mode_tools method that registers validate_code and execute_code tools on a [pmcp::ServerBuilder].

§Field Name Convention (v0.1.0)

The macro identifies required fields by fixed well-known names. This is the v0.1.0 contract – the field names are the API:

Field NameRequired TypePurpose
code_mode_configCodeModeConfigValidation pipeline configuration
token_secretTokenSecretHMAC signing secret
policy_evaluatorArc<dyn PolicyEvaluator> or Arc<P>Policy evaluation
code_executorArc<dyn CodeExecutor> or Arc<E>Code execution

If any required field is missing, the macro emits a single compile error listing all absent fields.

§Struct-Level Attributes (v0.2.0)

AttributeTypeDefaultPurpose
context_fromString(none)Method name returning ValidationContext
languageString"graphql"Selects validation path and tool metadata

Supported language values:

ValueValidation MethodFeature Required
"graphql" (default)validate_graphql_query_async(none)
"javascript" / "js"validate_javascript_codeopenapi-code-mode
"sql"validate_sql_querysql-code-mode
"mcp"validate_mcp_compositionmcp-code-mode

When context_from is specified, register_code_mode_tools requires self: &Arc<Self> and the generated handler calls self.parent.{method}(&extra) to obtain real ValidationContext bound to the current user/session.

When context_from is omitted, register_code_mode_tools uses &self (no Arc required) with placeholder context values and a #[deprecated] warning guiding users toward the production path.

§Generated Code

The macro generates:

  1. A register_code_mode_tools method on the struct that takes a ServerBuilder by value and returns it (by-value fluent pattern).
  2. Two internal handler structs (ValidateCodeHandler and ExecuteCodeHandler) that implement pmcp::ToolHandler.
  3. A Send + Sync compile-time assertion (per D-08).

§Examples

Production (with context_from):

#[derive(CodeMode)]
#[code_mode(context_from = "get_context", language = "graphql")]
struct MyServer {
    code_mode_config: CodeModeConfig,
    token_secret: TokenSecret,
    policy_evaluator: Arc<NoopPolicyEvaluator>,
    code_executor: Arc<MyExecutor>,
}

impl MyServer {
    fn get_context(&self, extra: &RequestHandlerExtra) -> ValidationContext {
        ValidationContext::new("user-1", "session-1", "schema-v1", "perms-v1")
    }
}

// Generated: MyServer::register_code_mode_tools(self: &Arc<Self>, builder) -> ServerBuilder

Testing (without context_from, deprecated placeholder path):

#[derive(CodeMode)]
struct MyServer {
    code_mode_config: CodeModeConfig,
    token_secret: TokenSecret,
    policy_evaluator: Arc<NoopPolicyEvaluator>,
    code_executor: Arc<MyExecutor>,
}

// Generated: #[deprecated] MyServer::register_code_mode_tools(&self, builder) -> ServerBuilder

Derive Macros§

CodeMode
Derive macro that generates register_code_mode_tools for Code Mode servers.