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 Name | Required Type | Purpose |
|---|---|---|
code_mode_config | CodeModeConfig | Validation pipeline configuration |
token_secret | TokenSecret | HMAC signing secret |
policy_evaluator | Arc<dyn PolicyEvaluator> or Arc<P> | Policy evaluation |
code_executor | Arc<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)
| Attribute | Type | Default | Purpose |
|---|---|---|---|
context_from | String | (none) | Method name returning ValidationContext |
language | String | "graphql" | Selects validation path and tool metadata |
Supported language values:
| Value | Validation Method | Feature Required |
|---|---|---|
"graphql" (default) | validate_graphql_query_async | (none) |
"javascript" / "js" | validate_javascript_code | openapi-code-mode |
"sql" | validate_sql_query | sql-code-mode |
"mcp" | validate_mcp_composition | mcp-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:
- A
register_code_mode_toolsmethod on the struct that takes aServerBuilderby value and returns it (by-value fluent pattern). - Two internal handler structs (
ValidateCodeHandlerandExecuteCodeHandler) that implementpmcp::ToolHandler. - A
Send + Synccompile-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) -> ServerBuilderTesting (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) -> ServerBuilderDerive Macros§
- Code
Mode - Derive macro that generates
register_code_mode_toolsfor Code Mode servers.