Skip to content

Commit cfe4570

Browse files
committed
fix(builds): remove unused dependencies and optimize build files to reduce final binary size by more than 50%
1 parent 6def492 commit cfe4570

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

Cargo.toml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
rmcp = { path = "modules/mcp/crates/rmcp", features = ["server", "client", "transport-sse-client-reqwest", "transport-streamable-http-client-reqwest", "reqwest"] }
8-
tokio = { version = "1", features = ["full"] }
9-
serde_json = "1.0"
10-
reqwest = { version = "0.12", features = ["json"] }
11-
lazy_static = "1.4"
7+
rmcp = { path = "modules/mcp/crates/rmcp", features = ["client", "transport-sse-client-reqwest", "transport-streamable-http-client-reqwest"] }
8+
tokio = { version = "1", features = ["rt-multi-thread", "sync", "time", "macros"], default-features = false }
9+
serde_json = { version = "1.0", default-features = false, features = ["std"] }
10+
reqwest = { version = "0.12", features = ["json", "rustls-tls"], default-features = false }
1211

1312
[lib]
1413
name = "mcp_ffi"
1514
crate-type = ["staticlib"]
1615

1716
[profile.release]
18-
opt-level = 3
19-
lto = true
17+
opt-level = "z"
18+
lto = "fat"
19+
codegen-units = 1
20+
panic = "abort"
21+
strip = true
22+
debug = false

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ MAKEFLAGS += -j$(CPUS)
3232

3333
# Compiler and flags
3434
CC = gcc
35-
CARGO_ENV = CARGO_TARGET_DIR=$(RUST_TARGET_DIR)
35+
CARGO_ENV = CARGO_TARGET_DIR=$(RUST_TARGET_DIR) RUSTFLAGS="-C opt-level=z -C lto=fat -C codegen-units=1 -C strip=symbols"
3636
ifeq ($(PLATFORM),android)
3737
OPENSSL_INSTALL_DIR = $(BUILD_DIR)/openssl/$(PLATFORM)/$(ARCH)
3838
CARGO_ENV += OPENSSL_DIR=$(CURDIR)/$(OPENSSL_INSTALL_DIR)
3939
endif
4040
CARGO = $(CARGO_ENV) cargo
41-
CFLAGS = -Wall -Wextra -Wno-unused-parameter -I$(SRC_DIR) -I$(LIBS_DIR)
41+
CFLAGS = -Wall -Wextra -Wno-unused-parameter -I$(SRC_DIR) -I$(LIBS_DIR) -Os -ffunction-sections -fdata-sections
4242

4343
# Directories
4444
SRC_DIR = src
@@ -50,7 +50,7 @@ VPATH = $(SRC_DIR)
5050

5151
# Rust FFI library
5252
MCP_FFI_LIB = $(RUST_TARGET_DIR)/release/libmcp_ffi.a
53-
LDFLAGS = -L$(RUST_TARGET_DIR)/release
53+
LDFLAGS = -L$(RUST_TARGET_DIR)/release -Wl,-dead_strip
5454

5555
# Platform-specific settings
5656
ifeq ($(PLATFORM),windows)
@@ -74,7 +74,7 @@ else ifeq ($(PLATFORM),macos)
7474
CFLAGS += -mmacosx-version-min=$(MACOS_MIN_VERSION)
7575
CARGO_ENV += MACOSX_DEPLOYMENT_TARGET=$(MACOS_MIN_VERSION)
7676
CARGO = $(CARGO_ENV) cargo
77-
STRIP = strip -x -S $@
77+
STRIP = strip -x -S -r $@
7878
LIBS = -lmcp_ffi -framework CoreFoundation -framework Security -lresolv
7979
T_LIBS = -lpthread -ldl -lm
8080
else ifeq ($(PLATFORM),android)

src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
use std::ffi::{CStr, CString};
99
use std::os::raw::c_char;
1010
use std::ptr;
11-
use std::sync::Mutex;
11+
use std::sync::{Mutex, OnceLock};
1212

1313
use rmcp::transport::{SseClientTransport, StreamableHttpClientTransport};
1414
use rmcp::{ServiceExt, RoleClient};
1515
use rmcp::model::{ClientInfo, ClientCapabilities, Implementation};
1616

1717
// Global client instance (simplified approach - one client per process)
18-
lazy_static::lazy_static! {
19-
static ref GLOBAL_CLIENT: Mutex<Option<McpClient>> = Mutex::new(None);
20-
}
18+
static GLOBAL_CLIENT: OnceLock<Mutex<Option<McpClient>>> = OnceLock::new();
2119

2220
/// Initialize the MCP library
2321
/// Returns 0 on success, non-zero on error
@@ -345,8 +343,8 @@ pub extern "C" fn mcp_connect(
345343
*new_client.server_url.lock().unwrap() = Some(url);
346344

347345
// Store the client globally
348-
let mut global_client = GLOBAL_CLIENT.lock().unwrap();
349-
*global_client = Some(new_client);
346+
let global_client = GLOBAL_CLIENT.get_or_init(|| Mutex::new(None));
347+
*global_client.lock().unwrap() = Some(new_client);
350348
}
351349

352350
match CString::new(result) {
@@ -360,8 +358,9 @@ pub extern "C" fn mcp_connect(
360358
#[no_mangle]
361359
pub extern "C" fn mcp_list_tools(_client_ptr: *mut McpClient) -> *mut c_char {
362360
// Get global client
363-
let global_client_guard = GLOBAL_CLIENT.lock().unwrap();
364-
let client = match global_client_guard.as_ref() {
361+
let global_client_guard = GLOBAL_CLIENT.get()
362+
.and_then(|c| Some(c.lock().unwrap()));
363+
let client = match global_client_guard.as_ref().and_then(|g| g.as_ref()) {
365364
Some(c) => c,
366365
None => {
367366
let error = r#"{"error": "Not connected. Call mcp_connect() first"}"#;
@@ -453,8 +452,9 @@ pub extern "C" fn mcp_call_tool(
453452
};
454453

455454
// Get global client
456-
let global_client_guard = GLOBAL_CLIENT.lock().unwrap();
457-
let client = match global_client_guard.as_ref() {
455+
let global_client_guard = GLOBAL_CLIENT.get()
456+
.and_then(|c| Some(c.lock().unwrap()));
457+
let client = match global_client_guard.as_ref().and_then(|g| g.as_ref()) {
458458
Some(c) => c,
459459
None => {
460460
let error = r#"{"error": "Not connected. Call mcp_connect() first"}"#;

0 commit comments

Comments
 (0)