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 incurs_mcp_client::{IoBridge, ToolCacheStore};
mod context;
mod executable;
use incurs_mcp_discovery::HostPaths;
use incurs_mcp_registry::{McpRegistry, NamespaceLedger, RegistryOptions};
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, DispatchSlot};
use crate::model::Verbosity;
use crate::model::{
CapabilityAnnotations, CapabilityDescriptor, CausalEvent, ConnectorConfig, ConnectorKind,
CoordinationMode, IdentityMode, OperationReceipt, OperationTarget, ProfileMode,
ProfileSelection, ReceiptState, SecretRef, VerificationMode, now_ms,
};
use crate::profile::Profile;
use crate::store::Store;
const WORKSPACE_CWD_HEADER: &str = "x-apoc-workspace-cwd";
const VERBOSITY_HEADER: &str = "x-apoc-verbosity";
const PROFILE_SELECTION_HEADER: &str = "x-apoc-profile-selection";
const HOOK_DEPTH_HEADER: &str = "x-apoc-hook-depth";
const DECIDER_DEPTH_HEADER: &str = "x-apoc-decider-depth";
#[derive(Debug, Clone, Default)]
pub struct RequestOrigin {
pub cwd: Option<String>,
pub verbosity: Option<String>,
pub profile_selection: Option<ProfileSelection>,
pub hook_depth: u32,
pub decider_depth: u32,
pub purpose: Option<String>,
pub idempotency_key: Option<String>,
}
impl RequestOrigin {
pub fn from_controller(controller: &crate::model::ProcessController) -> Self {
Self {
cwd: controller.cwd.clone(),
verbosity: controller.verbosity.clone(),
profile_selection: None,
hook_depth: controller.hook_depth,
decider_depth: controller.decider_depth,
purpose: None,
idempotency_key: None,
}
}
}
#[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>,
dispatch: DispatchSlot,
}
#[async_trait]
impl Connector for UnrestrictedConnector {
fn name(&self) -> &str {
self.inner.name()
}
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 prepare_arguments(
&self,
seq: u64,
tool: &incurs_codemode::ConnectorTool,
arguments: Value,
context: &ToolContext,
) -> Result<Value, String> {
if self.inner.name() == "apoc" {
return Ok(context::arguments(seq, tool, arguments, context));
}
self.inner
.prepare_arguments(seq, tool, arguments, context)
.await
}
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 header = |name: &str| {
context
.request
.as_ref()
.and_then(|request| request.headers.get(name))
.cloned()
};
let cwd = header(WORKSPACE_CWD_HEADER);
let verbosity = header(VERBOSITY_HEADER);
let profile_selection = header(PROFILE_SELECTION_HEADER)
.map(|selection| serde_json::from_str::<ProfileSelection>(&selection))
.transpose()
.map_err(|error| format!("PROFILE_CONTEXT_INVALID: {error}"))?;
let hook_depth = header(HOOK_DEPTH_HEADER)
.map(|depth| depth.parse::<u32>())
.transpose()
.map_err(|error| format!("HOOK_CONTEXT_INVALID: {error}"))?
.unwrap_or_default();
let decider_depth = header(DECIDER_DEPTH_HEADER)
.map(|depth| depth.parse::<u32>())
.transpose()
.map_err(|error| format!("DECIDER_CONTEXT_INVALID: {error}"))?
.unwrap_or_default();
let dispatch = self.dispatch.clone();
external(async move {
dispatch
.scope(crate::process::with_execution_context(
execution_id,
cwd,
verbosity,
profile_selection,
hook_depth,
decider_depth,
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 {
fn name(&self) -> &str {
&self.name
}
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>>>,
dispatch: DispatchSlot,
}
#[async_trait]
impl Connector for ExecutionLeaseConnector {
fn name(&self) -> &str {
self.inner.name()
}
async fn describe(&self) -> Result<ConnectorDescription, String> {
self.inner.describe().await
}
async fn prepare_arguments(
&self,
seq: u64,
tool: &incurs_codemode::ConnectorTool,
arguments: Value,
context: &ToolContext,
) -> Result<Value, String> {
self.inner
.prepare_arguments(seq, tool, arguments, context)
.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 cancelled = self
.dispatch
.scope(crate::process::with_execution_id(
execution_id.to_string(),
DaemonClient.call::<Value>(
"execution.cancel",
serde_json::json!({
"id": id,
"timeout_ms": 5000,
"idempotency_key": format!("code-owned/{execution_id}/{id}/cancel"),
}),
),
))
.await;
if let Err(error) = cancelled {
tracing::warn!(
program = %execution_id,
execution = %id,
error = %error,
"could not cancel a command owned by a finished Code Mode program"
);
}
}
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> {
Self::new_with_profile(store, None, DispatchSlot::default())
}
fn new_with_profile(
store: Arc<Store>,
profile: Option<Profile>,
dispatch: DispatchSlot,
) -> Result<Self, String> {
let controlled = store.controlled_profile_keys()?;
let configs = store
.list_providers()?
.into_iter()
.map(serde_json::from_value::<ConnectorConfig>)
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
.into_iter()
.filter(|config| config.enabled)
.filter(|config| {
!provider_uses_controlled_ambient(config, &controlled, profile.is_some())
})
.map(|config| provider_config_for_profile(config, profile.as_ref()))
.filter_map(Result::ok)
.flatten()
.collect::<Vec<_>>();
const QUICKJS_STACK_BYTES: usize = 2 * 1024 * 1024;
let service = LocalCodeModeService::spawn_with_stack_size(
4 * QUICKJS_STACK_BYTES,
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 execution_batch for a known command set and Promise.all for independent \
heterogeneous operations. Preserve returned execution, selection, process, and \
receipt identifiers. Synchronous execution tools return { id, outcome }; \
read execution logs explicitly when needed. After an outer Code Mode \
interruption, recover direct children with execution_list({ parent, ... }) and \
expand only selected records with execution_get or execution_logs. receipt_get is \
for provider-action receipts, not command lifecycle. Host dispatch renews the \
uninterrupted-JavaScript timeout budget.",
)
.with_policy_resolver(Arc::new(UnrestrictedPolicy));
let local: Arc<dyn Connector> = Arc::new(UnrestrictedConnector {
inner: Arc::new(connector),
dispatch: dispatch.clone(),
});
let local: Arc<dyn Connector> = Arc::new(ExecutionLeaseConnector {
inner: local,
runs: Mutex::new(HashMap::new()),
dispatch: dispatch.clone(),
});
let runtime: Arc<dyn RuntimeStore> = store.clone();
let mut connectors: Vec<Arc<dyn Connector>> = vec![local];
for config in configs {
if let Ok(connector) = external_connector(config, store.clone()) {
connectors.push(connector);
}
}
for discovered in discovered_connectors(&store) {
connectors.push(Arc::new(UnrestrictedConnector {
inner: discovered,
dispatch: dispatch.clone(),
}));
}
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: QUICKJS_STACK_BYTES,
}),
connectors,
)
},
)?;
Ok(Self { service })
}
pub async fn start(
&self,
code: String,
origin: RequestOrigin,
) -> Result<ExecutionState, String> {
let code = context::source(code, origin.cwd.as_deref())?;
let mut headers = HashMap::new();
if let Some(purpose) = origin.purpose {
headers.insert(context::PURPOSE_HEADER.to_string(), purpose);
}
if let Some(key) = origin.idempotency_key {
headers.insert(context::KEY_HEADER.to_string(), key);
}
if let Some(cwd) = origin.cwd {
headers.insert(WORKSPACE_CWD_HEADER.to_string(), cwd);
}
if let Some(view) = origin.verbosity {
headers.insert(VERBOSITY_HEADER.to_string(), view);
}
if let Some(selection) = origin.profile_selection.as_ref() {
headers.insert(
PROFILE_SELECTION_HEADER.to_string(),
serde_json::to_string(selection).map_err(|error| error.to_string())?,
);
}
if origin.hook_depth > 0 {
headers.insert(HOOK_DEPTH_HEADER.to_string(), origin.hook_depth.to_string());
}
if origin.decider_depth > 0 {
headers.insert(
DECIDER_DEPTH_HEADER.to_string(),
origin.decider_depth.to_string(),
);
}
let request = (!headers.is_empty()).then(|| 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,
origin: RequestOrigin,
timeout: Option<Duration>,
) -> Result<ExecutionState, String> {
let state = self.start(code, origin).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 {
store: Arc<Store>,
services: std::sync::RwLock<Actors>,
profile_services: Mutex<HashMap<String, Vec<CodeService>>>,
next: AtomicUsize,
owners: Mutex<HashMap<String, CodeService>>,
workers: usize,
dispatch: DispatchSlot,
}
struct Actors {
generation: String,
services: Vec<CodeService>,
}
impl CodePool {
pub fn new(store: Arc<Store>) -> Result<Self, String> {
Self::with_dispatch(store, DispatchSlot::default())
}
pub(crate) fn with_dispatch(store: Arc<Store>, dispatch: DispatchSlot) -> 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 generation = discovery_generation();
let services = (0..workers)
.map(|_| CodeService::new_with_profile(store.clone(), None, dispatch.clone()))
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
store,
services: std::sync::RwLock::new(Actors {
generation,
services,
}),
profile_services: Mutex::new(HashMap::new()),
next: AtomicUsize::new(0),
owners: Mutex::new(HashMap::new()),
workers,
dispatch,
})
}
pub fn workers(&self) -> usize {
self.workers
}
pub async fn start(
&self,
code: String,
origin: RequestOrigin,
) -> Result<ExecutionState, String> {
let services = self.services_for(origin.profile_selection.as_ref()).await?;
let index = self.next.fetch_add(1, Ordering::Relaxed) % services.len();
let service = services[index].clone();
let state = service.start(code, origin).await?;
if !terminal(state.status) {
self.owners.lock().await.insert(state.id.clone(), service);
}
Ok(state)
}
pub async fn run(
&self,
code: String,
origin: RequestOrigin,
timeout: Option<Duration>,
) -> Result<ExecutionState, String> {
let state = self.start(code, origin).await?;
self.wait(&state.id, timeout).await
}
pub async fn get(&self, id: String) -> Result<ExecutionState, String> {
self.first()?.get(id).await
}
pub async fn owns(&self, id: &str) -> bool {
self.owners.lock().await.contains_key(id)
}
pub async fn wait(
&self,
id: &str,
timeout: Option<Duration>,
) -> Result<ExecutionState, String> {
let owner = self.owners.lock().await.get(id).cloned();
let service = match owner {
Some(service) => service,
None => self.first()?,
};
let state = service.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 owner = self.owners.lock().await.get(&id).cloned();
let service = match owner {
Some(service) => service,
None => self.first()?,
};
let state = service.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.first()?.search(query).await
}
pub async fn artifact(&self, execution: String, artifact: String) -> Result<Value, String> {
self.first()?.artifact(execution, artifact).await
}
fn current(&self) -> Result<Vec<CodeService>, String> {
let generation = discovery_generation();
{
let actors = self
.services
.read()
.map_err(|_| "Code Mode actors are poisoned".to_string())?;
if actors.generation == generation {
return Ok(actors.services.clone());
}
}
let services = (0..self.workers)
.map(|_| CodeService::new_with_profile(self.store.clone(), None, self.dispatch.clone()))
.collect::<Result<Vec<_>, _>>()?;
let mut actors = self
.services
.write()
.map_err(|_| "Code Mode actors are poisoned".to_string())?;
*actors = Actors {
generation,
services: services.clone(),
};
Ok(services)
}
fn first(&self) -> Result<CodeService, String> {
self.current()?
.into_iter()
.next()
.ok_or_else(|| "no Code Mode actor is running".to_string())
}
async fn services_for(
&self,
selection: Option<&ProfileSelection>,
) -> Result<Vec<CodeService>, String> {
let Some(selection) = selection else {
return self.current();
};
let key = format!(
"{}@{}#{}",
selection.name,
selection.generation,
discovery_generation()
);
let mut profiles = self.profile_services.lock().await;
if let Some(services) = profiles.get(&key) {
return Ok(services.clone());
}
let profile = self
.store
.get_profile_generation(&selection.name, &selection.generation)?
.ok_or_else(|| {
format!(
"PROFILE_GENERATION_NOT_FOUND: profile \"{}\" generation \"{}\" is unavailable",
selection.name, selection.generation
)
})?;
let services = (0..self.workers)
.map(|_| {
CodeService::new_with_profile(
self.store.clone(),
Some(profile.clone()),
self.dispatch.clone(),
)
})
.collect::<Result<Vec<_>, _>>()?;
profiles.insert(key, services.clone());
Ok(services)
}
}
fn provider_uses_controlled_ambient(
config: &ConnectorConfig,
controlled: &[String],
profile_selected: bool,
) -> bool {
config
.secret_env
.iter()
.filter(|(destination, _)| {
!profile_selected || !config.profile_env.contains_key(*destination)
})
.map(|(_, secret)| secret)
.chain(
config
.secret_headers
.iter()
.filter(|(destination, _)| {
!profile_selected || !config.profile_headers.contains_key(*destination)
})
.map(|(_, secret)| secret),
)
.filter(|secret| secret.provider == "env")
.any(|secret| controlled.binary_search(&secret.reference).is_ok())
}
struct HttpMcpClient {
client: reqwest::Client,
url: String,
headers: BTreeMap<String, String>,
initialization: Mutex<HttpMcpInitialization>,
sequence: AtomicU64,
}
enum HttpMcpInitialization {
Uninitialized,
Initialized { session: Option<String> },
}
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)?,
initialization: Mutex::new(HttpMcpInitialization::Uninitialized),
sequence: AtomicU64::new(1),
})
}
async fn initialize(&self) -> Result<(), String> {
let mut initialization = self.initialization.lock().await;
if matches!(*initialization, HttpMcpInitialization::Initialized { .. }) {
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?;
*initialization = HttpMcpInitialization::Initialized { session };
Ok(())
}
async fn request(&self, method: &str, params: Value) -> Result<Value, String> {
self.initialize().await?;
let session = match &*self.initialization.lock().await {
HttpMcpInitialization::Initialized { session } => session.clone(),
HttpMcpInitialization::Uninitialized => {
return Err("MCP HTTP provider was not initialized".to_string());
}
};
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,
}
pub(crate) struct StdioMcpClient {
command: String,
args: Vec<String>,
cwd: Option<String>,
env: BTreeMap<String, String>,
state: Mutex<Option<StdioMcpState>>,
sequence: AtomicU64,
}
impl StdioMcpClient {
pub(crate) 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
}
}
fn provider_config_for_profile(
mut config: ConnectorConfig,
profile: Option<&Profile>,
) -> Result<Option<ConnectorConfig>, String> {
if config.profile_mode == ProfileMode::None {
if !config.profile_env.is_empty() || !config.profile_headers.is_empty() {
return Err(format!(
"PROVIDER_PROFILE_MODE_INVALID: provider \"{}\" declares profile mappings with profile_mode none",
config.name
));
}
return Ok(Some(config));
}
let Some(profile) = profile else {
return if config.profile_mode == ProfileMode::Required {
Ok(None)
} else {
Ok(Some(config))
};
};
for (destination, source) in config.profile_env.clone() {
config.env.remove(&destination);
config.secret_env.remove(&destination);
if let Some(value) = profile.literals.get(&source) {
config.env.insert(destination, value.clone());
} else if let Some(secret) = profile.secrets.get(&source) {
config.secret_env.insert(destination, secret.clone());
} else {
return Err(format!(
"PROVIDER_PROFILE_VALUE_MISSING: provider \"{}\" requires profile variable \"{source}\"",
config.name
));
}
}
for (destination, source) in config.profile_headers.clone() {
config.headers.remove(&destination);
config.secret_headers.remove(&destination);
if let Some(value) = profile.literals.get(&source) {
config.headers.insert(destination, value.clone());
} else if let Some(secret) = profile.secrets.get(&source) {
config.secret_headers.insert(destination, secret.clone());
} else {
return Err(format!(
"PROVIDER_PROFILE_VALUE_MISSING: provider \"{}\" requires profile variable \"{source}\"",
config.name
));
}
}
Ok(Some(config))
}
pub(crate) fn external_connector_for_profile(
config: ConnectorConfig,
store: Arc<Store>,
profile: Option<&Profile>,
) -> Result<Arc<dyn Connector>, String> {
let name = config.name.clone();
let config = provider_config_for_profile(config, profile)?.ok_or_else(|| {
format!("PROVIDER_PROFILE_REQUIRED: provider \"{name}\" requires a selected profile")
})?;
external_connector(config, store)
}
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,
dispatch: DispatchSlot::default(),
}))
}
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()
}
fn mcp_runtime() -> &'static tokio::runtime::Runtime {
static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
RUNTIME.get_or_init(|| {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.thread_name("apoc-mcp-host")
.enable_all()
.build()
.expect("apoc mcp host runtime")
})
}
pub fn project_execution(state: ExecutionState, level: Verbosity) -> serde_json::Value {
use serde_json::{Map, Value, json};
if level <= Verbosity::Off {
return Value::Object(Map::new());
}
let mut out = Map::new();
out.insert("id".to_string(), json!(state.id));
out.insert("status".to_string(), json!(state.status));
if let Some(result) = &state.result {
out.insert("result".to_string(), result.clone());
}
if let Some(error) = &state.error {
out.insert("error".to_string(), execution_error(error, level));
}
if level >= Verbosity::Info {
out.insert("calls".to_string(), json!(state.log.len()));
let withheld = state
.capabilities
.as_ref()
.filter(|capabilities| !capabilities.connectors.is_empty())
.map(|capabilities| {
let bytes = serde_json::to_vec(&capabilities.connectors)
.map(|encoded| encoded.len())
.unwrap_or_default();
json!({
"connectors": capabilities
.connectors
.iter()
.map(|connector| connector.name.clone())
.collect::<Vec<_>>(),
"bytes": bytes,
})
});
if let Some(withheld) = withheld
&& level < Verbosity::Trace
{
out.insert("capabilities_withheld".to_string(), withheld);
}
out.insert(
"inspect".to_string(),
json!([format!("apoc code get {} --verbosity trace", state.id)]),
);
}
if level >= Verbosity::Debug {
out.insert("logs".to_string(), json!(state.logs));
out.insert("connectors".to_string(), json!(state.connectors));
}
if level >= Verbosity::Trace {
return serde_json::to_value(&state).unwrap_or(Value::Object(out));
}
Value::Object(out)
}
fn execution_error(error: &str, level: Verbosity) -> serde_json::Value {
use serde_json::Value;
let Ok(Value::Object(mut decoded)) = serde_json::from_str::<Value>(error) else {
return Value::String(error.to_string());
};
if level < Verbosity::Warn {
return match decoded.remove("message") {
Some(Value::String(mut message)) => {
if let Some(fields) = decoded.get("fieldErrors").and_then(Value::as_array) {
for field in fields {
let Some(detail) = field.get("message").and_then(Value::as_str) else {
continue;
};
let path = field.get("path").and_then(Value::as_str).unwrap_or("input");
message.push_str(&format!("; {path}: {detail}"));
if let Some(expected) = field.get("expected").and_then(Value::as_str) {
message.push_str(&format!(" (expected {expected})"));
}
}
}
Value::String(message)
}
_ => Value::String(error.to_string()),
};
}
decoded.retain(|_, value| !value.is_null());
Value::Object(decoded)
}
fn mcp_cache_root() -> Result<std::path::PathBuf, String> {
Ok(crate::store::state_dir()?.join("mcp"))
}
fn reserved_namespaces(store: &Arc<Store>) -> Vec<String> {
let mut reserved = vec!["apoc".to_string()];
if let Ok(providers) = store.list_providers() {
for provider in providers {
if let Ok(config) = serde_json::from_value::<ConnectorConfig>(provider) {
reserved.push(config.name);
}
}
}
reserved
}
type DiscoveredByRoot =
std::sync::Mutex<HashMap<(std::path::PathBuf, String), Vec<Arc<dyn Connector>>>>;
fn discovered_connectors(store: &Arc<Store>) -> Vec<Arc<dyn Connector>> {
static DISCOVERED: OnceLock<DiscoveredByRoot> = OnceLock::new();
let root = store
.path()
.parent()
.map(|state| state.join("mcp"))
.unwrap_or_else(|| std::path::PathBuf::from("mcp"));
let registries = DISCOVERED.get_or_init(Default::default);
let mut registries = registries
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let generation = discovery_generation();
registries.retain(|(known, built), _| known != &root || built == &generation);
registries
.entry((root.clone(), generation))
.or_insert_with(|| discover_connectors(store, root))
.clone()
}
fn discover_connectors(store: &Arc<Store>, root: std::path::PathBuf) -> Vec<Arc<dyn Connector>> {
{
let Some(paths) = discovery_paths() else {
return Vec::new();
};
let cache = ToolCacheStore::new(&root);
let ledger_path = root.join("registry.json");
let mut ledger: NamespaceLedger = std::fs::read_to_string(&ledger_path)
.ok()
.and_then(|text| serde_json::from_str(&text).ok())
.unwrap_or_default();
let registry = McpRegistry::build(
&paths,
IoBridge::new(mcp_runtime().handle().clone()),
Some(Arc::new(cache)),
&mut ledger,
RegistryOptions {
exclude: reserved_namespaces(store),
..RegistryOptions::default()
},
);
if std::fs::create_dir_all(&root).is_ok()
&& let Ok(bytes) = serde_json::to_vec_pretty(&ledger)
{
let staged = ledger_path.with_extension(format!("json.tmp.{}", std::process::id()));
if std::fs::write(&staged, bytes).is_ok() {
let _ = std::fs::rename(&staged, &ledger_path);
}
}
registry.connectors()
}
}
fn discovery_paths() -> Option<HostPaths> {
match std::env::var_os("APOC_MCP_ROOT") {
Some(root) if !root.is_empty() => {
let root = std::path::PathBuf::from(root);
Some(HostPaths::rooted(&root).with_project(&root))
}
_ => {
let isolated = std::env::var_os("APOC_HOME")
.filter(|value| !value.is_empty())
.map(std::path::PathBuf::from);
HostPaths::from_env_rooted(isolated.as_deref()).ok()
}
}
}
fn discovery_generation() -> String {
static LAST: OnceLock<std::sync::Mutex<(String, String, Vec<std::path::PathBuf>)>> =
OnceLock::new();
let Some(paths) = discovery_paths() else {
return String::new();
};
let stamp = config_stamp(&paths);
let mut last = LAST
.get_or_init(Default::default)
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if last.0 != stamp {
let found = incurs_mcp_discovery::discover(&paths);
*last = (
stamp,
server_generation(&found.servers),
executable::paths(&found.servers),
);
}
format!("{}\n{}", last.1, executable::stamp(&last.2))
}
fn config_stamp(paths: &HostPaths) -> String {
incurs_mcp_discovery::locations(paths)
.iter()
.map(|location| {
let meta = std::fs::metadata(&location.path).ok();
format!(
"{}:{:?}:{:?}",
location.path.display(),
meta.as_ref().map(std::fs::Metadata::len),
meta.and_then(|meta| meta.modified().ok())
)
})
.collect::<Vec<_>>()
.join("\n")
}
fn server_generation(found: &[incurs_mcp_discovery::DiscoveredMcpServer]) -> String {
let mut servers: Vec<String> = found
.iter()
.map(|server| {
format!(
"{}|{:?}|{}",
server.local_name, server.fingerprint, server.enabled
)
})
.collect();
servers.sort();
servers.dedup();
servers.join("\n")
}
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}"));
}
let value = std::env::var(&secret.reference)
.map_err(|_| format!("Secret value for {name} is unavailable"))?;
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 {
#[test]
fn a_code_execution_withholds_connector_schemas_below_trace() {
use incurs_codemode::{CapabilitySnapshot, ConnectorDescription};
let snapshot = CapabilitySnapshot::new(vec![ConnectorDescription {
name: "fat".to_string(),
instructions: Some("x".repeat(256 * 1024)),
tools: Vec::new(),
}])
.unwrap();
let state = ExecutionState {
id: "exec-1".to_string(),
code: "return 1;".to_string(),
status: ExecutionStatus::Completed,
log: Vec::new(),
result: Some(serde_json::json!({ "ok": true })),
error: None,
logs: Vec::new(),
connectors: vec!["fat".to_string()],
capabilities: Some(snapshot),
events: Vec::new(),
created_at: 1,
updated_at: 2,
};
let size = |level| {
serde_json::to_vec(&super::project_execution(state.clone(), level))
.unwrap()
.len()
};
let off = size(Verbosity::Off);
let error = size(Verbosity::Error);
let info = size(Verbosity::Info);
let trace = size(Verbosity::Trace);
assert!(
off < error,
"off must be emptier than error: {off} vs {error}"
);
assert!(
error < info,
"error must be terser than info: {error} vs {info}"
);
assert!(info < 8 * 1024, "info must stay small, got {info} bytes");
assert!(trace > 256 * 1024, "trace must still carry the schemas");
let empty = super::project_execution(state.clone(), Verbosity::Off);
assert_eq!(empty, serde_json::json!({}));
let terse = super::project_execution(state.clone(), Verbosity::Error);
assert_eq!(terse["id"], "exec-1");
assert_eq!(terse["result"], serde_json::json!({ "ok": true }));
assert!(terse.get("capabilities_withheld").is_none());
let routine = super::project_execution(state.clone(), Verbosity::Info);
let withheld = &routine["capabilities_withheld"];
assert_eq!(withheld["connectors"][0], "fat");
assert!(withheld["bytes"].as_u64().unwrap() > 256 * 1024);
assert!(
routine["inspect"][0]
.as_str()
.is_some_and(|hint| hint.contains("--verbosity trace")),
"info must name the fuller read: {}",
routine["inspect"]
);
assert_eq!(routine["result"], serde_json::json!({ "ok": true }));
assert!(
terse.get("error").is_none(),
"a completed program has no error to report: {terse}"
);
let full = super::project_execution(state, Verbosity::Trace);
assert!(full.get("capabilities_withheld").is_none());
}
#[test]
fn default_validation_errors_keep_the_actionable_fields() {
let error = serde_json::json!({
"code": "VALIDATION_ERROR",
"message": "Validation failed",
"fieldErrors": [
{"path": "idempotency_key", "message": "Required", "expected": "string"},
{"path": "expect_exit_code", "message": "Invalid value", "expected": "array"}
]
})
.to_string();
let projected = super::execution_error(&error, Verbosity::Error);
assert_eq!(
projected,
serde_json::json!(
"Validation failed; idempotency_key: Required (expected string); expect_exit_code: Invalid value (expected array)"
)
);
}
#[test]
fn a_failed_program_reports_its_error_once_at_the_default() {
let recorded = serde_json::json!({
"code": "EXECUTION_COMMAND_FAILED",
"message": "the command had no logs",
"retryable": null,
})
.to_string();
let state = ExecutionState {
id: "exec-2".to_string(),
code: String::new(),
status: incurs_codemode::ExecutionStatus::Error,
log: Vec::new(),
result: None,
error: Some(recorded),
logs: Vec::new(),
connectors: vec!["one".to_string(), "two".to_string()],
capabilities: None,
events: Vec::new(),
created_at: 1,
updated_at: 2,
};
let terse = super::project_execution(state.clone(), Verbosity::Error);
assert_eq!(
terse,
serde_json::json!({
"id": "exec-2",
"status": "error",
"error": "the command had no logs",
})
);
let anomalies = super::project_execution(state.clone(), Verbosity::Warn);
assert_eq!(
anomalies["error"],
serde_json::json!({
"code": "EXECUTION_COMMAND_FAILED",
"message": "the command had no logs",
})
);
let plain = ExecutionState {
error: Some("Error: expected-failure".to_string()),
..state
};
assert_eq!(
super::project_execution(plain, Verbosity::Error)["error"],
"Error: expected-failure"
);
}
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::extract::{Query, State};
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use tempfile::tempdir;
use super::*;
use crate::model::ProcessController;
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
}
async fn transport_mcp_fixture(headers: HeaderMap, Json(body): Json<Value>) -> Response {
let method = body.get("method").and_then(Value::as_str).unwrap();
assert_eq!(
headers
.get("x-fixture")
.and_then(|value| value.to_str().ok()),
Some("present")
);
if method != "initialize" {
assert_eq!(
headers
.get("mcp-session-id")
.and_then(|value| value.to_str().ok()),
Some("transport-session")
);
}
if method == "notifications/initialized" {
return StatusCode::ACCEPTED.into_response();
}
let id = body.get("id").cloned().unwrap();
let response = match method {
"initialize" => serde_json::json!({
"jsonrpc": "2.0",
"id": id,
"result": {
"protocolVersion": "2025-06-18",
"capabilities": {"tools": {}},
"serverInfo": {"name": "transport", "version": "1"}
}
}),
"tools/list" if headers.get("x-mode").is_some() => {
serde_json::json!({"jsonrpc": "2.0", "id": id, "result": {}})
}
"tools/list" => serde_json::json!({
"jsonrpc": "2.0",
"id": id,
"result": {"tools": [{
"name": "echo",
"description": "Exercise every MCP result shape.",
"outputSchema": {"type": "object"},
"annotations": {"readOnlyHint": false, "openWorldHint": true}
}]}
}),
"tools/call" => {
let mode = body["params"]["arguments"]["mode"]
.as_str()
.unwrap_or("structured");
if mode == "rpc_error" {
serde_json::json!({
"jsonrpc": "2.0",
"id": id,
"error": {"code": -32000, "message": "remote failure"}
})
} else {
let result = match mode {
"is_error" => serde_json::json!({"isError": true, "content": []}),
"content" => {
serde_json::json!({"content": [{"type": "text", "text": "ok"}]})
}
"fallback" => serde_json::json!({"fallback": true}),
_ => serde_json::json!({"structuredContent": {"structured": true}}),
};
serde_json::json!({"jsonrpc": "2.0", "id": id, "result": result})
}
}
"missing/result" => serde_json::json!({"jsonrpc": "2.0", "id": id}),
_ => panic!("unexpected MCP method: {method}"),
};
let mut response = Json(response).into_response();
if method == "initialize" {
response.headers_mut().insert(
"mcp-session-id",
HeaderValue::from_static("transport-session"),
);
}
response
}
async fn openapi_transport_fixture(
Query(query): Query<BTreeMap<String, String>>,
headers: HeaderMap,
Json(body): Json<Value>,
) -> Json<Value> {
Json(serde_json::json!({
"provider_header": headers
.get("x-provider")
.and_then(|value| value.to_str().ok()),
"request_header": headers
.get("x-request")
.and_then(|value| value.to_str().ok()),
"query": query,
"body": body
}))
}
fn transport_config(name: &str, kind: ConnectorKind) -> ConnectorConfig {
let now = crate::model::now_ms();
ConnectorConfig {
name: name.to_string(),
kind,
url: None,
base_url: None,
command: None,
args: Vec::new(),
cwd: None,
env: BTreeMap::new(),
secret_env: BTreeMap::new(),
headers: BTreeMap::new(),
secret_headers: BTreeMap::new(),
profile_mode: ProfileMode::None,
profile_env: BTreeMap::new(),
profile_headers: BTreeMap::new(),
instructions: None,
enabled: true,
generation: "generation".to_string(),
created_at: now,
updated_at: now,
}
}
#[test]
fn provider_profile_mappings_use_only_the_selected_generation() {
let mut profile = Profile::empty("velo");
profile.literals.insert(
"GITHUB_AUTHORIZATION".to_string(),
"Bearer profile".to_string(),
);
profile.secrets.insert(
"API_TOKEN".to_string(),
SecretRef {
provider: "env".to_string(),
reference: "SOURCE_TOKEN".to_string(),
version: None,
},
);
profile.refresh_generation();
let mut config = transport_config("github", ConnectorKind::McpHttp);
config.profile_mode = ProfileMode::Required;
config.profile_headers.insert(
"authorization".to_string(),
"GITHUB_AUTHORIZATION".to_string(),
);
config
.profile_headers
.insert("x-api-token".to_string(), "API_TOKEN".to_string());
assert!(
provider_config_for_profile(config.clone(), None)
.unwrap()
.is_none()
);
let effective = provider_config_for_profile(config, Some(&profile))
.unwrap()
.unwrap();
assert_eq!(
effective.headers.get("authorization").map(String::as_str),
Some("Bearer profile")
);
assert_eq!(
effective
.secret_headers
.get("x-api-token")
.map(|secret| secret.reference.as_str()),
Some("SOURCE_TOKEN")
);
}
#[derive(Default)]
struct StatelessMcpFixture {
initialize_calls: AtomicUsize,
}
async fn stateless_mcp_fixture(
State(state): State<Arc<StatelessMcpFixture>>,
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();
}
if method == "initialize" {
state.initialize_calls.fetch_add(1, Ordering::SeqCst);
}
let id = body.get("id").cloned().unwrap();
let result = match method {
"initialize" => serde_json::json!({
"protocolVersion": "2025-06-18",
"capabilities": {"tools": {}},
"serverInfo": {"name": "stateless-fixture", "version": "1"}
}),
"tools/list" => serde_json::json!({"tools": []}),
_ => panic!("unexpected MCP method: {method}"),
};
Json(serde_json::json!({"jsonrpc": "2.0", "id": id, "result": result})).into_response()
}
#[derive(Default)]
struct CoordinationFixture {
active: AtomicUsize,
maximum: AtomicUsize,
}
struct CoordinationTestConnector {
state: Arc<CoordinationFixture>,
}
#[async_trait]
impl Connector for CoordinationTestConnector {
fn name(&self) -> &str {
"resources"
}
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 covers_policy_target_annotation_secret_and_summary_helpers() {
let mut controller = ProcessController::capture(true, Some("outer".to_string()));
controller.cwd = Some("/tmp/workspace".to_string());
controller.verbosity = Some("debug".to_string());
let origin = RequestOrigin::from_controller(&controller);
assert_eq!(origin.cwd.as_deref(), Some("/tmp/workspace"));
assert_eq!(origin.verbosity.as_deref(), Some("debug"));
let resolver = UnrestrictedPolicy;
let read_only = ToolAnnotations {
read_only: Some(true),
destructive: Some(false),
idempotent: Some(true),
open_world: Some(false),
};
let policy = resolver.resolve(ToolOrigin::Local, &read_only);
assert!(!policy.requires_approval);
assert_eq!(policy.replay, ReplayPolicy::Reexecute);
for annotations in [
ToolAnnotations {
destructive: Some(true),
..read_only.clone()
},
ToolAnnotations {
open_world: Some(true),
..read_only.clone()
},
ToolAnnotations {
read_only: Some(false),
..read_only.clone()
},
] {
assert_eq!(
resolver.resolve(ToolOrigin::Local, &annotations).replay,
ReplayPolicy::Log
);
}
for (arguments, expected) in [
(
serde_json::json!({"id": "record"}),
"provider/fixture/id/record",
),
(
serde_json::json!({"resource_id": 42}),
"provider/fixture/resource_id/42",
),
(serde_json::json!({"pid": 7}), "provider/fixture/pid/7"),
(
serde_json::json!({"path": "/tmp/file"}),
"provider/fixture/path//tmp/file",
),
(
serde_json::json!({"window_id": "main"}),
"provider/fixture/window_id/main",
),
(serde_json::json!({"id": true}), "provider/fixture"),
(Value::Null, "provider/fixture"),
] {
assert_eq!(provider_target("fixture", &arguments), expected);
}
let first = provider_lock("same").await;
let second = provider_lock("same").await;
let different = provider_lock("different").await;
assert!(Arc::ptr_eq(&first, &second));
assert!(!Arc::ptr_eq(&first, &different));
let value = serde_json::json!({"required": "value", "number": 7});
assert_eq!(required_string(&value, "required").unwrap(), "value");
assert!(required_string(&value, "missing").is_err());
assert!(required_string(&value, "number").is_err());
assert_eq!(
optional_string(&value, "required").as_deref(),
Some("value")
);
assert!(optional_string(&value, "missing").is_none());
let standard = serde_json::json!({
"annotations": {
"title": "Fixture",
"readOnlyHint": true,
"destructiveHint": false,
"idempotentHint": true,
"openWorldHint": false
}
});
let annotations = mcp_annotations(&standard).unwrap();
assert_eq!(annotations.title.as_deref(), Some("Fixture"));
assert_eq!(annotations.read_only_hint, Some(true));
assert_eq!(annotations.destructive_hint, Some(false));
assert_eq!(annotations.idempotent_hint, Some(true));
assert_eq!(annotations.open_world_hint, Some(false));
let legacy = serde_json::json!({
"annotations": {
"read_only_hint": false,
"destructive_hint": true
}
});
let annotations = mcp_annotations(&legacy).unwrap();
assert_eq!(annotations.read_only_hint, Some(false));
assert_eq!(annotations.destructive_hint, Some(true));
assert!(mcp_annotations(&Value::Null).is_none());
assert_eq!(
annotation_bool(&legacy["annotations"], "readOnlyHint", "read_only_hint"),
Some(false)
);
let literal = BTreeMap::from([("LITERAL".to_string(), "value".to_string())]);
assert_eq!(
resolved_values(&literal, &BTreeMap::new()).unwrap(),
literal
);
let unsupported = BTreeMap::from([(
"SECRET".to_string(),
SecretRef {
provider: "keychain".to_string(),
reference: "fixture".to_string(),
version: None,
},
)]);
assert!(resolved_values(&BTreeMap::new(), &unsupported).is_err());
let unavailable = BTreeMap::from([(
"SECRET".to_string(),
SecretRef {
provider: "env".to_string(),
reference: "APOC_DEFINITELY_MISSING_SECRET_FOR_TEST".to_string(),
version: None,
},
)]);
assert!(resolved_values(&BTreeMap::new(), &unavailable).is_err());
let available = BTreeMap::from([(
"PATH_COPY".to_string(),
SecretRef {
provider: "env".to_string(),
reference: "PATH".to_string(),
version: None,
},
)]);
assert_eq!(
resolved_values(&BTreeMap::new(), &available).unwrap()["PATH_COPY"],
std::env::var("PATH").unwrap()
);
let state = ExecutionState {
id: "execution".to_string(),
code: "return 1;".to_string(),
status: ExecutionStatus::Completed,
log: Vec::new(),
result: Some(serde_json::json!(1)),
error: None,
logs: vec!["complete".to_string()],
connectors: Vec::new(),
capabilities: None,
events: Vec::new(),
created_at: 1,
updated_at: 2,
};
let summary = execution_summary(&state);
assert_eq!(summary["id"], "execution");
assert_eq!(summary["calls"], 0);
assert!(!terminal(ExecutionStatus::Running));
assert!(!terminal(ExecutionStatus::Paused));
for status in [
ExecutionStatus::Completed,
ExecutionStatus::Error,
ExecutionStatus::Rejected,
ExecutionStatus::RolledBack,
ExecutionStatus::Cancelled,
] {
assert!(terminal(status));
}
}
#[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, purpose: 'Inspecting one process.' }}));\
const [a, processes, b] = await Promise.all([\
apoc.process_select({{ target: 'pid:{}', purpose: 'Selecting one process.' }}),\
Promise.all(queries),\
apoc.process_ports({{ port: 9, purpose: 'Checking one port.' }})\
]);\
return {{ selection: a.id, processCount: processes.length, \
ports: b.length }};",
std::process::id(),
std::process::id()
),
RequestOrigin::default(),
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};"), RequestOrigin::default())
.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 waits_through_the_host_runtime_in_code_mode() {
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let service = CodeService::new(store).unwrap();
let started = tokio::time::Instant::now();
let execution = service
.run(
"return await apoc.wait({ ms: 25, purpose: 'Wait in the host runtime.' });"
.to_string(),
RequestOrigin::default(),
Some(Duration::from_secs(5)),
)
.await
.unwrap();
assert_eq!(
execution.status,
ExecutionStatus::Completed,
"{execution:#?}"
);
assert_eq!(execution.result.as_ref().unwrap()["waited_ms"], 25);
assert!(started.elapsed() >= Duration::from_millis(20));
}
#[tokio::test]
async fn rejects_unbounded_code_mode_waits_without_sleeping() {
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let service = CodeService::new(store).unwrap();
let execution = service
.run(
"return await apoc.wait({ ms: 60001, purpose: 'Reject an excessive wait.' });"
.to_string(),
RequestOrigin::default(),
Some(Duration::from_secs(5)),
)
.await
.unwrap();
assert_eq!(execution.status, ExecutionStatus::Error, "{execution:#?}");
assert!(
execution
.error
.as_deref()
.is_some_and(|error| error.contains("WAIT_LIMIT")),
"{execution:#?}"
);
}
#[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(),
profile_mode: ProfileMode::None,
profile_env: BTreeMap::new(),
profile_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(),
RequestOrigin::default(),
Some(Duration::from_secs(15)),
)
.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(),
profile_mode: ProfileMode::None,
profile_env: BTreeMap::new(),
profile_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(),
RequestOrigin::default(),
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 exercises_external_transport_and_capability_edge_contracts() {
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(transport_mcp_fixture))
.route("/empty", post(|| async { StatusCode::NO_CONTENT }))
.route(
"/sse",
post(|| async {
let mut response =
"event: message\ndata: {\"jsonrpc\":\"2.0\",\"result\":{\"sse\":true}}\n\n"
.into_response();
response.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/event-stream"),
);
response
}),
)
.route(
"/empty-sse",
post(|| async {
let mut response = "event: ping\n\n".into_response();
response.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/event-stream"),
);
response
}),
)
.route("/bad-json", post(|| async { "not json" }))
.route(
"/status",
post(|| async { (StatusCode::IM_A_TEAPOT, "teapot") }),
)
.route(
"/spec.yaml",
get(|| async {
"openapi: 3.1.0\ninfo:\n title: transport\n version: '1'\npaths: {}\n"
}),
)
.route("/invalid.yaml", get(|| async { "[unterminated" }))
.route("/plain", get(|| async { "plain response" }))
.route(
"/absolute",
get(|| async { Json(serde_json::json!({"absolute": true})) }),
)
.route(
"/error",
get(|| async { (StatusCode::BAD_REQUEST, "bad request") }),
)
.route("/items", post(openapi_transport_fixture));
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let mut mcp_config = transport_config("transport_mcp", ConnectorKind::McpHttp);
mcp_config.url = Some(format!("http://{address}/mcp"));
mcp_config
.headers
.insert("x-fixture".to_string(), "present".to_string());
let mcp = HttpMcpClient::new(&mcp_config).unwrap();
let tools = mcp.list_tools().await.unwrap();
assert_eq!(tools.len(), 1);
assert_eq!(tools[0].name, "echo");
assert_eq!(tools[0].input_schema, serde_json::json!({"type": "object"}));
assert_eq!(
tools[0].output_schema,
Some(serde_json::json!({"type": "object"}))
);
assert_eq!(
tools[0]
.annotations
.as_ref()
.and_then(|annotations| annotations.open_world_hint),
Some(true)
);
mcp.initialize().await.unwrap();
assert_eq!(
mcp.call_tool("echo", serde_json::json!({"mode": "structured"}))
.await
.unwrap(),
serde_json::json!({"structured": true})
);
assert_eq!(
mcp.call_tool("echo", serde_json::json!({"mode": "content"}))
.await
.unwrap(),
serde_json::json!([{"type": "text", "text": "ok"}])
);
assert_eq!(
mcp.call_tool("echo", serde_json::json!({"mode": "fallback"}))
.await
.unwrap(),
serde_json::json!({"fallback": true})
);
assert!(
mcp.call_tool("echo", serde_json::json!({"mode": "is_error"}))
.await
.unwrap_err()
.contains("isError")
);
assert!(
mcp.call_tool("echo", serde_json::json!({"mode": "rpc_error"}))
.await
.unwrap_err()
.contains("remote failure")
);
assert!(
mcp.request("missing/result", serde_json::json!({}))
.await
.unwrap_err()
.contains("no result")
);
let mut missing_tools_config = mcp_config.clone();
missing_tools_config
.headers
.insert("x-mode".to_string(), "missing-tools".to_string());
assert!(
HttpMcpClient::new(&missing_tools_config)
.unwrap()
.list_tools()
.await
.unwrap_err()
.contains("no tools array")
);
for (path, expected) in [
("empty", None),
(
"sse",
Some(serde_json::json!({"jsonrpc": "2.0", "result": {"sse": true}})),
),
] {
let mut config = transport_config("direct_mcp", ConnectorKind::McpHttp);
config.url = Some(format!("http://{address}/{path}"));
let (value, _) = HttpMcpClient::new(&config)
.unwrap()
.post(serde_json::json!({}), None)
.await
.unwrap();
assert_eq!(value, expected.unwrap_or(Value::Null));
}
for (path, message) in [
("empty-sse", "contained no data"),
("bad-json", "expected ident"),
("status", "418"),
] {
let mut config = transport_config("failing_mcp", ConnectorKind::McpHttp);
config.url = Some(format!("http://{address}/{path}"));
let error = HttpMcpClient::new(&config)
.unwrap()
.post(serde_json::json!({}), None)
.await
.unwrap_err();
assert!(error.contains(message), "{path}: {error}");
}
assert!(
HttpMcpClient::new(&transport_config("missing_mcp", ConnectorKind::McpHttp))
.err()
.unwrap()
.contains("requires url")
);
let mut openapi_config = transport_config("transport_api", ConnectorKind::OpenApi);
openapi_config.url = Some(format!("http://{address}/spec.yaml"));
openapi_config.base_url = Some(format!("http://{address}/"));
openapi_config
.headers
.insert("x-provider".to_string(), "provider".to_string());
let openapi = HttpOpenApiClient::new(&openapi_config).unwrap();
let specification = openapi.specification().await.unwrap();
assert_eq!(specification["openapi"], "3.1.0");
let response = openapi
.request(OpenApiRequest {
path: "/items".to_string(),
method: "post".to_string(),
parameters: BTreeMap::from([
("text".to_string(), serde_json::json!("value")),
("number".to_string(), serde_json::json!(7)),
]),
body: Some(serde_json::json!({"payload": true})),
headers: BTreeMap::from([("x-request".to_string(), "request".to_string())]),
})
.await
.unwrap();
assert_eq!(response["provider_header"], "provider");
assert_eq!(response["request_header"], "request");
assert_eq!(response["query"]["text"], "value");
assert_eq!(response["query"]["number"], "7");
assert_eq!(response["body"]["payload"], true);
assert_eq!(
openapi
.request(OpenApiRequest {
path: format!("http://{address}/absolute"),
method: "get".to_string(),
parameters: BTreeMap::new(),
body: None,
headers: BTreeMap::new(),
})
.await
.unwrap()["absolute"],
true
);
assert_eq!(
openapi
.request(OpenApiRequest {
path: "/plain".to_string(),
method: "get".to_string(),
parameters: BTreeMap::new(),
body: None,
headers: BTreeMap::new(),
})
.await
.unwrap(),
"plain response"
);
assert!(
openapi
.request(OpenApiRequest {
path: "/error".to_string(),
method: "get".to_string(),
parameters: BTreeMap::new(),
body: None,
headers: BTreeMap::new(),
})
.await
.unwrap_err()
.contains("400")
);
assert!(
openapi
.request(OpenApiRequest {
path: "/plain".to_string(),
method: "bad method".to_string(),
parameters: BTreeMap::new(),
body: None,
headers: BTreeMap::new(),
})
.await
.is_err()
);
let mut invalid_yaml = openapi_config.clone();
invalid_yaml.url = Some(format!("http://{address}/invalid.yaml"));
assert!(
HttpOpenApiClient::new(&invalid_yaml)
.unwrap()
.specification()
.await
.is_err()
);
let mut missing_base = openapi_config.clone();
missing_base.base_url = None;
assert!(
HttpOpenApiClient::new(&missing_base)
.err()
.unwrap()
.contains("base_url")
);
let mut missing_spec = openapi_config.clone();
missing_spec.url = None;
assert!(
HttpOpenApiClient::new(&missing_spec)
.err()
.unwrap()
.contains("requires url")
);
let stdio_fixture = r#"
import json, os, sys
for line in sys.stdin:
request = json.loads(line)
method = request.get('method')
if 'id' not in request:
continue
identifier = request['id']
if method == 'initialize':
result = {'protocolVersion':'2025-06-18','capabilities':{'tools':{}},'serverInfo':{'name':'fixture','version':os.environ['TRANSPORT_ENV']}}
elif method == 'tools/list':
print(json.dumps({'jsonrpc':'2.0','id':0,'result':{}}), flush=True)
result = {'tools':[{'name':'shape','description':'Return result shapes.','outputSchema':{'type':'object'},'annotations':{'readOnlyHint':False}}]}
elif method == 'tools/call':
mode = request['params']['arguments'].get('mode')
if mode == 'is_error': result = {'isError':True,'content':[]}
elif mode == 'content': result = {'content':[{'type':'text','text':'stdio'}]}
elif mode == 'fallback': result = {'fallback':True}
else: result = {'structuredContent':{'structured':True}}
elif method == 'bad_json':
print('not json', flush=True)
continue
elif method == 'missing_result':
print(json.dumps({'jsonrpc':'2.0','id':identifier}), flush=True)
continue
elif method == 'rpc_error':
print(json.dumps({'jsonrpc':'2.0','id':identifier,'error':{'code':-32000,'message':'stdio failure'}}), flush=True)
continue
elif method == 'exit':
sys.exit(0)
else:
result = {}
print(json.dumps({'jsonrpc':'2.0','id':identifier,'result':result}), flush=True)
"#;
let directory = tempdir().unwrap();
let mut stdio_config = transport_config("transport_stdio", ConnectorKind::McpStdio);
stdio_config.command = Some("/usr/bin/python3".to_string());
stdio_config.args = vec![
"-u".to_string(),
"-c".to_string(),
stdio_fixture.to_string(),
];
stdio_config.cwd = Some(directory.path().display().to_string());
stdio_config
.env
.insert("TRANSPORT_ENV".to_string(), "present".to_string());
let stdio = StdioMcpClient::new(&stdio_config).unwrap();
let tools = stdio.list_tools().await.unwrap();
assert_eq!(tools[0].input_schema, serde_json::json!({"type": "object"}));
assert_eq!(
stdio
.call_tool("shape", serde_json::json!({"mode": "content"}))
.await
.unwrap(),
serde_json::json!([{"type": "text", "text": "stdio"}])
);
assert_eq!(
stdio
.call_tool("shape", serde_json::json!({"mode": "fallback"}))
.await
.unwrap(),
serde_json::json!({"fallback": true})
);
assert!(
stdio
.call_tool("shape", serde_json::json!({"mode": "is_error"}))
.await
.unwrap_err()
.contains("isError")
);
assert!(
stdio
.request("bad_json", serde_json::json!({}))
.await
.is_err()
);
assert!(
stdio
.request("missing_result", serde_json::json!({}))
.await
.unwrap_err()
.contains("no result")
);
assert!(
stdio
.request("rpc_error", serde_json::json!({}))
.await
.unwrap_err()
.contains("stdio failure")
);
assert!(
stdio
.request("exit", serde_json::json!({}))
.await
.unwrap_err()
.contains("exited before responding")
);
assert!(
StdioMcpClient::new(&transport_config("missing_stdio", ConnectorKind::McpStdio))
.err()
.unwrap()
.contains("requires command")
);
let mut missing_command = stdio_config.clone();
missing_command.command = Some("/definitely/missing/apoc-mcp-provider".to_string());
assert!(
StdioMcpClient::new(&missing_command)
.unwrap()
.list_tools()
.await
.unwrap_err()
.contains("Could not start")
);
let missing_tools_fixture = r#"
import json, sys
for line in sys.stdin:
request = json.loads(line)
if 'id' not in request:
continue
method = request.get('method')
if method == 'initialize':
result = {'protocolVersion':'2025-06-18','capabilities':{'tools':{}},'serverInfo':{'name':'fixture','version':'1'}}
else:
result = {}
print(json.dumps({'jsonrpc':'2.0','id':request['id'],'result':result}), flush=True)
"#;
let mut missing_tools_stdio = stdio_config.clone();
missing_tools_stdio.args = vec![
"-u".to_string(),
"-c".to_string(),
missing_tools_fixture.to_string(),
];
assert!(
StdioMcpClient::new(&missing_tools_stdio)
.unwrap()
.list_tools()
.await
.unwrap_err()
.contains("no tools array")
);
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let mut builtin = transport_config("builtin", ConnectorKind::Builtin);
builtin.instructions = Some("instructions".to_string());
assert!(
external_connector(builtin, store.clone())
.err()
.unwrap()
.contains("Builtin")
);
for kind in [
ConnectorKind::McpHttp,
ConnectorKind::OpenApi,
ConnectorKind::McpStdio,
] {
let mut config = transport_config("configured", kind);
config.url = Some(format!("http://{address}/mcp"));
config.base_url = Some(format!("http://{address}"));
config.command = Some("/usr/bin/true".to_string());
config.instructions = Some("instructions".to_string());
external_connector(config, store.clone()).unwrap();
}
let tools = vec![
incurs_codemode::ConnectorTool {
name: "exact".to_string(),
description: None,
input_schema: serde_json::json!({
"type": "object",
"properties": {"id": {}, "version": {}}
}),
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::default(),
},
incurs_codemode::ConnectorTool {
name: "stable".to_string(),
description: None,
input_schema: serde_json::json!({
"type": "object",
"properties": {"resource_id": {}}
}),
output_schema: None,
instructions: None,
examples: Vec::new(),
annotations: ToolAnnotations {
read_only: Some(false),
destructive: Some(false),
idempotent: Some(true),
open_world: Some(false),
},
policy: ToolPolicy::default(),
},
incurs_codemode::ConnectorTool {
name: "opaque".to_string(),
description: None,
input_schema: serde_json::json!({"type": "object"}),
output_schema: None,
instructions: None,
examples: Vec::new(),
annotations: ToolAnnotations {
read_only: Some(true),
destructive: Some(false),
idempotent: Some(true),
open_world: Some(false),
},
policy: ToolPolicy::default(),
},
incurs_codemode::ConnectorTool {
name: "opaque_mutation".to_string(),
description: None,
input_schema: serde_json::json!({"type": "object"}),
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::default(),
},
];
let description = ConnectorDescription {
name: "capabilities".to_string(),
instructions: None,
tools,
};
let descriptors = capability_descriptors(&mcp_config, &description);
assert_eq!(descriptors[0].identity_mode, IdentityMode::Exact);
assert_eq!(descriptors[0].coordination_mode, CoordinationMode::Resource);
assert_eq!(descriptors[1].identity_mode, IdentityMode::Stable);
assert_eq!(descriptors[1].coordination_mode, CoordinationMode::Resource);
assert_eq!(descriptors[2].identity_mode, IdentityMode::Opaque);
assert_eq!(descriptors[2].coordination_mode, CoordinationMode::None);
assert_eq!(descriptors[3].identity_mode, IdentityMode::Opaque);
assert_eq!(descriptors[3].coordination_mode, CoordinationMode::Provider);
server.abort();
}
#[tokio::test]
async fn initializes_a_stateless_http_mcp_server_once() {
let state = Arc::new(StatelessMcpFixture::default());
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(stateless_mcp_fixture))
.with_state(state.clone());
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let now = crate::model::now_ms();
let config = ConnectorConfig {
name: "stateless".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(),
profile_mode: crate::model::ProfileMode::None,
profile_env: BTreeMap::new(),
profile_headers: BTreeMap::new(),
instructions: None,
enabled: true,
generation: String::new(),
created_at: now,
updated_at: now,
};
let client = HttpMcpClient::new(&config).unwrap();
client.list_tools().await.unwrap();
client.list_tools().await.unwrap();
assert_eq!(state.initialize_calls.load(Ordering::SeqCst), 1);
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':{},'purpose':{'type':'string'},'idempotency_key':{'type':'string'}},'required':['value']},'annotations':{'readOnlyHint':True,'destructiveHint':False,'idempotentHint':True,'openWorldHint':False}}]}
elif method == 'tools/call':
result = {'structuredContent':{'echoed':request['params']['arguments']['value'],'arguments':request['params']['arguments']}}
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(),
profile_mode: ProfileMode::None,
profile_env: BTreeMap::new(),
profile_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(),
RequestOrigin {
purpose: Some("Inspect fixture".to_string()),
idempotency_key: Some("external-fixture".to_string()),
..Default::default()
},
Some(Duration::from_secs(5)),
)
.await
.unwrap();
assert_eq!(
execution.status,
ExecutionStatus::Completed,
"{execution:#?}"
);
assert_eq!(execution.result.as_ref().unwrap()["echoed"], "apocalypse");
assert_eq!(
execution.result.as_ref().unwrap()["arguments"],
serde_json::json!({"value": "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);
}
#[tokio::test]
async fn covers_connector_delegation_and_code_pool_lifecycle_edges() {
let state = Arc::new(CoordinationFixture::default());
let inner: Arc<dyn Connector> = Arc::new(CoordinationTestConnector { state });
let context = ToolContext {
execution_id: "delegation".to_string(),
control: Default::default(),
request: None,
};
let arguments = serde_json::json!({"id": "resource"});
let result = serde_json::json!({"applied": true});
let unrestricted = UnrestrictedConnector {
inner: inner.clone(),
dispatch: DispatchSlot::default(),
};
assert!(
!unrestricted
.revert("touch", arguments.clone(), result.clone(), &context)
.await
.unwrap()
);
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
let coordinated = CoordinatedConnector {
name: "delegated".to_string(),
inner: inner.clone(),
store: store.clone(),
description: Mutex::new(None),
};
coordinated.describe().await.unwrap();
assert!(
!coordinated
.revert("touch", arguments.clone(), result.clone(), &context)
.await
.unwrap()
);
let leased = ExecutionLeaseConnector {
inner,
runs: Mutex::new(HashMap::new()),
dispatch: DispatchSlot::default(),
};
leased.describe().await.unwrap();
leased
.execute("touch", arguments.clone(), &context)
.await
.unwrap();
let mut execution_owned = arguments;
execution_owned["owner"] = serde_json::json!("execution");
leased
.execute("touch", execution_owned, &context)
.await
.unwrap();
assert!(
!leased
.revert("touch", serde_json::json!({}), result, &context)
.await
.unwrap()
);
leased.pass_ended("delegation", "completed").await;
leased.execution_ended("delegation", "completed").await;
let pool = CodePool::new(store).unwrap();
let running = pool
.start(
r#"await apoc.execution_command({
executable: "sleep",
arg: ["5"],
purpose: "Holding one execution open for the lifecycle test.",
idempotency_key: "lifecycle-hold",
expect_exit_code: 0
});
return 1;"#
.to_string(),
RequestOrigin::default(),
)
.await
.unwrap();
let pending = pool.wait(&running.id, Some(Duration::ZERO)).await.unwrap();
assert!(
!terminal(pending.status),
"a program awaiting a subprocess must not be terminal: {pending:#?}"
);
let cancelled = pool.cancel(running.id.clone()).await.unwrap();
assert!(terminal(cancelled.status), "{cancelled:#?}");
assert!(!pool.owners.lock().await.contains_key(&running.id));
let completed = pool
.run(
"return 7;".to_string(),
RequestOrigin::default(),
Some(Duration::from_secs(5)),
)
.await
.unwrap();
assert_eq!(completed.status, ExecutionStatus::Completed);
assert_eq!(
pool.get(completed.id.clone()).await.unwrap().result,
Some(serde_json::json!(7))
);
assert!(pool.search("process".to_string()).await.unwrap().total > 0);
assert!(
pool.artifact(completed.id, "missing-artifact".to_string())
.await
.is_err()
);
assert!(pool.cancel("missing-execution".to_string()).await.is_err());
}
}
#[derive(Debug, serde::Serialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct McpServerStatus {
pub namespace: String,
pub display_name: String,
pub transport: String,
pub sources: Vec<String>,
pub enabled: bool,
pub state: String,
pub tool_count: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(Debug, serde::Serialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct McpCheckOutput {
pub cache_root: String,
pub entries: usize,
pub self_excluded: usize,
pub servers: Vec<McpServerStatus>,
pub findings: Vec<McpFinding>,
}
#[derive(Debug, serde::Serialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct McpFinding {
pub severity: String,
pub code: String,
pub path: String,
}
#[derive(Debug, serde::Serialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct McpRefreshed {
pub namespace: String,
pub tools: usize,
}
#[derive(Debug, serde::Serialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct McpFailed {
pub namespace: String,
pub reason: String,
}
#[derive(Debug, serde::Serialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct McpRefreshOutput {
pub refreshed: Vec<McpRefreshed>,
pub failed: Vec<McpFailed>,
}
fn diagnostic_registry() -> Result<(McpRegistry, std::path::PathBuf), String> {
let root = mcp_cache_root()?;
let paths = match std::env::var_os("APOC_MCP_ROOT") {
Some(named) if !named.is_empty() => {
let named = std::path::PathBuf::from(named);
HostPaths::rooted(&named).with_project(&named)
}
_ => HostPaths::from_env().map_err(|error| error.to_string())?,
};
let mut ledger: NamespaceLedger = std::fs::read_to_string(root.join("registry.json"))
.ok()
.and_then(|text| serde_json::from_str(&text).ok())
.unwrap_or_default();
let store = crate::store::Store::open_default().map(Arc::new)?;
let registry = McpRegistry::build(
&paths,
IoBridge::new(mcp_runtime().handle().clone()),
Some(Arc::new(ToolCacheStore::new(&root))),
&mut ledger,
RegistryOptions {
exclude: reserved_namespaces(&store),
..RegistryOptions::default()
},
);
Ok((registry, root))
}
pub fn mcp_check() -> Result<McpCheckOutput, String> {
let (registry, root) = diagnostic_registry()?;
let cached = registry.cached_tool_counts();
let refusals = registry.refusals();
let failures = registry.cached_failures();
let servers = registry
.servers()
.iter()
.map(|server| {
let known = failures
.get(&server.namespace)
.and_then(Option::as_ref)
.or_else(|| refusals.get(&server.namespace).and_then(Option::as_ref));
let tools = cached.get(&server.namespace).copied().flatten();
McpServerStatus {
namespace: server.namespace.clone(),
display_name: server.display_name.clone(),
transport: match &server.server.transport {
incurs_mcp_discovery::McpTransport::Stdio(_) => "stdio".to_string(),
incurs_mcp_discovery::McpTransport::StreamableHttp(_) => "http".to_string(),
incurs_mcp_discovery::McpTransport::Sse(_) => "sse".to_string(),
},
sources: server
.sources
.iter()
.map(|source| source.slug().to_string())
.collect(),
enabled: server.enabled,
state: known.map_or_else(
|| {
if tools.is_some() {
"cached".to_string()
} else {
"unknown".to_string()
}
},
|report| format!("{:?}", report.state).to_lowercase(),
),
tool_count: tools,
reason: known.map(|report| report.summary().to_string()),
}
})
.collect();
Ok(McpCheckOutput {
cache_root: root.to_string_lossy().to_string(),
entries: registry.entry_count(),
self_excluded: registry.self_excluded(),
servers,
findings: registry
.diagnostics()
.iter()
.map(|finding| McpFinding {
severity: format!("{:?}", finding.severity).to_lowercase(),
code: finding.code.clone(),
path: finding.path.clone(),
})
.collect(),
})
}
pub async fn mcp_refresh() -> Result<McpRefreshOutput, String> {
let (registry, _root) = diagnostic_registry()?;
let mut refreshed = Vec::new();
let mut failed = Vec::new();
for (namespace, outcome) in registry.refresh().await {
match outcome {
Ok(tools) => refreshed.push(McpRefreshed { namespace, tools }),
Err(report) => failed.push(McpFailed {
namespace,
reason: report.summary().to_string(),
}),
}
}
registry.shutdown().await;
Ok(McpRefreshOutput { refreshed, failed })
}
#[cfg(test)]
mod discovery_generation_tests {
use super::server_generation;
use incurs_mcp_discovery::HostPaths;
use serde_json::{Value, json};
fn write(root: &std::path::Path, servers: Value, unrelated: &str) {
let config = json!({ "mcpServers": servers, "unrelated": unrelated });
std::fs::write(root.join(".mcp.json"), config.to_string()).unwrap();
}
#[test]
fn the_generation_follows_servers_not_unrelated_configuration() {
let root = tempfile::tempdir().unwrap();
let paths = HostPaths::rooted(root.path()).with_project(root.path());
let one = json!({ "a": { "command": "a-server" } });
write(root.path(), one.clone(), "first");
let first = server_generation(&incurs_mcp_discovery::discover(&paths).servers);
write(root.path(), one, "second");
assert_eq!(
server_generation(&incurs_mcp_discovery::discover(&paths).servers),
first,
"an unrelated key must not rebuild actors"
);
let two = json!({ "a": { "command": "a-server" }, "b": { "command": "b-server" } });
write(root.path(), two, "second");
assert_ne!(
server_generation(&incurs_mcp_discovery::discover(&paths).servers),
first,
"a new server must change the generation"
);
}
}