Skip to content

mcp_tools() fails with "system error 232" on Windows when env is set in MCP server config #62

@selesnow

Description

@selesnow

Description:
When using mcp_tools(config = "...") in ellmer with an MCP server configuration that includes an env block, the connection immediately fails on Windows with the following error:

Error in `process_write_input(self, private, str, sep)`:
! Native call to `processx_connection_write_bytes` failed
Caused by error in `chain_call(c_processx_connection_write_bytes, con, str)`:
! Cannot write connection (system error 232, Идет закрытие канала.
) @processx-connection.c:977 (processx_c_connection_write_bytes)
Type .Last.error to see the more details.

If the same configuration is used without the env key, everything works as expected.


Steps to reproduce:

  1. Create a minimal R MCP server script, e.g. create_pf_mcp_server.R.
  2. Add it to the MCP config with env:
{
  "mcpServers": {
    "netpeak_pfworker": {
      "command": "Rscript",
      "args": ["-e", "source('C:/Users/User/Documents/mcp_servers/create_pf_mcp_server.R')"],
      "env": {
        "PFW_TOKEN": "00000000111111111xxxx"
      }
    }
  }
}
  1. Call:
library(ellmer)
library(mcptools)

chat <- ellmer::chat_google_gemini(system_prompt = "Test")
chat$set_tools(mcptools::mcp_tools(config = "C:/path/config.json"))
  1. Observe the error above.

  2. Modify the config to remove env:

{
  "mcpServers": {
    "netpeak_pfworker": {
      "command": "Rscript",
      "args": ["-e", "source('C:/Users/User/Documents/mcp_servers/create_pf_mcp_server.R')"]
    }
  }
}
  1. Run again — works fine.

Expected behavior:
Passing env should merge environment variables with the current process environment (as is common on other platforms), not completely replace them.

Actual behavior:
On Windows, processx appears to replace the entire environment, which causes the Rscript process to fail almost immediately, breaking the MCP connection.


System info:

other attached packages:
[1] ellmer_0.3.0        mcptools_0.1.0.9000

loaded via a namespace (and not attached):
 [1] coro_1.1.0        R6_2.5.1          magrittr_2.0.3    rappdirs_0.3.3    glue_1.8.0        
 [6] lifecycle_1.0.4   ps_1.8.1          cli_3.6.3         S7_0.2.0          processx_3.8.4    
[11] compiler_4.4.2    rstudioapi_0.17.1 tools_4.4.2       httr2_1.2.1       rlang_1.1.4       
[16] jsonlite_1.8.9

Windows 10/11
R 4.x


Notes:
This looks like a known processx behavior on Windows where env fully overrides the environment instead of merging it. If this is intentional, mcp_tools() might need to explicitly merge the provided env with the current Sys.getenv() before spawning the process, to ensure required variables like PATH and R_HOME are preserved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions