Skip to main content

Adapter for exposing Command Registry commands as tools for AI models via MCP Proxy.

Project description

MCP Proxy Adapter

Adapter for integrating Command Registry with MCP Proxy, allowing you to use commands as tools for AI models.

Overview

MCP Proxy Adapter transforms commands registered in the Command Registry into a format compatible with MCP Proxy. This enables:

  1. Using existing commands as tools for AI models
  2. Creating a hybrid REST/JSON-RPC API for command execution
  3. Automatic generation of OpenAPI schemas optimized for MCP Proxy
  4. Managing tool metadata for better AI system integration

Installation

pip install mcp-proxy-adapter

Quick Start

from mcp_proxy_adapter import MCPProxyAdapter, CommandRegistry
from fastapi import FastAPI

# Create a command registry instance
registry = CommandRegistry()

# Register commands
@registry.command
def calculate_total(prices: list[float], discount: float = 0.0) -> float:
    """
    Calculates the total price with discount.
    Args:
        prices: List of item prices
        discount: Discount percentage (0-100)
    Returns:
        Total price with discount
    """
    subtotal = sum(prices)
    return subtotal * (1 - discount / 100)

# Create FastAPI app
app = FastAPI()

# Create and configure MCP Proxy adapter
adapter = MCPProxyAdapter(registry)

# Register endpoints in FastAPI app
adapter.register_endpoints(app)

# Generate and save MCP Proxy config
adapter.save_config_to_file("mcp_proxy_config.json")

Supported Request Formats

The adapter supports three request formats for command execution:

1. JSON-RPC format

{
  "jsonrpc": "2.0",
  "method": "command_name",
  "params": {
    "param1": "value1",
    "param2": "value2"
  },
  "id": 1
}

Example request to /cmd endpoint:

curl -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "method": "calculate_total",
  "params": {
    "prices": [100, 200, 300],
    "discount": 10
  },
  "id": 1
}' http://localhost:8000/cmd

Response:

{
  "jsonrpc": "2.0",
  "result": 540.0,
  "id": 1
}

2. MCP Proxy format

{
  "command": "command_name",
  "params": {
    "param1": "value1",
    "param2": "value2"
  }
}

Example request:

curl -X POST -H "Content-Type: application/json" -d '{
  "command": "calculate_total",
  "params": {
    "prices": [100, 200, 300],
    "discount": 10
  }
}' http://localhost:8000/cmd

Response:

{
  "result": 540.0
}

3. Params-only format

{
  "params": {
    "command": "command_name",
    "param1": "value1",
    "param2": "value2"
  }
}

or

{
  "params": {
    "query": "command_name",
    "param1": "value1",
    "param2": "value2"
  }
}

Example request:

curl -X POST -H "Content-Type: application/json" -d '{
  "params": {
    "command": "calculate_total",
    "prices": [100, 200, 300],
    "discount": 10
  }
}' http://localhost:8000/cmd

Response:

{
  "result": 540.0
}

Full Example: Integration with FastAPI

import logging
from fastapi import FastAPI, APIRouter
from mcp_proxy_adapter import CommandRegistry, MCPProxyAdapter, configure_logger

# Configure logging
logging.basicConfig(level=logging.INFO)
project_logger = logging.getLogger("my_project")

# Create FastAPI app
app = FastAPI(title="My API with MCP Proxy Integration")

# Create existing API router
router = APIRouter()

@router.get("/items")
async def get_items():
    """Returns a list of items."""
    return [
        {"id": 1, "name": "Smartphone X", "price": 999.99},
        {"id": 2, "name": "Laptop Y", "price": 1499.99},
    ]

app.include_router(router)

# Register commands
registry = CommandRegistry()

@registry.command
def get_discounted_price(price: float, discount: float = 0.0) -> float:
    """
    Returns the price after applying a discount.
    """
    return price * (1 - discount / 100)

# Create and register MCP Proxy adapter
adapter = MCPProxyAdapter(registry)
adapter.register_endpoints(app)

# Save MCP Proxy config
adapter.save_config_to_file("mcp_proxy_config.json")

Features

  • Universal JSON-RPC endpoint for command execution
  • Automatic OpenAPI schema generation and optimization for MCP Proxy
  • Tool metadata for AI models
  • Customizable endpoints and logging
  • Full test coverage and examples

License

MIT

Documentation

See docs/ for detailed guides, architecture, and examples.

CI/CD & PyPI automation

This project uses GitHub Actions for continuous integration and automated publishing to PyPI.

  • All tests are run on every push and pull request.
  • On push of a new tag (vX.Y.Z), the package is built and published to PyPI automatically.

See .github/workflows/publish.yml for details.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_proxy_adapter-2.0.1.tar.gz (118.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_proxy_adapter-2.0.1-py3-none-any.whl (38.3 kB view details)

Uploaded Python 3

File details

Details for the file mcp_proxy_adapter-2.0.1.tar.gz.

File metadata

  • Download URL: mcp_proxy_adapter-2.0.1.tar.gz
  • Upload date:
  • Size: 118.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for mcp_proxy_adapter-2.0.1.tar.gz
Algorithm Hash digest
SHA256 d1ec57ff1f85f2b83de2c4cbf3d03f47e4fe296a081c78832a14f5743451a278
MD5 3d5b5f4a488e409555a2a71272df8f35
BLAKE2b-256 2085539e0895dca6dc17a38aa82f9e95bcdb0535611b7e6f5ad087006f7b86ae

See more details on using hashes here.

File details

Details for the file mcp_proxy_adapter-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_proxy_adapter-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3b9e2d44f755d4924cb5b8af890afc8a9edcf3ae54435e26a8b872c5adbf4b5c
MD5 99c2482d081e88e04be7c3a050f948ec
BLAKE2b-256 7a046b4a2c40848500be5d7df2a16bce02cc660ab440352826f64cf97e988ba3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page