Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat: add /v1/orchestrations for server-side MCP tool execution (#7800)

Claytond469c2e38c7e
diff --git a/src-tauri/plugins/tauri-plugin-foundation-models/permissions/schemas/schema.json b/src-tauri/plugins/tauri-plugin-foundation-models/permissions/schemas/schema.json
new file mode 100644
index 000000000..f0bcf4093
--- /dev/null
+++ b/src-tauri/plugins/tauri-plugin-foundation-models/permissions/schemas/schema.json
@@ -0,0 +1,402 @@
+{
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "title": "PermissionFile",
+ "description": "Permission file that can define a default permission, a set of permissions or a list of inlined permissions.",
+ "type": "object",
+ "properties": {
+ "default": {
+ "description": "The default permission set for the plugin",
+ "anyOf": [
+ {
+ "$ref": "#/definitions/DefaultPermission"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "set": {
+ "description": "A list of permissions sets defined",
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/PermissionSet"
+ }
+ },
+ "permission": {
+ "description": "A list of inlined permissions",
+ "default": [],
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/Permission"
+ }
+ }
+ },
+ "definitions": {
+ "DefaultPermission": {
+ "description": "The default permission set of the plugin.\n\nWorks similarly to a permission with the \"default\" identifier.",
+ "type": "object",
+ "required": [
+ "permissions"
+ ],
+ "properties": {
+ "version": {
+ "description": "The version of the permission.",
+ "type": [
+ "integer",
+ "null"
+ ],
+ "format": "uint64",
+ "minimum": 1.0
+ },
+ "description": {
+ "description": "Human-readable description of what the permission does. Tauri convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
+ "type": [
+ "string",
+ "null"
+ ]
+ },
+ "permissions": {
+ "description": "All permissions this set contains.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "PermissionSet": {
+ "description": "A set of direct permissions grouped together under a new name.",
+ "type": "object",
+ "required": [
+ "description",
+ "identifier",
+ "permissions"
+ ],
+ "properties": {
+ "identifier": {
+ "description": "A unique identifier for the permission.",
+ "type": "string"
+ },
+ "description": {
+ "description": "Human-readable description of what the permission does.",
+ "type": "string"
+ },
+ "permissions": {
+ "description": "All permissions this set contains.",
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/PermissionKind"
+ }
+ }
+ }
+ },
+ "Permission": {
+ "description": "Descriptions of explicit privileges of commands.\n\nIt can enable commands to be accessible in the frontend of the application.\n\nIf the scope is defined it can be used to fine grain control the access of individual or multiple commands.",
+ "type": "object",
+ "required": [
+ "identifier"
+ ],
+ "properties": {
+ "version": {
+ "description": "The version of the permission.",
+ "type": [
+ "integer",
+ "null"
+ ],
+ "format": "uint64",
+ "minimum": 1.0
+ },
+ "identifier": {
+ "description": "A unique identifier for the permission.",
+ "type": "string"
+ },
+ "description": {
+ "description": "Human-readable description of what the permission does. Tauri internal convention is to use `<h4>` headings in markdown content for Tauri documentation generation purposes.",
+ "type": [
+ "string",
+ "null"
+ ]
+ },
+ "commands": {
+ "description": "Allowed or denied commands when using this permission.",
+ "default": {
+ "allow": [],
+ "deny": []
+ },
+ "allOf": [
+ {
+ "$ref": "#/definitions/Commands"
+ }
+ ]
+ },
+ "scope": {
+ "description": "Allowed or denied scoped when using this permission.",
+ "allOf": [
+ {
+ "$ref": "#/definitions/Scopes"
+ }
+ ]
+ },
+ "platforms": {
+ "description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
+ "type": [
+ "array",
+ "null"
+ ],
+ "items": {
+ "$ref": "#/definitions/Target"
+ }
+ }
+ }
+ },
+ "Commands": {
+ "description": "Allowed and denied commands inside a permission.\n\nIf two commands clash inside of `allow` and `deny`, it should be denied by default.",
+ "type": "object",
+ "properties": {
+ "allow": {
+ "description": "Allowed command.",
+ "default": [],
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ },
+ "deny": {
+ "description": "Denied command, which takes priority.",
+ "default": [],
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "Scopes": {
+ "description": "An argument for fine grained behavior control of Tauri commands.\n\nIt can be of any serde serializable type and is used to allow or prevent certain actions inside a Tauri command. The configured scope is passed to the command and will be enforced by the command implementation.\n\n## Example\n\n```json { \"allow\": [{ \"path\": \"$HOME/**\" }], \"deny\": [{ \"path\": \"$HOME/secret.txt\" }] } ```",
+ "type": "object",
+ "properties": {
+ "allow": {
+ "description": "Data that defines what is allowed by the scope.",
+ "type": [
+ "array",
+ "null"
+ ],
+ "items": {
+ "$ref": "#/definitions/Value"
+ }
+ },
+ "deny": {
+ "description": "Data that defines what is denied by the scope. This should be prioritized by validation logic.",
+ "type": [
+ "array",
+ "null"
+ ],
+ "items": {
+ "$ref": "#/definitions/Value"
+ }
+ }
+ }
+ },
+ "Value": {
+ "description": "All supported ACL values.",
+ "anyOf": [
+ {
+ "description": "Represents a null JSON value.",
+ "type": "null"
+ },
+ {
+ "description": "Represents a [`bool`].",
+ "type": "boolean"
+ },
+ {
+ "description": "Represents a valid ACL [`Number`].",
+ "allOf": [
+ {
+ "$ref": "#/definitions/Number"
+ }
+ ]
+ },
+ {
+ "description": "Represents a [`String`].",
+ "type": "string"
+ },
+ {
+ "description": "Represents a list of other [`Value`]s.",
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/Value"
+ }
+ },
+ {
+ "description": "Represents a map of [`String`] keys to [`Value`]s.",
+ "type": "object",
+ "additionalProperties": {
+ "$ref": "#/definitions/Value"
+ }
+ }
+ ]
+ },
+ "Number": {
+ "description": "A valid ACL number.",
+ "anyOf": [
+ {
+ "description": "Represents an [`i64`].",
+ "type": "integer",
+ "format": "int64"
+ },
+ {
+ "description": "Represents a [`f64`].",
+ "type": "number",
+ "format": "double"
+ }
+ ]
+ },
+ "Target": {
+ "description": "Platform target.",
+ "oneOf": [
+ {
+ "description": "MacOS.",
+ "type": "string",
+ "enum": [
+ "macOS"
+ ]
+ },
+ {
+ "description": "Windows.",
+ "type": "string",
+ "enum": [
+ "windows"
+ ]
+ },
+ {
+ "description": "Linux.",
+ "type": "string",
+ "enum": [
+ "linux"
+ ]
+ },
+ {
+ "description": "Android.",
+ "type": "string",
+ "enum": [
+ "android"
+ ]
+ },
+ {
+ "description": "iOS.",
+ "type": "string",
+ "enum": [
+ "iOS"
+ ]
+ }
+ ]
+ },
+ "PermissionKind": {
+ "type": "string",
+ "oneOf": [
+ {
+ "description": "Enables the abort_foundation_models_stream command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-abort-foundation-models-stream",
+ "markdownDescription": "Enables the abort_foundation_models_stream command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the abort_foundation_models_stream command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-abort-foundation-models-stream",
+ "markdownDescription": "Denies the abort_foundation_models_stream command without any pre-configured scope."
+ },
+ {
+ "description": "Enables the check_foundation_models_availability command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-check-foundation-models-availability",
+ "markdownDescription": "Enables the check_foundation_models_availability command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the check_foundation_models_availability command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-check-foundation-models-availability",
+ "markdownDescription": "Denies the check_foundation_models_availability command without any pre-configured scope."
+ },
+ {
+ "description": "Enables the cleanup_foundation_models_processes command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-cleanup-foundation-models-processes",
+ "markdownDescription": "Enables the cleanup_foundation_models_processes command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the cleanup_foundation_models_processes command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-cleanup-foundation-models-processes",
+ "markdownDescription": "Denies the cleanup_foundation_models_processes command without any pre-configured scope."
+ },
+ {
+ "description": "Enables the foundation_models_chat_completion command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-foundation-models-chat-completion",
+ "markdownDescription": "Enables the foundation_models_chat_completion command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the foundation_models_chat_completion command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-foundation-models-chat-completion",
+ "markdownDescription": "Denies the foundation_models_chat_completion command without any pre-configured scope."
+ },
+ {
+ "description": "Enables the foundation_models_chat_completion_stream command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-foundation-models-chat-completion-stream",
+ "markdownDescription": "Enables the foundation_models_chat_completion_stream command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the foundation_models_chat_completion_stream command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-foundation-models-chat-completion-stream",
+ "markdownDescription": "Denies the foundation_models_chat_completion_stream command without any pre-configured scope."
+ },
+ {
+ "description": "Enables the is_foundation_models_loaded command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-is-foundation-models-loaded",
+ "markdownDescription": "Enables the is_foundation_models_loaded command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the is_foundation_models_loaded command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-is-foundation-models-loaded",
+ "markdownDescription": "Denies the is_foundation_models_loaded command without any pre-configured scope."
+ },
+ {
+ "description": "Enables the load_foundation_models command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-load-foundation-models",
+ "markdownDescription": "Enables the load_foundation_models command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the load_foundation_models command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-load-foundation-models",
+ "markdownDescription": "Denies the load_foundation_models command without any pre-configured scope."
+ },
+ {
+ "description": "Enables the unload_foundation_models command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-unload-foundation-models",
+ "markdownDescription": "Enables the unload_foundation_models command without any pre-configured scope."
+ },
+ {
+ "description": "Denies the unload_foundation_models command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-unload-foundation-models",
+ "markdownDescription": "Denies the unload_foundation_models command without any pre-configured scope."
+ },
+ {
+ "description": "Default permissions for the Foundation Models plugin\n#### This default permission set includes:\n\n- `allow-cleanup-foundation-models-processes`\n- `allow-check-foundation-models-availability`\n- `allow-load-foundation-models`\n- `allow-unload-foundation-models`\n- `allow-is-foundation-models-loaded`\n- `allow-foundation-models-chat-completion`\n- `allow-foundation-models-chat-completion-stream`\n- `allow-abort-foundation-models-stream`",
+ "type": "string",
+ "const": "default",
+ "markdownDescription": "Default permissions for the Foundation Models plugin\n#### This default permission set includes:\n\n- `allow-cleanup-foundation-models-processes`\n- `allow-check-foundation-models-availability`\n- `allow-load-foundation-models`\n- `allow-unload-foundation-models`\n- `allow-is-foundation-models-loaded`\n- `allow-foundation-models-chat-completion`\n- `allow-foundation-models-chat-completion-stream`\n- `allow-abort-foundation-models-stream`"
+ }
+ ]
+ }
+ }
+}
\ No newline at end of file
diff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rs
index 64a07d929..2a1b42a60 100644
--- a/src-tauri/src/core/cli/mod.rs
+++ b/src-tauri/src/core/cli/mod.rs
@@ -116,6 +116,9 @@ pub async fn cli_start_server(
vec![vec![]],
proxy_timeout,
app_state.provider_configs.clone(),
+ app_state.mcp_servers.clone(),
+ app_state.mcp_settings.clone(),
+ resolve_jan_data_folder().to_string_lossy().into_owned(),
)
.await
.map_err(|e| e.to_string())
diff --git a/src-tauri/src/core/server/commands.rs b/src-tauri/src/core/server/commands.rs
index d5c3d823e..061795b35 100644
--- a/src-tauri/src/core/server/commands.rs
+++ b/src-tauri/src/core/server/commands.rs
@@ -3,6 +3,7 @@ use tauri_plugin_llamacpp::state::LlamacppState;
use tauri_plugin_mlx::state::MlxState;
use crate::core::server::proxy;
+use crate::core::app::commands::get_jan_data_folder_path;
use crate::core::state::AppState;
@@ -48,6 +49,9 @@ pub async fn start_server<R: Runtime>(
vec![trusted_hosts],
proxy_timeout,
state.provider_configs.clone(),
+ state.mcp_servers.clone(),
+ state.mcp_settings.clone(),
+ get_jan_data_folder_path(app_handle.clone()).to_string_lossy().into_owned(),
)
.await
.map_err(|e| e.to_string())?;
diff --git a/src-tauri/src/core/server/proxy.rs b/src-tauri/src/core/server/proxy.rs
index d33ecdf3e..51ea08bc8 100644
--- a/src-tauri/src/core/server/proxy.rs
+++ b/src-tauri/src/core/server/proxy.rs
@@ -7,12 +7,17 @@ use reqwest::Client;
use serde_json;
use std::collections::HashMap;
use std::convert::Infallible;
+use std::fs;
use std::net::SocketAddr;
+use std::path::PathBuf;
use std::sync::Arc;
use tauri_plugin_llamacpp::LLamaBackendSession;
use tokio::sync::Mutex;
-use crate::core::state::{ProviderConfig, ServerHandle};
+use crate::core::{
+ mcp::models::McpSettings,
+ state::{ProviderConfig, ServerHandle, SharedMcpServers},
+};
/// Transform Anthropic /messages API body to OpenAI /chat/completions body
fn transform_anthropic_to_openai(body: &serde_json::Value) -> Option<serde_json::Value> {
@@ -423,6 +428,343 @@ pub fn get_destination_path(original_path: &str, prefix: &str) -> String {
use tauri_plugin_mlx::state::{MlxBackendSession, SessionInfo};
+use rmcp::model::{CallToolRequestParam, CallToolResult};
+
+fn assistant_json_path(jan_data_folder: &str, assistant_id: &str) -> PathBuf {
+ PathBuf::from(jan_data_folder)
+ .join("assistants")
+ .join(assistant_id)
+ .join("assistant.json")
+}
+
+fn load_assistant_config(
+ jan_data_folder: &str,
+ assistant_id: &str,
+) -> Result<(Option<String>, Option<String>), String> {
+ let assistant_path = assistant_json_path(jan_data_folder, assistant_id);
+ let raw = fs::read_to_string(&assistant_path)
+ .map_err(|e| format!("Failed to read assistant.json: {assistant_path:?}: {e}"))?;
+
+ let parsed: serde_json::Value = serde_json::from_str(&raw)
+ .map_err(|e| format!("Invalid assistant.json ({assistant_id}): {e}"))?;
+
+ let instructions = parsed
+ .get("instructions")
+ .and_then(|v| v.as_str())
+ .map(|s| s.to_string());
+
+ let model = parsed
+ .get("model")
+ .and_then(|v| v.as_str())
+ .map(|s| s.to_string());
+
+ Ok((instructions, model))
+}
+
+fn parse_openai_messages(messages: &serde_json::Value) -> Result<Vec<serde_json::Value>, String> {
+ let arr = messages
+ .as_array()
+ .ok_or("Request body must include 'messages' as an array")?;
+
+ let mut out = Vec::with_capacity(arr.len());
+ for msg in arr {
+ let role = msg
+ .get("role")
+ .and_then(|v| v.as_str())
+ .ok_or("Each message must include a string 'role'")?;
+ let content = msg
+ .get("content")
+ .and_then(|v| v.as_str())
+ .ok_or("Each message must include 'content' as a string")?;
+
+ // Keep upstream format minimal and predictable.
+ out.push(serde_json::json!({
+ "role": role,
+ "content": content
+ }));
+ }
+ Ok(out)
+}
+
+fn set_system_prompt(messages: &mut Vec<serde_json::Value>, system_prompt: &str) {
+ messages.retain(|m| m.get("role").and_then(|r| r.as_str()) != Some("system"));
+ messages.insert(
+ 0,
+ serde_json::json!({
+ "role": "system",
+ "content": system_prompt
+ }),
+ );
+}
+
+fn extract_tool_calls(response: &serde_json::Value) -> Vec<serde_json::Value> {
+ response
+ .get("choices")
+ .and_then(|c| c.as_array())
+ .and_then(|choices| choices.first())
+ .and_then(|c| c.get("message"))
+ .and_then(|m| m.get("tool_calls"))
+ .and_then(|tc| tc.as_array())
+ .map(|arr| arr.to_vec())
+ .unwrap_or_default()
+}
+
+fn extract_choice_message(response: &serde_json::Value) -> Option<&serde_json::Value> {
+ response
+ .get("choices")
+ .and_then(|c| c.as_array())
+ .and_then(|choices| choices.first())
+ .and_then(|c| c.get("message"))
+}
+
+fn mcp_call_result_to_string(result: &CallToolResult) -> String {
+ let parts: Vec<String> = result
+ .content
+ .iter()
+ .filter_map(|c| c.as_text())
+ .map(|t| t.text.clone())
+ .collect();
+
+ if result.is_error == Some(true) {
+ if parts.is_empty() {
+ "ERROR".to_string()
+ } else {
+ format!("ERROR: {}", parts.join("\n"))
+ }
+ } else {
+ parts.join("\n")
+ }
+}
+
+async fn resolve_upstream_for_model(
+ model_id: &str,
+ provider_configs: Arc<Mutex<HashMap<String, ProviderConfig>>>,
+ sessions: Arc<Mutex<HashMap<i32, LLamaBackendSession>>>,
+ mlx_sessions: Arc<Mutex<HashMap<i32, MlxBackendSession>>>,
+) -> Result<(String, Option<String>), String> {
+ let destination_path = "/chat/completions";
+
+ // Prefer remote provider if model is registered there.
+ let pc = provider_configs.lock().await;
+ let provider_name = pc
+ .iter()
+ .find(|(_, config)| config.models.iter().any(|m| m == model_id))
+ .map(|(_, config)| config.provider.clone())
+ .or_else(|| {
+ if let Some(sep_pos) = model_id.find('/') {
+ let potential_provider: &str = &model_id[..sep_pos];
+ if pc.contains_key(potential_provider) {
+ return Some(potential_provider.to_string());
+ }
+ }
+ pc.get(model_id).map(|c| c.provider.clone())
+ });
+ drop(pc);
+
+ if let Some(provider) = provider_name {
+ let pc2 = provider_configs.lock().await;
+ if let Some(provider_cfg) = pc2.get(provider.as_str()).cloned() {
+ let api_url = provider_cfg
+ .base_url
+ .clone()
+ .ok_or_else(|| format!("Missing base_url for provider '{provider}'"))?;
+ let url = format!("{}{}", api_url, destination_path);
+ return Ok((url, provider_cfg.api_key.clone()));
+ }
+ }
+
+ // Fall back to local sessions.
+ let sessions_guard = sessions.lock().await;
+ if let Some(session) = sessions_guard.values().find(|s| s.info.model_id == model_id) {
+ let target_port = session.info.port;
+ return Ok((
+ format!("http://127.0.0.1:{target_port}/v1{destination_path}"),
+ Some(session.info.api_key.clone()),
+ ));
+ }
+ drop(sessions_guard);
+
+ let mlx_guard = mlx_sessions.lock().await;
+ if let Some(info) = mlx_guard.values().find(|s| s.info.model_id == model_id) {
+ let target_port = info.info.port;
+ return Ok((
+ format!("http://127.0.0.1:{target_port}/v1{destination_path}"),
+ Some(info.info.api_key.clone()),
+ ));
+ }
+
+ Err(format!("No upstream session found for model '{model_id}'"))
+}
+
+fn copy_optional_chat_params(from: &serde_json::Value, into: &mut serde_json::Map<String, serde_json::Value>) {
+ for key in [
+ "temperature",
+ "top_p",
+ "top_k",
+ "max_tokens",
+ "stop_sequences",
+ "stop",
+ "frequency_penalty",
+ "presence_penalty",
+ ] {
+ if let Some(v) = from.get(key) {
+ into.insert(key.to_string(), v.clone());
+ }
+ }
+}
+
+async fn collect_mcp_openai_tools(
+ mcp_servers: &SharedMcpServers,
+ mcp_settings: &Arc<Mutex<McpSettings>>,
+) -> Result<(Vec<serde_json::Value>, HashMap<String, String>), String> {
+ let timeout_duration = mcp_settings.lock().await.tool_call_timeout_duration();
+ let servers = mcp_servers.lock().await;
+
+ let mut openai_tools = Vec::new();
+ let mut tool_to_server: HashMap<String, String> = HashMap::new();
+
+ for (server_name, service) in servers.iter() {
+ let tools_future = service.list_all_tools();
+ let tools = match tokio::time::timeout(timeout_duration, tools_future).await {
+ Ok(Ok(tools)) => tools,
+ Ok(Err(e)) => {
+ log::warn!("MCP server {} failed to list tools: {}", server_name, e);
+ continue;
+ }
+ Err(_) => {
+ log::warn!(
+ "Listing MCP tools timed out after {} seconds on server {}",
+ timeout_duration.as_secs(),
+ server_name
+ );
+ continue;
+ }
+ };
+
+ for tool in tools {
+ tool_to_server.insert(tool.name.to_string(), server_name.clone());
+
+ // Reuse the same schema conversion as the `get_tools` command.
+ let parameters = serde_json::Value::Object((*tool.input_schema).clone());
+ let description = tool
+ .description
+ .as_ref()
+ .map(|d| d.to_string())
+ .unwrap_or_default();
+
+ openai_tools.push(serde_json::json!({
+ "type": "function",
+ "function": {
+ "name": tool.name,
+ "description": description,
+ "parameters": parameters
+ }
+ }));
+ }
+ }
+
+ Ok((openai_tools, tool_to_server))
+}
+
+async fn execute_mcp_tool_calls(
+ tool_calls: &[serde_json::Value],
+ tool_to_server: &HashMap<String, String>,
+ mcp_servers: &SharedMcpServers,
+ mcp_settings: &Arc<Mutex<McpSettings>>,
+) -> Result<Vec<(String, String)>, String> {
+ let timeout_duration = mcp_settings.lock().await.tool_call_timeout_duration();
+ let servers = mcp_servers.lock().await;
+
+ let mut results = Vec::with_capacity(tool_calls.len());
+
+ for tc in tool_calls {
+ let tool_call_id = tc.get("id").and_then(|v| v.as_str()).unwrap_or("").to_string();
+ let tool_name = tc
+ .get("function")
+ .and_then(|f| f.get("name"))
+ .and_then(|v| v.as_str())
+ .unwrap_or("")
+ .to_string();
+
+ let args_str = tc
+ .get("function")
+ .and_then(|f| f.get("arguments"))
+ .and_then(|v| v.as_str())
+ .unwrap_or("{}");
+
+ let args_value: serde_json::Value = serde_json::from_str(args_str)
+ .unwrap_or_else(|_| serde_json::json!({}));
+
+ let args_map: serde_json::Map<String, serde_json::Value> = if let Some(obj) = args_value.as_object() {
+ obj.clone()
+ } else {
+ serde_json::Map::new()
+ };
+
+ let server_name = tool_to_server
+ .get(&tool_name)
+ .ok_or_else(|| format!("No MCP server registered for tool '{tool_name}'"))?;
+
+ let service = servers
+ .get(server_name)
+ .ok_or_else(|| format!("MCP server '{server_name}' not found in runtime state"))?;
+
+ let tool_call = service.call_tool(CallToolRequestParam {
+ name: tool_name.clone().into(),
+ arguments: Some(args_map),
+ });
+
+ let result = match tokio::time::timeout(timeout_duration, tool_call).await {
+ Ok(call_result) => call_result.map_err(|e| e.to_string()),
+ Err(_) => Err(format!(
+ "Tool call '{tool_name}' timed out after {} seconds",
+ timeout_duration.as_secs()
+ )),
+ };
+
+ let tool_result_string = match result {
+ Ok(res) => mcp_call_result_to_string(&res),
+ Err(e) => format!("ERROR: {e}"),
+ };
+
+ results.push((tool_call_id, tool_result_string));
+ }
+
+ Ok(results)
+}
+
+async fn call_openai_chat_completions(
+ client: &Client,
+ upstream_url: &str,
+ api_key: &Option<String>,
+ body: &serde_json::Value,
+) -> Result<serde_json::Value, String> {
+ let mut req = client
+ .post(upstream_url)
+ .header("Content-Type", "application/json")
+ .header("Accept-Encoding", "identity");
+
+ if let Some(key) = api_key {
+ req = req.header("Authorization", format!("Bearer {key}"));
+ }
+
+ let resp = req
+ .body(body.to_string())
+ .send()
+ .await
+ .map_err(|e| format!("Upstream request failed: {e}"))?;
+
+ let status = resp.status();
+ let text = resp.text().await.map_err(|e| e.to_string())?;
+
+ if !status.is_success() {
+ return Err(format!("Upstream returned HTTP {status}: {text}"));
+ }
+
+ serde_json::from_str::<serde_json::Value>(&text)
+ .map_err(|e| format!("Failed to parse upstream JSON: {e}. Body: {text}"))
+}
+
/// Handles the proxy request logic
async fn proxy_request(
req: Request<Body>,
@@ -431,6 +773,9 @@ async fn proxy_request(
sessions: Arc<Mutex<HashMap<i32, LLamaBackendSession>>>,
mlx_sessions: Arc<Mutex<HashMap<i32, MlxBackendSession>>>,
provider_configs: Arc<Mutex<HashMap<String, ProviderConfig>>>,
+ mcp_servers: SharedMcpServers,
+ mcp_settings: Arc<Mutex<McpSettings>>,
+ jan_data_folder: String,
) -> Result<Response<Body>, hyper::Error> {
if req.method() == hyper::Method::OPTIONS {
log::debug!(
@@ -828,6 +1173,347 @@ async fn proxy_request(
}
}
}
+ (hyper::Method::POST, "/orchestrations") => {
+ // Headless orchestration endpoint:
+ // - Load assistant system prompt + assistant model hint
+ // - Ask the model for tool_calls
+ // - Execute MCP tools server-side
+ // - Feed tool results back and continue until completion
+ log::info!("Handling POST request to {destination_path} for assistant tool orchestration");
+
+ let body_bytes = match hyper::body::to_bytes(body).await {
+ Ok(bytes) => bytes,
+ Err(_) => {
+ let mut error_response =
+ Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response.body(Body::from("Failed to read request body")).unwrap());
+ }
+ };
+
+ let json_body: serde_json::Value = match serde_json::from_slice(&body_bytes) {
+ Ok(v) => v,
+ Err(e) => {
+ let mut error_response = Response::builder()
+ .status(StatusCode::BAD_REQUEST);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response
+ .body(Body::from(format!("Invalid JSON body: {e}")))
+ .unwrap());
+ }
+ };
+
+ let assistant_id = json_body
+ .get("assistant_id")
+ .and_then(|v| v.as_str())
+ .map(str::trim)
+ .filter(|v| !v.is_empty())
+ .map(|v| v.to_string());
+
+ let stream = json_body.get("stream").and_then(|v| v.as_bool()).unwrap_or(false);
+ if stream {
+ let mut error_response = Response::builder()
+ .status(StatusCode::BAD_REQUEST);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response.body(Body::from("stream=true is not supported for /orchestrations")).unwrap());
+ }
+
+ let messages_value = match json_body.get("messages") {
+ Some(v) => v,
+ None => {
+ let mut error_response = Response::builder()
+ .status(StatusCode::BAD_REQUEST);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response
+ .body(Body::from("Missing required field 'messages'"))
+ .unwrap());
+ }
+ };
+
+ let mut conversation_messages = match parse_openai_messages(messages_value) {
+ Ok(msgs) => msgs,
+ Err(e) => {
+ let mut error_response = Response::builder()
+ .status(StatusCode::BAD_REQUEST);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response.body(Body::from(e)).unwrap());
+ }
+ };
+
+ // Load assistant config for system prompt + model hint when assistant_id is provided.
+ let (assistant_instructions, assistant_model_hint) = if let Some(assistant_id) = assistant_id.as_deref() {
+ match load_assistant_config(&jan_data_folder, assistant_id) {
+ Ok(v) => v,
+ Err(e) => {
+ let mut error_response =
+ Response::builder().status(StatusCode::BAD_REQUEST);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response.body(Body::from(e)).unwrap());
+ }
+ }
+ } else {
+ (None, None)
+ };
+
+ if let Some(sys) = assistant_instructions {
+ set_system_prompt(&mut conversation_messages, &sys);
+ }
+
+ // Resolve model to use for orchestration.
+ let model_override = json_body.get("model").and_then(|v| v.as_str());
+ let mut model_id: Option<String> = None;
+
+ if let Some(ov) = model_override {
+ model_id = Some(ov.to_string());
+ } else if let Some(h) = assistant_model_hint {
+ let trimmed = h.trim();
+ if !trimmed.is_empty() && trimmed != "*" {
+ model_id = Some(trimmed.to_string());
+ } else {
+ // Fall back to the first available local session model.
+ let sessions_guard = sessions.lock().await;
+ if let Some(session) = sessions_guard.values().next() {
+ model_id = Some(session.info.model_id.clone());
+ }
+ drop(sessions_guard);
+
+ if model_id.is_none() {
+ let mlx_guard = mlx_sessions.lock().await;
+ model_id = mlx_guard.values().next().map(|s| s.info.model_id.clone());
+ }
+ }
+ } else {
+ // Fall back to the first available local session model.
+ let sessions_guard = sessions.lock().await;
+ if let Some(session) = sessions_guard.values().next() {
+ model_id = Some(session.info.model_id.clone());
+ }
+ drop(sessions_guard);
+
+ if model_id.is_none() {
+ let mlx_guard = mlx_sessions.lock().await;
+ model_id = mlx_guard.values().next().map(|s| s.info.model_id.clone());
+ }
+ }
+
+ let model_id = match model_id {
+ Some(v) => v,
+ None => {
+ let mut error_response = Response::builder()
+ .status(StatusCode::SERVICE_UNAVAILABLE);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response
+ .body(Body::from("No running model sessions available"))
+ .unwrap());
+ }
+ };
+
+ // Tool execution support (MCP only for now).
+ let (openai_tools, tool_to_server) =
+ match collect_mcp_openai_tools(&mcp_servers, &mcp_settings).await {
+ Ok(v) => v,
+ Err(e) => {
+ let mut error_response =
+ Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response.body(Body::from(e)).unwrap());
+ }
+ };
+
+ let (upstream_url, session_api_key) = match resolve_upstream_for_model(
+ &model_id,
+ provider_configs.clone(),
+ sessions.clone(),
+ mlx_sessions.clone(),
+ )
+ .await
+ {
+ Ok(v) => v,
+ Err(e) => {
+ let mut error_response =
+ Response::builder().status(StatusCode::NOT_FOUND);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response.body(Body::from(e)).unwrap());
+ }
+ };
+
+ let max_turns = json_body
+ .get("max_turns")
+ .and_then(|v| v.as_u64())
+ .unwrap_or(8)
+ .clamp(1, 20) as usize;
+
+ let mut last_response: Option<serde_json::Value> = None;
+
+ for _turn in 0..max_turns {
+ // Build upstream request body for each turn so messages are updated.
+ let mut completion_map = serde_json::Map::new();
+ completion_map.insert("model".to_string(), serde_json::json!(model_id));
+ completion_map.insert(
+ "messages".to_string(),
+ serde_json::Value::Array(conversation_messages.clone()),
+ );
+ completion_map.insert("stream".to_string(), serde_json::json!(false));
+ completion_map.insert("tool_choice".to_string(), serde_json::json!("auto"));
+
+ if !openai_tools.is_empty() {
+ completion_map.insert("tools".to_string(), serde_json::Value::Array(openai_tools.clone()));
+ }
+
+ copy_optional_chat_params(&json_body, &mut completion_map);
+
+ let request_value = serde_json::Value::Object(completion_map);
+
+ let completion = match call_openai_chat_completions(
+ &client,
+ &upstream_url,
+ &session_api_key,
+ &request_value,
+ )
+ .await
+ {
+ Ok(v) => v,
+ Err(e) => {
+ let mut error_response =
+ Response::builder().status(StatusCode::BAD_GATEWAY);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response.body(Body::from(e)).unwrap());
+ }
+ };
+
+ let tool_calls = extract_tool_calls(&completion);
+ last_response = Some(completion.clone());
+
+ if tool_calls.is_empty() {
+ let body_str = serde_json::to_string(&completion).unwrap_or_else(|_| "{}".to_string());
+ let mut response_builder = Response::builder()
+ .status(StatusCode::OK)
+ .header(hyper::header::CONTENT_TYPE, "application/json");
+ response_builder = add_cors_headers_with_host_and_origin(
+ response_builder,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(response_builder.body(Body::from(body_str)).unwrap());
+ }
+
+ // Append assistant tool call message, then execute tool calls.
+ if let Some(choice_message) = extract_choice_message(&completion) {
+ let assistant_content = choice_message
+ .get("content")
+ .cloned()
+ .unwrap_or(serde_json::Value::Null);
+ conversation_messages.push(serde_json::json!({
+ "role": "assistant",
+ "content": assistant_content,
+ "tool_calls": tool_calls.clone()
+ }));
+ } else {
+ conversation_messages.push(serde_json::json!({
+ "role": "assistant",
+ "content": serde_json::Value::Null,
+ "tool_calls": tool_calls.clone()
+ }));
+ }
+
+ let tool_results = match execute_mcp_tool_calls(
+ &tool_calls,
+ &tool_to_server,
+ &mcp_servers,
+ &mcp_settings,
+ )
+ .await
+ {
+ Ok(v) => v,
+ Err(e) => {
+ let mut error_response =
+ Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ return Ok(error_response.body(Body::from(e)).unwrap());
+ }
+ };
+
+ for (tool_call_id, result_text) in tool_results {
+ conversation_messages.push(serde_json::json!({
+ "role": "tool",
+ "tool_call_id": tool_call_id,
+ "content": result_text
+ }));
+ }
+ }
+
+ // Safety catch: model kept requesting tools beyond max_turns.
+ let body_json = last_response.unwrap_or_else(|| serde_json::json!({}));
+ let body_str = serde_json::to_string(&body_json).unwrap_or_else(|_| "{}".to_string());
+ let mut error_response = Response::builder().status(StatusCode::UNPROCESSABLE_ENTITY);
+ error_response = add_cors_headers_with_host_and_origin(
+ error_response,
+ &host_header,
+ &origin_header,
+ &config.trusted_hosts,
+ );
+ let payload = format!(
+ "{{\"error\":\"max_turns reached while resolving tool calls\",\"last_response\":{body_str}}}"
+ );
+ let response = error_response.body(Body::from(payload)).unwrap();
+ return Ok(response);
+ }
(hyper::Method::POST, "/chat/completions")
| (hyper::Method::POST, "/completions")
| (hyper::Method::POST, "/embeddings")
@@ -1567,6 +2253,9 @@ pub async fn start_server(
trusted_hosts: Vec<Vec<String>>,
proxy_timeout: u64,
provider_configs: Arc<Mutex<HashMap<String, ProviderConfig>>>,
+ mcp_servers: SharedMcpServers,
+ mcp_settings: Arc<Mutex<McpSettings>>,
+ jan_data_folder: String,
) -> Result<u16, Box<dyn std::error::Error + Send + Sync>> {
start_server_internal(
server_handle,
@@ -1579,6 +2268,9 @@ pub async fn start_server(
trusted_hosts,
proxy_timeout,
provider_configs,
+ mcp_servers,
+ mcp_settings,
+ jan_data_folder,
)
.await
}
@@ -1594,6 +2286,9 @@ async fn start_server_internal(
trusted_hosts: Vec<Vec<String>>,
proxy_timeout: u64,
provider_configs: Arc<Mutex<HashMap<String, ProviderConfig>>>,
+ mcp_servers: SharedMcpServers,
+ mcp_settings: Arc<Mutex<McpSettings>>,
+ jan_data_folder: String,
) -> Result<u16, Box<dyn std::error::Error + Send + Sync>> {
let mut handle_guard = server_handle.lock().await;
if handle_guard.is_some() {
@@ -1624,6 +2319,9 @@ async fn start_server_internal(
let sessions = sessions.clone();
let mlx_sessions = mlx_sessions.clone();
let provider_configs = provider_configs.clone();
+ let mcp_servers = mcp_servers.clone();
+ let mcp_settings = mcp_settings.clone();
+ let jan_data_folder = jan_data_folder.clone();
async move {
Ok::<_, Infallible>(service_fn(move |req| {
@@ -1634,6 +2332,9 @@ async fn start_server_internal(
sessions.clone(),
mlx_sessions.clone(),
provider_configs.clone(),
+ mcp_servers.clone(),
+ mcp_settings.clone(),
+ jan_data_folder.clone(),
)
}))
}
diff --git a/src-tauri/static/openapi.json b/src-tauri/static/openapi.json
index 2d36ff76a..b0660b55a 100644
--- a/src-tauri/static/openapi.json
+++ b/src-tauri/static/openapi.json
@@ -113,6 +113,53 @@
}
}
}
+ },
+ "/orchestrations": {
+ "post": {
+ "summary": "Run headless assistant orchestration with tool execution",
+ "description": "Optionally loads assistant settings by `assistant_id`, executes server-side MCP tool calls, and continues generation until the model returns a final response.",
+ "operationId": "createOrchestration",
+ "tags": ["Inference"],
+ "requestBody": {
+ "required": true,
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "assistant_id": { "type": "string", "description": "Optional assistant id to load system prompt/model hint from" },
+ "project_id": { "type": "string", "nullable": true, "description": "Reserved for future parity; MCP-only execution currently" },
+ "model": { "type": "string", "description": "Optional model override. If omitted, uses assistant's model hint or the first running local model." },
+ "messages": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "role": { "type": "string", "enum": ["system", "user", "assistant"] },
+ "content": { "type": "string" }
+ },
+ "required": ["role", "content"]
+ }
+ },
+ "stream": { "type": "boolean", "default": false, "description": "Streaming is not supported for this endpoint." },
+ "max_turns": { "type": "integer", "default": 8, "description": "Maximum tool-calling turns before giving up." }
+ },
+ "required": ["messages"]
+ }
+ }
+ }
+ },
+ "responses": {
+ "200": {
+ "description": "Completion result",
+ "content": {
+ "application/json": {
+ "schema": { "$ref": "#/components/schemas/ChatCompletionResponseDto" }
+ }
+ }
+ }
+ }
+ }
}
},
"components": {