Skip to content

Feature/mcp server - #95

Merged
correspondMerchant merged 3 commits into
mainfrom
feature/mcp-server
Aug 24, 2026
Merged

Feature/mcp server#95
correspondMerchant merged 3 commits into
mainfrom
feature/mcp-server

Conversation

@correspondMerchant

Copy link
Copy Markdown
Owner

What this changes

Adds an MCP (Model Context Protocol) server so any MCP-aware AI agent can run LAGO optimizations, and completes the Python wrapper with a sensitivity() function. Everything is under python/ (excluded from the R build), so the R package is unaffected.

MCP server (python/lago/mcp_server.py)

A FastMCP server, runnable as python -m lago.mcp_server (stdio transport), exposing two tools that reuse the existing rpy2 wrapper to call the real R functions:

  • optimize — wraps lago.optimize. Trial data is passed as either data_csv (a path to a CSV) or data_records (inline row dicts); exactly one is required. Returns a JSON dict: rec_int, rec_int_cost, est_outcome_goal, and the confidence set / interval when computed.
  • sensitivity — wraps lago.sensitivity; sweeps one input and returns per-value records.

Robustness: tool errors are returned as a clean {"error": ...} payload rather than crashing the server, and non-finite scalars are mapped to null so the returned JSON is always valid. The interactive / file-writing functions (visualize_cost, lago_report) are intentionally not exposed.

Python wrapper

Adds sensitivity() (mirrors optimize(), wraps R lago_sensitivity()), which the wrapper was previously missing.

Packaging

mcp is an optional extra (pip install ".[mcp]") plus a lago-mcp script entry; importing the base wrapper does not require mcp. Because rpy2 embeds R, running the server needs R and the installed LAGOtrials R package.

Docs & tests

  • python/README.md gets an MCP section (install, run, the two tools, and a copy-pasteable mcpServers client-config stanza with the needed R_HOME/R_LIBS/LD_LIBRARY_PATH env); the top-level README and NEWS.md note the server.
  • Tests cover both tools: data_records/data_csv equivalence, both/neither-data error paths, argmin-invariant sensitivity, tool registration/schema introspection, and a call_tool JSON round-trip (the serialization boundary a real client uses). 27 Python tests pass.

Related issue

Closes #

Checklist

  • Tests added or updated — Python tests under python/tests/ (27 pass); no R tests needed (no R changes)
  • devtools::document() — n/a, no R/roxygen changes
  • devtools::check() passes locally — n/a, all changes under python/ (Rbuildignored); R package untouched (confirmed via git diff)
  • NEWS.md updated (MCP server + Python sensitivity())

Adds python/lago/mcp_server.py, a Model Context Protocol server (FastMCP,
runnable as `python -m lago.mcp_server`) that lets any MCP-aware agent run LAGO.
It exposes two tools, optimize and sensitivity, that reuse the existing rpy2
wrapper to call the real R functions. Trial data is passed as either a CSV path
or inline records; results come back as JSON-safe dicts, and tool errors are
returned as a clean {"error": ...} payload rather than crashing the server.
Non-finite scalars are mapped to null so the returned JSON is always valid. The
interactive/file-writing functions (visualize_cost, lago_report) are not
exposed.

Also completes the Python wrapper with a sensitivity() that mirrors optimize()
and wraps R lago_sensitivity(). The mcp SDK is an optional extra (pip install
".[mcp]"); importing the base wrapper does not require it. Everything lives
under python/ (excluded from the R build), so the R package is unaffected.

Adds tests for both tools (records/CSV equivalence, error paths, argmin-
invariant sensitivity, schema registration, and a call_tool JSON round-trip),
and a README section with the client config stanza. 27 python tests pass.
@correspondMerchant
correspondMerchant merged commit 2b50e1d into main Aug 24, 2026
7 checks passed
@correspondMerchant
correspondMerchant deleted the feature/mcp-server branch August 24, 2026 18:19
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant