Skip to content

Example: a cmd2 application with a built-in MCP server which describes its commands and arguments #1852

In review

Description

@tleonhardt

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, nargs ranges, 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, the choices, and the type where there is one
    • its subcommands, described the same way, to any depth
  • Commands which have no argparse parser (plain do_* 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_commands tool, which returns the names, categories and one-line descriptions
  • a describe_command tool, 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 localhost from 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, py and ipy are 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 with uv 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 CommandSet is registered or unregistered at runtime.
  • The example is listed in examples/README.md.

Design documents

Activity

  1. tleonhardt commented on Dec 1, 2026

    @tleonhardt
    MemberAuthor

    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 cmd2 itself, perhaps as an optional feature like we so with the ipy command.

    I anticipate some minor changes will be needed to cmd2 proper in order to properly support this capability. Creating an example will iron out those details for what precisely is needed.

  2. self-assigned this
    on Dec 1, 2026
  3. tleonhardt commented on Dec 4, 2026

    @tleonhardt
    MemberAuthor

    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.

    Approach Advantages Tradeoffs
    #1855 — example with a reusable mixin Meets the original example scope. Authors can inspect, copy, and customize the integration. The MCP SDK is an example dependency, and cmd2 takes on no public server API. Each adopting application integrates and maintains the mixin. Its order in the base classes and the timing of lifecycle overrides matter. Fixes to the example must be brought into applications that copied it.
    #1866 — supported, opt-in server on Cmd Authors install cmd2[mcp] and set mcp_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 project takes on a public API, SDK compatibility, transport maintenance, and CI for both base and MCP-enabled installations. This expands the scope beyond publishing an example.

    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.

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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions