Repository navigation
Description
Summary
Add an example cmd2 application that includes a built-in Model Context Protocol (MCP) server. The server describes every command the application offers, and the arguments of each one, so that an MCP client (an LLM assistant) can turn an objective stated in plain English into the correct command line for that application.
Motivation
A cmd2 application already holds a complete, machine-readable description of itself: the command names, their help categories, and an argparse parser for every command which uses @with_argparser or @with_annotated. Today that description only reaches a person through help and tab completion, and the person still has to know which command to look for.
An MCP server exposes the same description to an LLM. A user can then say what they want ("show the last 20 commands I ran which mention deploy") and have the assistant work out which command and which arguments do it. This helps most in applications with many commands, with nested subcommands, or with commands the user seldom runs.
An example is the right first step. It shows application authors how to do this with what cmd2 already provides, and it adds no dependency to cmd2 itself.
What the example should show
- A small but realistic application: several commands in more than one help category, at least one command with subcommands, and arguments of several kinds (positionals, flags,
choices,nargsranges, defaults, required options). - An MCP server which is started by the application and serves a description built from the live application, not from a hand-written list. For each command it should report:
- the name, the help category and the one-line description
- the usage string and the full help text
- each argument: its name or flags, its help text, whether it is required,
nargs, the default, thechoices, and the type where there is one - its subcommands, described the same way, to any depth
- Commands which have no
argparseparser (plaindo_*methods and@with_argument_list) are still listed, with their docstring as the description. - Aliases, macros and shortcuts, since they change what a user can type.
- A short section in
examples/README.md, and instructions at the top of the example for connecting an MCP client to it and trying a few English-language objectives.
Suggested shape
These are starting points, not requirements.
Where the information comes from. Cmd.get_visible_commands() gives the command names, Cmd.command_parsers.get(command_func) gives the Cmd2ArgumentParser of a command (or None), and Cmd.get_help_topics() gives the help topics. Walking the parser's actions gives the arguments and the subcommands. Reading these when a request arrives, and not once at startup, keeps the answer correct when a CommandSet is registered or unregistered, or a command is disabled, while the application runs.
What the server offers. For example:
- a
list_commandstool, which returns the names, categories and one-line descriptions - a
describe_commandtool, which returns the full description of one command or subcommand - possibly the same data as MCP resources, for clients which prefer those
Transport. The stdio transport of MCP uses the standard input and output of the process, and so does the interactive prompt, so one process cannot do both at once. The example needs to pick one of these, or show both:
- serve over HTTP on
localhostfrom a background thread while the REPL runs in the main thread - a command-line switch which runs the application as a stdio MCP server and does not start the REPL
Dependency. The MCP Python SDK would be needed by the example only. It must not become a runtime dependency of cmd2.
Questions to settle
- Describe only, or also run? The request here is for a server which describes commands. A tool which also runs a command for the client is a natural next step, but it lets an LLM act on the application, and that needs thought about confirmation and about which commands are allowed. I suggest the first version is read-only, with running commands left as a follow-up.
- Hidden and disabled commands. Presumably they are left out, as they are from
help, but that should be a deliberate choice. - Built-in commands. Whether
shell,run_pyscript,pyandipyare described. They are of little use for mapping an objective to a command, and they matter a great deal if a later version runs commands. - How the user states the objective. The server only supplies information; an MCP client with an LLM does the mapping. The example should say this plainly. Whether it should also have a command inside the application which takes an English objective and suggests a command line is a separate question, because that needs a model and an API key.
- Home of the reusable part. If the code which turns a parser into a description turns out to be generally useful, it could later become a public helper in
cmd2. That is out of scope for the example.
Acceptance criteria
- A new example under
examples/which runs withuv run, with its extra dependency documented. - An MCP client connected to it can list all visible commands and get the full argument description of any command or subcommand.
- The description changes when a
CommandSetis registered or unregistered at runtime. - The example is listed in
examples/README.md.
Design documents
- Design specification: An example cmd2 application with a built-in MCP server, in the python-cmd2/design repository. It settles the questions listed above, and divides the work into two stages: making
Cmd.get_command_category()public incmd2, then the example with its tests and a documentation page. - Implementation plan: MCP Server Example Implementation Plan.
Activity
tleonhardt commentedon Dec 1, 2026
After a good working example for this exists which we are fully happy with, we should evaluate whether it would be beneficial to build this capability natively into
cmd2itself, perhaps as an optional feature like we so with theipycommand.I anticipate some minor changes will be needed to
cmd2proper in order to properly support this capability. Creating an example will iron out those details for what precisely is needed.tleonhardt commentedon Dec 4, 2026
Two implementation options
Both PRs demonstrate the same user experience in the Fleet console. A local MCP client can list visible commands, inspect arguments and subcommands, and look up aliases, macros, and shortcuts. The tools describe commands for a person to review and run. The decision is who owns the integration once an application adopts it.
Cmdcmd2[mcp]and setmcp_port. cmd2 owns startup, snapshot refresh, and shutdown at the command-loop boundaries. Embedded applications can explicitly start and stop the listener. Fixes ship through normal cmd2 upgrades.The built-in approach keeps the SDK optional. A default
Cmd()starts no listener and does no snapshot work. Both approaches listen on loopback and expose command metadata to local clients, so applications should consider what their help text, defaults, and aliases reveal.PR relationship. #1855 targets
main. #1866 targets #1855’s branch so reviewers can see exactly what promoting the example into the library changes. The example can ship on its own, or the two changes can land in sequence to provide official support. Merged PR #1856 supplied the public command-category lookup used by both.