дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +02:00
In remote development (SSH, WSL, Dev Containers) the built-in IDE MCP server ran only in the desktop shell: openidea-server never started it or answered --mcp-stdio, MCP status and confirmation commands ran locally, and the Settings window showed the desktop's mcpServer settings for a remote window. McpService and the MCP commands move into ide-host, so openidea-server runs its own MCP server per host with a stable ~/.openidea/bin/openidea-server --mcp-stdio bridge. mcpServer.* settings of a remote window apply to the remote host only. Remote MCP confirmation requests show only when this machine trusts the remote project, and their answers route back to the server. Several servers sharing one data directory (several remote windows on a host) each record a discovery entry; the stdio bridge picks the one that has the project open by probing tools/list, and a server leaving promotes a live one. Review follow-ups: the bridge no longer matches the initialize instructions text, a starting server drops entries of servers that stopped listening, the stdio link is written only when the MCP server starts, the MCP settings page reloads its status after Apply (a remote server's settings events reach the project window only), and the unused process-wide MCP executable in ide-agent is gone. Co-authored-by: dengliu <[email protected]>
139 рядки
5.8 KiB
Rust
139 рядки
5.8 KiB
Rust
//! `openidea-server --stdio`: the IDE backend for remote development, serving
|
|
//! one desktop client over stdin / stdout (started by the client over SSH).
|
|
|
|
use ide_host::catalogue::TrustClass;
|
|
use ide_host::plugins::remote::{RemotePluginLanguages, SYNC_COMMAND};
|
|
use ide_host::plugins::remote_host::RemotePlugins;
|
|
use ide_host::rpc::{self, Dispatcher};
|
|
use ide_host::{Host, HostConfig};
|
|
use ide_language::LanguageRegistry;
|
|
use std::path::PathBuf;
|
|
use std::sync::{Arc, OnceLock};
|
|
|
|
fn main() {
|
|
let raw_args: Vec<String> = std::env::args().collect();
|
|
if let Some(project) = ide_mcp::stdio_request(&raw_args) {
|
|
let base = server_home();
|
|
std::process::exit(ide_mcp::stdio::run(&ide_mcp::discovery_path(&base.join("data")), project));
|
|
}
|
|
let args: Vec<String> = raw_args.into_iter().skip(1).collect();
|
|
if args.iter().any(|a| a == "--version") {
|
|
println!("openidea-server {} (protocol {})", env!("CARGO_PKG_VERSION"), rpc::PROTOCOL_VERSION);
|
|
return;
|
|
}
|
|
if args.first().is_some_and(|a| a == "--connect") {
|
|
// One forwarded connection (see `ide_host::relay`): stdout carries its bytes.
|
|
let code = match ide_host::relay::parse_args(&args[1..]).and_then(|(host, port)| {
|
|
ide_host::relay::relay(&host, port, std::io::stdin(), std::io::stdout().lock())
|
|
.map_err(|e| format!("cannot connect to {host}:{port}: {e}"))
|
|
}) {
|
|
Ok(()) => 0,
|
|
Err(e) => {
|
|
eprintln!("{e}");
|
|
1
|
|
}
|
|
};
|
|
std::process::exit(code);
|
|
}
|
|
if !args.iter().any(|a| a == "--stdio") {
|
|
eprintln!(
|
|
"usage: openidea-server --stdio | --mcp-stdio [--project <path>] | --connect <host> <port> | --version"
|
|
);
|
|
std::process::exit(2);
|
|
}
|
|
// stdout carries the protocol: logs go to stderr.
|
|
tracing_subscriber::fmt()
|
|
.with_writer(std::io::stderr)
|
|
.with_env_filter(tracing_subscriber::EnvFilter::try_from_env("OPENIDEA_LOG").unwrap_or_else(|_| "info".into()))
|
|
.init();
|
|
let base = server_home();
|
|
// An SSH exec channel gets a minimal environment: take the login shell's, read
|
|
// afresh on every start, plus the IDE-managed tools. Before any other thread starts.
|
|
ide_host::shell_env::inherit_login_shell_env("openidea-server");
|
|
let data_dir = base.join("data");
|
|
ide_tools::extend_process_path(&data_dir);
|
|
ide_dap::adapter::set_tools_root(ide_tools::tools_dir(&data_dir));
|
|
ide_sdk::java::add_jdk_root(&ide_tools::jdk::jdks_dir(&data_dir));
|
|
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().expect("tokio runtime");
|
|
let code = runtime.block_on(run(base));
|
|
std::process::exit(code);
|
|
}
|
|
|
|
fn server_home() -> PathBuf {
|
|
let home = dirs::home_dir().unwrap_or_default();
|
|
std::env::var_os("OPENIDEA_SERVER_HOME").map(PathBuf::from).unwrap_or_else(|| home.join(".openidea"))
|
|
}
|
|
|
|
async fn run(base: PathBuf) -> i32 {
|
|
let (config_dir, data_dir) = (base.join("config"), base.join("data"));
|
|
for dir in [&config_dir, &data_dir] {
|
|
if let Err(e) = std::fs::create_dir_all(dir) {
|
|
eprintln!("cannot create {}: {e}", dir.display());
|
|
return 1;
|
|
}
|
|
}
|
|
let languages = match LanguageRegistry::load(Some(&config_dir.join("languages"))) {
|
|
Ok(l) => Arc::new(l),
|
|
Err(e) => {
|
|
eprintln!("language definitions: {e}");
|
|
return 1;
|
|
}
|
|
};
|
|
// The desktop's plugin languages (`plugin_sync_languages`); programs pinned on this machine.
|
|
let plugin_languages = RemotePluginLanguages::new(Some(&data_dir));
|
|
languages.attach_plugins(plugin_languages.store().clone());
|
|
let peer = Arc::new(OnceLock::new());
|
|
let sink_peer = peer.clone();
|
|
let plugins_data = data_dir.clone();
|
|
let host = Arc::new(Host::new(HostConfig {
|
|
config_dir,
|
|
local_data_dir: data_dir.clone(),
|
|
data_dir,
|
|
languages,
|
|
syntax: Arc::default(),
|
|
sink_for: Box::new(move |window| rpc::event_sink(sink_peer.clone(), Some(window.to_owned()))),
|
|
owner: |w| w,
|
|
runtime: tokio::runtime::Handle::current(),
|
|
project_environment: true,
|
|
}));
|
|
// A stable path for `--mcp-stdio` client configurations across server upgrades.
|
|
host.mcp.set_stdio_link(base.join("bin").join("openidea-server"));
|
|
host.mcp.apply(&host);
|
|
// The desktop's code plugins run here, next to the project (`plugin_sync_packages`).
|
|
let plugins = match RemotePlugins::start(&plugins_data, &host, peer.clone()) {
|
|
Ok(p) => Some(p),
|
|
Err(e) => {
|
|
tracing::warn!("plugins are unavailable: {e}");
|
|
None
|
|
}
|
|
};
|
|
if let Some(root) = std::env::var_os(ide_host::trust::TRUSTED_ROOT_ENV)
|
|
&& let Err(e) = host.trust.inherit(std::path::Path::new(&root))
|
|
{
|
|
tracing::warn!("cannot trust {}: {e}", root.to_string_lossy());
|
|
}
|
|
tracing::info!("openidea-server {} ready", env!("CARGO_PKG_VERSION"));
|
|
let mut dispatcher = Dispatcher::standard();
|
|
if let Some(plugins) = &plugins {
|
|
plugins.register(&mut dispatcher);
|
|
}
|
|
dispatcher.add(SYNC_COMMAND, TrustClass::Trusted, move |c| {
|
|
let remote = plugin_languages.clone();
|
|
async move {
|
|
let report = remote.sync(c.arg("languages")?);
|
|
c.host.plugin_languages_changed();
|
|
Ok(report)
|
|
}
|
|
});
|
|
rpc::serve(host.clone(), dispatcher, peer, tokio::io::stdin(), tokio::io::stdout()).await;
|
|
host.mcp.stop(&host);
|
|
if let Some(plugins) = &plugins {
|
|
plugins.host().shutdown();
|
|
}
|
|
// `process::exit` skips destructors: nothing started for the client may outlive it.
|
|
if tokio::time::timeout(std::time::Duration::from_secs(10), host.shutdown()).await.is_err() {
|
|
tracing::warn!("language servers did not shut down in time");
|
|
}
|
|
0
|
|
}
|