use std::collections::{BTreeMap, HashMap};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use async_trait::async_trait;
use incurs::command::{McpAnnotations, RequestContext};
use incurs_codemode::{
ArtifactStore, CodeMode, CodeModeRunOptions, CodeModeService, Connector, ConnectorDescription,
IncurConnector, McpClient, McpConnector, McpTool, OpenApiClient, OpenApiConnector,
OpenApiRequest, ReplayPolicy, RuntimeStore, SearchOutput, ToolAnnotations, ToolContext,
ToolOrigin, ToolPolicy, ToolPolicyResolver,
};
use incurs_codemode::{ExecutionState, ExecutionStatus};
use incurs_codemode_local::{LocalCodeModeService, LocalExecutor, LocalExecutorOptions};
use serde_json::Value;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin, ChildStdout, Command};
use tokio::sync::Mutex;
use crate::cli::build_agent_cli_with_store;
use crate::daemon::DaemonClient;
use crate::model::{
CapabilityAnnotations, CapabilityDescriptor, CausalEvent, ConnectorConfig, ConnectorKind,
CoordinationMode, IdentityMode, OperationReceipt, OperationTarget, ReceiptState, SecretRef,
VerificationMode, now_ms,
};
use crate::store::Store;
const WORKSPACE_CWD_HEADER: &str = "x-apoc-workspace-cwd";
#[derive(Debug)]
struct UnrestrictedPolicy;
impl ToolPolicyResolver for UnrestrictedPolicy {
fn resolve(&self, _origin: ToolOrigin, annotations: &ToolAnnotations) -> ToolPolicy {
ToolPolicy {
requires_approval: false,
replay: if annotations.read_only == Some(true)
&& annotations.destructive != Some(true)
&& annotations.open_world != Some(true)
{
ReplayPolicy::Reexecute
} else {
ReplayPolicy::Log
},
}
}
}
struct UnrestrictedConnector {
inner: Arc<dyn Connector>,
}
#[async_trait]
impl Connector for UnrestrictedConnector {
async fn describe(&self) -> Result<ConnectorDescription, String> {
let inner = Arc::clone(&self.inner);
let mut description = external(async move { inner.describe().await }).await?;
for tool in &mut description.tools {
tool.policy = ToolPolicy {
requires_approval: false,
replay: if tool.annotations.read_only == Some(true)
&& tool.annotations.destructive != Some(true)
&& tool.annotations.open_world != Some(true)
{
ReplayPolicy::Reexecute
} else {
ReplayPolicy::Log
},
};
}
Ok(description)
}
async fn execute(
&self,
method: &str,
arguments: Value,
context: &ToolContext,
) -> Result<Value, String> {
let inner = Arc::clone(&self.inner);
let method = method.to_string();
let context = context.clone();
let execution_id = context.execution_id.clone();
let cwd = context
.request
.as_ref()
.and_then(|request| request.headers.get(WORKSPACE_CWD_HEADER))
.cloned();
external(async move {
crate::process::with_execution_context(
execution_id,
cwd,
inner.execute(&method, arguments, &context),
)
.await
})
.await
}
async fn revert(
&self,
method: &str,
arguments: Value,
result: Value,
context: &ToolContext,
) -> Result<bool, String> {
let inner = Arc::clone(&self.inner);
let method = method.to_string();
let context = context.clone();
external(async move { inner.revert(&method, arguments, result, &context).await }).await
}
}
struct CoordinatedConnector {
name: String,
inner: Arc<dyn Connector>,
store: Arc<Store>,
description: Mutex<Option<ConnectorDescription>>,
}
#[async_trait]
impl Connector for CoordinatedConnector {
async fn describe(&self) -> Result<ConnectorDescription, String> {
let description = self.inner.describe().await?;
*self.description.lock().await = Some(description.clone());
Ok(description)
}
async fn execute(
&self,
method: &str,
arguments: Value,
context: &ToolContext,
) -> Result<Value, String> {
let description = match self.description.lock().await.clone() {
Some(description) => description,
None => self.inner.describe().await?,
};
let read_only = description
.tools
.iter()
.find(|tool| tool.name == method)
.is_some_and(|tool| {
tool.annotations.read_only == Some(true)
&& tool.annotations.destructive != Some(true)
});
if read_only {
return self.inner.execute(method, arguments, context).await;
}
let target = provider_target(&self.name, &arguments);
let lock = provider_lock(&target).await;
let _guard = lock.lock().await;
let id = uuid::Uuid::now_v7().to_string();
let operation = format!("{}.{}", self.name, method);
let key = format!("{}/{id}", context.execution_id);
let at = now_ms();
let mut receipt = OperationReceipt {
id: id.clone(),
idempotency_key: key.clone(),
operation: operation.clone(),
actor: "code".to_string(),
execution_id: Some(context.execution_id.clone()),
state: ReceiptState::Unknown,
targets: vec![OperationTarget {
key: target,
applied: false,
error_code: None,
error: None,
}],
started_at: at,
completed_at: None,
};
self.store.put_operation_receipt(&receipt)?;
self.store.append_causal_event(CausalEvent {
sequence: 0,
id: uuid::Uuid::now_v7().to_string(),
at,
kind: "provider.action.intent".to_string(),
subject: operation.clone(),
actor: "code".to_string(),
correlation_id: id.clone(),
causation_id: None,
execution_id: Some(context.execution_id.clone()),
session_id: None,
payload_id: None,
details: serde_json::json!({
"receipt": id,
"argument_keys": arguments
.as_object()
.map(|values| values.keys().cloned().collect::<Vec<_>>())
.unwrap_or_default()
}),
})?;
let result = self.inner.execute(method, arguments, context).await;
receipt.completed_at = Some(now_ms());
match &result {
Ok(_) => {
receipt.state = ReceiptState::Applied;
receipt.targets[0].applied = true;
}
Err(error) => {
receipt.state = ReceiptState::Failed;
receipt.targets[0].error_code = Some("PROVIDER_ACTION_FAILED".to_string());
receipt.targets[0].error = Some(error.clone());
}
}
self.store.put_operation_receipt(&receipt)?;
self.store.append_causal_event(CausalEvent {
sequence: 0,
id: uuid::Uuid::now_v7().to_string(),
at: receipt.completed_at.expect("receipt was completed"),
kind: "provider.action.result".to_string(),
subject: operation,
actor: "code".to_string(),
correlation_id: id,
causation_id: None,
execution_id: Some(context.execution_id.clone()),
session_id: None,
payload_id: None,
details: serde_json::json!({"idempotency_key": key, "state": receipt.state}),
})?;
result
}
async fn revert(
&self,
method: &str,
arguments: Value,
result: Value,
context: &ToolContext,
) -> Result<bool, String> {
self.inner.revert(method, arguments, result, context).await
}
}
async fn provider_lock(name: &str) -> Arc<Mutex<()>> {
static LOCKS: OnceLock<Mutex<HashMap<String, Arc<Mutex<()>>>>> = OnceLock::new();
LOCKS
.get_or_init(|| Mutex::new(HashMap::new()))
.lock()
.await
.entry(name.to_string())
.or_insert_with(|| Arc::new(Mutex::new(())))
.clone()
}
fn provider_target(name: &str, arguments: &Value) -> String {
for key in ["id", "resource_id", "pid", "path", "window_id"] {
let Some(value) = arguments.get(key) else {
continue;
};
if value.is_string() || value.is_number() {
return format!(
"provider/{name}/{key}/{}",
value
.as_str()
.map_or_else(|| value.to_string(), ToString::to_string)
);
}
}
format!("provider/{name}")
}
struct ExecutionLeaseConnector {
inner: Arc<dyn Connector>,
runs: Mutex<HashMap<String, Vec<String>>>,
}
#[async_trait]
impl Connector for ExecutionLeaseConnector {
async fn describe(&self) -> Result<ConnectorDescription, String> {
self.inner.describe().await
}
async fn execute(
&self,
method: &str,
arguments: Value,
context: &ToolContext,
) -> Result<Value, String> {
let execution_lifetime = arguments
.get("owner")
.and_then(Value::as_str)
.unwrap_or("persistent")
== "execution";
let result = crate::process::with_execution_id(
context.execution_id.clone(),
self.inner.execute(method, arguments, context),
)
.await?;
if method == "execution_start"
&& execution_lifetime
&& let Some(id) = result.get("id").and_then(Value::as_str)
{
self.runs
.lock()
.await
.entry(context.execution_id.clone())
.or_default()
.push(id.to_string());
}
Ok(result)
}
async fn revert(
&self,
method: &str,
arguments: Value,
result: Value,
context: &ToolContext,
) -> Result<bool, String> {
self.inner.revert(method, arguments, result, context).await
}
async fn pass_ended(&self, execution_id: &str, status: &str) {
self.inner.pass_ended(execution_id, status).await;
}
async fn execution_ended(&self, execution_id: &str, status: &str) {
let runs = self
.runs
.lock()
.await
.remove(execution_id)
.unwrap_or_default();
for id in runs {
let _ = crate::process::with_execution_id(
execution_id.to_string(),
DaemonClient.call::<Value>(
"execution.cancel",
serde_json::json!({"id": id, "timeout_ms": 5000}),
),
)
.await;
}
self.inner.execution_ended(execution_id, status).await;
}
}
#[derive(Clone)]
pub struct CodeService {
service: LocalCodeModeService,
}
impl CodeService {
pub fn new(store: Arc<Store>) -> Result<Self, String> {
let service = LocalCodeModeService::spawn(move || {
let catalog = build_agent_cli_with_store(Some(store.clone())).tool_catalog();
let connector = IncurConnector::new(catalog)
.with_name("apoc")
.with_instructions(
"Unrestricted host process control. Calls never pause for approval. \
Use Promise.all for independent operations and preserve returned execution, \
selection, process, and receipt identifiers. Synchronous execution tools return \
{ execution, replayed, logs }; inspect execution.status for the outcome.",
)
.with_policy_resolver(Arc::new(UnrestrictedPolicy));
let local: Arc<dyn Connector> = Arc::new(UnrestrictedConnector {
inner: Arc::new(connector),
});
let local: Arc<dyn Connector> = Arc::new(ExecutionLeaseConnector {
inner: local,
runs: Mutex::new(HashMap::new()),
});
let runtime: Arc<dyn RuntimeStore> = store.clone();
let mut connectors: Vec<Arc<dyn Connector>> = vec![local];
for config in store
.list_providers()
.unwrap_or_default()
.into_iter()
.filter_map(|value| serde_json::from_value::<ConnectorConfig>(value).ok())
.filter(|config| config.enabled)
{
if let Ok(connector) = external_connector(config, store.clone()) {
connectors.push(connector);
}
}
let artifacts: Arc<dyn ArtifactStore> = store;
CodeMode::with_artifact_store(
runtime,
artifacts,
LocalExecutor::new(LocalExecutorOptions {
timeout: Duration::from_secs(300),
memory_limit: 256 * 1024 * 1024,
stack_size: 2 * 1024 * 1024,
}),
connectors,
)
})?;
Ok(Self { service })
}
pub async fn start(&self, code: String, cwd: Option<String>) -> Result<ExecutionState, String> {
let request = cwd.map(|cwd| {
let mut headers = HashMap::new();
headers.insert(WORKSPACE_CWD_HEADER.to_string(), cwd);
RequestContext {
headers,
method: "code".to_string(),
path: "run".to_string(),
}
});
self.service
.execute(
code,
CodeModeRunOptions {
request,
..CodeModeRunOptions::default()
},
)
.await
}
pub async fn run(
&self,
code: String,
cwd: Option<String>,
timeout: Option<Duration>,
) -> Result<ExecutionState, String> {
let state = self.start(code, cwd).await?;
self.wait(&state.id, timeout).await
}
pub async fn get(&self, id: String) -> Result<ExecutionState, String> {
self.service.execution(id).await
}
pub async fn wait(
&self,
id: &str,
timeout: Option<Duration>,
) -> Result<ExecutionState, String> {
let started = tokio::time::Instant::now();
loop {
let state = self.get(id.to_string()).await?;
if terminal(state.status) {
return Ok(state);
}
if timeout.is_some_and(|timeout| started.elapsed() >= timeout) {
return Ok(state);
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
}
pub async fn cancel(&self, id: String) -> Result<ExecutionState, String> {
self.service.cancel(id).await
}
pub async fn search(&self, query: String) -> Result<SearchOutput, String> {
self.service.search(query).await
}
pub async fn artifact(&self, execution: String, artifact: String) -> Result<Value, String> {
self.service.artifact(execution, artifact).await
}
}
pub struct CodePool {
services: Vec<CodeService>,
next: AtomicUsize,
owners: Mutex<HashMap<String, usize>>,
}
impl CodePool {
pub fn new(store: Arc<Store>) -> Result<Self, String> {
let available = std::thread::available_parallelism()
.map(usize::from)
.unwrap_or(1);
let workers = std::env::var("APOC_CODE_WORKERS")
.ok()
.and_then(|value| value.parse::<usize>().ok())
.filter(|value| *value > 0)
.unwrap_or_else(|| available.min(8));
let services = (0..workers)
.map(|_| CodeService::new(store.clone()))
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
services,
next: AtomicUsize::new(0),
owners: Mutex::new(HashMap::new()),
})
}
pub fn workers(&self) -> usize {
self.services.len()
}
pub async fn start(&self, code: String, cwd: Option<String>) -> Result<ExecutionState, String> {
let index = self.next.fetch_add(1, Ordering::Relaxed) % self.services.len();
let state = self.services[index].start(code, cwd).await?;
if !terminal(state.status) {
self.owners.lock().await.insert(state.id.clone(), index);
}
Ok(state)
}
pub async fn run(
&self,
code: String,
cwd: Option<String>,
timeout: Option<Duration>,
) -> Result<ExecutionState, String> {
let state = self.start(code, cwd).await?;
self.wait(&state.id, timeout).await
}
pub async fn get(&self, id: String) -> Result<ExecutionState, String> {
self.services[0].get(id).await
}
pub async fn wait(
&self,
id: &str,
timeout: Option<Duration>,
) -> Result<ExecutionState, String> {
let index = self.owners.lock().await.get(id).copied().unwrap_or(0);
let state = self.services[index].wait(id, timeout).await?;
if terminal(state.status) {
self.owners.lock().await.remove(id);
}
Ok(state)
}
pub async fn cancel(&self, id: String) -> Result<ExecutionState, String> {
let index = self.owners.lock().await.get(&id).copied().unwrap_or(0);
let state = self.services[index].cancel(id.clone()).await?;
if terminal(state.status) {
self.owners.lock().await.remove(&id);
}
Ok(state)
}
pub async fn search(&self, query: String) -> Result<SearchOutput, String> {
self.services[0].search(query).await
}
pub async fn artifact(&self, execution: String, artifact: String) -> Result<Value, String> {
self.services[0].artifact(execution, artifact).await
}
}
struct HttpMcpClient {
client: reqwest::Client,
url: String,
headers: BTreeMap<String, String>,
session: Mutex<Option<String>>,
sequence: AtomicU64,
}
impl HttpMcpClient {
fn new(config: &ConnectorConfig) -> Result<Self, String> {
Ok(Self {
client: reqwest::Client::new(),
url: config
.url
.clone()
.ok_or_else(|| "MCP HTTP provider requires url".to_string())?,
headers: resolved_values(&config.headers, &config.secret_headers)?,
session: Mutex::new(None),
sequence: AtomicU64::new(1),
})
}
async fn initialize(&self) -> Result<(), String> {
let mut active = self.session.lock().await;
if active.is_some() {
return Ok(());
}
let (_, session) = self
.post(
serde_json::json!({
"jsonrpc": "2.0",
"id": self.sequence.fetch_add(1, Ordering::Relaxed),
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": {"name": "apoc", "version": env!("CARGO_PKG_VERSION")}
}
}),
None,
)
.await?;
let _ = self
.post(
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/initialized"
}),
session.as_deref(),
)
.await?;
*active = session;
drop(active);
Ok(())
}
async fn request(&self, method: &str, params: Value) -> Result<Value, String> {
self.initialize().await?;
let session = self.session.lock().await.clone();
let (value, _) = self
.post(
serde_json::json!({
"jsonrpc": "2.0",
"id": self.sequence.fetch_add(1, Ordering::Relaxed),
"method": method,
"params": params
}),
session.as_deref(),
)
.await?;
if let Some(error) = value.get("error") {
return Err(error.to_string());
}
value
.get("result")
.cloned()
.ok_or_else(|| "MCP response has no result".to_string())
}
async fn post(
&self,
body: Value,
session: Option<&str>,
) -> Result<(Value, Option<String>), String> {
let mut request = self
.client
.post(&self.url)
.header("accept", "application/json, text/event-stream")
.header("content-type", "application/json");
for (name, value) in &self.headers {
request = request.header(name, value);
}
if let Some(session) = session {
request = request.header("mcp-session-id", session);
}
let response = request
.json(&body)
.send()
.await
.map_err(|error| error.to_string())?;
let status = response.status();
let session = response
.headers()
.get("mcp-session-id")
.and_then(|value| value.to_str().ok())
.map(ToString::to_string);
let content_type = response
.headers()
.get("content-type")
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_string();
let body = response.text().await.map_err(|error| error.to_string())?;
if !status.is_success() {
return Err(format!("MCP HTTP {status}: {body}"));
}
let value = if content_type.contains("text/event-stream") {
body.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(str::trim)
.find(|line| !line.is_empty())
.ok_or_else(|| "MCP event stream contained no data".to_string())
.and_then(|line| serde_json::from_str(line).map_err(|error| error.to_string()))?
} else if body.trim().is_empty() {
Value::Null
} else {
serde_json::from_str(&body).map_err(|error| error.to_string())?
};
Ok((value, session))
}
}
#[async_trait]
impl McpClient for HttpMcpClient {
async fn list_tools(&self) -> Result<Vec<McpTool>, String> {
let result = self.request("tools/list", serde_json::json!({})).await?;
result
.get("tools")
.and_then(Value::as_array)
.ok_or_else(|| "MCP tools/list returned no tools array".to_string())?
.iter()
.map(|tool| {
Ok(McpTool {
name: required_string(tool, "name")?,
description: optional_string(tool, "description"),
input_schema: tool
.get("inputSchema")
.cloned()
.unwrap_or_else(|| serde_json::json!({"type": "object"})),
output_schema: tool.get("outputSchema").cloned(),
annotations: mcp_annotations(tool),
})
})
.collect()
}
async fn call_tool(&self, name: &str, arguments: Value) -> Result<Value, String> {
let result = self
.request(
"tools/call",
serde_json::json!({"name": name, "arguments": arguments}),
)
.await?;
if result.get("isError").and_then(Value::as_bool) == Some(true) {
return Err(result.to_string());
}
if let Some(value) = result.get("structuredContent") {
return Ok(value.clone());
}
Ok(result.get("content").cloned().unwrap_or(result))
}
}
struct StdioMcpState {
_child: Child,
input: ChildStdin,
output: BufReader<ChildStdout>,
initialized: bool,
}
struct StdioMcpClient {
command: String,
args: Vec<String>,
cwd: Option<String>,
env: BTreeMap<String, String>,
state: Mutex<Option<StdioMcpState>>,
sequence: AtomicU64,
}
impl StdioMcpClient {
fn new(config: &ConnectorConfig) -> Result<Self, String> {
Ok(Self {
command: config
.command
.clone()
.ok_or_else(|| "MCP stdio provider requires command".to_string())?,
args: config.args.clone(),
cwd: config.cwd.clone(),
env: resolved_values(&config.env, &config.secret_env)?,
state: Mutex::new(None),
sequence: AtomicU64::new(1),
})
}
fn spawn(&self) -> Result<StdioMcpState, String> {
let mut command = Command::new(&self.command);
command
.args(&self.args)
.envs(&self.env)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::inherit())
.kill_on_drop(true);
if let Some(cwd) = &self.cwd {
command.current_dir(cwd);
}
let mut child = command.spawn().map_err(|error| {
format!(
"Could not start MCP stdio provider {}: {error}",
self.command
)
})?;
let input = child
.stdin
.take()
.ok_or_else(|| "MCP stdio provider has no stdin".to_string())?;
let output = child
.stdout
.take()
.ok_or_else(|| "MCP stdio provider has no stdout".to_string())?;
Ok(StdioMcpState {
_child: child,
input,
output: BufReader::new(output),
initialized: false,
})
}
async fn request(&self, method: &str, params: Value) -> Result<Value, String> {
let mut active = self.state.lock().await;
if active.is_none() {
*active = Some(self.spawn()?);
}
let state = active.as_mut().expect("stdio state was initialized");
if !state.initialized {
let id = self.sequence.fetch_add(1, Ordering::Relaxed);
Self::send(
state,
&serde_json::json!({
"jsonrpc": "2.0",
"id": id,
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": {"name": "apoc", "version": env!("CARGO_PKG_VERSION")}
}
}),
)
.await?;
Self::response(state, id).await?;
Self::send(
state,
&serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/initialized"
}),
)
.await?;
state.initialized = true;
}
let id = self.sequence.fetch_add(1, Ordering::Relaxed);
Self::send(
state,
&serde_json::json!({
"jsonrpc": "2.0",
"id": id,
"method": method,
"params": params
}),
)
.await?;
Self::response(state, id).await
}
async fn send(state: &mut StdioMcpState, value: &Value) -> Result<(), String> {
let mut message = serde_json::to_vec(value).map_err(|error| error.to_string())?;
message.push(b'\n');
state
.input
.write_all(&message)
.await
.map_err(|error| error.to_string())?;
state.input.flush().await.map_err(|error| error.to_string())
}
async fn response(state: &mut StdioMcpState, id: u64) -> Result<Value, String> {
loop {
let mut line = String::new();
let bytes = state
.output
.read_line(&mut line)
.await
.map_err(|error| error.to_string())?;
if bytes == 0 {
return Err("MCP stdio provider exited before responding".to_string());
}
let value = serde_json::from_str::<Value>(&line)
.map_err(|error| format!("Invalid MCP stdio response: {error}"))?;
if value.get("id").and_then(Value::as_u64) != Some(id) {
continue;
}
if let Some(error) = value.get("error") {
return Err(error.to_string());
}
return value
.get("result")
.cloned()
.ok_or_else(|| "MCP response has no result".to_string());
}
}
}
#[async_trait]
impl McpClient for StdioMcpClient {
async fn list_tools(&self) -> Result<Vec<McpTool>, String> {
let result = self.request("tools/list", serde_json::json!({})).await?;
result
.get("tools")
.and_then(Value::as_array)
.ok_or_else(|| "MCP tools/list returned no tools array".to_string())?
.iter()
.map(|tool| {
Ok(McpTool {
name: required_string(tool, "name")?,
description: optional_string(tool, "description"),
input_schema: tool
.get("inputSchema")
.cloned()
.unwrap_or_else(|| serde_json::json!({"type": "object"})),
output_schema: tool.get("outputSchema").cloned(),
annotations: mcp_annotations(tool),
})
})
.collect()
}
async fn call_tool(&self, name: &str, arguments: Value) -> Result<Value, String> {
let result = self
.request(
"tools/call",
serde_json::json!({"name": name, "arguments": arguments}),
)
.await?;
if result.get("isError").and_then(Value::as_bool) == Some(true) {
return Err(result.to_string());
}
if let Some(value) = result.get("structuredContent") {
return Ok(value.clone());
}
Ok(result.get("content").cloned().unwrap_or(result))
}
}
struct HttpOpenApiClient {
client: reqwest::Client,
spec_url: String,
base_url: String,
headers: BTreeMap<String, String>,
}
impl HttpOpenApiClient {
fn new(config: &ConnectorConfig) -> Result<Self, String> {
Ok(Self {
client: reqwest::Client::new(),
spec_url: config
.url
.clone()
.ok_or_else(|| "OpenAPI provider requires url".to_string())?,
base_url: config
.base_url
.clone()
.ok_or_else(|| "OpenAPI connector requires base_url".to_string())?,
headers: resolved_values(&config.headers, &config.secret_headers)?,
})
}
fn headers(&self, mut request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
for (name, value) in &self.headers {
request = request.header(name, value);
}
request
}
async fn response(&self, response: reqwest::Response) -> Result<Value, String> {
let status = response.status();
let body = response.text().await.map_err(|error| error.to_string())?;
if !status.is_success() {
return Err(format!("OpenAPI HTTP {status}: {body}"));
}
serde_json::from_str(&body).or_else(|_| Ok(Value::String(body)))
}
}
#[async_trait]
impl OpenApiClient for HttpOpenApiClient {
async fn specification(&self) -> Result<Value, String> {
let response = self
.headers(self.client.get(&self.spec_url))
.send()
.await
.map_err(|error| error.to_string())?;
let value = self.response(response).await?;
if let Value::String(document) = value {
let yaml = serde_yaml::from_str::<serde_yaml::Value>(&document)
.map_err(|error| error.to_string())?;
serde_json::to_value(yaml).map_err(|error| error.to_string())
} else {
Ok(value)
}
}
async fn request(&self, request: OpenApiRequest) -> Result<Value, String> {
let url = if request.path.starts_with("http://") || request.path.starts_with("https://") {
request.path
} else {
format!(
"{}/{}",
self.base_url.trim_end_matches('/'),
request.path.trim_start_matches('/')
)
};
let method = reqwest::Method::from_bytes(request.method.to_ascii_uppercase().as_bytes())
.map_err(|error| error.to_string())?;
let mut builder = self.headers(self.client.request(method, url));
for (name, value) in request.headers {
builder = builder.header(name, value);
}
let query = request
.parameters
.into_iter()
.map(|(name, value)| {
let value = value
.as_str()
.map(ToString::to_string)
.unwrap_or_else(|| value.to_string());
(name, value)
})
.collect::<Vec<_>>();
builder = builder.query(&query);
if let Some(body) = request.body {
builder = builder.json(&body);
}
self.response(builder.send().await.map_err(|error| error.to_string())?)
.await
}
}
pub(crate) fn external_connector(
config: ConnectorConfig,
store: Arc<Store>,
) -> Result<Arc<dyn Connector>, String> {
let instructions = config.instructions.clone();
let inner: Arc<dyn Connector> = match config.kind {
ConnectorKind::McpHttp => {
let mut connector =
McpConnector::new(&config.name, Arc::new(HttpMcpClient::new(&config)?))
.with_policy_resolver(Arc::new(UnrestrictedPolicy));
if let Some(instructions) = instructions {
connector = connector.with_instructions(instructions);
}
Arc::new(connector)
}
ConnectorKind::OpenApi => {
let mut connector =
OpenApiConnector::new(&config.name, Arc::new(HttpOpenApiClient::new(&config)?))
.with_policy_resolver(Arc::new(UnrestrictedPolicy));
if let Some(instructions) = instructions {
connector = connector.with_instructions(instructions);
}
Arc::new(connector)
}
ConnectorKind::Builtin => {
return Err("Builtin providers are constructed by apoc".to_string());
}
ConnectorKind::McpStdio => {
let mut connector =
McpConnector::new(&config.name, Arc::new(StdioMcpClient::new(&config)?))
.with_policy_resolver(Arc::new(UnrestrictedPolicy));
if let Some(instructions) = instructions {
connector = connector.with_instructions(instructions);
}
Arc::new(connector)
}
};
let inner: Arc<dyn Connector> = Arc::new(CoordinatedConnector {
name: config.name,
inner,
store,
description: Mutex::new(None),
});
Ok(Arc::new(UnrestrictedConnector { inner }))
}
pub(crate) fn capability_descriptors(
config: &ConnectorConfig,
description: &ConnectorDescription,
) -> Vec<CapabilityDescriptor> {
description
.tools
.iter()
.map(|tool| {
let properties = tool
.input_schema
.get("properties")
.and_then(Value::as_object);
let stable = properties.is_some_and(|properties| {
["id", "resource_id", "pid", "path", "window_id"]
.iter()
.any(|name| properties.contains_key(*name))
});
let exact = stable
&& properties.is_some_and(|properties| {
["version", "generation", "started_at_ns"]
.iter()
.any(|name| properties.contains_key(*name))
});
let read_only = tool.annotations.read_only == Some(true)
&& tool.annotations.destructive != Some(true);
CapabilityDescriptor {
provider: config.name.clone(),
method: tool.name.clone(),
description: tool.description.clone(),
input_schema: tool.input_schema.clone(),
output_schema: tool.output_schema.clone(),
annotations: CapabilityAnnotations {
read_only: tool.annotations.read_only,
destructive: tool.annotations.destructive,
idempotent: tool.annotations.idempotent,
open_world: tool.annotations.open_world,
},
identity_mode: if exact {
IdentityMode::Exact
} else if stable {
IdentityMode::Stable
} else {
IdentityMode::Opaque
},
coordination_mode: if read_only {
CoordinationMode::None
} else if stable {
CoordinationMode::Resource
} else {
CoordinationMode::Provider
},
verification_mode: VerificationMode::Unavailable,
generation: config.generation.clone(),
}
})
.collect()
}
async fn external<T: Send + 'static>(
future: impl Future<Output = Result<T, String>> + Send + 'static,
) -> Result<T, String> {
static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
let runtime = RUNTIME.get_or_init(|| {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(4)
.thread_name("apoc-tool-host")
.enable_all()
.build()
.expect("apoc external connector runtime")
});
runtime
.spawn(future)
.await
.map_err(|error| error.to_string())?
}
fn required_string(value: &Value, key: &str) -> Result<String, String> {
value
.get(key)
.and_then(Value::as_str)
.map(ToString::to_string)
.ok_or_else(|| format!("Missing string field \"{key}\""))
}
fn optional_string(value: &Value, key: &str) -> Option<String> {
value
.get(key)
.and_then(Value::as_str)
.map(ToString::to_string)
}
fn mcp_annotations(tool: &Value) -> Option<McpAnnotations> {
let annotations = tool.get("annotations")?;
Some(McpAnnotations {
title: optional_string(annotations, "title"),
read_only_hint: annotation_bool(annotations, "readOnlyHint", "read_only_hint"),
destructive_hint: annotation_bool(annotations, "destructiveHint", "destructive_hint"),
idempotent_hint: annotation_bool(annotations, "idempotentHint", "idempotent_hint"),
open_world_hint: annotation_bool(annotations, "openWorldHint", "open_world_hint"),
})
}
fn annotation_bool(value: &Value, standard: &str, legacy: &str) -> Option<bool> {
value
.get(standard)
.or_else(|| value.get(legacy))
.and_then(Value::as_bool)
}
fn resolved_values(
literal: &BTreeMap<String, String>,
secrets: &BTreeMap<String, SecretRef>,
) -> Result<BTreeMap<String, String>, String> {
let mut values = literal.clone();
for (name, secret) in secrets {
if secret.provider != "env" {
return Err(format!(
"Unsupported secret provider \"{}\" for {name}",
secret.provider
));
}
let value = std::env::var(&secret.reference).map_err(|_| {
format!(
"Secret environment variable \"{}\" is unset",
secret.reference
)
})?;
values.insert(name.clone(), value);
}
Ok(values)
}
pub fn execution_summary(state: &ExecutionState) -> Value {
serde_json::json!({
"id": state.id,
"status": state.status,
"result": state.result,
"error": state.error,
"logs": state.logs,
"calls": state.log.len(),
"created_at": state.created_at,
"updated_at": state.updated_at
})
}
fn terminal(status: ExecutionStatus) -> bool {
matches!(
status,
ExecutionStatus::Completed
| ExecutionStatus::Error
| ExecutionStatus::Rejected
| ExecutionStatus::RolledBack
| ExecutionStatus::Cancelled
)
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use axum::Json;
use axum::Router;
use axum::http::{HeaderValue, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use tempfile::tempdir;
use super::*;
async fn openapi_spec() -> Json<Value> {
Json(serde_json::json!({
"openapi": "3.1.0",
"info": {"title": "fixture", "version": "1"},
"paths": {
"/hello": {
"get": {
"operationId": "hello",
"description": "Return a greeting.",
"responses": {"200": {"description": "ok"}}
}
}
}
}))
}
async fn mcp_fixture(Json(body): Json<Value>) -> Response {
let method = body.get("method").and_then(Value::as_str).unwrap();
if method == "notifications/initialized" {
return StatusCode::ACCEPTED.into_response();
}
let id = body.get("id").cloned().unwrap();
let result = match method {
"initialize" => serde_json::json!({
"protocolVersion": "2025-06-18",
"capabilities": {"tools": {}},
"serverInfo": {"name": "fixture", "version": "1"}
}),
"tools/list" => serde_json::json!({
"tools": [{
"name": "echo",
"description": "Echo one value.",
"inputSchema": {
"type": "object",
"properties": {"value": {}},
"required": ["value"]
}
}]
}),
"tools/call" => serde_json::json!({
"structuredContent": {
"echoed": body["params"]["arguments"]["value"]
}
}),
_ => panic!("unexpected MCP method: {method}"),
};
let mut response =
Json(serde_json::json!({"jsonrpc": "2.0", "id": id, "result": result})).into_response();
if method == "initialize" {
response
.headers_mut()
.insert("mcp-session-id", HeaderValue::from_static("fixture"));
}
response
}
#[derive(Default)]
struct CoordinationFixture {
active: AtomicUsize,
maximum: AtomicUsize,
}
struct CoordinationTestConnector {
state: Arc<CoordinationFixture>,
}
#[async_trait]
impl Connector for CoordinationTestConnector {
async fn describe(&self) -> Result<ConnectorDescription, String> {
Ok(ConnectorDescription {
name: "resources".to_string(),
instructions: None,
tools: vec![incurs_codemode::ConnectorTool {
name: "touch".to_string(),
description: Some("Mutate one stable resource.".to_string()),
input_schema: serde_json::json!({
"type": "object",
"properties": {"id": {"type": "string"}},
"required": ["id"]
}),
output_schema: None,
instructions: None,
examples: Vec::new(),
annotations: ToolAnnotations {
read_only: Some(false),
destructive: Some(true),
idempotent: Some(false),
open_world: Some(true),
},
policy: ToolPolicy {
requires_approval: false,
replay: ReplayPolicy::Log,
},
}],
})
}
async fn execute(
&self,
_method: &str,
_arguments: Value,
_context: &ToolContext,
) -> Result<Value, String> {
let active = self.state.active.fetch_add(1, Ordering::SeqCst) + 1;
self.state.maximum.fetch_max(active, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(25)).await;
self.state.active.fetch_sub(1, Ordering::SeqCst);
Ok(serde_json::json!({"applied": true}))
}
}
#[tokio::test]
async fn executes_multiple_process_queries_concurrently() {
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let service = CodeService::new(store.clone()).unwrap();
let execution = service
.run(
format!(
"const target = 'pid:{}';\
const queries = Array.from({{ length: 32 }}, () => \
apoc.process_get({{ target }}));\
const [a, processes, b] = await Promise.all([\
apoc.process_select({{ target: 'pid:{}' }}),\
Promise.all(queries),\
apoc.process_ports({{ port: 9 }})\
]);\
return {{ selection: a.id, processCount: processes.length, \
ports: b.length }};",
std::process::id(),
std::process::id()
),
None,
Some(Duration::from_secs(5)),
)
.await
.unwrap();
assert_eq!(
execution.status,
ExecutionStatus::Completed,
"{execution:#?}"
);
assert_eq!(execution.result.as_ref().unwrap()["processCount"], 32);
assert!(store.get_execution(&execution.id).await.unwrap().is_some());
let events = store.list_process_events(100).unwrap();
assert!(events.iter().any(|event| {
event.operation == "process.select"
&& event.controller.execution_id.as_deref() == Some(&execution.id)
}));
assert_eq!(
events
.iter()
.filter(|event| {
event.operation == "process.get"
&& event.controller.execution_id.as_deref() == Some(&execution.id)
})
.count(),
32
);
}
#[tokio::test]
async fn runs_many_javascript_requests_across_the_bounded_actor_pool() {
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let pool = Arc::new(CodePool::new(store).unwrap());
assert!((1..=8).contains(&pool.workers()));
let starts = (0..16)
.map(|index| {
let pool = pool.clone();
tokio::spawn(async move { pool.start(format!("return {index};"), None).await })
})
.collect::<Vec<_>>();
let states = futures::future::join_all(starts).await;
let waits = states
.into_iter()
.map(|state| {
let id = state.unwrap().unwrap().id;
let pool = pool.clone();
tokio::spawn(async move { pool.wait(&id, Some(Duration::from_secs(5))).await })
})
.collect::<Vec<_>>();
let states = futures::future::join_all(waits).await;
assert!(
states
.into_iter()
.all(|state| { state.unwrap().unwrap().status == ExecutionStatus::Completed })
);
}
#[tokio::test]
async fn composes_a_registered_openapi_operation() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let app = Router::new().route("/spec", get(openapi_spec)).route(
"/hello",
get(|| async { Json(serde_json::json!({"message": "apocalypse"})) }),
);
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let now = crate::model::now_ms();
let config = ConnectorConfig {
name: "demo".to_string(),
kind: ConnectorKind::OpenApi,
url: Some(format!("http://{address}/spec")),
base_url: Some(format!("http://{address}")),
command: None,
args: Vec::new(),
cwd: None,
env: BTreeMap::new(),
secret_env: BTreeMap::new(),
headers: BTreeMap::new(),
secret_headers: BTreeMap::new(),
instructions: None,
enabled: true,
generation: String::new(),
created_at: now,
updated_at: now,
};
store
.put_connector("demo", &serde_json::to_value(config).unwrap())
.unwrap();
let service = CodeService::new(store.clone()).unwrap();
let execution = service
.run(
"return await demo.hello({});".to_string(),
None,
Some(Duration::from_secs(5)),
)
.await
.unwrap();
assert_eq!(
execution.status,
ExecutionStatus::Completed,
"{execution:#?}"
);
assert_eq!(execution.result.as_ref().unwrap()["message"], "apocalypse");
server.abort();
}
#[tokio::test]
async fn composes_a_registered_streamable_http_mcp_tool() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let app = Router::new().route("/mcp", post(mcp_fixture));
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let now = crate::model::now_ms();
let config = ConnectorConfig {
name: "remote".to_string(),
kind: ConnectorKind::McpHttp,
url: Some(format!("http://{address}/mcp")),
base_url: None,
command: None,
args: Vec::new(),
cwd: None,
env: BTreeMap::new(),
secret_env: BTreeMap::new(),
headers: BTreeMap::new(),
secret_headers: BTreeMap::new(),
instructions: None,
enabled: true,
generation: String::new(),
created_at: now,
updated_at: now,
};
store
.put_connector("remote", &serde_json::to_value(config).unwrap())
.unwrap();
let service = CodeService::new(store.clone()).unwrap();
let execution = service
.run(
"return await remote.echo({ value: 42 });".to_string(),
None,
Some(Duration::from_secs(5)),
)
.await
.unwrap();
assert_eq!(execution.status, ExecutionStatus::Completed);
assert_eq!(execution.result.as_ref().unwrap()["echoed"], 42);
let receipts = store.operation_receipts(10).unwrap();
assert_eq!(receipts.len(), 1);
assert_eq!(receipts[0].operation, "remote.echo");
assert_eq!(receipts[0].state, ReceiptState::Applied);
let events = store.causal_events(0, 10).unwrap();
let provider_events = events
.iter()
.filter(|event| event.subject == "remote.echo")
.map(|event| event.kind.as_str())
.collect::<Vec<_>>();
assert_eq!(
provider_events,
vec!["provider.action.intent", "provider.action.result"]
);
server.abort();
}
#[tokio::test]
async fn supervises_and_composes_a_registered_stdio_mcp_tool() {
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let now = crate::model::now_ms();
let fixture = r#"
import json, sys
for line in sys.stdin:
request = json.loads(line)
method = request.get('method')
if 'id' not in request:
continue
if method == 'initialize':
result = {'protocolVersion':'2025-06-18','capabilities':{'tools':{}},'serverInfo':{'name':'fixture','version':'1'}}
elif method == 'tools/list':
result = {'tools':[{'name':'echo','description':'Echo one value.','inputSchema':{'type':'object','properties':{'value':{}},'required':['value']},'annotations':{'readOnlyHint':True,'destructiveHint':False,'idempotentHint':True,'openWorldHint':False}}]}
elif method == 'tools/call':
result = {'structuredContent':{'echoed':request['params']['arguments']['value']}}
else:
result = {}
print(json.dumps({'jsonrpc':'2.0','id':request['id'],'result':result}), flush=True)
"#;
let config = ConnectorConfig {
name: "local".to_string(),
kind: ConnectorKind::McpStdio,
url: None,
base_url: None,
command: Some("/usr/bin/python3".to_string()),
args: vec!["-u".to_string(), "-c".to_string(), fixture.to_string()],
cwd: None,
env: BTreeMap::new(),
secret_env: BTreeMap::new(),
headers: BTreeMap::new(),
secret_headers: BTreeMap::new(),
instructions: None,
enabled: true,
generation: String::new(),
created_at: now,
updated_at: now,
};
store
.put_provider("local", &serde_json::to_value(config).unwrap())
.unwrap();
let service = CodeService::new(store.clone()).unwrap();
let execution = service
.run(
"return await local.echo({ value: 'apocalypse' });".to_string(),
None,
Some(Duration::from_secs(5)),
)
.await
.unwrap();
assert_eq!(
execution.status,
ExecutionStatus::Completed,
"{execution:#?}"
);
assert_eq!(execution.result.as_ref().unwrap()["echoed"], "apocalypse");
assert!(store.operation_receipts(10).unwrap().is_empty());
}
#[tokio::test]
async fn orders_same_provider_resources_and_overlaps_disjoint_resources() {
let state = Arc::new(CoordinationFixture::default());
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let connector = CoordinatedConnector {
name: "resources".to_string(),
inner: Arc::new(CoordinationTestConnector {
state: state.clone(),
}),
store,
description: Mutex::new(None),
};
let context = ToolContext {
execution_id: "execution".to_string(),
control: Default::default(),
request: None,
};
let (first, second) = tokio::join!(
connector.execute("touch", serde_json::json!({"id": "same"}), &context),
connector.execute("touch", serde_json::json!({"id": "same"}), &context)
);
first.unwrap();
second.unwrap();
assert_eq!(state.maximum.load(Ordering::SeqCst), 1);
state.maximum.store(0, Ordering::SeqCst);
let (first, second) = tokio::join!(
connector.execute("touch", serde_json::json!({"id": "a"}), &context),
connector.execute("touch", serde_json::json!({"id": "b"}), &context)
);
first.unwrap();
second.unwrap();
assert_eq!(state.maximum.load(Ordering::SeqCst), 2);
}
}