Extend Click applications with Model Context Protocol (MCP) support
Project description
click-mcp
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_mcpdecorator 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
greetbecomes MCP toolgreet - 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 development dependencies:
git clone https://github.com/aws/click-mcp.git
cd click-mcp
pip install -e ".[dev]"
Testing
Run tests with pytest:
pytest
Run tests with coverage:
pytest --cov=click_mcp
Code Formatting
Format code with black:
black click_mcp tests
Run linting checks:
flake8 click_mcp tests
Building
Build the package:
python -m build
Documentation
Generate documentation:
cd docs
make html
Related Resources
- Model Context Protocol (MCP) Specification
- Click Documentation
- MCP Tools Registry - A collection of MCP-compatible tools
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file click_mcp-0.1.0.tar.gz.
File metadata
- Download URL: click_mcp-0.1.0.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b22cca507e64ef0f9f292ff6772f85c1c5dfa92ad50b358f4919652cc24f9b55
|
|
| MD5 |
d66bedc69be8059ce2f185be016e44b2
|
|
| BLAKE2b-256 |
17135c85232888350872409de3f5c4dae15bb8e59f62698667807b81b878a8d5
|
Provenance
The following attestation bundles were made for click_mcp-0.1.0.tar.gz:
Publisher:
publish-to-pypi.yml on crowecawcaw/click-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
click_mcp-0.1.0.tar.gz -
Subject digest:
b22cca507e64ef0f9f292ff6772f85c1c5dfa92ad50b358f4919652cc24f9b55 - Sigstore transparency entry: 193124737
- Sigstore integration time:
-
Permalink:
crowecawcaw/click-mcp@ec9d08cea80e0b9ace0b17fe6b1d285d156e3c8a -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/crowecawcaw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@ec9d08cea80e0b9ace0b17fe6b1d285d156e3c8a -
Trigger Event:
release
-
Statement type:
File details
Details for the file click_mcp-0.1.0-py3-none-any.whl.
File metadata
- Download URL: click_mcp-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3001a3665bbc327d7bf47d3e1e3a64f18fdc7e3eb7406e39988c2fd77582c323
|
|
| MD5 |
80ed90e4c44c65e2601de87b16774f4b
|
|
| BLAKE2b-256 |
e0e8665834f98cd90d481a741c01af168cab9d9241ce749152751aad4be09de4
|
Provenance
The following attestation bundles were made for click_mcp-0.1.0-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on crowecawcaw/click-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
click_mcp-0.1.0-py3-none-any.whl -
Subject digest:
3001a3665bbc327d7bf47d3e1e3a64f18fdc7e3eb7406e39988c2fd77582c323 - Sigstore transparency entry: 193124739
- Sigstore integration time:
-
Permalink:
crowecawcaw/click-mcp@ec9d08cea80e0b9ace0b17fe6b1d285d156e3c8a -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/crowecawcaw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@ec9d08cea80e0b9ace0b17fe6b1d285d156e3c8a -
Trigger Event:
release
-
Statement type: