Feature/mcp server - #95
Merged
Merged
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 underpython/(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 existingrpy2wrapper to call the real R functions:optimize— wrapslago.optimize. Trial data is passed as eitherdata_csv(a path to a CSV) ordata_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— wrapslago.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 tonullso the returned JSON is always valid. The interactive / file-writing functions (visualize_cost,lago_report) are intentionally not exposed.Python wrapper
Adds
sensitivity()(mirrorsoptimize(), wraps Rlago_sensitivity()), which the wrapper was previously missing.Packaging
mcpis an optional extra (pip install ".[mcp]") plus alago-mcpscript entry; importing the base wrapper does not requiremcp. Becauserpy2embeds R, running the server needs R and the installedLAGOtrialsR package.Docs & tests
python/README.mdgets an MCP section (install, run, the two tools, and a copy-pasteablemcpServersclient-config stanza with the neededR_HOME/R_LIBS/LD_LIBRARY_PATHenv); the top-level README andNEWS.mdnote the server.data_records/data_csvequivalence, both/neither-data error paths, argmin-invariantsensitivity, tool registration/schema introspection, and acall_toolJSON round-trip (the serialization boundary a real client uses). 27 Python tests pass.Related issue
Closes #
Checklist
python/tests/(27 pass); no R tests needed (no R changes)devtools::document()— n/a, no R/roxygen changesdevtools::check()passes locally — n/a, all changes underpython/(Rbuildignored); R package untouched (confirmed viagit diff)NEWS.mdupdated (MCP server + Pythonsensitivity())