Skip to main content

An open MCP client implementation

Project description

MCP Open Client

An open client implementation for the Model Context Protocol (MCP) with REST API server management capabilities.

Features

  • ๐Ÿš€ FastAPI-based REST API for MCP server management
  • ๐Ÿ”ง FastMCP integration with multiple transport support (STDIO, WebSocket, HTTP)
  • โš™๏ธ Process management for MCP servers with automatic cleanup
  • ๐ŸŽฏ Click-based CLI with rich console output
  • ๐Ÿ“ Filesystem support for configuration and data persistence
  • ๐Ÿ›ก๏ธ Type safety with Pydantic models throughout
  • ๐Ÿ”„ Async/await support for high-performance operations

Installation

Install from source (editable mode)

Clone this repository and install the package in editable mode:

git clone https://github.com/yourusername/mcp-open-client.git
cd mcp-open-client
pip install -e .

Install development dependencies

For development, install the optional development dependencies:

pip install -e ".[dev]"

Quick Start

1. Start the API Server

# Start the MCP Open Client API server
mcp-client api serve --port 8001

2. Add an MCP Server

# Add a filesystem MCP server
mcp-client api add --name filesystem \
    --command npm.cmd \
    --args -x --args -y --args @modelcontextprotocol/server-filesystem --args .

3. Start the MCP Server

# Start the configured server
mcp-client api start <server-id>

4. List Available Tools

# List tools from the running server
mcp-client api tools <server-id>

Usage

REST API

The MCP Open Client provides a REST API for managing MCP servers:

Server Management Endpoints

# Add a new server configuration
POST /servers/
{
  "server": {
    "name": "filesystem",
    "command": "npm.cmd",
    "args": ["-x", "-y", "@modelcontextprotocol/server-filesystem", "."]
  }
}

# List all servers
GET /servers/

# Start a server
POST /servers/{server_id}/start

# Stop a server
POST /servers/{server_id}/stop

# Get server tools
GET /servers/{server_id}/tools

Example with curl

# Add server
curl -X POST "http://localhost:8001/servers/" \
  -H "Content-Type: application/json" \
  -d '{"server": {"name": "filesystem", "command": "npm.cmd", "args": ["-x", "-y", "@modelcontextprotocol/server-filesystem", "."]}}'

# Start server
curl -X POST "http://localhost:8001/servers/{server-id}/start"

# Get tools
curl -X GET "http://localhost:8001/servers/{server-id}/tools"

Command-line Interface

API Management Commands

# Show API help
mcp-client api --help

# Start the API server
mcp-client api serve --port 8001 --host 127.0.0.1

# Add a new server
mcp-client api add --name my-server --command npm.cmd --args -x --args -y --args @modelcontextprotocol/server-name

# List all servers (table format)
mcp-client api list

# List servers in JSON format
mcp-client api list --format json

# Start a server
mcp-client api start <server-id>

# Stop a server
mcp-client api stop <server-id>

# Get tools from a server
mcp-client api tools <server-id>

# Get tools in JSON format
mcp-client api tools <server-id> --format json

Direct MCP Client Commands

# Connect to a server and test connection
mcp-client --verbose connect http://localhost:8080

# List available resources
mcp-client --timeout 60.0 list-resources http://localhost:8080

# List available tools
mcp-client list-tools http://localhost:8080

# Call a custom method
mcp-client call http://localhost:8080 custom/method -p '{"param": "value"}'

As a Python Library

import asyncio
from mcp_open_client import MCPClient

async def main():
    # Create a client instance
    client = MCPClient("http://localhost:8080")
    
    # Use the client as a context manager
    async with client:
        # Initialize the session
        await client.initialize()
        
        # List available resources
        resources = await client.list_resources()
        print("Resources:", resources)
        
        # List available tools
        tools = await client.list_tools()
        print("Tools:", tools)

# Run the async main function
asyncio.run(main())

Server Management API

import asyncio
from mcp_open_client.core.manager import MCPServerManager
from mcp_open_client.api.models.server import ServerConfig

async def main():
    # Create server manager
    manager = MCPServerManager()
    
    # Add a server configuration
    config = ServerConfig(
        name="filesystem",
        command="npm.cmd",
        args=["-x", "-y", "@modelcontextprotocol/server-filesystem", "."]
    )
    
    server = await manager.add_server_from_config(config)
    print(f"Added server: {server.id}")
    
    # Start the server
    started_server = await manager.start_server(server.id)
    print(f"Server status: {started_server.status}")
    
    # Get tools
    tools = await manager.get_server_tools(server.id)
    print(f"Available tools: {len(tools)}")
    
    # Shutdown all servers
    await manager.shutdown_all()

asyncio.run(main())

Supported MCP Servers

The client works with any MCP-compliant server. Here are some examples:

Filesystem Server

mcp-client api add --name filesystem \
    --command npm.cmd \
    --args -x --args -y --args @modelcontextprotocol/server-filesystem --args /path/to/directory

Other Popular MCP Servers

# Sequential thinking server
mcp-client api add --name thinking \
    --command npm.cmd \
    --args -x --args -y --args @modelcontextprotocol/server-sequential-thinking

# Custom Python server
mcp-client api add --name my-python-server \
    --command python \
    --args -m --args my_mcp_server

Project Structure

mcp-open-client/
โ”œโ”€โ”€ pyproject.toml              # Project configuration
โ”œโ”€โ”€ README.md                   # This file
โ”œโ”€โ”€ servers.json               # Server configurations (auto-generated)
โ””โ”€โ”€ mcp_open_client/           # Main package directory
    โ”œโ”€โ”€ __init__.py            # Package initialization
    โ”œโ”€โ”€ client.py              # Core MCP client
    โ”œโ”€โ”€ exceptions.py          # Custom exceptions
    โ”œโ”€โ”€ cli.py                 # Command-line interface
    โ”œโ”€โ”€ api/                   # FastAPI REST API
    โ”‚   โ”œโ”€โ”€ __init__.py
    โ”‚   โ”œโ”€โ”€ main.py           # FastAPI application
    โ”‚   โ”œโ”€โ”€ cli.py            # API CLI entry point
    โ”‚   โ”œโ”€โ”€ models/           # Pydantic models
    โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
    โ”‚   โ”‚   โ””โ”€โ”€ server.py     # Server configuration models
    โ”‚   โ””โ”€โ”€ endpoints/        # API endpoints
    โ”‚       โ”œโ”€โ”€ __init__.py
    โ”‚       โ””โ”€โ”€ servers.py    # Server management endpoints
    โ””โ”€โ”€ core/                 # Core business logic
        โ”œโ”€โ”€ __init__.py
        โ”œโ”€โ”€ manager.py        # MCP server manager
        โ””โ”€โ”€ process.py        # Process management

Development

Code formatting

This project uses several tools for code quality:

  • black: Code formatting
  • isort: Import sorting
  • flake8: Linting
  • mypy: Type checking

Run these tools with:

# Format code
black mcp_open_client/

# Sort imports
isort mcp_open_client/

# Run linting
flake8 mcp_open_client/

# Run type checking
mypy mcp_open_client/

Running tests

pytest

Configuration

Environment Variables

  • MCP_API_URL: Default API URL (default: http://localhost:8001)
  • MCP_API_HOST: API server host (default: 127.0.0.1)
  • MCP_API_PORT: API server port (default: 8001)

Server Configuration

Server configurations are stored in servers.json in the current working directory. The format is:

{
  "servers": [
    {
      "id": "unique-server-id",
      "config": {
        "name": "server-name",
        "transport": "stdio",
        "command": "npm.cmd",
        "args": ["-x", "-y", "@modelcontextprotocol/server-name"],
        "env": {"KEY": "VALUE"},
        "cwd": "/working/directory"
      },
      "status": "running",
      "created_at": "2025-01-01T00:00:00.000000",
      "started_at": "2025-01-01T00:01:00.000000"
    }
  ]
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Troubleshooting

Common Issues

  1. Server fails to start: Check that the command and arguments are correct for your system
  2. Connection refused: Ensure the API server is running on the specified port
  3. Permission denied: Make sure the server process has permission to access required directories

Debug Mode

Enable verbose output to troubleshoot issues:

mcp-client --verbose api list
mcp-client --verbose api start <server-id>

Logs

The API server logs MCP server activity and errors to the console. Use --verbose flag for detailed logging.

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

mcp_open_client-0.8.0.tar.gz (64.6 kB view details)

Uploaded Source

Built Distribution

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

mcp_open_client-0.8.0-py3-none-any.whl (83.7 kB view details)

Uploaded Python 3

File details

Details for the file mcp_open_client-0.8.0.tar.gz.

File metadata

  • Download URL: mcp_open_client-0.8.0.tar.gz
  • Upload date:
  • Size: 64.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for mcp_open_client-0.8.0.tar.gz
Algorithm Hash digest
SHA256 3fa4fd1c7e3dc2f5d3281964e1b144dfb04c7f0f679a0cba818536575463c1ba
MD5 d8efda5595686541675bb2b9ac7f6b1d
BLAKE2b-256 a7b0b4c77b48b774363ae06aebb0f6e8d69a687f67e6d724c04d27864eb8d3fc

See more details on using hashes here.

File details

Details for the file mcp_open_client-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_open_client-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b238edbedf4525b9a45eedb4556288d777592b1db488ac14b5b94345cad6d0e
MD5 92d1482289f113ab70b071625ca73fb8
BLAKE2b-256 8677443385a4a577a40a330cc7a642759c49896cae1208a2fc3ae3c7e3c21b04

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