pub mod aap;
pub mod admin;
pub mod api_keys;
pub mod auth;
pub mod mcp_http;
pub mod serve;
use std::sync::Arc;
use crate::live_index::SharedIndex;
use crate::protocol::SymForgeServer;
use crate::sidecar::governor::RequestGovernor;
use crate::stel::ledger_store::StelLedgerStore;
pub use api_keys::{ApiKeyRecord, ApiKeyStore, MintedKey};
pub use auth::{
AuthConfig, AuthLayerState, AuthStartupError, OriginLayerState, apply_bearer_auth,
apply_origin_gate,
};
#[derive(Clone)]
pub struct ServerRuntime {
index: SharedIndex,
protocol: Arc<SymForgeServer>,
governor: Arc<RequestGovernor>,
auth: AuthConfig,
ledger_store: Option<StelLedgerStore>,
key_store: Option<Arc<ApiKeyStore>>,
started_at: std::time::Instant,
project_name: String,
}
impl ServerRuntime {
pub fn build_runtime(
index: SharedIndex,
protocol: Arc<SymForgeServer>,
governor: Arc<RequestGovernor>,
auth: AuthConfig,
ledger_store: Option<StelLedgerStore>,
) -> Self {
let project_name = protocol.project_name.clone();
Self {
index,
protocol,
governor,
auth,
ledger_store,
key_store: None,
started_at: std::time::Instant::now(),
project_name,
}
}
pub fn with_key_store(mut self, key_store: Arc<ApiKeyStore>) -> Self {
self.key_store = Some(key_store);
self
}
pub fn key_store(&self) -> Option<&Arc<ApiKeyStore>> {
self.key_store.as_ref()
}
pub fn uptime(&self) -> std::time::Duration {
self.started_at.elapsed()
}
pub fn project_name(&self) -> &str {
&self.project_name
}
pub fn index(&self) -> &SharedIndex {
&self.index
}
pub fn protocol(&self) -> &Arc<SymForgeServer> {
&self.protocol
}
pub fn governor(&self) -> &Arc<RequestGovernor> {
&self.governor
}
pub fn auth(&self) -> &AuthConfig {
&self.auth
}
pub fn ledger_store(&self) -> Option<&StelLedgerStore> {
self.ledger_store.as_ref()
}
pub async fn dispatch_tool_call(
&self,
tool_name: &str,
params: serde_json::Value,
) -> Result<rmcp::model::CallToolResult, rmcp::ErrorData> {
self.protocol
.dispatch_tool_result_for_tests(tool_name, params)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::live_index::LiveIndex;
use crate::watcher::WatcherInfo;
use parking_lot::Mutex;
fn build_test_runtime(auth: AuthConfig) -> ServerRuntime {
let index = LiveIndex::empty();
let watcher_info = Arc::new(Mutex::new(WatcherInfo::default()));
let protocol = Arc::new(SymForgeServer::new(
Arc::clone(&index),
"test_project".to_string(),
watcher_info,
None,
None,
));
let governor = Arc::new(RequestGovernor::new());
ServerRuntime::build_runtime(index, protocol, governor, auth, None)
}
#[test]
fn build_runtime_owns_state() {
let runtime = build_test_runtime(AuthConfig::new(Some("k".to_string())));
assert!(runtime.auth().requires_auth(true));
assert!(runtime.ledger_store().is_none());
assert!(runtime.governor().max_concurrency() > 0);
let _ = runtime.index().published_state();
}
#[tokio::test]
async fn dispatch_tool_call_returns_parity_result_shape() {
let runtime = build_test_runtime(AuthConfig::new(None));
let result = runtime
.dispatch_tool_call("status", serde_json::json!({}))
.await
.expect("status dispatch returns a CallToolResult");
assert!(
!result.content.is_empty(),
"status result should carry content (parity with stdio dispatch)"
);
}
#[tokio::test]
async fn dispatch_unknown_tool_returns_structured_invalid_request() {
let runtime = build_test_runtime(AuthConfig::new(None));
let result = runtime
.dispatch_tool_call("definitely_not_a_tool", serde_json::json!({}))
.await
.expect("unknown tool yields a statused result, not an Err");
assert!(!result.content.is_empty());
}
}