Skip to content

Commit f7a9a58

Browse files
committed
port 6715651 to stdio-only client implementation
1 parent e8445e2 commit f7a9a58

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

R/client.R

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,8 @@ mcp_tools <- function(config = NULL) {
132132
for (i in seq_along(config)) {
133133
config_i <- config[[i]]
134134
name_i <- names(config)[i]
135-
config_i_env <- if ("env" %in% names(config_i)) {
136-
unlist(config_i$env)
137-
} else {
138-
NULL
139-
}
140-
141-
process <- processx::process$new(
142-
# seems like the R process has a different PATH than process_exec
143-
command = Sys.which(config_i$command),
144-
args = config_i$args,
145-
env = config_i_env,
146-
stdin = "|",
147-
stdout = "|",
148-
stderr = "|"
149-
)
150135

151-
the$server_processes <- c(
152-
the$server_processes,
153-
list2(
154-
!!paste0(c(config_i$command, config_i$args), collapse = " ") := process
155-
)
156-
)
157-
158-
add_mcp_server(process = process, name = name_i)
136+
add_mcp_server(config = config_i, name = name_i)
159137
}
160138

161139
servers_as_ellmer_tools()
@@ -213,7 +191,6 @@ read_mcp_config <- function(config, call = caller_env()) {
213191
config$mcpServers
214192
}
215193

216-
217194
error_no_mcp_config <- function(call) {
218195
cli::cli_abort(
219196
c(
@@ -225,13 +202,64 @@ error_no_mcp_config <- function(call) {
225202
)
226203
}
227204

228-
add_mcp_server <- function(process, name) {
229-
response_initialize <- send_and_receive(process, mcp_request_initialize())
230-
send_and_receive(process, mcp_request_initialized())
231-
response_tools_list <- send_and_receive(process, mcp_request_tools_list())
205+
add_mcp_server <- function(config, name, call = caller_env()) {
206+
config_env <- if ("env" %in% names(config)) {
207+
unlist(config$env)
208+
} else {
209+
NULL
210+
}
211+
212+
process <- processx::process$new(
213+
command = Sys.which(config$command),
214+
args = config$args %||% character(),
215+
env = config_env,
216+
stdin = "|",
217+
stdout = "|",
218+
stderr = "|"
219+
)
220+
221+
the$server_processes <- c(
222+
the$server_processes,
223+
list2(
224+
!!paste0(
225+
c(config$command, config$args %||% ""),
226+
collapse = " "
227+
) := process
228+
)
229+
)
230+
231+
# Fail gracefully if the process failed on startup (#82)
232+
tryCatch(
233+
{
234+
response_initialize <- send_and_receive(
235+
process,
236+
mcp_request_initialize()
237+
)
238+
239+
send_and_receive(process, mcp_request_initialized())
240+
response_tools_list <- send_and_receive(
241+
process,
242+
mcp_request_tools_list()
243+
)
244+
},
245+
error = function(e) {
246+
if (process$get_exit_status() %in% c(1L, 2L)) {
247+
cli::cli_abort(
248+
c(
249+
"The command {.code {config$command}} failed with the following error:",
250+
"x" = "{paste0(process$read_all_error_lines(), collapse = '. ')}"
251+
),
252+
call = call
253+
)
254+
}
255+
256+
cnd_signal(e)
257+
}
258+
)
232259

233260
the$mcp_servers[[name]] <- list(
234261
name = name,
262+
type = "stdio",
235263
process = process,
236264
tools = response_tools_list$result,
237265
id = 3

0 commit comments

Comments
 (0)