Skip to content

Support an opt-in read-only MCP server via cmd2[mcp] - #1866

Open
tleonhardt wants to merge 2 commits into
mcp-server-examplefrom
feature/opt-in-mcp-server
Open

tleonhardt wants to merge 2 commits into
mcp-server-examplefrom
feature/opt-in-mcp-server

Conversation

@tleonhardt

@tleonhardt tleonhardt commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Alternative solution for #1852. #1855 demonstrates read-only MCP discovery through a reusable example mixin. This PR promotes the same three tools to a supported, opt-in Cmd feature. Applications install cmd2[mcp] and select a port, and the Fleet console demonstrates the public API.

This PR targets #1855’s mcp-server-example branch so the diff shows what official support adds. The adoption and maintenance tradeoffs are summarized in the issue discussion.

Usage

Install cmd2[mcp] and select a port when constructing the application.

import cmd2

app = cmd2.Cmd(mcp_port=8765, mcp_name="My console")
app.cmdloop()

A client can then connect to http://127.0.0.1:8765/mcp. Port 0 requests an available port. The default None leaves MCP disabled, with no SDK import, snapshot work, or listener.

Behavior

  • The outermost command loop owns the listener it starts. Embedded applications can manage it with mcp_start() and mcp_stop() and inspect mcp_url or mcp_status().
  • The command thread publishes snapshots after preloop, after the prompt callback returns, and after the outermost command finishes. Clients can discover commands and arguments, shortcuts, aliases, and macros. Command execution stays at the prompt.
  • Per-instance lifecycle locking prevents simultaneous start and stop requests from leaving a listener behind.
  • The feature guide covers activation, lifecycle, visibility, logging, and metadata exposure. The Fleet walkthrough includes client setup and a dynamic command-set example.

Verification

  • make check and make docs-test passed.
  • The Python 3.15 full suite passed with the extra installed (3,469 passed, 3 skipped).
  • The Python 3.11 MCP suite passed with the SDK and coverage enabled (234 passed).
  • Combining both runs covered every statement in the MCP server and snapshot modules.
  • Verified a clean base installation without the SDK or uvicorn. The CI matrix checks both base and MCP-enabled installations.

Let Cmd own listener startup, shutdown, and snapshot refresh at its command and prompt boundaries. Keep the SDK optional and make the fleet console a consumer of the supported API. Exercise default installations and real MCP clients separately in CI.
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.79%. Comparing base (c98238e) to head (945c290).

Additional details and impacted files
@@                  Coverage Diff                   @@
##           mcp-server-example    #1866      +/-   ##
======================================================
+ Coverage               99.76%   99.79%   +0.02%     
======================================================
  Files                      23       25       +2     
  Lines                    6801     7730     +929     
======================================================
+ Hits                     6785     7714     +929     
  Misses                     16       16              
Flag Coverage Δ
unittests 99.79% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

This branch has not been deployed

No deployments
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