An MCP tool is a plain function inside register_tools() in
packages/studyloop/src/studyloop/mcp/tools.py, decorated with the module's @tool() wrapper and
served over stdio by packages/studyloop/src/studyloop/mcp/server.py. It must call the same
service the matching CLI command and web route call — parity is the rule, and a test pins it — and
its test reaches it through the FastMCP registry, never through a subprocess.
- Add the function inside
register_tools()with typed parameters and anArgs:docstring. - Call the existing service; raise
ToolErrorfor a refusal, with the allowed values named. - Write
packages/studyloop/tests/test_mcp_<name>.pythat fetches the tool from the registry. - If the tool mirrors a CLI command or route, assert the same keys or the same engine output.
- Run the MCP test files; they are part of the default suite (F7).
packages/studyloop/src/studyloop/mcp/tools.py— every tool, in oneregister_tools(mcp, *, include_exercises=False)function. The localtool()decorator wraps each function in_guard_scope, which turns an unconfigured-scope error into a structuredToolErrorpayload, then hands it tomcp.tool(). Developer-preview tools (the exercise pipeline) register only wheninclude_exercises=True; they are absent from discovery, not registered-and-refusing.packages/studyloop/src/studyloop/mcp/server.py—build_mcp(dev=...), thelifespanthat opens the database and settings once, the import-timemcpinstance tests use, andmain()behind thestudyloop-mcpconsole script (--devadds the preview tools).packages/studyloop/src/studyloop/mcp/inventory.py— the plan-tool names as a tuple importable without the SDK;packages/studyloop/tests/test_docs_plan_integration_contract.pygrounds it in the real registry and indocs/agent-install.md, so a renamed tool fails a test rather than leaving a stale sentence.- The SDK is the optional
mcpextra (mcp[cli]>=1.0.0,<2inpackages/studyloop/pyproject.toml); testsimportorskip("mcp"), so check it is installed before trusting a green run.
Read get_next_action in packages/studyloop/src/studyloop/mcp/tools.py (about fifty lines).
It is the parity example: studyloop now, GET /api/now in
packages/studyloop/src/studyloop/web/routes/now.py and this tool all call
build_now_plan() from packages/studyloop/src/studyloop/learning/decision.py. MCP clients send
strings, so the tool validates each argument against the engine's Literal types and raises
ToolError naming the allowed set before anything is scored. The smallest write tool is
record_study_progress, earlier in the same file: sixteen lines, three typed parameters, one
service call, one status dict.
Read packages/studyloop/tests/test_mcp_next_action.py (133 lines). The pattern, which every
test_mcp_*.py file shares, is:
from studyloop.mcp.server import mcp, thenmcp._tool_manager._tools[name]—.fnis the callable,.parametersis the JSON schema clients see.- Isolate the world first:
tmp_pathHOME and config via a fixture, as the cookbook's unit template does for the CLI; patch the service where the tool looks it up. - Assert the schema (parameter names and defaults), the refusal (
pytest.raises(ToolError)with the message naming the allowed values) and the result — compared with the same engine output the CLI and web tests pin.
uv run --group dev pytest packages/studyloop/tests/test_mcp_next_action.py -qThe parity file is packages/studyloop/tests/test_mcp_session_parity.py: it locks
list_session_options and end_session to the shapes the web picker and end-session button use,
so an agent driving StudyLoop over MCP has exactly the power the browser has.
uv run --group dev pytest packages/studyloop/tests/test_mcp_session_parity.py -quv run --group dev pytest packages/studyloop/tests/test_mcp_*.py -qAll of these run in the default selection, so just test and just preflight include them (F7).
The one exception is packages/studyloop/tests/test_mcp_stdio_smoke.py: it spawns the real
server and drives it with the SDK client over stdio, so it is marked integration, deselected by
default and run nightly in CI (F8). Run it yourself with -m integration only against a throwaway
HOME — its lifespan opens the configured database.
- The tool calls a service in
packages/studyloop/src/studyloop/services/orpackages/studyloop/src/studyloop/learning/, not the database or the filesystem directly, and anything derived from a tool argument goes through_safe_course_diror an equivalent check. - Every parameter is typed with a default where the CLI has one; the docstring's
Args:block is what the agent reads as the tool description, so it names the accepted values. - Refusals are
ToolErrorwith the allowed set in the message, never a bare exception or a silently coerced value. - Parity is asserted, not described: the test compares keys or output with the CLI or route test for the same engine.
- A new learner-facing tool is named in
docs/agent-install.md; a preview tool is behindinclude_exercises, not behind an environment variable.