click-mcp

click-mcp

Turn click CLIs into MCP servers with one line of code

crowecawcaw

Developer Tools
Visit Server

README

click-mcp

PyPI version License: MIT

A Python library that extends Click applications with Model Context Protocol (MCP) support, allowing AI agents to interact with CLI tools.

Overview

click-mcp provides a simple decorator that converts Click commands into MCP tools. This enables AI agents to discover and interact with your CLI applications programmatically.

The Model Context Protocol (MCP) is an open standard for AI agents to interact with tools and applications in a structured way.

Key Features

  • Simple @click_mcp decorator syntax
  • Automatic conversion of Click commands to MCP tools
  • Support for nested command groups
  • Stdio-based MCP server for easy integration

Installation

pip install click-mcp

Basic Usage

import click
from click_mcp import click_mcp

@click_mcp
@click.group()
def cli():
    """Sample CLI application."""
    pass

@cli.command()
@click.option('--name', required=True, help='Name to greet')
def greet(name):
    """Greet someone."""
    click.echo(f"Hello, {name}!")

if __name__ == '__main__':
    cli()

When you run the MCP server, Click commands are converted into MCP tools:

  • Command greet becomes MCP tool greet
  • Nested commands use dot notation (e.g., users.create)

To invoke a command via MCP, send a request like:

{
  "type": "invoke",
  "tool": "greet",
  "parameters": {
    "name": "World"
  }
}

To start the MCP server:

$ python my_app.py mcp

Advanced Usage

Customizing the MCP Command Name

By default, click-mcp adds an mcp command to your CLI application. You can customize this name using the command_name parameter:

@click_mcp(command_name="start-mcp")
@click.group()
def cli():
    """Sample CLI application with custom MCP command name."""
    pass

With this configuration, you would start the MCP server using:

$ python my_app.py start-mcp

This can be useful when:

  • The name "mcp" conflicts with an existing command
  • You want a more descriptive command name
  • You're integrating with a specific AI agent that expects a certain command name

Working with Nested Command Groups

click-mcp supports nested command groups. When you have a complex CLI structure with subcommands, all commands are exposed as MCP tools:

@click_mcp
@click.group()
def cli():
    """Main CLI application."""
    pass

@cli.group()
def users():
    """User management commands."""
    pass

@users.command()
@click.option('--username', required=True)
def create(username):
    """Create a new user."""
    click.echo(f"Creating user: {username}")

@users.command()
@click.argument('username')
def delete(username):
    """Delete a user."""
    click.echo(f"Deleting user: {username}")

When exposed as MCP tools, the nested commands will be available with their full path using dot notation (e.g., "users.create" and "users.delete").

Handling Command Errors

When a Click command raises an exception, click-mcp captures the error and returns it as part of the MCP response. This allows AI agents to handle errors gracefully:

@cli.command()
@click.option('--filename', required=True)
def process(filename):
    """Process a file."""
    try:
        with open(filename, 'r') as f:
            content = f.read()
        click.echo(f"Processed file: {filename}")
    except FileNotFoundError:
        raise click.UsageError(f"File not found: {filename}")

If the file doesn't exist, the AI agent will receive an error message that it can present to the user or use to take corrective action.

Development

Setup

Clone the repository and install Hatch:

git clone https://github.com/aws/click-mcp.git
cd click-mcp
pip install hatch

Testing

Run tests with Hatch:

# Run all tests
hatch run test

# Run tests with coverage
hatch run cov

Code Formatting

Format code with Black:

# Format code
hatch run format

# Check formatting
hatch run check-format

Linting

Run linting checks with Ruff:

hatch run lint

Type Checking

Run type checking with MyPy:

hatch run typecheck

Run All Checks

Run all checks (formatting, linting, type checking, and tests):

hatch run check-all

Building

Build the package:

hatch run build

Documentation

Generate documentation:

hatch run docs

Related Resources

License

MIT

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
MCP Package Docs Server

MCP Package Docs Server

Facilitates LLMs to efficiently access and fetch structured documentation for packages in Go, Python, and NPM, enhancing software development with multi-language support and performance optimization.

Featured
Local
TypeScript
Claude Code MCP

Claude Code MCP

An implementation of Claude Code as a Model Context Protocol server that enables using Claude's software engineering capabilities (code generation, editing, reviewing, and file operations) through the standardized MCP interface.

Featured
Local
JavaScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

Model Context Protocol server for Task Management. This allows Claude Desktop (or any MCP client) to manage and execute tasks in a queue-based system.

Featured
Local
JavaScript
Linear MCP Server

Linear MCP Server

Enables interaction with Linear's API for managing issues, teams, and projects programmatically through the Model Context Protocol.

Featured
JavaScript
mermaid-mcp-server

mermaid-mcp-server

A Model Context Protocol (MCP) server that converts Mermaid diagrams to PNG images.

Featured
JavaScript
Jira-Context-MCP

Jira-Context-MCP

MCP server to provide Jira Tickets information to AI coding agents like Cursor

Featured
TypeScript
Linear MCP Server

Linear MCP Server

A Model Context Protocol server that integrates with Linear's issue tracking system, allowing LLMs to create, update, search, and comment on Linear issues through natural language interactions.

Featured
JavaScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

This server facilitates structured problem-solving by breaking down complex issues into sequential steps, supporting revisions, and enabling multiple solution paths through full MCP integration.

Featured
Python