Skip to contents

mvp_mcp_server() starts a Model Context Protocol (MCP) server whose tools run MVPapp's simulations, sensitivity analyses, NCA and plots, so that an AI assistant (e.g. Claude Desktop or Claude Code) can answer requests such as "simulate a 2-compartment model in 100 NHANES children and give me the plot and the reproducible bundle". mvp_mcp_tools() returns the tools, e.g. to use them with an 'ellmer' chat directly.

Usage

mvp_mcp_tools(
  output_dir = file.path(tempdir(), "mvp-mcp"),
  prompts_path = NULL,
  max_retries = 3
)

mvp_mcp_server(
  output_dir = file.path(tempdir(), "mvp-mcp"),
  prompts_path = NULL,
  max_retries = 3,
  ...
)

Arguments

output_dir

Folder for the files the tools write (plots, data, bundles, NCA reports). Defaults to a folder in tempdir(), which is deleted when the server stops; set it to keep the files.

prompts_path

Path to an R file with the AI-translation prompts, as in run_mvp(). Default: the prompts installed with MVPapp. Used when a model has to be translated to mrgsolve (see Details).

max_retries

How many times the assistant is asked to fix code that does not compile before it is told to stop and ask the user.

...

Passed to mcptools::mcp_server(), e.g. type = "http".

Value

mvp_mcp_tools(): a list of ellmer tool definitions. mvp_mcp_server(): does not return while the server runs.

Details

Needs the 'mcptools' and 'ellmer' packages. Models are compiled on the computer running the server, which needs a C++ compiler (as for the app). Compiled models and results are kept in memory while the server runs and are referred to by ids (m1, s1, ...).

Your own models. mvp_load_model takes a template, mrgsolve code, or a model file: an mrgsolve .cpp file, code saved from the app's editor, a NONMEM control stream, or a PDF/text file describing a model. A NONMEM model with its .ext file is translated with 'nonmem2mrgsolve' (if installed), as in the app. Otherwise the tool returns the app's translation prompts, so the assistant translates the model itself (no API key needed), and code that does not compile comes back with the compiler's errors and the app's fixing prompt. A paper or model file attached to the conversation (which the server cannot open) is translated the same way: attachment = its name returns the instructions, and the assistant translates what it reads. For NONMEM models, the loaded model is compared with the final estimates in the .ext file. Translated code is saved in output_dir. mvp_translate writes NONMEM or rxode2 code with the app's prompts and checks it before saving it.

Model code runs on this computer. Compiling mrgsolve code (and checking rxode2 code with rxode2) runs that code in the server's R session, as running it yourself would. Code translated by an assistant from a paper or file of unknown origin should be read before it is used, as the app's AI translation should.

Files. Plots, data, bundles and reports are saved in output_dir, and an existing file is never replaced (-2, -3, ... is added to the name), so a folder kept between sessions keeps earlier results. Populations are limited to 5000 subjects, as in the app.

The tools: mvp_guide, mvp_list_templates, mvp_load_model, mvp_translate, mvp_simulate, mvp_population, mvp_sensitivity, mvp_batch, mvp_exposures, mvp_target, mvp_compare, mvp_plot, mvp_save_data, mvp_bundle, mvp_open_in_app, mvp_fetch_paper, mvp_prepare_data, mvp_data_summary, mvp_plot_dataset and mvp_nca. Most call the scripting function of the same name (see mvp_simulate()); mvp_population gives mvp_population_summary() and mvp_population_plot(), mvp_prepare_data mvp_data(), and mvp_plot_dataset also draws mvp_corr_plot() and mvp_hist_plot(). mvp_guide returns the assistant guide (SKILL.md).

Data files are read as on the app's Data Input page (mvp_data(): upper-case column names, BLQ rows removed, ...). Filters are given as text, e.g. "DOSE == 100 & CMT == 2", and are parsed, never evaluated as R code.

Configuration

Claude Desktop (claude_desktop_config.json):

{
  "mcpServers": {
    "mvpapp": {
      "command": "Rscript",
      "args": ["-e", "MVPapp::mvp_mcp_server(output_dir = '~/MVP-outputs')"]
    }
  }
}

On Windows, R is usually not on the PATH: use the full path of Rscript.exe. mvp_mcp_config() writes these settings for you.

Claude Code (in a terminal):

claude mcp add mvpapp -- Rscript -e "MVPapp::mvp_mcp_server(output_dir = '~/MVP-outputs')"

The SKILL.md shipped in system.file("ai", package = "MVPapp") tells an assistant how to use the tools (or the R functions) well.

See also

mvp_simulate() and the other scripting functions.

Examples

# \donttest{
if (requireNamespace("ellmer", quietly = TRUE)) {
  tools <- mvp_mcp_tools()
  vapply(tools, function(t) t@name, character(1))
}
#>  [1] "mvp_list_templates" "mvp_translate"      "mvp_load_model"    
#>  [4] "mvp_simulate"       "mvp_sensitivity"    "mvp_batch"         
#>  [7] "mvp_exposures"      "mvp_plot"           "mvp_save_data"     
#> [10] "mvp_bundle"         "mvp_nca"            "mvp_plot_dataset"  
#> [13] "mvp_compare"        "mvp_target"         "mvp_open_in_app"   
#> [16] "mvp_fetch_paper"    "mvp_prepare_data"   "mvp_data_summary"  
#> [19] "mvp_population"     "mvp_guide"         
# }